From a13ce0b5d544f2472e81f861801ff3aa3e0f811e Mon Sep 17 00:00:00 2001 From: Evan Ferrao Date: Sun, 15 Feb 2026 17:47:11 +0530 Subject: [PATCH] add directed cycle detection and bipartite graphs --- Graphs/BipartiteGraphUsingBFSandDFS.java | 134 ++++++++++++++++++++ Graphs/CycleDetectionDirectedGraphDFS.java | 136 +++++++++++++++++++++ 2 files changed, 270 insertions(+) create mode 100644 Graphs/BipartiteGraphUsingBFSandDFS.java create mode 100644 Graphs/CycleDetectionDirectedGraphDFS.java diff --git a/Graphs/BipartiteGraphUsingBFSandDFS.java b/Graphs/BipartiteGraphUsingBFSandDFS.java new file mode 100644 index 0000000..13b2be0 --- /dev/null +++ b/Graphs/BipartiteGraphUsingBFSandDFS.java @@ -0,0 +1,134 @@ +// https://leetcode.com/problems/is-graph-bipartite/ + +import java.util.*; +import java.io.*; + +class BipartiteGraphUsingBFSandDFS { + + // BFS + public static boolean bfs(int node, int color, int colorArray[], ArrayList> adj){ + Queue q = new LinkedList<>(); + + q.add(node); + colorArray[node] = color; + + while(!q.isEmpty()){ + Integer currNodeode = q.poll(); + + for(Integer it : adj.get(currNodeode)){ + // if(!visited[it]){ + // visited[it] = true; + // q.add(it); + // } + if (colorArray[it]==-1){ // not colored yet (not visited basically) + colorArray[it] = 1 - colorArray[currNodeode]; + q.add(it); + } else if (colorArray[it]==colorArray[currNodeode]){ + return false; + } + } + } + + return true; + } + + + // DFS + public static boolean dfs(int node, int color, int colorArray[], ArrayList> adj){ + colorArray[node] = color; + + for (Integer it: adj.get(node)){ + if (colorArray[it]==-1){ // not colored yet (not visited basically) + if (dfs(it,1-color, colorArray, adj)==false) return false; + } else if (colorArray[it]==color){ + return false; + } + } + return true; + } + + + public static boolean isBipartite(int graph[][]){ + + int V = graph.length; + + ArrayList> adj = new ArrayList<>(); + for(int i=0;i()); + + for(int u=0; u> adj){ + + visited[node] = true; + pathVis[node] = true; + + for(int next : adj.get(node)){ + + if(!visited[next]){ + if(dfsCheck(next, visited, pathVis, adj)) return true; + } + else if(pathVis[next]){ + return true; + } + } + + pathVis[node] = false; + return false; + } + + + public static boolean isCyclic(int V, ArrayList> adj){ + + boolean visited[] = new boolean[V]; + boolean pathVis[] = new boolean[V]; + + for(int i=0;i> makeGraph(int V){ + ArrayList> adj = new ArrayList<>(); + for(int i=0;i()); + return adj; + } + + + // DRIVER + public static void main(String args[]){ + + // ================= CASE 1 ================= + { + int V = 6; + ArrayList> adj = makeGraph(V); + + /* + 0 → 1 → 2 → 3 + ↑ ↓ + └─────┘ + 4 → 5 + */ + + adj.get(0).add(1); + adj.get(1).add(2); + adj.get(2).add(3); + adj.get(3).add(1); + adj.get(4).add(5); + + System.out.printf("Case 1: %s\n", + isCyclic(V, adj) ? "Cycle detected" : "No cycle"); + } + + + // ================= CASE 2 ================= + { + int V = 4; + ArrayList> adj = makeGraph(V); + + /* + 0 → 1 → 2 → 3 + No cycle + */ + + adj.get(0).add(1); + adj.get(1).add(2); + adj.get(2).add(3); + + System.out.printf("Case 2: %s\n", + isCyclic(V, adj) ? "Cycle detected" : "No cycle"); + } + + + // ================= CASE 3 ================= + { + int V = 3; + ArrayList> adj = makeGraph(V); + + /* + 0 → 1 + 1 → 2 + 2 → 0 + */ + + adj.get(0).add(1); + adj.get(1).add(2); + adj.get(2).add(0); + + System.out.printf("Case 3: %s\n", + isCyclic(V, adj) ? "Cycle detected" : "No cycle"); + } + + + // ================= CASE 4 ================= + { + int V = 5; + ArrayList> adj = makeGraph(V); + + /* + disconnected, no cycle + 0 → 1 + 2 → 3 + 4 alone + */ + + adj.get(0).add(1); + adj.get(2).add(3); + + System.out.printf("Case 4: %s\n", + isCyclic(V, adj) ? "Cycle detected" : "No cycle"); + } + } +}