forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIsGraphBipartite.java
More file actions
86 lines (72 loc) · 2.56 KB
/
IsGraphBipartite.java
File metadata and controls
86 lines (72 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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<Edge>[] graph) {
int col[] = new int[graph.length];
Arrays.fill(col, -1); // initialize all vertices with -1 (uncolored)
Queue<Integer> 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<Edge>[] 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));
}
}