Detect cycle in undirected graph using BFS
import java.util.*;
public class CycleDetectionBFS {
static class Edge {
int src;
int dest;
public Edge(int s, int d) {
this.src = s;
this.dest = d;
}
}
public static void createGraph(ArrayList<Edge> graph[]) {
for (int i = 0; i < graph.length; i++) {
graph[i] = new ArrayList<>();
}
graph[0].add(new Edge(0, 1));
graph[0].add(new Edge(0, 2));
graph[1].add(new Edge(1, 0));
graph[1].add(new Edge(1, 2));
graph[2].add(new Edge(2, 0));
graph[2].add(new Edge(2, 1));
graph[2].add(new Edge(2, 3));
graph[3].add(new Edge(3, 4));
graph[4].add(new Edge(4, 3));
}
public static boolean Detect(ArrayList<Edge> graph[]) {
boolean vis[] = new boolean[graph.length];
int par[] = new int[graph.length];
Arrays.fill(par, -1);
Queue<Integer> q = new LinkedList<>();
q.add(0);
vis[0]=true;
while (!q.isEmpty()) {
int curr = q.remove();
for (int i = 0; i < graph[curr].size(); i++) {
Edge e = graph[curr].get(i);
if (!vis[e.dest]) {
vis[e.dest] = true;
q.add(e.dest);
par[e.dest] = curr;
} else if (par[curr] != e.dest) {
return true;
}
}
}
return false;
}
public static void main(String[] args) {
int v = 5;
ArrayList<Edge> graph[] = new ArrayList[v];
createGraph(graph);
System.out.println("Cycle exists: "+Detect(graph));
}
}
Comments
Post a Comment