|
10 | 10 |
|
11 | 11 |
|
12 | 12 | def check_bipartite(graph):
|
| 13 | + """ |
| 14 | + >>> check_bipartite({}) |
| 15 | + True |
| 16 | + >>> check_bipartite({0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2]}) |
| 17 | + True |
| 18 | + >>> check_bipartite({0: [1, 2, 3], 1: [0, 2], 2: [0, 1, 3], 3: [0, 2]}) |
| 19 | + False |
| 20 | + >>> check_bipartite({0: [4], 1: [], 2: [4], 3: [4], 4: [0, 2, 3]}) |
| 21 | + True |
| 22 | + >>> check_bipartite({0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 4: [0]}) |
| 23 | + False |
| 24 | + >>> check_bipartite({7: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 4: [0]}) |
| 25 | + Traceback (most recent call last): |
| 26 | + ... |
| 27 | + KeyError: 0 |
| 28 | + >>> check_bipartite({0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 9: [0]}) |
| 29 | + Traceback (most recent call last): |
| 30 | + ... |
| 31 | + KeyError: 4 |
| 32 | + >>> check_bipartite({0: [-1, 3], 1: [0, -2]}) |
| 33 | + Traceback (most recent call last): |
| 34 | + ... |
| 35 | + IndexError: list index out of range |
| 36 | + >>> check_bipartite({-1: [0, 2], 0: [-1, 1], 1: [0, 2], 2: [-1, 1]}) |
| 37 | + True |
| 38 | + >>> check_bipartite({0.9: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2]}) |
| 39 | + Traceback (most recent call last): |
| 40 | + ... |
| 41 | + KeyError: 0 |
| 42 | + >>> check_bipartite({0: [1.0, 3.0], 1.0: [0, 2.0], 2.0: [1.0, 3.0], 3.0: [0, 2.0]}) |
| 43 | + Traceback (most recent call last): |
| 44 | + ... |
| 45 | + TypeError: list indices must be integers or slices, not float |
| 46 | + >>> check_bipartite({"a": [1, 3], "b": [0, 2], "c": [1, 3], "d": [0, 2]}) |
| 47 | + Traceback (most recent call last): |
| 48 | + ... |
| 49 | + KeyError: 0 |
| 50 | + >>> check_bipartite({0: ["b", "d"], 1: ["a", "c"], 2: ["b", "d"], 3: ["a", "c"]}) |
| 51 | + Traceback (most recent call last): |
| 52 | + ... |
| 53 | + TypeError: list indices must be integers or slices, not str |
| 54 | + """ |
13 | 55 | queue = Queue()
|
14 | 56 | visited = [False] * len(graph)
|
15 | 57 | color = [-1] * len(graph)
|
@@ -45,3 +87,6 @@ def bfs():
|
45 | 87 | if __name__ == "__main__":
|
46 | 88 | # Adjacency List of graph
|
47 | 89 | print(check_bipartite({0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2]}))
|
| 90 | + import doctest |
| 91 | + |
| 92 | + doctest.testmod() |
0 commit comments