Check whether a given graph is Bipartite or not - GeeksforGeeks
A Bipartite Graph is a graph whose vertices can be divided into two independent sets, U and V such that every edge (u, v) either connects a vertex from U to V or a vertex from V to U. In other words, for every edge (u, v), either u belongs to U and v to V, or u belongs to V and v to U. We can also say that there is no edge that connects vertices of same set.
A bipartite graph is possible if the graph coloring is possible using two colors such that vertices in a set are colored with the same color.
Graph BFS:
2. Color all the neighbors with BLUE color (putting into set V).
3. Color all neighbor’s neighbor with RED color (putting into set U).
4. This way, assign color to all vertices such that it satisfies all the constraints of m way coloring problem where m = 2.
5. While assigning colors, if we find a neighbor which is colored with same color as current vertex, then the graph cannot be colored with 2 vertices (or graph is not Bipartite)
http://likemyblogger.blogspot.com/2015/08/mj-23-bipartite-graph.html
BFS Java Code: http://www.sanfoundry.com/java-program-check-whether-graph-bipartite-using-bfs/
http://karmaandcoding.blogspot.com/2012/03/bipartite-graph-check.html
http://www.fgdsb.com/2015/01/03/check-whether-a-graph-is-bipartite-or-not/
DFS:
http://algs4.cs.princeton.edu/41undirected/Bipartite.java.html
http://www.sanfoundry.com/cpp-program-graph-bipartite-dfs/
public static <T> boolean isBipartite(UndirectedGraph<T> g) {
Map<T, Boolean> parityTable = new HashMap<T, Boolean>();
for (T node: g)
if (!parityTable.containsKey(node) &&
!dfsExplore(node, g, parityTable, true))
return false;
return true;
}
private static <T> boolean dfsExplore(T node, UndirectedGraph<T> g,
Map<T, Boolean> parityTable,
boolean parity) {
if (parityTable.containsKey(node))
return parityTable.get(node).equals(parity);
parityTable.put(node, parity);
for (T endpoint: g.edgesFrom(node))
if (!dfsExplore(endpoint, g, parityTable, !parity))
return false;
return true;
}
http://www.sanfoundry.com/java-program-check-whether-graph-bipartite-using-2-color-algorithm/
http://www.cnblogs.com/EdwardLiu/p/6552027.html
Read full article from Check whether a given graph is Bipartite or not - GeeksforGeeks
A Bipartite Graph is a graph whose vertices can be divided into two independent sets, U and V such that every edge (u, v) either connects a vertex from U to V or a vertex from V to U. In other words, for every edge (u, v), either u belongs to U and v to V, or u belongs to V and v to U. We can also say that there is no edge that connects vertices of same set.
A bipartite graph is possible if the graph coloring is possible using two colors such that vertices in a set are colored with the same color.
Graph BFS:
Time Complexity of the above approach is same as that Breadth First Search. In above implementation is O(V^2) where V is number of vertices. If graph is represented using adjacency list, then the complexity becomes O(V+E).
1. Assign RED color to the source vertex (putting into set U).2. Color all the neighbors with BLUE color (putting into set V).
3. Color all neighbor’s neighbor with RED color (putting into set U).
4. This way, assign color to all vertices such that it satisfies all the constraints of m way coloring problem where m = 2.
5. While assigning colors, if we find a neighbor which is colored with same color as current vertex, then the graph cannot be colored with 2 vertices (or graph is not Bipartite)
http://likemyblogger.blogspot.com/2015/08/mj-23-bipartite-graph.html
final
static
int
V =
4
;
// No. of Vertices
// This function returns true if graph G[V][V] is Bipartite, else false
boolean
isBipartite(
int
G[][],
int
src)
{
// Create a color array to store colors assigned to all veritces.
// Vertex number is used as index in this array. The value '-1'
// of colorArr[i] is used to indicate that no color is assigned
// to vertex 'i'. The value 1 is used to indicate first color
// is assigned and value 0 indicates second color is assigned.
int
colorArr[] =
new
int
[V];
for
(
int
i=
0
; i<V; ++i)
colorArr[i] = -
1
;
// Assign first color to source
colorArr[src] =
1
;
// Create a queue (FIFO) of vertex numbers and enqueue
// source vertex for BFS traversal
LinkedList<Integer>q =
new
LinkedList<Integer>();
q.add(src);
// Run while there are vertices in queue (Similar to BFS)
while
(q.size() !=
0
)
{
// Dequeue a vertex from queue
int
u = q.poll();
// Find all non-colored adjacent vertices
for
(
int
v=
0
; v<V; ++v)
{
// An edge from u to v exists and destination v is
// not colored
if
(G[u][v]==
1
&& colorArr[v]==-
1
)
{
// Assign alternate color to this adjacent v of u
colorArr[v] =
1
-colorArr[u];
q.add(v);
}
// An edge from u to v exists and destination v is
// colored with same color as u
else
if
(G[u][v]==
1
&& colorArr[v]==colorArr[u])
return
false
;
}
}
// If we reach here, then all adjacent vertices can
// be colored with alternate color
return
true
;
}
http://karmaandcoding.blogspot.com/2012/03/bipartite-graph-check.html
http://www.fgdsb.com/2015/01/03/check-whether-a-graph-is-bipartite-or-not/
DFS:
http://algs4.cs.princeton.edu/41undirected/Bipartite.java.html
http://www.sanfoundry.com/cpp-program-graph-bipartite-dfs/
private boolean isBipartite; // is the graph bipartite? private boolean[] color; // color[v] gives vertices on one side of bipartition private boolean[] marked; // marked[v] = true if v has been visited in DFS private int[] edgeTo; // edgeTo[v] = last edge on path to v private Stack<Integer> cycle; // odd-length cycle public Bipartite(Graph G) { isBipartite = true; color = new boolean[G.V()]; marked = new boolean[G.V()]; edgeTo = new int[G.V()]; for (int v = 0; v < G.V(); v++) { if (!marked[v]) { dfs(G, v); } } assert check(G); } private void dfs(Graph G, int v) { marked[v] = true; for (int w : G.adj(v)) { // short circuit if odd-length cycle found if (cycle != null) return; // found uncolored vertex, so recur if (!marked[w]) { edgeTo[w] = v; color[w] = !color[v]; dfs(G, w); } // if v-w create an odd-length cycle, find it else if (color[w] == color[v]) { isBipartite = false; cycle = new Stack<Integer>(); cycle.push(w); // don't need this unless you want to include start vertex twice for (int x = v; x != w; x = edgeTo[x]) { cycle.push(x); } cycle.push(w); } } }http://www.keithschwarz.com/interesting/code/bipartite-verify/BipartiteVerify.java.html
public static <T> boolean isBipartite(UndirectedGraph<T> g) {
Map<T, Boolean> parityTable = new HashMap<T, Boolean>();
for (T node: g)
if (!parityTable.containsKey(node) &&
!dfsExplore(node, g, parityTable, true))
return false;
return true;
}
private static <T> boolean dfsExplore(T node, UndirectedGraph<T> g,
Map<T, Boolean> parityTable,
boolean parity) {
if (parityTable.containsKey(node))
return parityTable.get(node).equals(parity);
parityTable.put(node, parity);
for (T endpoint: g.edgesFrom(node))
if (!dfsExplore(endpoint, g, parityTable, !parity))
return false;
return true;
}
http://www.sanfoundry.com/java-program-check-whether-graph-bipartite-using-2-color-algorithm/
http://www.cnblogs.com/EdwardLiu/p/6552027.html
input friends relations{{1,2}, {2,3}, {3,4}}
把人分成两拨,每拨人互相不认识,
所以应该是group1{1,3}, group2{2,4}
这道题应该是how to bipartite a graph
Taken from GeeksforGeeks
Following is a simple algorithm to find out whether a given graph is Birpartite or not using Breadth First Search (BFS) :-
- Assign RED color to the source vertex (putting into set U).
- Color all the neighbors with BLUE color (putting into set V).
- Color all neighbor’s neighbor with RED color (putting into set U).
- This way, assign color to all vertices such that it satisfies all the constraints of m way coloring problem where m = 2.
- While assigning colors, if we find a neighbor which is colored with same color as current vertex, then the graph cannot be colored with 2 vertices (or graph is not Bipartite).
Also, NOTE :-
-> It is possible to color a cycle graph with even cycle using two colors.
-> It is not possible to color a cycle graph with odd cycle using two colors.
EDIT :-
If a graph is not connected, it may have more than one bipartition. You need to check all those components separately with the algorithm as mentioned above.
So, for various disconnected sub-graph of the same graph, you need to perform this bipartition check on all of them separately using the same algorithm discussed above. All of those various disconnected sub-graph of the same graph will account for its own set of bipartition.
And, the graph will be termed bipartite, IF AND ONLY IF, each of its connected components are proved to be bipartite .
6 HashSet<Integer> list1 = new HashSet<Integer>(); 7 HashSet<Integer> list2 = new HashSet<Integer>(); 8 9 public void bfs(int[][] relations) { 10 HashMap<Integer, HashSet<Integer>> graph = new HashMap<Integer, HashSet<Integer>>(); 11 for (int[] each : relations) { 12 if (!graph.containsKey(each[0])) 13 graph.put(each[0], new HashSet<Integer>()); 14 if (!graph.containsKey(each[1])) 15 graph.put(each[1], new HashSet<Integer>()); 16 graph.get(each[0]).add(each[1]); 17 graph.get(each[1]).add(each[0]); 18 } 19 20 21 Queue<Integer> queue = new LinkedList<Integer>(); 22 queue.offer(relations[0][0]); 23 list1.add(relations[0][0]); 24 HashSet<Integer> visited = new HashSet<Integer>(); 25 visited.add(relations[0][0]); 26 int count = 1; 27 while (!queue.isEmpty()) { 28 int size = queue.size(); 29 for (int i=0; i<size; i++) { 30 int person = queue.poll(); 31 HashSet<Integer> friends = graph.get(person); 32 for (int each : friends) { 33 if (list1.contains(each)&&list1.contains(person) || list2.contains(each)&&list2.contains(person)) { 34 list1.clear(); 35 list2.clear(); 36 return; 37 } 38 39 if (!visited.contains(each)) { 40 if (count%2 == 1) list2.add(each); 41 else list1.add(each); 42 queue.offer(each); 43 visited.add(each); 44 } 45 } 46 } 47 count++; 48 } 49 }Graph Coloring | Set 1 (Introduction and Applications)
Read full article from Check whether a given graph is Bipartite or not - GeeksforGeeks