-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Bi directional dijkstra #7982
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
ChrisO345
merged 27 commits into
TheAlgorithms:master
from
SwayamInSync:bi_directional_dijkstra
Nov 20, 2022
Merged
Bi directional dijkstra #7982
Changes from 2 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
1bca1de
Added Bi-Directional Dijkstra
SwayamInSync c74289b
Added Bi-Directional Dijkstra
SwayamInSync c369745
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 4de4d62
Added doctest and type hints
SwayamInSync c689393
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 0ddf115
Rename Bi_directional_Dijkstra.py to bi_directional_dijkstra.py
SwayamInSync 62fc026
Update bi_directional_dijkstra.py
SwayamInSync 12902d7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] a006750
Update bi_directional_dijkstra.py
SwayamInSync 6620798
Update bi_directional_dijkstra.py
SwayamInSync 80396fe
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] baabd64
Update bi_directional_dijkstra.py
SwayamInSync 4ff3b59
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] a22e0d9
Update bi_directional_dijkstra.py
SwayamInSync 91670cc
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 35c9e4b
Update bi_directional_dijkstra.py
SwayamInSync 23ac248
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] f3cede3
Update bi_directional_dijkstra.py
SwayamInSync 3adab98
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 9552936
Update bi_directional_dijkstra.py
SwayamInSync 36e68d3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 8a00b3e
Update bi_directional_dijkstra.py
SwayamInSync d64667e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 10b529a
Update bi_directional_dijkstra.py
SwayamInSync 9425f6f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c5c4ebe
Update bi_directional_dijkstra.py
SwayamInSync dc57aae
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
from queue import PriorityQueue | ||
import numpy as np | ||
|
||
""" | ||
Bi-directional Dijkstra's algorithm. | ||
|
||
A bi-directional approach is an efficient and less time consuming optimization for Dijkstra's searching algorithm | ||
Link for reference: https://www.homepages.ucl.ac.uk/~ucahmto/math/2020/05/30/bidirectional-dijkstra.html | ||
""" | ||
|
||
# Author: Swayam Singh (https://github.com/practice404) | ||
|
||
def bidirectional_dij(source, destination, graph_forward, graph_backward) -> int: | ||
""" | ||
Bi-directional Dijkstra's algorithm. | ||
Link for reference: https://www.homepages.ucl.ac.uk/~ucahmto/math/2020/05/30/bidirectional-dijkstra.html | ||
Args: | ||
source : Source stop id | ||
destination: destination stop id | ||
graph_forward: forward flow of graph | ||
graph_backward: backward flow of graph | ||
|
||
Returns: | ||
shortest_path_distance (int): length of the shortest path. | ||
|
||
Warnings: | ||
If the destination is not reachable, function returns -1 | ||
""" | ||
shortest_path_distance = -1 | ||
|
||
visited_forward = set() | ||
visited_backward = set() | ||
cost_forward = {source: 0} | ||
cost_backward = {destination: 0} | ||
parent_forward = {source: None} | ||
parent_backward = {destination: None} | ||
queue_forward = PriorityQueue() | ||
queue_backward = PriorityQueue() | ||
|
||
shortest_distance = np.inf | ||
|
||
queue_forward.put((0, source)) | ||
queue_backward.put((0, destination)) | ||
|
||
if source == destination: | ||
return 0 | ||
|
||
while queue_forward and queue_backward: | ||
while not queue_forward.empty(): | ||
_, vertex_forward = queue_forward.get() | ||
|
||
if vertex_forward not in visited_forward: | ||
break | ||
else: | ||
break | ||
visited_forward.add(vertex_forward) | ||
|
||
while not queue_backward.empty(): | ||
_, vertex_backward = queue_backward.get() | ||
|
||
if vertex_backward not in visited_backward: | ||
break | ||
else: | ||
break | ||
visited_backward.add(vertex_backward) | ||
|
||
# forward pass and relaxation | ||
for next_forward, d_forward in graph_forward[vertex_forward]: | ||
if next_forward in visited_forward: | ||
continue | ||
old_cost_f = cost_forward.get(next_forward, np.inf) | ||
new_cost_f = cost_forward[vertex_forward] + d_forward | ||
if new_cost_f < old_cost_f: | ||
queue_forward.put((new_cost_f, next_forward)) | ||
cost_forward[next_forward] = new_cost_f | ||
parent_forward[next_forward] = vertex_forward | ||
if next_forward in visited_backward and cost_forward[vertex_forward] + d_forward + \ | ||
cost_backward[next_forward] < shortest_distance: | ||
shortest_distance = cost_forward[vertex_forward] + d_forward + cost_backward[next_forward] | ||
|
||
# backward pass and relaxation | ||
for next_backward, d_backward in graph_backward[vertex_backward]: | ||
if next_backward in visited_backward: | ||
continue | ||
old_cost_b = cost_backward.get(next_backward, np.inf) | ||
new_cost_b = cost_backward[vertex_backward] + d_backward | ||
if new_cost_b < old_cost_b: | ||
queue_backward.put((new_cost_b, next_backward)) | ||
cost_backward[next_backward] = new_cost_b | ||
parent_backward[next_backward] = vertex_backward | ||
|
||
if next_backward in visited_forward and cost_backward[vertex_backward] + d_backward + \ | ||
cost_forward[next_backward] < shortest_distance: | ||
shortest_distance = cost_backward[vertex_backward] + d_backward + cost_forward[next_backward] | ||
|
||
if cost_forward[vertex_forward] + cost_backward[vertex_backward] >= shortest_distance: | ||
break | ||
|
||
if shortest_distance == np.inf: | ||
return shortest_path_distance | ||
shortest_path_distance = shortest_distance | ||
return shortest_path_distance | ||
|
||
|
||
if __name__ == "__main__": | ||
r""" | ||
Layout of input Graph: | ||
E -- 1 --> B -- 1 --> C -- 1 --> D -- 1 --> F | ||
\ /\ | ||
\ || | ||
-------- 2 ---------> G ------- 1 ------ | ||
""" | ||
|
||
graph_fwd = { | ||
"B": [["C", 1]], | ||
"C": [["D", 1]], | ||
"D": [["F", 1]], | ||
"E": [["B", 1], ["G", 2]], | ||
"F": [], | ||
"G": [["F", 1]], | ||
} | ||
graph_bwd = { | ||
"B": [["E", 1]], | ||
"C": [["B", 1]], | ||
"D": [["C", 1]], | ||
"F": [["D", 1], ["G", 1]], | ||
"G": [["E", 2]] | ||
} | ||
print(bidirectional_dij("E", "F", graph_fwd, graph_bwd)) | ||
# E -- 2 --> G -- 1 --> F == 3 |
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.