From dee4727338aa0e00f733fade74262728f16ce6df Mon Sep 17 00:00:00 2001 From: Sanskar Agrawal Date: Sat, 11 Oct 2025 20:04:13 +0530 Subject: [PATCH 1/3] Added Is a Graph Bipartite Algorithm --- .../thealgorithms/graph/IsGraphBipartite.java | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 src/main/java/com/thealgorithms/graph/IsGraphBipartite.java diff --git a/src/main/java/com/thealgorithms/graph/IsGraphBipartite.java b/src/main/java/com/thealgorithms/graph/IsGraphBipartite.java new file mode 100644 index 000000000000..475ecd7cf467 --- /dev/null +++ b/src/main/java/com/thealgorithms/graph/IsGraphBipartite.java @@ -0,0 +1,86 @@ +package com.thealgorithms.graph; + +import java.util.*; // For ArrayList, Queue, LinkedList + +public class IsGraphBipartite { + // Inner class to represent an edge + class Edge { + int src; + int dest; + + public Edge(int s, int d) { + this.src = s; + this.dest = d; + } + } + + // Function to check if a graph is bipartite + public static boolean isbipartite(ArrayList[] graph) { + int col[] = new int[graph.length]; + Arrays.fill(col, -1); // initialize all vertices with -1 (uncolored) + + Queue q = new LinkedList<>(); + + for (int i = 0; i < graph.length; i++) { + if (col[i] == -1) { // unvisited node + q.add(i); + col[i] = 0; // assign first color + + while (!q.isEmpty()) { + int curr = q.remove(); + for (int j = 0; j < graph[curr].size(); j++) { + Edge e = graph[curr].get(j); + + if (col[e.dest] == -1) { + int nextCol = (col[curr] == 0) ? 1 : 0; + col[e.dest] = nextCol; + q.add(e.dest); + } else if (col[e.dest] == col[curr]) { + return false; // same color conflict + } + } + } + } + } + return true; + } + + // Converts adjacency list (int[][]) to Edge-based graph and checks bipartiteness + public boolean isBipartite(int[][] graph) { + ArrayList[] graph2 = new ArrayList[graph.length]; + for (int i = 0; i < graph2.length; i++) { + graph2[i] = new ArrayList<>(); + } + + for (int i = 0; i < graph.length; i++) { + for (int j = 0; j < graph[i].length; j++) { + int neighbor = graph[i][j]; + graph2[i].add(new Edge(i, neighbor)); + } + } + + return isbipartite(graph2); + } + + // Test the function + public static void main(String[] args) { + IsGraphBipartite sol = new IsGraphBipartite(); + + // Example 1: Bipartite graph + int[][] graph1 = { + {1, 3}, + {0, 2}, + {1, 3}, + {0, 2} + }; + System.out.println("Graph 1 is bipartite: " + sol.isBipartite(graph1)); // true + + // Example 2: Non-bipartite graph (odd cycle) + int[][] graph2 = { + {1, 2}, + {0, 2}, + {0, 1} + }; + System.out.println("Graph 2 is bipartite: " + sol.isBipartite(graph2)); // false + } +} From cebd90eab591f46bea55814c30b67cec9b3193b1 Mon Sep 17 00:00:00 2001 From: Sanskar Agrawal Date: Sat, 18 Oct 2025 23:14:17 +0530 Subject: [PATCH 2/3] Implemented Graph Bipartite checking Algorithm in graph folder --- src/main/java/com/thealgorithms/graph/IsGraphBipartite.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/graph/IsGraphBipartite.java b/src/main/java/com/thealgorithms/graph/IsGraphBipartite.java index 475ecd7cf467..1db8d7187f3d 100644 --- a/src/main/java/com/thealgorithms/graph/IsGraphBipartite.java +++ b/src/main/java/com/thealgorithms/graph/IsGraphBipartite.java @@ -81,6 +81,6 @@ public static void main(String[] args) { {0, 2}, {0, 1} }; - System.out.println("Graph 2 is bipartite: " + sol.isBipartite(graph2)); // false + System.out.println("Graph 2 is bipartite: " + sol.isBipartite(graph2)); } } From 56b7cefc5d229aa628138649fb504f831ba3b04f Mon Sep 17 00:00:00 2001 From: Sanskar Agrawal Date: Sat, 18 Oct 2025 23:20:46 +0530 Subject: [PATCH 3/3] Implemented Graph Articulation point checking Algorithm in graph folder --- .../graph/ArticulationPoint.java | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/main/java/com/thealgorithms/graph/ArticulationPoint.java diff --git a/src/main/java/com/thealgorithms/graph/ArticulationPoint.java b/src/main/java/com/thealgorithms/graph/ArticulationPoint.java new file mode 100644 index 000000000000..c210118ee23e --- /dev/null +++ b/src/main/java/com/thealgorithms/graph/ArticulationPoint.java @@ -0,0 +1,70 @@ +package com.thealgorithms.graph; +import java.util.Arrays; +import java.util.ArrayList; +import java.util.List; + +public class ArticulationPoint { + // Inner class to represent an edge + class Edge { + int src; + int dest; + + public Edge(int s, int d) { + this.src = s; + this.dest = d; + } + } + + public static void dfs(ArrayList[] graph, int curr, int par, int[] dt, int[] low, boolean[] vis, int time, + List> res) { + vis[curr] = true; + dt[curr] = low[curr] = ++time; + for (int i = 0; i < graph[curr].size(); i++) { + Edge e = graph[curr].get(i); + int neigh = e.dest; + if (neigh == par) { + continue; + } else if (!vis[neigh]) { + dfs(graph, neigh, curr, dt, low, vis, time, res); + low[curr] = Math.min(low[neigh], low[curr]); + if (dt[curr] < low[neigh]) { + ArrayList pair = new ArrayList<>(); + pair.add(curr); + pair.add(neigh); + res.add(pair); + } + } else { + low[curr] = Math.min(low[curr], dt[neigh]); + } + } + } + + public static List> tarjans(ArrayList[] graph, int V) { + List> res = new ArrayList<>(); + int dt[] = new int[V]; + int low[] = new int[V]; + int time = 0; + boolean vis[] = new boolean[V]; + for (int i = 0; i < V; i++) { + if (!vis[i]) { + dfs(graph, i, -1, dt, low, vis, time, res); + } + } + return res; + } + + public List> criticalConnections(int n, List> connections) { + ArrayList[] graph = new ArrayList[n]; + for (int i = 0; i < graph.length; i++) { + graph[i] = new ArrayList<>(); + } + for (List list : connections) { + int u = list.get(0); + int v = list.get(1); + graph[u].add(new Edge(u, v)); + graph[v].add(new Edge(v, u)); + } + return tarjans(graph, n); + } + +}