-
Notifications
You must be signed in to change notification settings - Fork 0
/
DirectedGraph.java
66 lines (59 loc) · 1.63 KB
/
DirectedGraph.java
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class DirectedGraph {
public int V;
public int E;
public List<List<Integer>> adj;
public int[] indegree;
// Constructor for the Digraph data structure
public DirectedGraph(int V) {
if (V < 0)
throw new IllegalArgumentException("Number of vertices passed must be bigger than or equal to zero");
this.V = V;
this.E = 0;
this.indegree = new int[V];
adj = new ArrayList<>(V);
for (int i = 0; i < V; i++) {
adj.add(new LinkedList<>());
}
}
// Method to add an edge to the graph
public void addEdge(int v, int w) {
if (isVertexValid(v) && isVertexValid(w)) {
if (!adj.get(v).contains(w)) {
adj.get(v).add(w);
this.indegree[w]++;
this.E++;
} else {
throw new IllegalArgumentException("edge " + v + " -> " + w + " is already in the graph");
}
} else {
throw new IllegalArgumentException("vertex " + v + " is not between 0 and " + (V - 1));
}
}
// finds the indegree of a target vertex in the graph
public int indegree(int v) {
if (isVertexValid(v)) {
return this.indegree[v];
} else {
throw new IllegalArgumentException("vertex " + v + " is not between 0 and " + (V - 1));
}
}
//returns whether a given vertex exists in a graph or not
public boolean isVertexValid(int v) {
if (v < 0 || v >= V) {
return false;
} else
return true;
}
// returns the reverse graph of a given Digraph
public DirectedGraph reverse() {
DirectedGraph reverse = new DirectedGraph(this.V);
for (int v = 0; v < this.V; v++) {
for (int w : this.adj.get(v))
reverse.addEdge(w, v);
}
return reverse;
}
}