-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiGraph.java
More file actions
150 lines (134 loc) · 3.58 KB
/
Copy pathDiGraph.java
File metadata and controls
150 lines (134 loc) · 3.58 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import java.util.ArrayList;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
/**
*
* @author TheDarkLord a.k.a Gaurav Shrivastava
*
*/
public class Digraph {
public Map<Integer, Set<Integer>> edges = new TreeMap<Integer, Set<Integer>>();
private ArrayList<Integer>[] adj; // adj[v] = adjacency list for vertex v
private int[] indegree;
/**
* Returns the number of vertices in this graph.
*
* @return the number of vertices in this graph
*/
private int V = 0;
private int E = 0;
public int V() {
return V;
}
/**
* Returns the number of edges in this graph.
*
* @return the number of edges in this graph
*/
public int E() {
return E;
}
/**
* Adds the undirected edge v-w to this graph.
*
* @param v one vertex in the edge
* @param w the other vertex in the edge
* @throws IndexOutOfBoundsException unless both 0 <= v < V and 0 <= w < V
*/
public void addNode(int u) {
if (!edges.containsKey(u)) {
edges.put(u, new TreeSet<Integer>());
}
}
public Digraph(int V) {
if (V < 0) throw new IllegalArgumentException("Number of vertices in a Digraph must be nonnegative");
this.V = V;
this.E = 0;
indegree = new int[V];
adj = (ArrayList<Integer>[]) new ArrayList[V];
for (int v = 0; v < V; v++) {
adj[v] = new ArrayList<Integer>();
}
}
public void removeNode(int u) {
if (!edges.containsKey(u)) {
return;
}
for (int v : edges.get(u)) {
edges.get(v).remove(u);
}
edges.remove(u);
}
public void addEdge(int u, int v) {
addNode(u);
addNode(v);
edges.get(u).add(v);
//edges.get(v).add(u);
}
public void removeEdge(int u, int v) {
edges.get(u).remove(v);
//edges.get(v).remove(u);
}
// private void validateVertex(int v) {
// if (v < 0 || v >= V)
// throw new IndexOutOfBoundsException("vertex " + v + " is not between 0 and " + (V-1));
// }
public Iterable<Integer> adj(int v) {
// validateVertex(v);
return edges.get(v);
}
/**
* Returns the degree of vertex <tt>v</tt>.
*
* @param v the vertex
* @return the degree of vertex <tt>v</tt>
* @throws IndexOutOfBoundsException unless 0 <= v < V
*/
public int degree(int v) {
// validateVertex(v);
return edges.get(v).size();
}
// Usage example
public static void main(String[] args) {
Graph g = new Graph();
g.addEdge(0, 1);
g.addEdge(1, 2);
System.out.println(g.adj(1));
System.out.println(g.degree(0));
g.removeEdge(1, 0);
System.out.println(g.edges);
g.removeNode(1);
System.out.println(g.edges);
}
public int outdegree(int v) {
// validateVertex(v);
return adj[v].size();
}
/**
* Returns the number of directed edges incident to vertex <tt>v</tt>.
* This is known as the <em>indegree</em> of vertex <tt>v</tt>.
*
* @param v the vertex
* @return the indegree of vertex <tt>v</tt>
* @throws IndexOutOfBoundsException unless 0 <= v < V
*/
public int indegree(int v) {
return indegree[v];
}
/**
* Returns the reverse of the digraph.
*
* @return the reverse of the digraph
*/
public Digraph reverse() {
Digraph R = new Digraph(V);
for (int v = 0; v < V; v++) {
for (int w : adj(v)) {
R.addEdge(w, v);
}
}
return R;
}
}