-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.java
More file actions
361 lines (293 loc) · 7.98 KB
/
Copy pathGraph.java
File metadata and controls
361 lines (293 loc) · 7.98 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
import java.util.List;
import java.util.Iterator;
import java.util.Set;
import java.util.Deque;
import java.util.ArrayDeque;
import java.util.HashSet;
import java.util.ArrayList;
enum GraphType{
DIR, UNDIR
}
abstract class Graph{
int V; //NumVertices
GraphType type;
public Graph(GraphType type){
V = 0;
this.type = type;
}
public int get_num_vertices(){
return V;
}
public void add_vertex(int v){
implement_add_vertex(v);
V=V+1;
}
public abstract void implement_add_vertex(int v);
public void add_edge(int u, int v){
implement_add_edge(u, v);
}
public abstract void implement_add_edge(int u, int v);
public List<Integer> get_neighbors(int v){
List<Integer> ll = implement_get_neighbors(v);
return ll;
}
public abstract List<Integer> implement_get_neighbors(int v);
public Set<Integer> get_vertices(){
Set<Integer> s = implement_get_vertices();
return s;
}
public abstract Set<Integer> implement_get_vertices();
//DFS (Recursive version)
public void dfs_rec(int v, Set<Integer> visited){
//Visit v
if(!visited.contains(v)){
visited.add(v);
visit(v);
//Get all one hop neighbors of v
List<Integer> ll = get_neighbors(v);
Iterator<Integer> it = ll.iterator();
while(it.hasNext()){
int w = it.next();
dfs_rec(w, visited);
}
}
}
public void visit(int v){
System.out.println("Visiting the vertex:" + v);
}
//DFS (using an explicit stack)
public void dfs_st(int v, Set<Integer> visited){
//Javadoc mentions to prefer Deque to implement
// a stack vs. Stack because Deque is more consistent.
//Perhaps it means to say that since stack inherits
//from vector, methods to add anywhere in the stack are exposed
//deque only exposes methods to add in front and last.
Deque<Integer> stack = new ArrayDeque<Integer>();
stack.push(v);
while(!stack.isEmpty()){
int u = stack.pop();
if(!visited.contains(u)){
visited.add(u);
visit(u);
//Get all neighbors of u
List<Integer> ll = get_neighbors(u);
Iterator<Integer> it = ll.iterator();
while(it.hasNext()){
int w = it.next();
stack.push(w);
}
}
}
}
//BFS (using queue)
public void bfs_qu(int v, Set<Integer> visited){
//As per javadoc. ArrayDeque implementation (resizable array)
//of queue is faster than linkedlist implementation
Deque<Integer> queue = new ArrayDeque<Integer>();
queue.add(v);
while(!queue.isEmpty()){
int u = queue.remove();
if(!visited.contains(u)){
visit(u);
visited.add(u);
//Get all neighbors of u
List<Integer> ll = get_neighbors(u);
Iterator<Integer> it = ll.iterator();
while(it.hasNext()){
int w = it.next();
queue.add(w);
}
}
}
}
//Bidirectional search (Using two parallel BFSs)
public void bidirectional_search(int u, int v, Set<Integer> visited){
Deque<Integer> qu = new ArrayDeque<Integer>();
Deque<Integer> qv = new ArrayDeque<Integer>();
qu.add(u);
qv.add(v);
while(!qu.isEmpty() && !qv.isEmpty()){
if(!qu.isEmpty()){
int u1 = qu.pop();
if(qv.contains(u1)){
System.out.print("BFS 1:");
visit(u1);
System.out.println("Collision occurred.");
break; //A collision has taken place.
}
if(!visited.contains(u1)){
System.out.print("BFS 1:");
visit(u1);
visited.add(u1);
List<Integer> ll = get_neighbors(u1);
Iterator<Integer> it = ll.iterator();
while(it.hasNext()){
int w= it.next();
qu.add(w);
}
}
}
if(!qv.isEmpty()){
int v1 = qv.pop();
if(qu.contains(v1)){
System.out.print("BFS 2:");
visit(v1);
System.out.println("Collision occured.");
break; //A collision has taken place
}
if(!visited.contains(v1)){
System.out.print("BFS 2:");
visit(v1);
visited.add(v1);
List<Integer> ll = get_neighbors(v1);
Iterator<Integer> it = ll.iterator();
while(it.hasNext()){
int w = it.next();
qv.add(w);
}
}
}
}
}
//Find shortest path using BFS (using queue)
//Array prev is used to maintain predecessor for each vertex
//***NOTE: This will give shortest path only for an undirected graph
public void find_shortest_path_bfs(int v, int dest, Set<Integer> visited, int[] prev){
//As per javadoc. ArrayDeque implementation (resizable array)
//of queue is faster than linkedlist implementation
Deque<Integer> queue = new ArrayDeque<Integer>();
queue.add(v);
prev[v] = -1;
while(!queue.isEmpty()){
int u = queue.remove();
if(!visited.contains(u)){
visit(u);
visited.add(u);
if(u == dest){break;}
//Get all neighbors of u
List<Integer> ll = get_neighbors(u);
Iterator<Integer> it = ll.iterator();
while(it.hasNext()){
int w = it.next();
queue.add(w);
//Set the predecessor for a vertex only if
//it has already not being visited.
if(!visited.contains(w)){
prev[w] = u;
}
}
}
}
}
//Print connected components in a graph
public void print_connected_components(){
Set<Integer> vertices = get_vertices();
Iterator<Integer> it = vertices.iterator();
Set<Integer> visited = new HashSet<Integer>();
while(it.hasNext()){
int v = it.next();
if(!visited.contains(v)){
System.out.println("Connected Component:");
dfs_rec(v, visited);
}
}
}
//Check for cycle in an undirected graph
//Reference: http://www.geeksforgeeks.org/detect-cycle-undirected-graph/
public boolean check_cycle_undir(int v, Set<Integer> visited, int parent){
if(!visited.contains(v)){
visit(v);
visited.add(v);
List<Integer> ll = get_neighbors(v);
Iterator<Integer> it = ll.iterator();
while(it.hasNext()){
int w = it.next();
if(w != parent){
if(true == check_cycle_undir(w, visited, v)){
return true;
}
}else{
System.out.println("Skip adding parent:" + parent + " to stack.");
}
}
return false;
}else{
System.out.println("Cycle found at:" + v);
return true;
}
}
//Check for cycle in a directed graph
//References:
//a. http://www.geeksforgeeks.org/detect-cycle-in-a-graph/
//b. Cormen book explains the concept of recactive by assigning colors
//to a vertex (white, gray, black) to mark the different stages it
//goes through.
public boolean check_cycle_dir(int v, Set<Integer>visited, Set<Integer> recactive){
if(!visited.contains(v)){
visited.add(v);
recactive.add(v);
visit(v);
List<Integer> ll = get_neighbors(v);
Iterator<Integer> it = ll.iterator();
while(it.hasNext()){
int w = it.next();
if(true == check_cycle_dir(w, visited, recactive)){
return true;
}
}
recactive.remove(v);;
}else{
if(recactive.contains(v)){
return true;
}
}
return false;
}
//Topological sort using DFS (Recursive version)
public void top_sort(int v, Set<Integer> visited, Deque<Integer> stack){
//Visit v
if(!visited.contains(v)){
visited.add(v);
visit(v);
//Get all one hop neighbors of v
List<Integer> ll = get_neighbors(v);
Iterator<Integer> it = ll.iterator();
while(it.hasNext()){
int w = it.next();
top_sort(w, visited, stack);
}
stack.push(v);
}
}
public void find_all_paths(int s, int d, Set<Integer> visited, int[] path, int path_index){
if(!visited.contains(s)){
visit(s);
visited.add(s);
path[path_index] = s;
path_index = path_index + 1;
if(s == d){
print_path(path, path_index);
//path.remove(path.size()-1);
}
List<Integer> ll = get_neighbors(s);
Iterator<Integer> it = ll.iterator();
while(it.hasNext()){
int u = it.next();
find_all_paths(u, d, visited, path, path_index);
}
visited.remove(s);
path_index = path_index-1;
//path.remove(path.size()-1);
}
}
public void print_path(int[] path, int path_index){
System.out.println("Path:");
for(int i=0; i<path_index; i++){
System.out.println(path[i] + ",");
}
}
public Set<Integer> min_spanning_tree(int root){
Set<Integer> mintree = new HashSet<Integer>();
return mintree;
}
}