|
| 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 depth-first search (DFS). |
| 7 | +
|
| 8 | + Args: |
| 9 | + graph: Adjacency list representing the graph. |
| 10 | +
|
| 11 | + Returns: |
| 12 | + True if bipartite, False otherwise. |
| 13 | +
|
| 14 | + Checks if the graph can be divided into two sets of vertices, such that no two |
| 15 | + vertices within the same set are connected by an edge. |
| 16 | +
|
| 17 | + Examples: |
| 18 | + # FIXME: This test should pass. |
| 19 | + >>> is_bipartite_dfs(defaultdict(list, {0: [1, 2], 1: [0, 3], 2: [0, 4]})) |
| 20 | + Traceback (most recent call last): |
| 21 | + ... |
| 22 | + RuntimeError: dictionary changed size during iteration |
| 23 | + >>> is_bipartite_dfs(defaultdict(list, {0: [1, 2], 1: [0, 3], 2: [0, 1]})) |
| 24 | + False |
| 25 | + >>> is_bipartite_dfs({}) |
| 26 | + True |
| 27 | + >>> is_bipartite_dfs({0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2]}) |
| 28 | + True |
| 29 | + >>> is_bipartite_dfs({0: [1, 2, 3], 1: [0, 2], 2: [0, 1, 3], 3: [0, 2]}) |
| 30 | + False |
| 31 | + >>> is_bipartite_dfs({0: [4], 1: [], 2: [4], 3: [4], 4: [0, 2, 3]}) |
| 32 | + True |
| 33 | + >>> is_bipartite_dfs({0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 4: [0]}) |
| 34 | + False |
| 35 | + >>> is_bipartite_dfs({7: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 4: [0]}) |
| 36 | + Traceback (most recent call last): |
| 37 | + ... |
| 38 | + KeyError: 0 |
| 39 | +
|
| 40 | + # FIXME: This test should fails with KeyError: 4. |
| 41 | + >>> is_bipartite_dfs({0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 9: [0]}) |
| 42 | + False |
| 43 | + >>> is_bipartite_dfs({0: [-1, 3], 1: [0, -2]}) |
| 44 | + Traceback (most recent call last): |
| 45 | + ... |
| 46 | + KeyError: -1 |
| 47 | + >>> is_bipartite_dfs({-1: [0, 2], 0: [-1, 1], 1: [0, 2], 2: [-1, 1]}) |
| 48 | + True |
| 49 | + >>> is_bipartite_dfs({0.9: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2]}) |
| 50 | + Traceback (most recent call last): |
| 51 | + ... |
| 52 | + KeyError: 0 |
| 53 | +
|
| 54 | + # FIXME: This test should fails with TypeError: list indices must be integers or... |
| 55 | + >>> is_bipartite_dfs({0: [1.0, 3.0], 1.0: [0, 2.0], 2.0: [1.0, 3.0], 3.0: [0, 2.0]}) |
| 56 | + True |
| 57 | + >>> is_bipartite_dfs({"a": [1, 3], "b": [0, 2], "c": [1, 3], "d": [0, 2]}) |
| 58 | + Traceback (most recent call last): |
| 59 | + ... |
| 60 | + KeyError: 1 |
| 61 | + >>> is_bipartite_dfs({0: ["b", "d"], 1: ["a", "c"], 2: ["b", "d"], 3: ["a", "c"]}) |
| 62 | + Traceback (most recent call last): |
| 63 | + ... |
| 64 | + KeyError: 'b' |
| 65 | + """ |
| 66 | + |
| 67 | + def depth_first_search(node: int, color: int) -> bool: |
| 68 | + """ |
| 69 | + Perform Depth-First Search (DFS) on the graph starting from a node. |
| 70 | +
|
| 71 | + Args: |
| 72 | + node: The current node being visited. |
| 73 | + color: The color assigned to the current node. |
| 74 | +
|
| 75 | + Returns: |
| 76 | + True if the graph is bipartite starting from the current node, |
| 77 | + False otherwise. |
| 78 | + """ |
| 79 | + if visited[node] == -1: |
| 80 | + visited[node] = color |
| 81 | + for neighbor in graph[node]: |
| 82 | + if not depth_first_search(neighbor, 1 - color): |
| 83 | + return False |
| 84 | + return visited[node] == color |
| 85 | + |
| 86 | + visited: defaultdict[int, int] = defaultdict(lambda: -1) |
| 87 | + for node in graph: |
| 88 | + if visited[node] == -1 and not depth_first_search(node, 0): |
| 89 | + return False |
| 90 | + return True |
| 91 | + |
| 92 | + |
| 93 | +def is_bipartite_bfs(graph: defaultdict[int, list[int]]) -> bool: |
| 94 | + """ |
| 95 | + Check if a graph is bipartite using a breadth-first search (BFS). |
| 96 | +
|
| 97 | + Args: |
| 98 | + graph: Adjacency list representing the graph. |
| 99 | +
|
| 100 | + Returns: |
| 101 | + True if bipartite, False otherwise. |
| 102 | +
|
| 103 | + Check if the graph can be divided into two sets of vertices, such that no two |
| 104 | + vertices within the same set are connected by an edge. |
| 105 | +
|
| 106 | + Examples: |
| 107 | + # FIXME: This test should pass. |
| 108 | + >>> is_bipartite_bfs(defaultdict(list, {0: [1, 2], 1: [0, 3], 2: [0, 4]})) |
| 109 | + Traceback (most recent call last): |
| 110 | + ... |
| 111 | + RuntimeError: dictionary changed size during iteration |
| 112 | + >>> is_bipartite_bfs(defaultdict(list, {0: [1, 2], 1: [0, 2], 2: [0, 1]})) |
| 113 | + False |
| 114 | + >>> is_bipartite_bfs({}) |
| 115 | + True |
| 116 | + >>> is_bipartite_bfs({0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2]}) |
| 117 | + True |
| 118 | + >>> is_bipartite_bfs({0: [1, 2, 3], 1: [0, 2], 2: [0, 1, 3], 3: [0, 2]}) |
| 119 | + False |
| 120 | + >>> is_bipartite_bfs({0: [4], 1: [], 2: [4], 3: [4], 4: [0, 2, 3]}) |
| 121 | + True |
| 122 | + >>> is_bipartite_bfs({0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 4: [0]}) |
| 123 | + False |
| 124 | + >>> is_bipartite_bfs({7: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 4: [0]}) |
| 125 | + Traceback (most recent call last): |
| 126 | + ... |
| 127 | + KeyError: 0 |
| 128 | +
|
| 129 | + # FIXME: This test should fails with KeyError: 4. |
| 130 | + >>> is_bipartite_bfs({0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 9: [0]}) |
| 131 | + False |
| 132 | + >>> is_bipartite_bfs({0: [-1, 3], 1: [0, -2]}) |
| 133 | + Traceback (most recent call last): |
| 134 | + ... |
| 135 | + KeyError: -1 |
| 136 | + >>> is_bipartite_bfs({-1: [0, 2], 0: [-1, 1], 1: [0, 2], 2: [-1, 1]}) |
| 137 | + True |
| 138 | + >>> is_bipartite_bfs({0.9: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2]}) |
| 139 | + Traceback (most recent call last): |
| 140 | + ... |
| 141 | + KeyError: 0 |
| 142 | +
|
| 143 | + # FIXME: This test should fails with TypeError: list indices must be integers or... |
| 144 | + >>> is_bipartite_bfs({0: [1.0, 3.0], 1.0: [0, 2.0], 2.0: [1.0, 3.0], 3.0: [0, 2.0]}) |
| 145 | + True |
| 146 | + >>> is_bipartite_bfs({"a": [1, 3], "b": [0, 2], "c": [1, 3], "d": [0, 2]}) |
| 147 | + Traceback (most recent call last): |
| 148 | + ... |
| 149 | + KeyError: 1 |
| 150 | + >>> is_bipartite_bfs({0: ["b", "d"], 1: ["a", "c"], 2: ["b", "d"], 3: ["a", "c"]}) |
| 151 | + Traceback (most recent call last): |
| 152 | + ... |
| 153 | + KeyError: 'b' |
| 154 | + """ |
| 155 | + visited: defaultdict[int, int] = defaultdict(lambda: -1) |
| 156 | + for node in graph: |
| 157 | + if visited[node] == -1: |
| 158 | + queue: deque[int] = deque() |
| 159 | + queue.append(node) |
| 160 | + visited[node] = 0 |
| 161 | + while queue: |
| 162 | + curr_node = queue.popleft() |
| 163 | + for neighbor in graph[curr_node]: |
| 164 | + if visited[neighbor] == -1: |
| 165 | + visited[neighbor] = 1 - visited[curr_node] |
| 166 | + queue.append(neighbor) |
| 167 | + elif visited[neighbor] == visited[curr_node]: |
| 168 | + return False |
| 169 | + return True |
| 170 | + |
| 171 | + |
| 172 | +if __name__ == "__main": |
| 173 | + import doctest |
| 174 | + |
| 175 | + result = doctest.testmod() |
| 176 | + if result.failed: |
| 177 | + print(f"{result.failed} test(s) failed.") |
| 178 | + else: |
| 179 | + print("All tests passed!") |
0 commit comments