Skip to content

Fix BFS shortest distances for non-single-character node names - #15629

Open
Shubham-Padkonde wants to merge 1 commit into
TheAlgorithms:masterfrom
Shubham-Padkonde:fix/bfs-visited-start-node
Open

Shubham-Padkonde wants to merge 1 commit into
TheAlgorithms:masterfrom
Shubham-Padkonde:fix/bfs-visited-start-node

Conversation

@Shubham-Padkonde

Copy link
Copy Markdown

Describe your change

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

Initialize the visited set with the start node itself. set(start) splits string node names into characters and raises TypeError for integer node names.

For example, bfs_shortest_path_distance({'AB': ['A'], 'A': ['C'], 'C': []}, 'AB', 'C') returns -1 before this fix and 2 afterward. The edge from integer node 0 to 1 now returns 1 instead of raising.

Validation on free-threaded Python 3.15: all 11 existing BFS doctest items pass. An external exhaustive check compares all source/target pairs in every directed four-node graph against Floyd-Warshall, with string, integer and tuple labels: 196,608 comparisons pass. Existing doctests remain unchanged, per the contribution guidance.

AI-assisted contribution.

Checklist

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass.

@algorithms-keeper algorithms-keeper Bot added awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files labels Oct 23, 2026

This branch has not been deployed

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

Labels

awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant