|
| 1 | +package com.thealgorithms.graph; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.Scanner; |
| 5 | + |
| 6 | +/** |
| 7 | + * This class provides an implementation to check whether a given graph is |
| 8 | + * bipartite using Depth First Search (DFS). |
| 9 | + * |
| 10 | + * A bipartite graph is one in which the set of vertices can be divided into two |
| 11 | + * disjoint subsets |
| 12 | + * such that no two vertices within the same subset are adjacent. |
| 13 | + * |
| 14 | + * For more information, see |
| 15 | + * <a href="https://en.wikipedia.org/wiki/Bipartite_graph">Wikipedia: Bipartite |
| 16 | + * Graph</a>. |
| 17 | + * |
| 18 | + * <p> |
| 19 | + * Example: |
| 20 | + * </p> |
| 21 | + * |
| 22 | + * <pre> |
| 23 | + * Input: |
| 24 | + * 4 4 |
| 25 | + * 0 1 |
| 26 | + * 1 2 |
| 27 | + * 2 3 |
| 28 | + * 3 0 |
| 29 | + * |
| 30 | + * Output: |
| 31 | + * Graph is Bipartite: true |
| 32 | + * </pre> |
| 33 | + * |
| 34 | + * Author: Ilma Akram Ansari |
| 35 | + */ |
| 36 | +public final class BipartiteGraphDFS { |
| 37 | + |
| 38 | + // Private constructor to prevent instantiation |
| 39 | + private BipartiteGraphDFS() { |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Checks whether the given undirected graph is bipartite using Depth First |
| 44 | + * Search (DFS). |
| 45 | + * |
| 46 | + * @param graph The adjacency list representation of the graph, |
| 47 | + * where graph[i] contains an array of all vertices adjacent to |
| 48 | + * vertex i. |
| 49 | + * @return {@code true} if the graph is bipartite, otherwise {@code false}. |
| 50 | + */ |
| 51 | + public static boolean isBipartite(int[][] graph) { |
| 52 | + int n = graph.length; |
| 53 | + int[] color = new int[n]; |
| 54 | + Arrays.fill(color, -1); // -1 = uncolored |
| 55 | + |
| 56 | + // Handle disconnected graphs |
| 57 | + for (int i = 0; i < n; i++) { |
| 58 | + if (color[i] == -1) { |
| 59 | + if (!dfs(graph, i, 0, color)) { |
| 60 | + return false; |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + return true; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Recursive DFS helper method to assign colors and check bipartiteness. |
| 69 | + * |
| 70 | + * @param graph The adjacency list of the graph. |
| 71 | + * @param node The current vertex being explored. |
| 72 | + * @param currColor The color to assign to this vertex (0 or 1). |
| 73 | + * @param color Array storing colors of all vertices. |
| 74 | + * @return {@code true} if no conflict is found, otherwise {@code false}. |
| 75 | + */ |
| 76 | + private static boolean dfs(int[][] graph, int node, int currColor, int[] color) { |
| 77 | + color[node] = currColor; |
| 78 | + |
| 79 | + for (int neighbor : graph[node]) { |
| 80 | + if (color[neighbor] == -1) { |
| 81 | + // Assign opposite color to neighbor |
| 82 | + if (!dfs(graph, neighbor, 1 - currColor, color)) { |
| 83 | + return false; |
| 84 | + } |
| 85 | + } else if (color[neighbor] == currColor) { |
| 86 | + // Found same color on adjacent nodes -> not bipartite |
| 87 | + return false; |
| 88 | + } |
| 89 | + } |
| 90 | + return true; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Main method to take input and check if the graph is bipartite. |
| 95 | + * Input format: |
| 96 | + * n m |
| 97 | + * u1 v1 |
| 98 | + * u2 v2 |
| 99 | + * ... |
| 100 | + * um vm |
| 101 | + */ |
| 102 | + public static void main(String[] args) { |
| 103 | + Scanner sc = new Scanner(System.in); |
| 104 | + |
| 105 | + System.out.println("Enter number of vertices and edges:"); |
| 106 | + int n = sc.nextInt(); // number of vertices |
| 107 | + int m = sc.nextInt(); // number of edges |
| 108 | + |
| 109 | + // Using adjacency list representation |
| 110 | + int[][] temp = new int[n][n]; // temporary adjacency matrix for building adjacency list |
| 111 | + int[] degree = new int[n]; // degree of each vertex |
| 112 | + |
| 113 | + System.out.println("Enter each edge (u v):"); |
| 114 | + for (int i = 0; i < m; i++) { |
| 115 | + int u = sc.nextInt(); |
| 116 | + int v = sc.nextInt(); |
| 117 | + temp[u][degree[u]++] = v; |
| 118 | + temp[v][degree[v]++] = u; // since graph is undirected |
| 119 | + } |
| 120 | + |
| 121 | + // Convert to adjacency list (trim extra zeros) |
| 122 | + int[][] graph = new int[n][]; |
| 123 | + for (int i = 0; i < n; i++) { |
| 124 | + graph[i] = Arrays.copyOf(temp[i], degree[i]); |
| 125 | + } |
| 126 | + |
| 127 | + boolean result = isBipartite(graph); |
| 128 | + System.out.println("Graph is Bipartite: " + result); |
| 129 | + |
| 130 | + sc.close(); |
| 131 | + } |
| 132 | +} |
0 commit comments