-
-
Notifications
You must be signed in to change notification settings - Fork 50.1k
Enhance exponential_search doctests with comprehensive examples #14286
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,20 @@ | ||
| from collections import defaultdict, deque | ||
| from collections.abc import Hashable, Iterable, Mapping | ||
|
|
||
|
|
||
| def is_bipartite_dfs(graph: dict[int, list[int]]) -> bool: | ||
| def is_bipartite_dfs(graph: Mapping[Hashable, Iterable[Hashable]]) -> bool: | ||
| """ | ||
| Check if a graph is bipartite using depth-first search (DFS). | ||
| Args: | ||
| `graph`: Adjacency list representing the graph. | ||
| `graph`: Mapping of nodes to their neighbors. Nodes must be hashable. | ||
| Returns: | ||
| ``True`` if bipartite, ``False`` otherwise. | ||
| Checks if the graph can be divided into two sets of vertices, such that no two | ||
| vertices within the same set are connected by an edge. | ||
| vertices within the same set are connected by an edge. Neighbor nodes that do | ||
| not appear as keys are treated as isolated nodes with no outgoing edges. | ||
| Examples: | ||
|
|
@@ -33,7 +35,6 @@ def is_bipartite_dfs(graph: dict[int, list[int]]) -> bool: | |
| >>> is_bipartite_dfs({7: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 4: [0]}) | ||
| False | ||
| >>> # FIXME: This test should fails with KeyError: 4. | ||
| >>> is_bipartite_dfs({0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 9: [0]}) | ||
| False | ||
| >>> is_bipartite_dfs({0: [-1, 3], 1: [0, -2]}) | ||
|
|
@@ -43,8 +44,6 @@ def is_bipartite_dfs(graph: dict[int, list[int]]) -> bool: | |
| >>> is_bipartite_dfs({0.9: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2]}) | ||
| True | ||
| >>> # FIXME: This test should fails with | ||
| >>> # TypeError: list indices must be integers or... | ||
| >>> is_bipartite_dfs({0: [1.0, 3.0], 1.0: [0, 2.0], 2.0: [1.0, 3.0], 3.0: [0, 2.0]}) | ||
| True | ||
| >>> is_bipartite_dfs({"a": [1, 3], "b": [0, 2], "c": [1, 3], "d": [0, 2]}) | ||
|
|
@@ -53,7 +52,7 @@ def is_bipartite_dfs(graph: dict[int, list[int]]) -> bool: | |
| True | ||
| """ | ||
|
|
||
| def depth_first_search(node: int, color: int) -> bool: | ||
| def depth_first_search(node: Hashable, color: int) -> bool: | ||
| """ | ||
| Perform Depth-First Search (DFS) on the graph starting from a node. | ||
|
|
@@ -74,25 +73,26 @@ def depth_first_search(node: int, color: int) -> bool: | |
| return False | ||
| return visited[node] == color | ||
|
|
||
| visited: defaultdict[int, int] = defaultdict(lambda: -1) | ||
| visited: defaultdict[Hashable, int] = defaultdict(lambda: -1) | ||
| for node in graph: | ||
| if visited[node] == -1 and not depth_first_search(node, 0): | ||
| return False | ||
| return True | ||
|
|
||
|
|
||
| def is_bipartite_bfs(graph: dict[int, list[int]]) -> bool: | ||
| def is_bipartite_bfs(graph: Mapping[Hashable, Iterable[Hashable]]) -> bool: | ||
| """ | ||
| Check if a graph is bipartite using a breadth-first search (BFS). | ||
| Args: | ||
| `graph`: Adjacency list representing the graph. | ||
| `graph`: Mapping of nodes to their neighbors. Nodes must be hashable. | ||
| Returns: | ||
| ``True`` if bipartite, ``False`` otherwise. | ||
| Check if the graph can be divided into two sets of vertices, such that no two | ||
| vertices within the same set are connected by an edge. | ||
| vertices within the same set are connected by an edge. Neighbor nodes that do | ||
| not appear as keys are treated as isolated nodes with no outgoing edges. | ||
|
Comment on lines
93
to
+95
|
||
| Examples: | ||
|
|
@@ -113,7 +113,6 @@ def is_bipartite_bfs(graph: dict[int, list[int]]) -> bool: | |
| >>> is_bipartite_bfs({7: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 4: [0]}) | ||
| False | ||
| >>> # FIXME: This test should fails with KeyError: 4. | ||
| >>> is_bipartite_bfs({0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 9: [0]}) | ||
| False | ||
| >>> is_bipartite_bfs({0: [-1, 3], 1: [0, -2]}) | ||
|
|
@@ -123,19 +122,17 @@ def is_bipartite_bfs(graph: dict[int, list[int]]) -> bool: | |
| >>> is_bipartite_bfs({0.9: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2]}) | ||
| True | ||
| >>> # FIXME: This test should fails with | ||
| >>> # TypeError: list indices must be integers or... | ||
| >>> is_bipartite_bfs({0: [1.0, 3.0], 1.0: [0, 2.0], 2.0: [1.0, 3.0], 3.0: [0, 2.0]}) | ||
| True | ||
| >>> is_bipartite_bfs({"a": [1, 3], "b": [0, 2], "c": [1, 3], "d": [0, 2]}) | ||
| True | ||
| >>> is_bipartite_bfs({0: ["b", "d"], 1: ["a", "c"], 2: ["b", "d"], 3: ["a", "c"]}) | ||
| True | ||
| """ | ||
| visited: defaultdict[int, int] = defaultdict(lambda: -1) | ||
| visited: defaultdict[Hashable, int] = defaultdict(lambda: -1) | ||
| for node in graph: | ||
| if visited[node] == -1: | ||
| queue: deque[int] = deque() | ||
| queue: deque[Hashable] = deque() | ||
| queue.append(node) | ||
| visited[node] = 0 | ||
| while queue: | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.