Bipartiteness
Last updated
Was this helpful?
Last updated
Was this helpful?
A graph is bipartite if it is possible to break a graph G into two independent sets (of vertexes), where every edge between the two sets is connected to from set 1 to set 2 or vise versa. So a single vertical cut can go through all the edges of the bipartite graph.
If such a rearrangement of graph exists, then the graph is bipartite.
Visually
The two independent sets and can be thought of having as a coloring of a graph using only two colors (a chromatic number of 2) with the restriction no two adjacent vertices share the same color. If such a property can exist in the graph, then the graph is bipartite.
For example, the following Peterson graph can be shown to have a minimumal coloring of 3.
Additionally, a graph is bipartite if and only if it does not contain an odd cycle (a cycle with an an odd number of vertices). Consider a triangle, for example.
Self Notes
Assumptions
Implementation from undirected weighted hashlist (from Book A)
Input graph is a single connected component.
Basically what I have done is alternate between two colors (red, and blue) and run a DFS on the graph, alternating colors as I go. As DFS backtracks, it will ensure that all nodes adjacent to some node will be a different color, otherwise the algorithm seizes and false
is returned.
Implementation
Test Files
Non-bipartite graphs:
Bipartite Graphs
Python Implementation
Same idea, except this time the input is more transparent as to what is going on and we're going with a BFS. Initially, all colors are set as NONE, which will also be used to indicate whether it is visited or not. Then for every node, we check if the neighbors have the same color as the parent. Otherwise, map between RED and BLUE depending on the color of the parent.