-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirectedGraph.java
More file actions
50 lines (44 loc) · 1.31 KB
/
Copy pathDirectedGraph.java
File metadata and controls
50 lines (44 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package graph;
/* See restrictions in Graph.java. */
import java.util.ArrayDeque;
import java.util.ArrayList;
/** Represents a general unlabeled directed graph whose vertices are denoted by
* positive integers. Graphs may have self edges.
*
* @author Khang
*/
public class DirectedGraph extends GraphObj {
@Override
public boolean isDirected() {
return true;
}
@Override
public int inDegree(int v) {
int count = 0;
if (this.contains(v)) {
for (int i = 0; i < maxVertex(); i++) {
ArrayList<Integer> adj = _edgeArray.get(i);
if (adj.get(v - 1) == 1) {
count++;
}
}
}
return count;
}
@Override
public Iteration<Integer> predecessors(int v) {
if (contains(v)) {
ArrayDeque<Integer> successor = new ArrayDeque<Integer>();
for (int i = 0; i < _maxVertex; i++) {
ArrayList<Integer> adj = _edgeArray.get(i);
if (adj.get(v - 1) == 1) {
successor.add(i + 1);
}
}
return Iteration.iteration(successor);
} else {
ArrayDeque<Integer> emptyList = new ArrayDeque<>();
return Iteration.iteration(emptyList);
}
}
}