|
| 1 | +from collections import defaultdict |
| 2 | +from queue import Queue |
| 3 | + |
| 4 | + |
| 5 | +def check_bipartite(graph: dict[int, list[int]]) -> bool: |
| 6 | + """ |
| 7 | + Check if a graph is Bipartite using Depth-First Search. |
| 8 | +
|
| 9 | + Args: |
| 10 | + graph: Adjacency list representing the graph. |
| 11 | +
|
| 12 | + Returns: |
| 13 | + bool: True if no edge connects same set vertices. |
| 14 | +
|
| 15 | + Examples: |
| 16 | + >>> is_bipartite(defaultdict(list, {0: [1, 2], 1: [0, 3], ...})) |
| 17 | + False |
| 18 | + >>> is_bipartite(defaultdict(list, {0: [1, 2], 1: [0, 2], 2: [0, 1]})) |
| 19 | + True |
| 20 | + """ |
| 21 | + queue: Queue = Queue() |
| 22 | + visited = [False] * len(graph) |
| 23 | + color = [-1] * len(graph) |
| 24 | + |
| 25 | + def bfs() -> bool: |
| 26 | + """ |
| 27 | + Perform Breadth-First Search (BFS) on a graph to check if it's bipartite. |
| 28 | +
|
| 29 | + Args: |
| 30 | + graph (dict[int, list[int]]): An adjacency list representing the graph. |
| 31 | +
|
| 32 | + Returns: |
| 33 | + bool: True if there's no edge, False otherwise. |
| 34 | +
|
| 35 | + Examples: |
| 36 | + >>> bfs({0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2]}) |
| 37 | + True |
| 38 | + >>> bfs({0: [1, 2, 3], 1: [0, 2], 2: [0, 1, 3], 3: [0, 2]}) |
| 39 | + False |
| 40 | + """ |
| 41 | + while not queue.empty(): |
| 42 | + u = queue.get() |
| 43 | + visited[u] = True |
| 44 | + |
| 45 | + for neighbour in graph[u]: |
| 46 | + if neighbour == u: |
| 47 | + return False |
| 48 | + |
| 49 | + if color[neighbour] == -1: |
| 50 | + color[neighbour] = 1 - color[u] |
| 51 | + queue.put(neighbour) |
| 52 | + |
| 53 | + elif color[neighbour] == color[u]: |
| 54 | + return False |
| 55 | + |
| 56 | + return True |
| 57 | + |
| 58 | + for i in range(len(graph)): |
| 59 | + if not visited[i]: |
| 60 | + queue.put(i) |
| 61 | + color[i] = 0 |
| 62 | + if bfs() is False: |
| 63 | + return False |
| 64 | + |
| 65 | + return True |
| 66 | + |
| 67 | + |
| 68 | +if __name__ == "__main__": |
| 69 | + # Adjacency List of graph |
| 70 | + print(check_bipartite({0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2]})) |
| 71 | + |
| 72 | + |
| 73 | +def is_bipartite(graph: defaultdict[int, list[int]]) -> bool: |
| 74 | + """ |
| 75 | + Check if a graph is Bipartite using Breadth-First Search. |
| 76 | +
|
| 77 | + Args: |
| 78 | + graph: Adjacency list representing the graph. |
| 79 | +
|
| 80 | + Returns: |
| 81 | + bool: True if no edge connects same set vertices. |
| 82 | +
|
| 83 | + Examples: |
| 84 | + >>> check_bipartite({0: [1, 3], 1: [0, 2], 2: [1, 3], ...}) |
| 85 | + True |
| 86 | + >>> check_bipartite({0: [1, 2, 3], 1: [0, 2], ...}) |
| 87 | + False |
| 88 | + """ |
| 89 | + |
| 90 | + def dfs(node: int, color: int) -> bool: |
| 91 | + """ |
| 92 | + Perform depth-first search from a node with specified color. |
| 93 | +
|
| 94 | + Args: |
| 95 | + node (int): Current node being visited. |
| 96 | + color (int): Color assigned to the current node. |
| 97 | +
|
| 98 | + Returns: |
| 99 | + bool: True if the graph is bipartite fromcurrent node,else False. |
| 100 | +
|
| 101 | + Examples: |
| 102 | + >>> dfs(0, 0, defaultdict(list, {0: [1, 2], ...})) |
| 103 | + False |
| 104 | + >>> dfs(0, 0, defaultdict(list, {0: [1, 2], 1: [0, 2], ...})) |
| 105 | + True |
| 106 | + """ |
| 107 | + visited[node] = color |
| 108 | + return any( |
| 109 | + visited[neighbour] == color |
| 110 | + or (visited[neighbour] == -1 and not dfs(neighbour, 1 - color)) |
| 111 | + for neighbour in graph[node] |
| 112 | + ) |
| 113 | + |
| 114 | + visited: defaultdict[int, int] = defaultdict(lambda: -1) |
| 115 | + |
| 116 | + return all(not (visited[node] == -1 and not dfs(node, 0)) for node in graph) |
| 117 | + |
| 118 | + |
| 119 | +if __name__ == "__main__": |
| 120 | + import doctest |
| 121 | + |
| 122 | + result = doctest.testmod() |
| 123 | + |
| 124 | + if result.failed: |
| 125 | + print(f"{result.failed} test(s) failed.") |
| 126 | + else: |
| 127 | + print("All tests passed!") |
| 128 | + |
0 commit comments