-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUndirectedGraph.java
More file actions
49 lines (42 loc) · 1.22 KB
/
Copy pathUndirectedGraph.java
File metadata and controls
49 lines (42 loc) · 1.22 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
package graph;
/* See restrictions in Graph.java. */
import java.util.ArrayDeque;
import java.util.ArrayList;
/** Represents an undirected graph. Out edges and in edges are not
* distinguished. Likewise for successors and predecessors.
*
* @author Khang
*/
public class UndirectedGraph extends GraphObj {
@Override
public boolean isDirected() {
return false;
}
@Override
public int inDegree(int v) {
int count = 0;
ArrayList<Integer> adj = _edgeArray.get(v - 1);
for (int i : adj) {
if (i != 0) {
count++;
}
}
return count;
}
@Override
public Iteration<Integer> predecessors(int v) {
if (contains(v)) {
ArrayList<Integer> adj = _edgeArray.get(v - 1);
ArrayDeque<Integer> predecessors = new ArrayDeque<Integer>();
for (int i = 0; i < _maxVertex; i++) {
if (adj.get(i) == 1) {
predecessors.add(i + 1);
}
}
return Iteration.iteration(predecessors);
} else {
ArrayDeque<Integer> emptyList = new ArrayDeque<>();
return Iteration.iteration(emptyList);
}
}
}