-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph_Example.java
319 lines (260 loc) · 8.2 KB
/
Graph_Example.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
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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
/**
*
* @author pulkit4tech
*/
public class Graph_Example implements Runnable {
BufferedReader c;
PrintWriter pout;
// static long mod = 1000000007;
public void run() {
try {
c = new BufferedReader(new InputStreamReader(System.in));
pout = new PrintWriter(System.out, true);
solve();
pout.close();
} catch (Exception e) {
pout.close();
e.printStackTrace();
System.exit(1);
}
}
public static void main(String[] args) throws Exception {
new Thread(new Graph_Example()).start();
}
void solve() throws Exception {
// For Referenced (GeekforGeek Solution)
// breadthFirst();
// depthFirst();
// is_there_Cycle();
topological_sort();
}
void topological_sort(){
Graph g = new Graph(6);
g.addEdge(5, 2);
g.addEdge(5, 0);
g.addEdge(4, 0);
g.addEdge(4, 1);
g.addEdge(2, 3);
g.addEdge(3, 1);
g.topologicalsort();
}
void is_there_Cycle(){
Graph g = new Graph(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(2, 1);
if(g.isCyclic())
pout.println("\nDirected Graph contains cycle");
else
pout.println("\nDirected Graph doesn't contain cycle");
if(g.isCyclic_SetOperation())
pout.println("Undirected Graph contains cycle (using set)");
else
pout.println("Unidirected Graph doesn't contain cycle (using set)");
// The time complexity of the union-find algorithm is O(ELogV). Like directed graphs, we can use DFS to detect cycle in an undirected graph in O(V+E) time. We do a DFS traversal of the given graph. For every visited vertex ‘v’, if there is an adjacent ‘u’ such that u is already visited and u is not parent of v, then there is a cycle in graph.
// If we don’t find such an adjacent for any vertex, we say that there is no cycle. The assumption of this approach is that there are no parallel edges between any two vertices.
if(g.isCyclic_Other())
pout.println("Graph contains cycle (using other)");
else
pout.println("Graph doesn't contain cycle (using other)");
}
void depthFirst(){
Graph g = new Graph(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
pout.println("\nFollowing is Depth First Traversal");
g.DFS();
}
void breadthFirst(){
Graph g = new Graph(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
pout.println("Following is Breadth First Traversal "+
"(starting from vertex 2)");
g.BFS(2);
}
class Graph{
int V;
ArrayList<Edge> edge;
LinkedList<Integer> adj[];
Graph(int v){
V = v;
edge = new ArrayList<>();
adj = new LinkedList[v];
for(int i=0;i<v;i++)
adj[i] = new LinkedList<>();
}
class Edge{
int src,dest;
}
void addEdge(int v,int w){
adj[v].add(w);
Edge temp = new Edge();
temp.src = v;
temp.dest = w;
edge.add(temp);
}
void topologicalsort(){
boolean visited[] = new boolean[V];
Stack<Integer> st = new Stack<>();
for(int i=0;i<V;i++)
if(!visited[i])
topologicalsort_Helper(visited, i, st);
pout.println("Topological Sort : ");
while(!st.isEmpty()){
pout.print(st.pop()+" ");
}
pout.println();
}
void topologicalsort_Helper(boolean visited[],int v,Stack<Integer> st){
if(!visited[v]){
visited[v] = true;
Iterator<Integer> it = adj[v].iterator();
while(it.hasNext()){
int n = it.next();
if(!visited[n])
topologicalsort_Helper(visited, n, st);
}
st.push(v);
}
}
boolean isCyclicUtil_Other(int v, boolean visited[], int parent)
{
// Mark the current node as visited
visited[v] = true;
Integer i;
// Recur for all the vertices adjacent to this vertex
Iterator<Integer> it = adj[v].iterator();
while (it.hasNext())
{
i = it.next();
// If an adjacent is not visited, then recur for that
// adjacent
if (!visited[i])
{
if (isCyclicUtil_Other(i, visited, v))
return true;
}
// If an adjacent is visited and not parent of current
// vertex, then there is a cycle.
else if (i != parent)
return true;
}
return false;
}
// Returns true if the graph contains a cycle, else false.
boolean isCyclic_Other()
{
// Mark all the vertices as not visited and not part of
// recursion stack
boolean visited[] = new boolean[V];
// Call the recursive helper function to detect cycle in
// different DFS trees
for (int u = 0; u < V; u++)
if (!visited[u]) // Don't recur for u if already visited
if (isCyclicUtil_Other(u, visited, -1))
return true;
return false;
}
// valid only for undirected graph
boolean isCyclic_SetOperation(){
int parent[] = new int[V];
Arrays.fill(parent, -1);
for(int i=0;i<edge.size();i++){
int x = find(parent,edge.get(i).src);
int y = find(parent,edge.get(i).dest);
if(x==y)
return true;
union(parent, x, y);
}
return false;
}
// Set operation
int find(int parent[],int i){
if(parent[i] == -1)
return i;
return find(parent,parent[i]);
}
void union(int parent[],int x,int y){
int xparent = find(parent,x);
int yparent = find(parent,y);
parent[xparent] = yparent;
}
boolean isCyclic(){
boolean visited[] = new boolean[V];
boolean recStack[] = new boolean[V];
for(int i=0;i<V;i++)
if(isCyclicUtil(i,visited,recStack))
return true;
return false;
}
boolean isCyclicUtil(int v,boolean visited[], boolean recStack[]){
if(!visited[v]){
visited[v] = true;
recStack[v] = true;
Iterator<Integer> it = adj[v].iterator();
while(it.hasNext()){
int n = it.next();
if(!visited[n]&&isCyclicUtil(n, visited, recStack))
return true;
else if(recStack[n])
return true;
}
}
recStack[v] = false;
return false;
}
void DFS(){
boolean visited[] = new boolean[V];
for(int i=0;i<V;i++)
if(!visited[i])
DFSutil(i,visited);
}
void DFSutil(int v,boolean visited[]){
visited[v] = true;
pout.print(v+" ");
Iterator<Integer> it = adj[v].iterator();
while(it.hasNext()){
int n = it.next();
if(!visited[n])
DFSutil(n,visited);
}
}
void BFS(int s){
boolean visited[] = new boolean[V];
Queue<Integer> queue = new LinkedList<>();
visited[s] = true;
queue.add(s);
while(queue.size()!=0){
s = queue.poll();
pout.print(s+" ");
// Iterate over adjacent vertex
Iterator<Integer> it = adj[s].iterator();
while(it.hasNext()){
int n = it.next();
if(!visited[n]){
visited[n] = true;
queue.add(n);
}
}
}
}
}
}