From 1bca1dead2447263ba0fed3034ff9cdb7a298f29 Mon Sep 17 00:00:00 2001 From: practice404 Date: Sun, 13 Nov 2022 23:14:48 +0530 Subject: [PATCH 01/27] Added Bi-Directional Dijkstra --- graphs/Bi_directional_Dijkstra.py | 123 ++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 graphs/Bi_directional_Dijkstra.py diff --git a/graphs/Bi_directional_Dijkstra.py b/graphs/Bi_directional_Dijkstra.py new file mode 100644 index 000000000000..5515a32f4875 --- /dev/null +++ b/graphs/Bi_directional_Dijkstra.py @@ -0,0 +1,123 @@ +from queue import PriorityQueue + +import numpy as np + + +def bidirectional_dij(source, destination, graph_forward, graph_backward) -> int: + """ + Bi-directional Dijkstra's algorithm. + + 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 From c74289b55d8fd32b20e1159c970a1698b4204c87 Mon Sep 17 00:00:00 2001 From: practice404 Date: Sun, 13 Nov 2022 23:24:02 +0530 Subject: [PATCH 02/27] Added Bi-Directional Dijkstra --- graphs/Bi_directional_Dijkstra.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/graphs/Bi_directional_Dijkstra.py b/graphs/Bi_directional_Dijkstra.py index 5515a32f4875..2e8e1dfa40eb 100644 --- a/graphs/Bi_directional_Dijkstra.py +++ b/graphs/Bi_directional_Dijkstra.py @@ -1,12 +1,19 @@ 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 From c36974585290de6d5442e842e5480a7931f1ec44 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 13 Nov 2022 17:57:33 +0000 Subject: [PATCH 03/27] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- graphs/Bi_directional_Dijkstra.py | 41 ++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/graphs/Bi_directional_Dijkstra.py b/graphs/Bi_directional_Dijkstra.py index 2e8e1dfa40eb..e48c32e2ba03 100644 --- a/graphs/Bi_directional_Dijkstra.py +++ b/graphs/Bi_directional_Dijkstra.py @@ -1,4 +1,5 @@ from queue import PriorityQueue + import numpy as np """ @@ -10,6 +11,7 @@ # Author: Swayam Singh (https://github.com/practice404) + def bidirectional_dij(source, destination, graph_forward, graph_backward) -> int: """ Bi-directional Dijkstra's algorithm. @@ -74,9 +76,18 @@ def bidirectional_dij(source, destination, graph_forward, graph_backward) -> int 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] + 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]: @@ -89,11 +100,23 @@ def bidirectional_dij(source, destination, graph_forward, graph_backward) -> int 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: + 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: @@ -124,7 +147,7 @@ def bidirectional_dij(source, destination, graph_forward, graph_backward) -> int "C": [["B", 1]], "D": [["C", 1]], "F": [["D", 1], ["G", 1]], - "G": [["E", 2]] + "G": [["E", 2]], } print(bidirectional_dij("E", "F", graph_fwd, graph_bwd)) # E -- 2 --> G -- 1 --> F == 3 From 4de4d627ff8b0d3e117e76adf1ceb2f68066379b Mon Sep 17 00:00:00 2001 From: Swayam <74960567+practice404@users.noreply.github.com> Date: Sun, 13 Nov 2022 23:42:53 +0530 Subject: [PATCH 04/27] Added doctest and type hints --- graphs/Bi_directional_Dijkstra.py | 94 +++++++++++++------------------ 1 file changed, 38 insertions(+), 56 deletions(-) diff --git a/graphs/Bi_directional_Dijkstra.py b/graphs/Bi_directional_Dijkstra.py index e48c32e2ba03..f79fc39ca1d6 100644 --- a/graphs/Bi_directional_Dijkstra.py +++ b/graphs/Bi_directional_Dijkstra.py @@ -1,5 +1,4 @@ from queue import PriorityQueue - import numpy as np """ @@ -11,8 +10,7 @@ # Author: Swayam Singh (https://github.com/practice404) - -def bidirectional_dij(source, destination, graph_forward, graph_backward) -> int: +def bidirectional_dij(source: str or int , destination: str or int, graph_forward: dict, graph_backward: dict) -> int: """ Bi-directional Dijkstra's algorithm. Link for reference: https://www.homepages.ucl.ac.uk/~ucahmto/math/2020/05/30/bidirectional-dijkstra.html @@ -27,6 +25,9 @@ def bidirectional_dij(source, destination, graph_forward, graph_backward) -> int Warnings: If the destination is not reachable, function returns -1 + + >>> bidirectional_dij("E", "F", graph_fwd, graph_bwd) + 3 """ shortest_path_distance = -1 @@ -76,18 +77,9 @@ def bidirectional_dij(source, destination, graph_forward, graph_backward) -> int 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] - ) + 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]: @@ -100,23 +92,11 @@ def bidirectional_dij(source, destination, graph_forward, graph_backward) -> int 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 - ): + 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: @@ -124,30 +104,32 @@ def bidirectional_dij(source, destination, graph_forward, graph_backward) -> int shortest_path_distance = shortest_distance return shortest_path_distance +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 if __name__ == "__main__": - r""" - Layout of input Graph: - E -- 1 --> B -- 1 --> C -- 1 --> D -- 1 --> F - \ /\ - \ || - -------- 2 ---------> G ------- 1 ------ - """ + import doctest + doctest.testmod() - 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 From c6893939fa1a5122113cb975c8f9b45b9ce50cd0 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 13 Nov 2022 18:13:54 +0000 Subject: [PATCH 05/27] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- graphs/Bi_directional_Dijkstra.py | 57 ++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 13 deletions(-) diff --git a/graphs/Bi_directional_Dijkstra.py b/graphs/Bi_directional_Dijkstra.py index f79fc39ca1d6..6b88fbdd3a18 100644 --- a/graphs/Bi_directional_Dijkstra.py +++ b/graphs/Bi_directional_Dijkstra.py @@ -1,4 +1,5 @@ from queue import PriorityQueue + import numpy as np """ @@ -10,7 +11,13 @@ # Author: Swayam Singh (https://github.com/practice404) -def bidirectional_dij(source: str or int , destination: str or int, graph_forward: dict, graph_backward: dict) -> int: + +def bidirectional_dij( + source: str or int, + destination: str or int, + graph_forward: dict, + graph_backward: dict, +) -> int: """ Bi-directional Dijkstra's algorithm. Link for reference: https://www.homepages.ucl.ac.uk/~ucahmto/math/2020/05/30/bidirectional-dijkstra.html @@ -77,9 +84,18 @@ def bidirectional_dij(source: str or int , destination: str or int, graph_forwar 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] + 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]: @@ -92,11 +108,23 @@ def bidirectional_dij(source: str or int , destination: str or int, graph_forwar 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: + 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: @@ -104,6 +132,7 @@ def bidirectional_dij(source: str or int , destination: str or int, graph_forwar shortest_path_distance = shortest_distance return shortest_path_distance + r""" Layout of input Graph: E -- 1 --> B -- 1 --> C -- 1 --> D -- 1 --> F @@ -125,11 +154,13 @@ def bidirectional_dij(source: str or int , destination: str or int, graph_forwar "C": [["B", 1]], "D": [["C", 1]], "F": [["D", 1], ["G", 1]], - "G": [["E", 2]] + "G": [["E", 2]], } -print(bidirectional_dij("E", "F", graph_fwd, graph_bwd)) # # E -- 2 --> G -- 1 --> F == 3 +print( + bidirectional_dij("E", "F", graph_fwd, graph_bwd) +) # # E -- 2 --> G -- 1 --> F == 3 if __name__ == "__main__": - import doctest - doctest.testmod() + import doctest + doctest.testmod() From 0ddf1153c79cd188fa979902a27b59e3940f0c53 Mon Sep 17 00:00:00 2001 From: Swayam <74960567+practice404@users.noreply.github.com> Date: Sun, 13 Nov 2022 23:46:30 +0530 Subject: [PATCH 06/27] Rename Bi_directional_Dijkstra.py to bi_directional_dijkstra.py --- graphs/{Bi_directional_Dijkstra.py => bi_directional_dijkstra.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename graphs/{Bi_directional_Dijkstra.py => bi_directional_dijkstra.py} (100%) diff --git a/graphs/Bi_directional_Dijkstra.py b/graphs/bi_directional_dijkstra.py similarity index 100% rename from graphs/Bi_directional_Dijkstra.py rename to graphs/bi_directional_dijkstra.py From 62fc026007a9c5c9b275efd7428758491e87900d Mon Sep 17 00:00:00 2001 From: Swayam <74960567+practice404@users.noreply.github.com> Date: Sun, 13 Nov 2022 23:59:58 +0530 Subject: [PATCH 07/27] Update bi_directional_dijkstra.py --- graphs/bi_directional_dijkstra.py | 62 ++++++++----------------------- 1 file changed, 16 insertions(+), 46 deletions(-) diff --git a/graphs/bi_directional_dijkstra.py b/graphs/bi_directional_dijkstra.py index 6b88fbdd3a18..3cd176bcd6f8 100644 --- a/graphs/bi_directional_dijkstra.py +++ b/graphs/bi_directional_dijkstra.py @@ -1,4 +1,5 @@ from queue import PriorityQueue +from typing import Any import numpy as np @@ -11,16 +12,9 @@ # Author: Swayam Singh (https://github.com/practice404) - -def bidirectional_dij( - source: str or int, - destination: str or int, - graph_forward: dict, - graph_backward: dict, -) -> int: +def bidirectional_dij(source: str or int , destination: str or int, graph_forward: dict, graph_backward: dict) -> 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 @@ -44,8 +38,8 @@ def bidirectional_dij( cost_backward = {destination: 0} parent_forward = {source: None} parent_backward = {destination: None} - queue_forward = PriorityQueue() - queue_backward = PriorityQueue() + queue_forward: PriorityQueue[Any] = PriorityQueue() + queue_backward: PriorityQueue[Any] = PriorityQueue() shortest_distance = np.inf @@ -84,18 +78,9 @@ def bidirectional_dij( 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] - ) + 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]: @@ -108,23 +93,11 @@ def bidirectional_dij( 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 - ): + 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: @@ -132,7 +105,6 @@ def bidirectional_dij( shortest_path_distance = shortest_distance return shortest_path_distance - r""" Layout of input Graph: E -- 1 --> B -- 1 --> C -- 1 --> D -- 1 --> F @@ -154,13 +126,11 @@ def bidirectional_dij( "C": [["B", 1]], "D": [["C", 1]], "F": [["D", 1], ["G", 1]], - "G": [["E", 2]], + "G": [["E", 2]] } -print( - bidirectional_dij("E", "F", graph_fwd, graph_bwd) -) # # E -- 2 --> G -- 1 --> F == 3 +print(bidirectional_dij("E", "F", graph_fwd, graph_bwd)) # # E -- 2 --> G -- 1 --> F == 3 if __name__ == "__main__": - import doctest + import doctest + doctest.testmod() - doctest.testmod() From 12902d7835e563ef570711e817ef52d822e93396 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 13 Nov 2022 18:30:52 +0000 Subject: [PATCH 08/27] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- graphs/bi_directional_dijkstra.py | 56 ++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 13 deletions(-) diff --git a/graphs/bi_directional_dijkstra.py b/graphs/bi_directional_dijkstra.py index 3cd176bcd6f8..eaa59e67ad9e 100644 --- a/graphs/bi_directional_dijkstra.py +++ b/graphs/bi_directional_dijkstra.py @@ -12,7 +12,13 @@ # Author: Swayam Singh (https://github.com/practice404) -def bidirectional_dij(source: str or int , destination: str or int, graph_forward: dict, graph_backward: dict) -> int: + +def bidirectional_dij( + source: str or int, + destination: str or int, + graph_forward: dict, + graph_backward: dict, +) -> int: """ Bi-directional Dijkstra's algorithm. Args: @@ -78,9 +84,18 @@ def bidirectional_dij(source: str or int , destination: str or int, graph_forwar 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] + 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]: @@ -93,11 +108,23 @@ def bidirectional_dij(source: str or int , destination: str or int, graph_forwar 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: + 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: @@ -105,6 +132,7 @@ def bidirectional_dij(source: str or int , destination: str or int, graph_forwar shortest_path_distance = shortest_distance return shortest_path_distance + r""" Layout of input Graph: E -- 1 --> B -- 1 --> C -- 1 --> D -- 1 --> F @@ -126,11 +154,13 @@ def bidirectional_dij(source: str or int , destination: str or int, graph_forwar "C": [["B", 1]], "D": [["C", 1]], "F": [["D", 1], ["G", 1]], - "G": [["E", 2]] + "G": [["E", 2]], } -print(bidirectional_dij("E", "F", graph_fwd, graph_bwd)) # # E -- 2 --> G -- 1 --> F == 3 +print( + bidirectional_dij("E", "F", graph_fwd, graph_bwd) +) # # E -- 2 --> G -- 1 --> F == 3 if __name__ == "__main__": - import doctest - doctest.testmod() + import doctest + doctest.testmod() From a006750c3e089b603e6023532aada8af1e018752 Mon Sep 17 00:00:00 2001 From: Swayam <74960567+practice404@users.noreply.github.com> Date: Mon, 14 Nov 2022 00:03:21 +0530 Subject: [PATCH 09/27] Update bi_directional_dijkstra.py --- graphs/bi_directional_dijkstra.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/graphs/bi_directional_dijkstra.py b/graphs/bi_directional_dijkstra.py index eaa59e67ad9e..e17a67641db4 100644 --- a/graphs/bi_directional_dijkstra.py +++ b/graphs/bi_directional_dijkstra.py @@ -14,8 +14,8 @@ def bidirectional_dij( - source: str or int, - destination: str or int, + source: str, + destination: str, graph_forward: dict, graph_backward: dict, ) -> int: From 66207984136eb61e95a9628a2b25bb3bb5090586 Mon Sep 17 00:00:00 2001 From: Swayam <74960567+practice404@users.noreply.github.com> Date: Mon, 14 Nov 2022 00:13:06 +0530 Subject: [PATCH 10/27] Update bi_directional_dijkstra.py --- graphs/bi_directional_dijkstra.py | 56 +++++++------------------------ 1 file changed, 13 insertions(+), 43 deletions(-) diff --git a/graphs/bi_directional_dijkstra.py b/graphs/bi_directional_dijkstra.py index e17a67641db4..5f98cf54e75f 100644 --- a/graphs/bi_directional_dijkstra.py +++ b/graphs/bi_directional_dijkstra.py @@ -12,13 +12,7 @@ # Author: Swayam Singh (https://github.com/practice404) - -def bidirectional_dij( - source: str, - destination: str, - graph_forward: dict, - graph_backward: dict, -) -> int: +def bidirectional_dij(source: str, destination: str, graph_forward: dict, graph_backward: dict) -> int: """ Bi-directional Dijkstra's algorithm. Args: @@ -84,18 +78,9 @@ def bidirectional_dij( 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] - ) + if next_forward in visited_backward: + if 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]: @@ -108,23 +93,11 @@ def bidirectional_dij( 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 - ): + if next_backward in visited_forward: + if 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: @@ -132,7 +105,6 @@ def bidirectional_dij( shortest_path_distance = shortest_distance return shortest_path_distance - r""" Layout of input Graph: E -- 1 --> B -- 1 --> C -- 1 --> D -- 1 --> F @@ -154,13 +126,11 @@ def bidirectional_dij( "C": [["B", 1]], "D": [["C", 1]], "F": [["D", 1], ["G", 1]], - "G": [["E", 2]], + "G": [["E", 2]] } -print( - bidirectional_dij("E", "F", graph_fwd, graph_bwd) -) # # E -- 2 --> G -- 1 --> F == 3 +print(bidirectional_dij("E", "F", graph_fwd, graph_bwd)) # # E -- 2 --> G -- 1 --> F == 3 if __name__ == "__main__": - import doctest + import doctest + doctest.testmod() - doctest.testmod() From 80396fe9543fce8dc2f4118f9169633e0ac26cb6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 13 Nov 2022 18:44:10 +0000 Subject: [PATCH 11/27] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- graphs/bi_directional_dijkstra.py | 49 ++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/graphs/bi_directional_dijkstra.py b/graphs/bi_directional_dijkstra.py index 5f98cf54e75f..487751fba1a4 100644 --- a/graphs/bi_directional_dijkstra.py +++ b/graphs/bi_directional_dijkstra.py @@ -12,7 +12,10 @@ # Author: Swayam Singh (https://github.com/practice404) -def bidirectional_dij(source: str, destination: str, graph_forward: dict, graph_backward: dict) -> int: + +def bidirectional_dij( + source: str, destination: str, graph_forward: dict, graph_backward: dict +) -> int: """ Bi-directional Dijkstra's algorithm. Args: @@ -79,8 +82,17 @@ def bidirectional_dij(source: str, destination: str, graph_forward: dict, graph_ cost_forward[next_forward] = new_cost_f parent_forward[next_forward] = vertex_forward if next_forward in visited_backward: - if cost_forward[vertex_forward] + d_forward + cost_backward[next_forward] < shortest_distance: - shortest_distance = cost_forward[vertex_forward] + d_forward + cost_backward[next_forward] + if ( + 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]: @@ -94,10 +106,22 @@ def bidirectional_dij(source: str, destination: str, graph_forward: dict, graph_ parent_backward[next_backward] = vertex_backward if next_backward in visited_forward: - if 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: + if ( + 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: @@ -105,6 +129,7 @@ def bidirectional_dij(source: str, destination: str, graph_forward: dict, graph_ shortest_path_distance = shortest_distance return shortest_path_distance + r""" Layout of input Graph: E -- 1 --> B -- 1 --> C -- 1 --> D -- 1 --> F @@ -126,11 +151,13 @@ def bidirectional_dij(source: str, destination: str, graph_forward: dict, graph_ "C": [["B", 1]], "D": [["C", 1]], "F": [["D", 1], ["G", 1]], - "G": [["E", 2]] + "G": [["E", 2]], } -print(bidirectional_dij("E", "F", graph_fwd, graph_bwd)) # # E -- 2 --> G -- 1 --> F == 3 +print( + bidirectional_dij("E", "F", graph_fwd, graph_bwd) +) # # E -- 2 --> G -- 1 --> F == 3 if __name__ == "__main__": - import doctest - doctest.testmod() + import doctest + doctest.testmod() From baabd64b146dae7ac0953c5469c4a5cb215ac58b Mon Sep 17 00:00:00 2001 From: Swayam <74960567+practice404@users.noreply.github.com> Date: Mon, 14 Nov 2022 00:21:45 +0530 Subject: [PATCH 12/27] Update bi_directional_dijkstra.py --- graphs/bi_directional_dijkstra.py | 100 +++++++++++------------------- 1 file changed, 36 insertions(+), 64 deletions(-) diff --git a/graphs/bi_directional_dijkstra.py b/graphs/bi_directional_dijkstra.py index 487751fba1a4..25e1c4ffeed2 100644 --- a/graphs/bi_directional_dijkstra.py +++ b/graphs/bi_directional_dijkstra.py @@ -12,10 +12,7 @@ # Author: Swayam Singh (https://github.com/practice404) - -def bidirectional_dij( - source: str, destination: str, graph_forward: dict, graph_backward: dict -) -> int: +def bidirectional_dij(source: str, destination: str, graph_forward: dict, graph_backward: dict) -> int: """ Bi-directional Dijkstra's algorithm. Args: @@ -37,8 +34,8 @@ def bidirectional_dij( visited_forward = set() visited_backward = set() - cost_forward = {source: 0} - cost_backward = {destination: 0} + cst_fwd = {source: 0} + cst_bwd = {destination: 0} parent_forward = {source: None} parent_backward = {destination: None} queue_forward: PriorityQueue[Any] = PriorityQueue() @@ -54,74 +51,53 @@ def bidirectional_dij( while queue_forward and queue_backward: while not queue_forward.empty(): - _, vertex_forward = queue_forward.get() + _, v_fwd = queue_forward.get() - if vertex_forward not in visited_forward: + if v_fwd not in visited_forward: break else: break - visited_forward.add(vertex_forward) + visited_forward.add(v_fwd) while not queue_backward.empty(): - _, vertex_backward = queue_backward.get() + _, v_bwd = queue_backward.get() - if vertex_backward not in visited_backward: + if v_bwd not in visited_backward: break else: break - visited_backward.add(vertex_backward) + visited_backward.add(v_bwd) # forward pass and relaxation - for next_forward, d_forward in graph_forward[vertex_forward]: - if next_forward in visited_forward: + for nxt_fwd, d_forward in graph_forward[v_fwd]: + if nxt_fwd in visited_forward: continue - old_cost_f = cost_forward.get(next_forward, np.inf) - new_cost_f = cost_forward[vertex_forward] + d_forward + old_cost_f = cst_fwd.get(nxt_fwd, np.inf) + new_cost_f = cst_fwd[v_fwd] + 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: - if ( - cost_forward[vertex_forward] - + d_forward - + cost_backward[next_forward] - < shortest_distance - ): - shortest_distance = ( - cost_forward[vertex_forward] - + d_forward - + cost_backward[next_forward] - ) + queue_forward.put((new_cost_f, nxt_fwd)) + cst_fwd[nxt_fwd] = new_cost_f + parent_forward[nxt_fwd] = v_fwd + if nxt_fwd in visited_backward: + if cst_fwd[v_fwd] + d_forward + cst_bwd[nxt_fwd] < shortest_distance: + shortest_distance = cst_fwd[v_fwd] + d_forward + cst_bwd[nxt_fwd] # backward pass and relaxation - for next_backward, d_backward in graph_backward[vertex_backward]: - if next_backward in visited_backward: + for nxt_bwd, d_backward in graph_backward[v_bwd]: + if nxt_bwd in visited_backward: continue - old_cost_b = cost_backward.get(next_backward, np.inf) - new_cost_b = cost_backward[vertex_backward] + d_backward + old_cost_b = cst_bwd.get(nxt_bwd, np.inf) + new_cost_b = cst_bwd[v_bwd] + 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: - if ( - 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 - ): + queue_backward.put((new_cost_b, nxt_bwd)) + cst_bwd[nxt_bwd] = new_cost_b + parent_backward[nxt_bwd] = v_bwd + + if nxt_bwd in visited_forward: + if cst_bwd[v_bwd] + d_backward + cst_fwd[nxt_bwd] < shortest_distance: + shortest_distance = cst_bwd[v_bwd] + d_backward + cst_fwd[nxt_bwd] + + if cst_fwd[v_fwd] + cst_bwd[v_bwd] >= shortest_distance: break if shortest_distance == np.inf: @@ -129,7 +105,6 @@ def bidirectional_dij( shortest_path_distance = shortest_distance return shortest_path_distance - r""" Layout of input Graph: E -- 1 --> B -- 1 --> C -- 1 --> D -- 1 --> F @@ -151,13 +126,10 @@ def bidirectional_dij( "C": [["B", 1]], "D": [["C", 1]], "F": [["D", 1], ["G", 1]], - "G": [["E", 2]], + "G": [["E", 2]] } -print( - bidirectional_dij("E", "F", graph_fwd, graph_bwd) -) # # E -- 2 --> G -- 1 --> F == 3 +print(bidirectional_dij("E", "F", graph_fwd, graph_bwd)) # # E -- 2 --> G -- 1 --> F == 3 if __name__ == "__main__": - import doctest - - doctest.testmod() + import doctest + doctest.testmod() From 4ff3b5946de6531768edde71d8dff25f34a0c9b8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 13 Nov 2022 18:52:46 +0000 Subject: [PATCH 13/27] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- graphs/bi_directional_dijkstra.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/graphs/bi_directional_dijkstra.py b/graphs/bi_directional_dijkstra.py index 25e1c4ffeed2..514b1524c1b4 100644 --- a/graphs/bi_directional_dijkstra.py +++ b/graphs/bi_directional_dijkstra.py @@ -12,7 +12,10 @@ # Author: Swayam Singh (https://github.com/practice404) -def bidirectional_dij(source: str, destination: str, graph_forward: dict, graph_backward: dict) -> int: + +def bidirectional_dij( + source: str, destination: str, graph_forward: dict, graph_backward: dict +) -> int: """ Bi-directional Dijkstra's algorithm. Args: @@ -105,6 +108,7 @@ def bidirectional_dij(source: str, destination: str, graph_forward: dict, graph_ shortest_path_distance = shortest_distance return shortest_path_distance + r""" Layout of input Graph: E -- 1 --> B -- 1 --> C -- 1 --> D -- 1 --> F @@ -126,10 +130,13 @@ def bidirectional_dij(source: str, destination: str, graph_forward: dict, graph_ "C": [["B", 1]], "D": [["C", 1]], "F": [["D", 1], ["G", 1]], - "G": [["E", 2]] + "G": [["E", 2]], } -print(bidirectional_dij("E", "F", graph_fwd, graph_bwd)) # # E -- 2 --> G -- 1 --> F == 3 +print( + bidirectional_dij("E", "F", graph_fwd, graph_bwd) +) # # E -- 2 --> G -- 1 --> F == 3 if __name__ == "__main__": - import doctest - doctest.testmod() + import doctest + + doctest.testmod() From a22e0d9a1d19b57a74ec87c3db53f9044e6ff947 Mon Sep 17 00:00:00 2001 From: Swayam <74960567+practice404@users.noreply.github.com> Date: Mon, 14 Nov 2022 16:01:48 +0530 Subject: [PATCH 14/27] Update bi_directional_dijkstra.py --- graphs/bi_directional_dijkstra.py | 69 +++++++++++++++---------------- 1 file changed, 33 insertions(+), 36 deletions(-) diff --git a/graphs/bi_directional_dijkstra.py b/graphs/bi_directional_dijkstra.py index 514b1524c1b4..11cdb5f5780e 100644 --- a/graphs/bi_directional_dijkstra.py +++ b/graphs/bi_directional_dijkstra.py @@ -1,29 +1,27 @@ -from queue import PriorityQueue -from typing import Any - -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 +A bi-directional approach is an efficient and +less time consuming optimization for Dijkstra's +searching algorithm + +Reference: shorturl.at/exHM7 """ # Author: Swayam Singh (https://github.com/practice404) +from queue import PriorityQueue +from typing import Any + +import numpy as np + + def bidirectional_dij( - source: str, destination: str, graph_forward: dict, graph_backward: dict + source: str, destination: str, graph_forward: dict, graph_backward: dict ) -> int: """ Bi-directional Dijkstra's algorithm. - 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. @@ -103,9 +101,8 @@ def bidirectional_dij( if cst_fwd[v_fwd] + cst_bwd[v_bwd] >= shortest_distance: break - if shortest_distance == np.inf: - return shortest_path_distance - shortest_path_distance = shortest_distance + if shortest_distance != np.inf: + shortest_path_distance = shortest_distance return shortest_path_distance @@ -117,26 +114,26 @@ def bidirectional_dij( -------- 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 - if __name__ == "__main__": import doctest + 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]], + "E": [[None, np.inf]], + "G": [["E", 2]], + } + print( + bidirectional_dij("E", "F", graph_fwd, graph_bwd) + ) # E -- 2 --> G -- 1 --> F == 3 doctest.testmod() From 91670cc49edbf7177be0ee8cd0cd5702d060e4e9 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 14 Nov 2022 10:34:34 +0000 Subject: [PATCH 15/27] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- graphs/bi_directional_dijkstra.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphs/bi_directional_dijkstra.py b/graphs/bi_directional_dijkstra.py index 11cdb5f5780e..6085d62c9c53 100644 --- a/graphs/bi_directional_dijkstra.py +++ b/graphs/bi_directional_dijkstra.py @@ -18,7 +18,7 @@ def bidirectional_dij( - source: str, destination: str, graph_forward: dict, graph_backward: dict + source: str, destination: str, graph_forward: dict, graph_backward: dict ) -> int: """ Bi-directional Dijkstra's algorithm. From 35c9e4be1bfaf7fa8c804b1ebf036be648a07218 Mon Sep 17 00:00:00 2001 From: Swayam <74960567+practice404@users.noreply.github.com> Date: Mon, 14 Nov 2022 16:10:22 +0530 Subject: [PATCH 16/27] Update bi_directional_dijkstra.py --- graphs/bi_directional_dijkstra.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/graphs/bi_directional_dijkstra.py b/graphs/bi_directional_dijkstra.py index 6085d62c9c53..71636fce553b 100644 --- a/graphs/bi_directional_dijkstra.py +++ b/graphs/bi_directional_dijkstra.py @@ -18,7 +18,7 @@ def bidirectional_dij( - source: str, destination: str, graph_forward: dict, graph_backward: dict + source: str, destination: str, graph_forward: dict, graph_backward: dict ) -> int: """ Bi-directional Dijkstra's algorithm. @@ -113,11 +113,7 @@ def bidirectional_dij( \ || -------- 2 ---------> G ------- 1 ------ """ - -if __name__ == "__main__": - import doctest - - graph_fwd = { +graph_fwd = { "B": [["C", 1]], "C": [["D", 1]], "D": [["F", 1]], @@ -125,14 +121,18 @@ def bidirectional_dij( "F": [], "G": [["F", 1]], } - graph_bwd = { - "B": [["E", 1]], - "C": [["B", 1]], - "D": [["C", 1]], - "F": [["D", 1], ["G", 1]], - "E": [[None, np.inf]], - "G": [["E", 2]], - } +graph_bwd = { + "B": [["E", 1]], + "C": [["B", 1]], + "D": [["C", 1]], + "F": [["D", 1], ["G", 1]], + "E": [[None, np.inf]], + "G": [["E", 2]], +} + +if __name__ == "__main__": + import doctest + print( bidirectional_dij("E", "F", graph_fwd, graph_bwd) ) # E -- 2 --> G -- 1 --> F == 3 From 23ac248d37267a085ac891cd4342bd3105019b57 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 14 Nov 2022 10:44:28 +0000 Subject: [PATCH 17/27] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- graphs/bi_directional_dijkstra.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/graphs/bi_directional_dijkstra.py b/graphs/bi_directional_dijkstra.py index 71636fce553b..0bc5cd2a0381 100644 --- a/graphs/bi_directional_dijkstra.py +++ b/graphs/bi_directional_dijkstra.py @@ -18,7 +18,7 @@ def bidirectional_dij( - source: str, destination: str, graph_forward: dict, graph_backward: dict + source: str, destination: str, graph_forward: dict, graph_backward: dict ) -> int: """ Bi-directional Dijkstra's algorithm. @@ -114,13 +114,13 @@ def bidirectional_dij( -------- 2 ---------> G ------- 1 ------ """ graph_fwd = { - "B": [["C", 1]], - "C": [["D", 1]], - "D": [["F", 1]], - "E": [["B", 1], ["G", 2]], - "F": [], - "G": [["F", 1]], - } + "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]], From f3cede32dddb87ce694380c9280ec739d75fe612 Mon Sep 17 00:00:00 2001 From: Swayam <74960567+practice404@users.noreply.github.com> Date: Mon, 14 Nov 2022 17:22:37 +0530 Subject: [PATCH 18/27] Update bi_directional_dijkstra.py --- graphs/bi_directional_dijkstra.py | 45 +++++++++++++------------------ 1 file changed, 18 insertions(+), 27 deletions(-) diff --git a/graphs/bi_directional_dijkstra.py b/graphs/bi_directional_dijkstra.py index 0bc5cd2a0381..55c2a692f373 100644 --- a/graphs/bi_directional_dijkstra.py +++ b/graphs/bi_directional_dijkstra.py @@ -18,7 +18,7 @@ def bidirectional_dij( - source: str, destination: str, graph_forward: dict, graph_backward: dict + source: str, destination: str, graph_forward: dict, graph_backward: dict ) -> int: """ Bi-directional Dijkstra's algorithm. @@ -106,34 +106,25 @@ def bidirectional_dij( return shortest_path_distance -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]], - "E": [[None, np.inf]], - "G": [["E", 2]], -} + if __name__ == "__main__": import doctest - print( - bidirectional_dij("E", "F", graph_fwd, graph_bwd) - ) # E -- 2 --> G -- 1 --> F == 3 + 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]], + "E": [[None, np.inf]], + "G": [["E", 2]], + } doctest.testmod() From 3adab98fcbd1b9bb84deff1d9e3a1dab725a60ca Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 14 Nov 2022 11:54:59 +0000 Subject: [PATCH 19/27] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- graphs/bi_directional_dijkstra.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/graphs/bi_directional_dijkstra.py b/graphs/bi_directional_dijkstra.py index 55c2a692f373..dc77f07708ad 100644 --- a/graphs/bi_directional_dijkstra.py +++ b/graphs/bi_directional_dijkstra.py @@ -18,7 +18,7 @@ def bidirectional_dij( - source: str, destination: str, graph_forward: dict, graph_backward: dict + source: str, destination: str, graph_forward: dict, graph_backward: dict ) -> int: """ Bi-directional Dijkstra's algorithm. @@ -106,8 +106,6 @@ def bidirectional_dij( return shortest_path_distance - - if __name__ == "__main__": import doctest From 9552936c46ad1b3116031a84687d699e469f37f2 Mon Sep 17 00:00:00 2001 From: Swayam <74960567+practice404@users.noreply.github.com> Date: Mon, 14 Nov 2022 17:32:18 +0530 Subject: [PATCH 20/27] Update bi_directional_dijkstra.py --- graphs/bi_directional_dijkstra.py | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/graphs/bi_directional_dijkstra.py b/graphs/bi_directional_dijkstra.py index dc77f07708ad..8c6a3525d041 100644 --- a/graphs/bi_directional_dijkstra.py +++ b/graphs/bi_directional_dijkstra.py @@ -18,7 +18,7 @@ def bidirectional_dij( - source: str, destination: str, graph_forward: dict, graph_backward: dict + source: str, destination: str, graph_forward: dict, graph_backward: dict ) -> int: """ Bi-directional Dijkstra's algorithm. @@ -106,10 +106,9 @@ def bidirectional_dij( return shortest_path_distance -if __name__ == "__main__": - import doctest - graph_fwd = { + +graph_fwd = { "B": [["C", 1]], "C": [["D", 1]], "D": [["F", 1]], @@ -117,12 +116,16 @@ def bidirectional_dij( "F": [], "G": [["F", 1]], } - graph_bwd = { - "B": [["E", 1]], - "C": [["B", 1]], - "D": [["C", 1]], - "F": [["D", 1], ["G", 1]], - "E": [[None, np.inf]], - "G": [["E", 2]], - } +graph_bwd = { + "B": [["E", 1]], + "C": [["B", 1]], + "D": [["C", 1]], + "F": [["D", 1], ["G", 1]], + "E": [[None, np.inf]], + "G": [["E", 2]], +} + +if __name__ == "__main__": + import doctest + doctest.testmod() From 36e68d38ff3cc99a3b831e64d4476445551b0d76 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 14 Nov 2022 12:03:19 +0000 Subject: [PATCH 21/27] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- graphs/bi_directional_dijkstra.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/graphs/bi_directional_dijkstra.py b/graphs/bi_directional_dijkstra.py index 8c6a3525d041..ac4e9ca4eca5 100644 --- a/graphs/bi_directional_dijkstra.py +++ b/graphs/bi_directional_dijkstra.py @@ -18,7 +18,7 @@ def bidirectional_dij( - source: str, destination: str, graph_forward: dict, graph_backward: dict + source: str, destination: str, graph_forward: dict, graph_backward: dict ) -> int: """ Bi-directional Dijkstra's algorithm. @@ -106,16 +106,14 @@ def bidirectional_dij( return shortest_path_distance - - graph_fwd = { - "B": [["C", 1]], - "C": [["D", 1]], - "D": [["F", 1]], - "E": [["B", 1], ["G", 2]], - "F": [], - "G": [["F", 1]], - } + "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]], From 8a00b3e882139bff6223a42230686469ddd10f7e Mon Sep 17 00:00:00 2001 From: Swayam <74960567+practice404@users.noreply.github.com> Date: Tue, 15 Nov 2022 09:52:12 +0530 Subject: [PATCH 22/27] Update bi_directional_dijkstra.py --- graphs/bi_directional_dijkstra.py | 37 +++++++++++++++---------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/graphs/bi_directional_dijkstra.py b/graphs/bi_directional_dijkstra.py index ac4e9ca4eca5..497041717d88 100644 --- a/graphs/bi_directional_dijkstra.py +++ b/graphs/bi_directional_dijkstra.py @@ -27,7 +27,7 @@ def bidirectional_dij( Warnings: If the destination is not reachable, function returns -1 - + >>> from bi_directional_dijkstra import bidirectional_dij >>> bidirectional_dij("E", "F", graph_fwd, graph_bwd) 3 """ @@ -106,24 +106,23 @@ def bidirectional_dij( return shortest_path_distance -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]], - "E": [[None, np.inf]], - "G": [["E", 2]], -} - if __name__ == "__main__": import doctest - doctest.testmod() + 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]], + "E": [[None, np.inf]], + "G": [["E", 2]], + } + doctest.testmod(globs={'graph_fwd': graph_fwd, 'graph_bwd': graph_bwd}) From d64667ec433ec48a44cab66fde3f4e79eab32fb7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 15 Nov 2022 04:24:04 +0000 Subject: [PATCH 23/27] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- graphs/bi_directional_dijkstra.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphs/bi_directional_dijkstra.py b/graphs/bi_directional_dijkstra.py index 497041717d88..f00764b84640 100644 --- a/graphs/bi_directional_dijkstra.py +++ b/graphs/bi_directional_dijkstra.py @@ -125,4 +125,4 @@ def bidirectional_dij( "E": [[None, np.inf]], "G": [["E", 2]], } - doctest.testmod(globs={'graph_fwd': graph_fwd, 'graph_bwd': graph_bwd}) + doctest.testmod(globs={"graph_fwd": graph_fwd, "graph_bwd": graph_bwd}) From 10b529a17f36532fe620b1253dadf093ead11c74 Mon Sep 17 00:00:00 2001 From: Swayam <74960567+practice404@users.noreply.github.com> Date: Tue, 15 Nov 2022 09:57:54 +0530 Subject: [PATCH 24/27] Update bi_directional_dijkstra.py --- graphs/bi_directional_dijkstra.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/graphs/bi_directional_dijkstra.py b/graphs/bi_directional_dijkstra.py index f00764b84640..e94ded1b0356 100644 --- a/graphs/bi_directional_dijkstra.py +++ b/graphs/bi_directional_dijkstra.py @@ -22,12 +22,13 @@ def bidirectional_dij( ) -> int: """ Bi-directional Dijkstra's algorithm. + Returns: shortest_path_distance (int): length of the shortest path. Warnings: If the destination is not reachable, function returns -1 - >>> from bi_directional_dijkstra import bidirectional_dij + >>> bidirectional_dij("E", "F", graph_fwd, graph_bwd) 3 """ @@ -125,4 +126,4 @@ def bidirectional_dij( "E": [[None, np.inf]], "G": [["E", 2]], } - doctest.testmod(globs={"graph_fwd": graph_fwd, "graph_bwd": graph_bwd}) + doctest.testmod(globs={'graph_fwd': graph_fwd, 'graph_bwd': graph_bwd}) From 9425f6ffc8dae28cf5cc25e7bf88da4b9bb88de2 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 15 Nov 2022 04:29:23 +0000 Subject: [PATCH 25/27] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- graphs/bi_directional_dijkstra.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/graphs/bi_directional_dijkstra.py b/graphs/bi_directional_dijkstra.py index e94ded1b0356..23644dbce810 100644 --- a/graphs/bi_directional_dijkstra.py +++ b/graphs/bi_directional_dijkstra.py @@ -22,7 +22,7 @@ def bidirectional_dij( ) -> int: """ Bi-directional Dijkstra's algorithm. - + Returns: shortest_path_distance (int): length of the shortest path. @@ -126,4 +126,4 @@ def bidirectional_dij( "E": [[None, np.inf]], "G": [["E", 2]], } - doctest.testmod(globs={'graph_fwd': graph_fwd, 'graph_bwd': graph_bwd}) + doctest.testmod(globs={"graph_fwd": graph_fwd, "graph_bwd": graph_bwd}) From c5c4ebe62c8d76568ba879c1c6fd1a59dd4f0db3 Mon Sep 17 00:00:00 2001 From: Swayam <74960567+practice404@users.noreply.github.com> Date: Fri, 18 Nov 2022 09:50:11 +0530 Subject: [PATCH 26/27] Update bi_directional_dijkstra.py --- graphs/bi_directional_dijkstra.py | 35 ++++++++++++++++--------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/graphs/bi_directional_dijkstra.py b/graphs/bi_directional_dijkstra.py index 23644dbce810..80394502c9a8 100644 --- a/graphs/bi_directional_dijkstra.py +++ b/graphs/bi_directional_dijkstra.py @@ -106,24 +106,25 @@ def bidirectional_dij( shortest_path_distance = shortest_distance return shortest_path_distance +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]], + "E": [[None, np.inf]], + "G": [["E", 2]], +} if __name__ == "__main__": import doctest - 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]], - "E": [[None, np.inf]], - "G": [["E", 2]], - } - doctest.testmod(globs={"graph_fwd": graph_fwd, "graph_bwd": graph_bwd}) + + doctest.testmod() From dc57aaec43b57cc1cd8db4cbe407b62bb9a523d6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 18 Nov 2022 04:22:12 +0000 Subject: [PATCH 27/27] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- graphs/bi_directional_dijkstra.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphs/bi_directional_dijkstra.py b/graphs/bi_directional_dijkstra.py index 80394502c9a8..fc53e2f0d8f3 100644 --- a/graphs/bi_directional_dijkstra.py +++ b/graphs/bi_directional_dijkstra.py @@ -106,6 +106,7 @@ def bidirectional_dij( shortest_path_distance = shortest_distance return shortest_path_distance + graph_fwd = { "B": [["C", 1]], "C": [["D", 1]], @@ -126,5 +127,4 @@ def bidirectional_dij( if __name__ == "__main__": import doctest - doctest.testmod()