-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCycle.java
More file actions
44 lines (38 loc) · 1.06 KB
/
Cycle.java
File metadata and controls
44 lines (38 loc) · 1.06 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
import java.util.Random;
public class Cycle{
private boolean[] marked;
private boolean hasCycle;
public Cycle(Graph G){
marked=new boolean[G.V()];
for (int s=0;s<G.V();s++){
if (!marked[s]){
dfs(G,s,s);
}
}
}
private void dfs(Graph G,int v,int u){
marked[v]=true;
for (int w:G.adj(v)){
if (!marked[w]){
dfs(G,w,v);
}
else if (w!=u) hasCycle=true;
}
}
public boolean hasCycle(){return hasCycle;}
public static void main(String[] args){
Random r = new Random();
int V=10;
Graph g=new Graph(V);
for (int i=0;i<2*V;i++){
int w=r.nextInt(V-1);
int v=r.nextInt(V-1);
if (v==w) continue;
g.addEdge(v,w);
}
//for (int w:g.adj(1)) System.out.println(w+"");
System.out.println(g);
Cycle cy=new Cycle(g);
System.out.println(cy.hasCycle());
}
}