Add 10 graph algorithms#14316
Conversation
There was a problem hiding this comment.
Click here to look at the relevant links ⬇️
🔗 Relevant Links
Repository:
Python:
Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.
algorithms-keeper commands and options
algorithms-keeper actions can be triggered by commenting on this PR:
@algorithms-keeper reviewto trigger the checks for only added pull request files@algorithms-keeper review-allto trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.
| Solve Chinese Postman Problem for weighted undirected graphs. | ||
| """ | ||
|
|
||
| def __init__(self, n: int): |
There was a problem hiding this comment.
Please provide return type hint for the function: __init__. If the function does not return a value, please provide the type hint as: def function() -> None:
Please provide descriptive name for the parameter: n
| self.adj: List[List[Tuple[int, int]]] = [[] for _ in range(n)] | ||
| self.total_weight = 0 | ||
|
|
||
| def add_edge(self, u: int, v: int, w: int) -> None: |
There was a problem hiding this comment.
Please provide descriptive name for the parameter: u
Please provide descriptive name for the parameter: v
Please provide descriptive name for the parameter: w
|
|
||
|
|
||
| def chinese_postman( | ||
| n: int, edges: List[Tuple[int, int, int]] |
There was a problem hiding this comment.
Please provide descriptive name for the parameter: n
| import random | ||
| import time | ||
|
|
||
| def benchmark(): |
There was a problem hiding this comment.
Please provide return type hint for the function: benchmark. If the function does not return a value, please provide the type hint as: def function() -> None:
| Maximum flow using Ford-Fulkerson with Edmonds-Karp (BFS). | ||
| """ | ||
|
|
||
| def __init__(self, n: int): |
There was a problem hiding this comment.
Please provide return type hint for the function: __init__. If the function does not return a value, please provide the type hint as: def function() -> None:
Please provide descriptive name for the parameter: n
| self.rev_graph[b].append(not_a) | ||
| self.rev_graph[a].append(not_b) | ||
|
|
||
| def add_implication(self, i: int, val_i: bool, j: int, val_j: bool) -> None: |
There was a problem hiding this comment.
Please provide descriptive name for the parameter: i
Please provide descriptive name for the parameter: j
| self.graph[a].append(b) | ||
| self.rev_graph[b].append(a) | ||
|
|
||
| def add_nand(self, i: int, val_i: bool, j: int, val_j: bool) -> None: |
There was a problem hiding this comment.
Please provide descriptive name for the parameter: i
Please provide descriptive name for the parameter: j
| visited = [False] * n | ||
| order = [] | ||
|
|
||
| def dfs1(u: int): |
There was a problem hiding this comment.
Please provide return type hint for the function: dfs1. If the function does not return a value, please provide the type hint as: def function() -> None:
Please provide descriptive name for the parameter: u
| component = [-1] * n | ||
| current_comp = 0 | ||
|
|
||
| def dfs2(u: int): |
There was a problem hiding this comment.
Please provide return type hint for the function: dfs2. If the function does not return a value, please provide the type hint as: def function() -> None:
Please provide descriptive name for the parameter: u
|
|
||
|
|
||
| def solve_2sat( | ||
| n: int, clauses: List[Tuple[int, bool, int, bool]] |
There was a problem hiding this comment.
Please provide descriptive name for the parameter: n
There was a problem hiding this comment.
Click here to look at the relevant links ⬇️
🔗 Relevant Links
Repository:
Python:
Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.
algorithms-keeper commands and options
algorithms-keeper actions can be triggered by commenting on this PR:
@algorithms-keeper reviewto trigger the checks for only added pull request files@algorithms-keeper review-allto trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.
| Solve Chinese Postman Problem for weighted undirected graphs. | ||
| """ | ||
|
|
||
| def __init__(self, n: int): |
There was a problem hiding this comment.
Please provide return type hint for the function: __init__. If the function does not return a value, please provide the type hint as: def function() -> None:
Please provide descriptive name for the parameter: n
| self.adj: List[List[Tuple[int, int]]] = [[] for _ in range(n)] | ||
| self.total_weight = 0 | ||
|
|
||
| def add_edge(self, u: int, v: int, w: int) -> None: |
There was a problem hiding this comment.
Please provide descriptive name for the parameter: u
Please provide descriptive name for the parameter: v
Please provide descriptive name for the parameter: w
|
|
||
|
|
||
| def chinese_postman( | ||
| n: int, edges: List[Tuple[int, int, int]] |
There was a problem hiding this comment.
Please provide descriptive name for the parameter: n
| import random | ||
| import time | ||
|
|
||
| def benchmark(): |
There was a problem hiding this comment.
Please provide return type hint for the function: benchmark. If the function does not return a value, please provide the type hint as: def function() -> None:
| Maximum flow using Ford-Fulkerson with Edmonds-Karp (BFS). | ||
| """ | ||
|
|
||
| def __init__(self, n: int): |
There was a problem hiding this comment.
Please provide return type hint for the function: __init__. If the function does not return a value, please provide the type hint as: def function() -> None:
Please provide descriptive name for the parameter: n
| self.rev_graph[b].append(not_a) | ||
| self.rev_graph[a].append(not_b) | ||
|
|
||
| def add_implication(self, i: int, val_i: bool, j: int, val_j: bool) -> None: |
There was a problem hiding this comment.
Please provide descriptive name for the parameter: i
Please provide descriptive name for the parameter: j
| self.graph[a].append(b) | ||
| self.rev_graph[b].append(a) | ||
|
|
||
| def add_nand(self, i: int, val_i: bool, j: int, val_j: bool) -> None: |
There was a problem hiding this comment.
Please provide descriptive name for the parameter: i
Please provide descriptive name for the parameter: j
| visited = [False] * n | ||
| order = [] | ||
|
|
||
| def dfs1(u: int): |
There was a problem hiding this comment.
Please provide return type hint for the function: dfs1. If the function does not return a value, please provide the type hint as: def function() -> None:
Please provide descriptive name for the parameter: u
| component = [-1] * n | ||
| current_comp = 0 | ||
|
|
||
| def dfs2(u: int): |
There was a problem hiding this comment.
Please provide return type hint for the function: dfs2. If the function does not return a value, please provide the type hint as: def function() -> None:
Please provide descriptive name for the parameter: u
|
|
||
|
|
||
| def solve_2sat( | ||
| n: int, clauses: List[Tuple[int, bool, int, bool]] |
There was a problem hiding this comment.
Please provide descriptive name for the parameter: n
New Algorithms: 1. Floyd-Warshall (all-pairs shortest path) - O(V³) 2. Johnson's Algorithm (sparse graph all-pairs) - O(V² log V + VE) 3. Hopcroft-Karp (maximum bipartite matching) - O(E√V) 4. Ford-Fulkerson with Edmonds-Karp (max flow) - O(VE²) 5. Push-Relabel (faster max flow) - O(V²√E) 6. 2-SAT Solver (using SCC) - O(V + E) 7. Chinese Postman Problem (route inspection) - O(V³ + 2^k k²) 8. Traveling Salesman (Held-Karp DP) - O(n² 2ⁿ) 9. Heavy-Light Decomposition (path queries) - O(n log² n) 10. Maximum Bipartite Independent Set - O(E√V) All algorithms include: - Type hints and docstrings with complexity analysis - Doctests with examples - Comprehensive pytest test suite (50+ tests) Test coverage: Added 50+ unit tests
for more information, see https://pre-commit.ci
4cca9f1 to
d063a89
Compare
There was a problem hiding this comment.
Click here to look at the relevant links ⬇️
🔗 Relevant Links
Repository:
Python:
Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.
algorithms-keeper commands and options
algorithms-keeper actions can be triggered by commenting on this PR:
@algorithms-keeper reviewto trigger the checks for only added pull request files@algorithms-keeper review-allto trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.
| from pathlib import Path | ||
|
|
||
|
|
||
| def fix_floyd_warshall(): |
There was a problem hiding this comment.
Please provide return type hint for the function: fix_floyd_warshall. If the function does not return a value, please provide the type hint as: def function() -> None:
| print("fixed graphs/floyd_warshall.py") | ||
|
|
||
|
|
||
| def fix_ford_fulkerson(): |
There was a problem hiding this comment.
Please provide return type hint for the function: fix_ford_fulkerson. If the function does not return a value, please provide the type hint as: def function() -> None:
| p.write_text(text) | ||
| print("fixed graphs/ford_fulkerson.py") | ||
|
|
||
|
|
There was a problem hiding this comment.
Please provide return type hint for the function: fix_heavy_light_decomposition. If the function does not return a value, please provide the type hint as: def function() -> None:
| p.write_text(text) | ||
| print("fixed graphs/heavy_light_decomposition.py") | ||
|
|
||
|
|
There was a problem hiding this comment.
Please provide return type hint for the function: fix_hopcroft_karp. If the function does not return a value, please provide the type hint as: def function() -> None:
| p.write_text(text) | ||
| print("fixed graphs/hopcroft_karp.py") | ||
|
|
||
|
|
There was a problem hiding this comment.
Please provide return type hint for the function: fix_max_bipartite_independent_set. If the function does not return a value, please provide the type hint as: def function() -> None:
| self.rev_graph[b].append(not_a) | ||
| self.rev_graph[a].append(not_b) | ||
|
|
||
| def add_implication(self, i: int, val_i: bool, j: int, val_j: bool) -> None: |
There was a problem hiding this comment.
Please provide descriptive name for the parameter: i
Please provide descriptive name for the parameter: j
| self.graph[a].append(b) | ||
| self.rev_graph[b].append(a) | ||
|
|
||
| def add_nand(self, i: int, val_i: bool, j: int, val_j: bool) -> None: |
There was a problem hiding this comment.
Please provide descriptive name for the parameter: i
Please provide descriptive name for the parameter: j
| visited = [False] * n | ||
| order = [] | ||
|
|
||
| def dfs1(u: int): |
There was a problem hiding this comment.
Please provide return type hint for the function: dfs1. If the function does not return a value, please provide the type hint as: def function() -> None:
Please provide descriptive name for the parameter: u
| component = [-1] * n | ||
| current_comp = 0 | ||
|
|
||
| def dfs2(u: int): |
There was a problem hiding this comment.
Please provide return type hint for the function: dfs2. If the function does not return a value, please provide the type hint as: def function() -> None:
Please provide descriptive name for the parameter: u
| return assignment | ||
|
|
||
|
|
||
| def solve_2sat(n: int, clauses: list[tuple[int, bool, int, bool]]) -> list[bool] | None: |
There was a problem hiding this comment.
Please provide descriptive name for the parameter: n
cda6b3f to
22d5ba3
Compare
for more information, see https://pre-commit.ci
There was a problem hiding this comment.
Click here to look at the relevant links ⬇️
🔗 Relevant Links
Repository:
Python:
Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.
algorithms-keeper commands and options
algorithms-keeper actions can be triggered by commenting on this PR:
@algorithms-keeper reviewto trigger the checks for only added pull request files@algorithms-keeper review-allto trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.
| from pathlib import Path | ||
|
|
||
|
|
||
| def fix_floyd_warshall(): |
There was a problem hiding this comment.
Please provide return type hint for the function: fix_floyd_warshall. If the function does not return a value, please provide the type hint as: def function() -> None:
| print("fixed graphs/floyd_warshall.py") | ||
|
|
||
|
|
||
| def fix_ford_fulkerson(): |
There was a problem hiding this comment.
Please provide return type hint for the function: fix_ford_fulkerson. If the function does not return a value, please provide the type hint as: def function() -> None:
| print("fixed graphs/ford_fulkerson.py") | ||
|
|
||
|
|
||
| def fix_heavy_light_decomposition(): |
There was a problem hiding this comment.
Please provide return type hint for the function: fix_heavy_light_decomposition. If the function does not return a value, please provide the type hint as: def function() -> None:
| print("fixed graphs/heavy_light_decomposition.py") | ||
|
|
||
|
|
||
| def fix_hopcroft_karp(): |
There was a problem hiding this comment.
Please provide return type hint for the function: fix_hopcroft_karp. If the function does not return a value, please provide the type hint as: def function() -> None:
| print("fixed graphs/hopcroft_karp.py") | ||
|
|
||
|
|
||
| def fix_max_bipartite_independent_set(): |
There was a problem hiding this comment.
Please provide return type hint for the function: fix_max_bipartite_independent_set. If the function does not return a value, please provide the type hint as: def function() -> None:
| self.rev_graph[b].append(not_a) | ||
| self.rev_graph[a].append(not_b) | ||
|
|
||
| def add_implication(self, i: int, val_i: bool, j: int, val_j: bool) -> None: |
There was a problem hiding this comment.
Please provide descriptive name for the parameter: i
Please provide descriptive name for the parameter: j
| self.graph[a].append(b) | ||
| self.rev_graph[b].append(a) | ||
|
|
||
| def add_nand(self, i: int, val_i: bool, j: int, val_j: bool) -> None: |
There was a problem hiding this comment.
Please provide descriptive name for the parameter: i
Please provide descriptive name for the parameter: j
| visited = [False] * n | ||
| order = [] | ||
|
|
||
| def dfs1(u: int): |
There was a problem hiding this comment.
Please provide return type hint for the function: dfs1. If the function does not return a value, please provide the type hint as: def function() -> None:
Please provide descriptive name for the parameter: u
| component = [-1] * n | ||
| current_comp = 0 | ||
|
|
||
| def dfs2(u: int): |
There was a problem hiding this comment.
Please provide return type hint for the function: dfs2. If the function does not return a value, please provide the type hint as: def function() -> None:
Please provide descriptive name for the parameter: u
| return assignment | ||
|
|
||
|
|
||
| def solve_2sat(n: int, clauses: list[tuple[int, bool, int, bool]]) -> list[bool] | None: |
There was a problem hiding this comment.
Please provide descriptive name for the parameter: n
Describe your change:
Checklist:
Description
Implemented 10 advanced graph algorithms with comprehensive tests.