Skip to content

Conversation

techntools
Copy link

Added a base case for BFS like DFS.

No need to use break in loop. Create the path and return it.

Changed the docstests to remove some ambiguity in variable names ( graph vs graph1) and added some more to cover the edge cases.

node = edge_to[node]
path.insert(0, start)
return path
if end in edge_to.keys():
Copy link
Author

Choose a reason for hiding this comment

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

Using keys() instead values() as do not have to visit the children of end node.

['G', 'E', 'F']

# unreachable node
>>> print(graph_search.find_path_dfs('C', 'H'))
Copy link
Author

Choose a reason for hiding this comment

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

Node is not reachable

None

# non existing node
>>> print(graph_search.find_path_dfs('C', 'X'))
Copy link
Author

Choose a reason for hiding this comment

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

End node does not exist in graph

@faif faif merged commit f6adf63 into faif:master Sep 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants