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