-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Add shortest path by BFS #1870
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
Merged
Merged
Add shortest path by BFS #1870
Changes from 3 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
0804aec
Merge pull request #2 from TheAlgorithms/master
SandersLin c65f91d
Create breadth_first_search_shortest_path.py
SandersLin 9f528c9
updating DIRECTORY.md
b64027b
Reduce side effect of `shortest_path`
poyea fb37f10
fixup! Format Python code with psf/black push
53a49d7
Fix typo `separately`
poyea c9ae6f1
Change to get() from dictionary
poyea 0f648a4
Move graph to the top
poyea b7039ea
fixup! Format Python code with psf/black push
264cdfa
Add doctest for shortest path
poyea 4b5e249
Add doctest for BFS
poyea b9e8404
fixup! Format Python code with psf/black push
e2aad87
Add typings for breadth_first_search_shortest_path
poyea 255832d
fixup! Format Python code with psf/black push
70f5105
Remove assert from doctests
cclauss 0e4af15
Add blank line to doctest
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
class Graph: | ||
def __init__(self, graph, source_vertex): | ||
"""graph is implemented as dictionary of adjancency lists""" | ||
self.graph = graph | ||
# mapping node to its parent in resulting breadth first tree | ||
self.parent = {} | ||
self.source_vertex = source_vertex | ||
|
||
def breath_first_search(self): | ||
""" | ||
""" | ||
visited = {self.source_vertex} | ||
self.parent[self.source_vertex] = None | ||
queue = [self.source_vertex] # first in first out queue | ||
|
||
while queue: | ||
vertex = queue.pop(0) | ||
for adjancent_vertex in self.graph[vertex]: | ||
if adjancent_vertex not in visited: | ||
visited.add(adjancent_vertex) | ||
self.parent[adjancent_vertex] = vertex | ||
queue.append(adjancent_vertex) | ||
|
||
def print_shortest_path(self, target_vertex): | ||
if target_vertex == self.source_vertex: | ||
print(self.source_vertex, end="") | ||
elif target_vertex not in self.parent or not self.parent[target_vertex]: | ||
poyea marked this conversation as resolved.
Show resolved
Hide resolved
|
||
print(f"No path from vertex:{self.source_vertex} to vertex:{target_vertex}") | ||
else: | ||
self.print_shortest_path(self.parent[target_vertex]) | ||
print(f"->{target_vertex}", end="") | ||
|
||
|
||
if __name__ == "__main__": | ||
graph = { | ||
"A": ["B", "C", "E"], | ||
"B": ["A", "D", "E"], | ||
"C": ["A", "F", "G"], | ||
"D": ["B"], | ||
"E": ["A", "B", "D"], | ||
"F": ["C"], | ||
"G": ["C"], | ||
} | ||
poyea marked this conversation as resolved.
Show resolved
Hide resolved
|
||
g = Graph(graph, "G") | ||
g.breath_first_search() | ||
g.print_shortest_path("D") | ||
print() | ||
g.print_shortest_path("G") | ||
print() | ||
g.print_shortest_path("Foo") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.