Skip to content

Add 10 graph algorithms - #14316

Closed
gangs2314 wants to merge 7 commits into
TheAlgorithms:masterfrom
gangs2314:add-10-graph-algorithms
Closed

Add 10 graph algorithms#14316
gangs2314 wants to merge 7 commits into
TheAlgorithms:masterfrom
gangs2314:add-10-graph-algorithms

Conversation

@gangs2314

Copy link
Copy Markdown

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This PR only changes one algorithm file or relates to only one algorithm.
  • All new algorithms include comprehensive docstrings with complexity analysis.
  • All functions have type hints.
  • All new algorithms include doctests with examples.
  • Tests have been added and all tests pass.
  • I have NOT edited the README or any other documentation files.

Description

Implemented 10 advanced graph algorithms with comprehensive tests.

@algorithms-keeperalgorithms-keeperBot added require descriptive names This PR needs descriptive function and/or variable names require type hints https://docs.python.org/3/library/typing.html labels Mar 1, 2026

@algorithms-keeperalgorithms-keeperBot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to 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):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: u

Please provide descriptive name for the parameter: v

Please provide descriptive name for the parameter: w

Comment threadgraphs/chinese_postman.py Outdated


def chinese_postman(
n: int, edges: List[Tuple[int, int, int]]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: n

import random
import time

def benchmark():

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment threadgraphs/two_sat.py
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:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: i

Please provide descriptive name for the parameter: j

Comment threadgraphs/two_sat.py
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:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: i

Please provide descriptive name for the parameter: j

Comment threadgraphs/two_sat.py
visited = [False] * n
order = []

def dfs1(u: int):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment threadgraphs/two_sat.py
component = [-1] * n
current_comp = 0

def dfs2(u: int):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment threadgraphs/two_sat.py Outdated


def solve_2sat(
n: int, clauses: List[Tuple[int, bool, int, bool]]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: n

@algorithms-keeperalgorithms-keeperBot added awaiting reviews This PR is ready to be reviewed tests are failing Do not merge until tests pass labels Mar 1, 2026

@algorithms-keeperalgorithms-keeperBot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to 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):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: u

Please provide descriptive name for the parameter: v

Please provide descriptive name for the parameter: w

Comment threadgraphs/chinese_postman.py Outdated


def chinese_postman(
n: int, edges: List[Tuple[int, int, int]]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: n

import random
import time

def benchmark():

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment threadgraphs/two_sat.py
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:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: i

Please provide descriptive name for the parameter: j

Comment threadgraphs/two_sat.py
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:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: i

Please provide descriptive name for the parameter: j

Comment threadgraphs/two_sat.py
visited = [False] * n
order = []

def dfs1(u: int):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment threadgraphs/two_sat.py
component = [-1] * n
current_comp = 0

def dfs2(u: int):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment threadgraphs/two_sat.py Outdated


def solve_2sat(
n: int, clauses: List[Tuple[int, bool, int, bool]]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: n

gangs2314and others added 5 commits June 19, 2026 12:38
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
@gangs2314
gangs2314force-pushed the add-10-graph-algorithms branch from 4cca9f1 to d063a89CompareJune 19, 2026 07:10

@algorithms-keeperalgorithms-keeperBot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to 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.

Comment threadfix_ruff_errors.py
from pathlib import Path


def fix_floyd_warshall():

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Comment threadfix_ruff_errors.py
print("fixed graphs/floyd_warshall.py")


def fix_ford_fulkerson():

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Comment threadfix_ruff_errors.py
p.write_text(text)
print("fixed graphs/ford_fulkerson.py")


Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Comment threadfix_ruff_errors.py
p.write_text(text)
print("fixed graphs/heavy_light_decomposition.py")


Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Comment threadfix_ruff_errors.py
p.write_text(text)
print("fixed graphs/hopcroft_karp.py")


Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Comment threadgraphs/two_sat.py
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:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: i

Please provide descriptive name for the parameter: j

Comment threadgraphs/two_sat.py
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:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: i

Please provide descriptive name for the parameter: j

Comment threadgraphs/two_sat.py
visited = [False] * n
order = []

def dfs1(u: int):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment threadgraphs/two_sat.py
component = [-1] * n
current_comp = 0

def dfs2(u: int):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment threadgraphs/two_sat.py
return assignment


def solve_2sat(n: int, clauses: list[tuple[int, bool, int, bool]]) -> list[bool] | None:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: n

@gangs2314
gangs2314force-pushed the add-10-graph-algorithms branch from cda6b3f to 22d5ba3CompareJune 19, 2026 07:19

@algorithms-keeperalgorithms-keeperBot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to 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.

Comment threadfix_ruff_errors.py
from pathlib import Path


def fix_floyd_warshall():

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Comment threadfix_ruff_errors.py
print("fixed graphs/floyd_warshall.py")


def fix_ford_fulkerson():

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Comment threadfix_ruff_errors.py
print("fixed graphs/ford_fulkerson.py")


def fix_heavy_light_decomposition():

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Comment threadfix_ruff_errors.py
print("fixed graphs/heavy_light_decomposition.py")


def fix_hopcroft_karp():

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Comment threadfix_ruff_errors.py
print("fixed graphs/hopcroft_karp.py")


def fix_max_bipartite_independent_set():

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Comment threadgraphs/two_sat.py
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:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: i

Please provide descriptive name for the parameter: j

Comment threadgraphs/two_sat.py
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:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: i

Please provide descriptive name for the parameter: j

Comment threadgraphs/two_sat.py
visited = [False] * n
order = []

def dfs1(u: int):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment threadgraphs/two_sat.py
component = [-1] * n
current_comp = 0

def dfs2(u: int):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment threadgraphs/two_sat.py
return assignment


def solve_2sat(n: int, clauses: list[tuple[int, bool, int, bool]]) -> list[bool] | None:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: n

@cclauss

Copy link
Copy Markdown
Member

Closing require_type_hints PRs to prepare for Hacktoberfest

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviewsThis PR is ready to be reviewedrequire descriptive namesThis PR needs descriptive function and/or variable namesrequire type hintshttps://docs.python.org/3/library/typing.htmltests are failingDo not merge until tests pass

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@gangs2314@cclauss