5 Best Ways to Check Whether a Graph is Bipartite in Python

Rate this post

πŸ’‘ Problem Formulation: You need to determine if a graph is bipartite, where a bipartite graph can be separated into two disjoint vertex sets such that no two vertices within the same set are adjacent. An input example might be an adjacency list representing the graph, and the desired output would be a boolean value indicating bipartiteness.

Method 1: Depth-First Search (DFS)

The DFS method entails starting at an arbitrary node and assigning it a color. Then, for each neighbor, if it’s uncolored, we recursively assign an alternate color. If we find a neighbor with the same color, the graph isn’t bipartite. This technique is a straightforward recursive algorithm for bipartiteness checking.

Here’s an example:

def is_bipartite_dfs(graph, node, color):
    if node not in color:
        color[node] = 0
    for neighbor in graph[node]:
        if neighbor in color:
            if color[neighbor] == color[node]:
                return False
        else:
            color[neighbor] = 1 - color[node]
            if not is_bipartite_dfs(graph, neighbor, color):
                return False
    return True

graph = {0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2]}
print(is_bipartite_dfs(graph, 0, {}))

Output: True

The function is_bipartite_dfs() is called with the graph and an empty dictionary for colors. The graph is traversed using depth-first search, and as it proceeds, it colors each node. If it identifies a conflict where two neighboring nodes have the same color, it returns False, indicating the graph isn’t bipartite. In this example, since the graph can be split into two sets – {0, 2} and {1, 3}, without neighbors being in the same set, the output is True.

Method 2: Breadth-First Search (BFS)

The BFS method uses a queue to traverse the graph level by level, coloring nodes with alternating colors as it goes. If a node is found to conflict with the coloring rule, the graph isn’t bipartite. BFS is an iterative alternative to the recursive DFS and is often more memory-efficient for larger graphs.

Here’s an example:

from collections import deque

def is_bipartite_bfs(graph, start):
    color = {}
    queue = deque([start])
    color[start] = 0
    
    while queue:
        node = queue.popleft()
        for neighbor in graph[node]:
            if neighbor in color:
                if color[neighbor] == color[node]:
                    return False
            else:
                color[neighbor] = 1 - color[node]
                queue.append(neighbor)
    return True

graph = {0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2]}
print(is_bipartite_bfs(graph, 0))

Output: True

In this code example, we utilize is_bipartite_bfs() to perform a level-by-level traversal of the provided graph using a queue, coloring nodes alternately based on their levels. Just like with DFS, it checks for same-colored neighboring nodes to determine bipartiteness. The graph provided is indeed bipartite, hence the function returns True.

Method 3: Union-Find Algorithm

The Union-Find algorithm is a data structure that keeps track of elements which are split into one or more disjoint sets. It supports two useful operations: Find and Union. For bipartite checks, we can iterate through edges, applying Union-Find operations to detect if there’s a cycle that would violate the bipartite properties.

Here’s an example:

def find(parent, i):
    if parent[i] == -1:
        return i
    return find(parent, parent[i])

def is_bipartite_union_find(graph, num_vertices):
    parent = [-1] * num_vertices
    for u in range(num_vertices):
        for v in graph[u]:
            x = find(parent, u)
            y = find(parent, v)
            if x == y:
                return False
            parent[x] = y
    return True

graph = {0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2]}
print(is_bipartite_union_find(graph, 4))

Output: True

The is_bipartite_union_find() function checks the graph bipartiteness using the Union-Find algorithm. It iteratively applies Find to determine the roots of the vertices and Union to merge the sets. If two vertices connected by an edge share the same root, there’s a cycle which makes the graph not bipartite. This graph passes the check, confirming it’s bipartite.

Method 4: Color-based Graph Validation

Color-based graph validation is similar to the DFS and BFS methods but focuses specifically on the use of a coloring heuristic to validate bipartiteness. Each node is assigned a color such that no two adjacent nodes share the same color, generally using only two colors.

Here’s an example:

def is_bipartite_color_validation(graph):
    color = [None] * len(graph)
    for node in range(len(graph)):
        if color[node] is None:
            if not valid_color(graph, node, color, True):
                return False
    return True

def valid_color(graph, node, color, current_color):
    if color[node] is not None:
        return color[node] == current_color
    color[node] = current_color
    return all(valid_color(graph, neighbor, color, not current_color) for neighbor in graph[node])

graph = {0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2]}
print(is_bipartite_color_validation(graph))

Output: True

This code initiates a check for each uncolored node and attempts to color the graph using a backtracking approach. It uses the helper function valid_color() to appropriately color each node. If at any point the function is unable to color the graph without causing a color conflict, it returns False. The graph in this example is found to be bipartite as it can be separated into two sets by colors without any issue.

Bonus One-Liner Method 5: NetworkX Library

For programmers who need a quick, one-liner solution, the NetworkX library in Python provides direct support for checking if a graph is bipartite. The is_bipartite() function does all the heavy lifting for you.

Here’s an example:

import networkx as nx

G = nx.Graph()
G.add_edges_from([(0, 1), (1, 2), (2, 3), (3, 0)])
print(nx.is_bipartite(G))

Output: True

In this succinct code snippet, we use the NetworkX package to create a graph and add edges. With a single call to nx.is_bipartite(G), we determine whether the graph is bipartite. The output confirms that the sample graph provided is indeed bipartite.

Summary/Discussion

  • Method 1: Depth-First Search (DFS). Thorough traversing approach. Can be less optimal for large graphs due to recursive call stack limits.
  • Method 2: Breadth-First Search (BFS). Iterative and memory-efficient for wide graphs. Requires more coding efforts compared to DFS.
  • Method 3: Union-Find Algorithm. Excellent for cycle detection. Can be overkill for small or simple graphs.
  • Method 4: Color-based Graph Validation. Heuristic and easy to understand. Performance can be hindered by the need to color every node.
  • Bonus Method 5: NetworkX Library. Easy and fast for small graphs. May not be practical for environments where external libraries are not allowed or where graph data handling needs to be custom-coded.