diff --git a/cycleCheckUGDfsJava b/cycleCheckUGDfsJava index 9a2ddff..df4e3fe 100644 --- a/cycleCheckUGDfsJava +++ b/cycleCheckUGDfsJava @@ -1,65 +1,61 @@ -import java.util.*; -import java.lang.*; -import java.io.*; -class GFG -{ - public static void main(String[] args) throws IOException - { - BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); - int T = Integer.parseInt(br.readLine().trim()); - while(T-->0) - { - String[] s = br.readLine().trim().split(" "); - int V = Integer.parseInt(s[0]); - int E = Integer.parseInt(s[1]); - ArrayList>adj = new ArrayList<>(); - for(int i = 0; i < V; i++) - adj.add(i, new ArrayList()); - for(int i = 0; i < E; i++){ - String[] S = br.readLine().trim().split(" "); - int u = Integer.parseInt(S[0]); - int v = Integer.parseInt(S[1]); - adj.get(u).add(v); - adj.get(v).add(u); +import java.util.ArrayList; + +public class DFS_Cycle { + + static void addEdge(ArrayList> adjacent, int from, int to){ + adjacent.get(from).add(to); + adjacent.get(to).add(from); + } + + static void isCycle(ArrayList> adjacent){ + int component = 0; + boolean visited[] = new boolean[adjacent.size()]; + ArrayList DFS_List = new ArrayList(); + for(int i=0; i> adj) { - vis[node] = true; - for(Integer it: adj.get(node)) { - if(vis[it] == false) { - if(checkForCycle(it, node, vis, adj) == true) - return true; + static boolean DFS_Cycle(int node, ArrayList> adjacent, boolean visited[], ArrayList DFS_List){ + DFS_List.add(node); + visited[node] = true; + for(Integer it : adjacent.get(node)){ + if(visited[it] == false){ + if(DFS_Cycle(it,adjacent,visited, DFS_List)==false){ + return false; + } + } + else if(node == it){ + return false; } - else if(it!=parent) - return true; } - - return false; + return true; } - // 0-based indexing Graph - public boolean isCycle(int V, ArrayList> adj) - { - boolean vis[] = new boolean[V]; - - for(int i = 0;i> adjacent = new ArrayList>(); + for(int i=0; i()); } - - return false; + + addEdge(adjacent,0,1); + addEdge(adjacent,0,2); + addEdge(adjacent,2,3); + addEdge(adjacent,2,4); + addEdge(adjacent,2,5); + addEdge(adjacent,5,6); + addEdge(adjacent,7,8); + addEdge(adjacent,8,11); + addEdge(adjacent,9,10); + addEdge(adjacent,10,13); + addEdge(adjacent,11,12); + addEdge(adjacent,12,14); + addEdge(adjacent,13,14); + + isCycle(adjacent); } }