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); + } + +} 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..1db8d7187f3d --- /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)); + } +}