From c583b049847b7bf1fc8411e4a08a5ec592af33c1 Mon Sep 17 00:00:00 2001 From: Evan Ferrao Date: Sat, 14 Feb 2026 15:24:35 +0530 Subject: [PATCH] keep code consistent --- Graphs/CycleDetectionBFS.java | 75 ++++++++++++++++++----------------- Graphs/CycleDetectionDFS.java | 58 +++++++++++++-------------- 2 files changed, 65 insertions(+), 68 deletions(-) diff --git a/Graphs/CycleDetectionBFS.java b/Graphs/CycleDetectionBFS.java index 48eb2a8..80f5c50 100644 --- a/Graphs/CycleDetectionBFS.java +++ b/Graphs/CycleDetectionBFS.java @@ -1,67 +1,68 @@ import java.util.*; +import java.io.*; -public class CycleDetectionBFS { +class Main { - static class Pair { - int first; - int second; + // helper structure + static class Node{ + int node; + int parent; - Pair(int first, int second) { - this.first = first; - this.second = second; + Node(int node, int parent){ + this.node = node; + this.parent = parent; } } - public static boolean bfs(int src, int V, ArrayList> adj,boolean[] vis) { + public static boolean bfs(int src, int V, + ArrayList> adj, + boolean visited[]){ - vis[src] = true; - Queue q = new LinkedList<>(); - q.add(new Pair(src, -1)); + visited[src] = true; - while (!q.isEmpty()) { - int node = q.peek().first; - int parent = q.peek().second; - q.remove(); + Queue q = new LinkedList<>(); + q.add(new Node(src, -1)); - for (int adjacentNode : adj.get(node)) { - if (!vis[adjacentNode]) { - vis[adjacentNode] = true; - q.add(new Pair(adjacentNode, node)); + while(!q.isEmpty()){ + Node cur = q.poll(); + int u = cur.node; + int par = cur.parent; + + for(int v : adj.get(u)){ + if(!visited[v]){ + visited[v] = true; + q.add(new Node(v, u)); } - - // everything except this 'else if' statement is normal BFS - else if (adjacentNode != parent) { - return true; // cycle detected + else if(v != par){ + return true; // cycle } } } return false; } - public static boolean isCycle(int V, ArrayList> adj) { + public static boolean isCycle(int V, ArrayList> adj){ - boolean[] vis = new boolean[V]; + boolean visited[] = new boolean[V]; - for (int i = 0; i < V; i++) { - if (!vis[i]) { - if (bfs(i, V, adj, vis)) { - return true; - } + for(int i=0;i> adj = new ArrayList<>(); - for (int i = 0; i < V; i++) { - adj.add(new ArrayList<>()); - } + for(int i=0;i()); - // Undirected graph edges + // Undirected graph adj.get(0).add(1); adj.get(1).add(0); @@ -71,12 +72,12 @@ public class CycleDetectionBFS { adj.get(2).add(3); adj.get(3).add(2); - adj.get(3).add(1); // creates a cycle + adj.get(3).add(1); adj.get(1).add(3); adj.get(3).add(4); adj.get(4).add(3); - System.out.printf(isCycle(V, adj) ? "Cycle detected" : "No cycle detected"); + System.out.printf(isCycle(V, adj) ? "Cycle detected\n" : "No cycle detected\n"); } } diff --git a/Graphs/CycleDetectionDFS.java b/Graphs/CycleDetectionDFS.java index 722df8e..5cebfc2 100644 --- a/Graphs/CycleDetectionDFS.java +++ b/Graphs/CycleDetectionDFS.java @@ -1,64 +1,60 @@ import java.util.*; +import java.io.*; -public class CycleDetectionDFS { +class Main { - static class Pair { + // helper structure + static class Node{ int node; int parent; - Pair(int node, int parent) { + Node(int node, int parent){ this.node = node; this.parent = parent; } } - public static boolean dfs(Pair current, boolean[] vis, ArrayList> adj) { + public static boolean dfs(Node cur, boolean visited[], + ArrayList> adj){ - int node = current.node; - int parent = current.parent; + int u = cur.node; + int par = cur.parent; - vis[node] = true; + visited[u] = true; - for (int adjacentNode : adj.get(node)) { - - if (!vis[adjacentNode]) { - if (dfs(new Pair(adjacentNode, node), vis, adj)) { - return true; - } + for(int v : adj.get(u)){ + if(!visited[v]){ + if(dfs(new Node(v, u), visited, adj)) return true; } - - // everything except this 'else if' statement is normal DFS - else if (adjacentNode != parent) { - return true; + else if(v != par){ + return true; // cycle } } return false; } - public static boolean isCycle(int V, ArrayList> adj) { + public static boolean isCycle(int V, ArrayList> adj){ - boolean[] vis = new boolean[V]; + boolean visited[] = new boolean[V]; - for (int i = 0; i < V; i++) { - if (!vis[i]) { - if (dfs(new Pair(i, -1), vis, adj)) { - return true; - } + for(int i=0;i> adj = new ArrayList<>(); - for (int i = 0; i < V; i++) { - adj.add(new ArrayList<>()); - } + for(int i=0;i()); - // Undirected graph edges + // Undirected graph adj.get(0).add(1); adj.get(1).add(0); @@ -68,12 +64,12 @@ public class CycleDetectionDFS { adj.get(2).add(3); adj.get(3).add(2); - adj.get(3).add(1); // creates a cycle + adj.get(3).add(1); adj.get(1).add(3); adj.get(3).add(4); adj.get(4).add(3); - System.out.printf(isCycle(V, adj) ? "Cycle detected" : "No cycle detected"); + System.out.printf(isCycle(V, adj) ? "Cycle detected\n" : "No cycle detected\n"); } }