|
| 1 | +from collections import defaultdict, deque |
| 2 | + |
| 3 | + |
| 4 | +def is_bipartite_dfs(graph: defaultdict[int, list[int]]) -> bool: |
| 5 | + """ |
| 6 | + Check if a graph is bipartite using DFS. |
| 7 | +
|
| 8 | + Args: |
| 9 | + graph (defaultdict[int, list[int]]): Adjacency list representing the graph. |
| 10 | +
|
| 11 | + Returns: |
| 12 | + bool: True if bipartite, False otherwise. |
| 13 | +
|
| 14 | + This function checks if the graph can be divided into two sets of vertices, |
| 15 | + such that no two vertices within the same set are connected by an edge. |
| 16 | +
|
| 17 | + Examples: |
| 18 | + >>> is_bipartite_dfs(defaultdict(list, {0: [1, 2], 1: [0, 3], 2: [0, 4]})) |
| 19 | + True |
| 20 | + >>> is_bipartite_dfs(defaultdict(list, {0: [1, 2], 1: [0, 3], 2: [0, 1]})) |
| 21 | + False |
| 22 | + """ |
| 23 | + |
| 24 | + def dfs(node, color): |
| 25 | + """ |
| 26 | + Perform Depth-First Search (DFS) on the graph starting from a node. |
| 27 | +
|
| 28 | + Args: |
| 29 | + node: The current node being visited. |
| 30 | + color: The color assigned to the current node. |
| 31 | +
|
| 32 | + Returns: |
| 33 | + bool: True if the graph is bipartite starting from the current node, False otherwise. |
| 34 | + """ |
| 35 | + if visited[node] == -1: |
| 36 | + visited[node] = color |
| 37 | + for neighbor in graph[node]: |
| 38 | + if not dfs(neighbor, 1 - color): |
| 39 | + return False |
| 40 | + return visited[node] == color |
| 41 | + |
| 42 | + visited = defaultdict(lambda: -1) |
| 43 | + for node in graph: |
| 44 | + if visited[node] == -1 and not dfs(node, 0): |
| 45 | + return False |
| 46 | + return True |
| 47 | + |
| 48 | + |
| 49 | +def is_bipartite_bfs(graph: defaultdict[int, list[int]]) -> bool: |
| 50 | + """ |
| 51 | + Check if a graph is bipartite using BFS. |
| 52 | +
|
| 53 | + Args: |
| 54 | + graph (defaultdict[int, list[int]]): Adjacency list representing the graph. |
| 55 | +
|
| 56 | + Returns: |
| 57 | + bool: True if bipartite, False otherwise. |
| 58 | +
|
| 59 | + This function checks if the graph can be divided into two sets of vertices, |
| 60 | + such that no two vertices within the same set are connected by an edge. |
| 61 | +
|
| 62 | + Examples: |
| 63 | + >>> is_bipartite_bfs(defaultdict(list, {0: [1, 2], 1: [0, 3], 2: [0, 4]})) |
| 64 | + True |
| 65 | + >>> is_bipartite_bfs(defaultdict(list, {0: [1, 2], 1: [0, 2], 2: [0, 1]})) |
| 66 | + False |
| 67 | + """ |
| 68 | + visited = defaultdict(lambda: -1) |
| 69 | + for node in graph: |
| 70 | + if visited[node] == -1: |
| 71 | + queue = deque() |
| 72 | + queue.append(node) |
| 73 | + visited[node] = 0 |
| 74 | + while queue: |
| 75 | + curr_node = queue.popleft() |
| 76 | + for neighbor in graph[curr_node]: |
| 77 | + if visited[neighbor] == -1: |
| 78 | + visited[neighbor] = 1 - visited[curr_node] |
| 79 | + queue.append(neighbor) |
| 80 | + elif visited[neighbor] == visited[curr_node]: |
| 81 | + return False |
| 82 | + return True |
| 83 | + |
| 84 | + |
| 85 | +if __name__ == "__main": |
| 86 | + import doctest |
| 87 | + |
| 88 | + result = doctest.testmod() |
| 89 | + |
| 90 | + if result.failed: |
| 91 | + print(f"{result.failed} test(s) failed.") |
| 92 | + else: |
| 93 | + print("All tests passed!") |
0 commit comments