10
10
DisjointSetForest , PriorityQueue )
11
11
from pydatastructs .graphs .graph import Graph
12
12
from pydatastructs .linear_data_structures .algorithms import merge_sort_parallel
13
+ from pydatastructs import PriorityQueue
13
14
14
15
__all__ = [
15
16
'breadth_first_search' ,
@@ -655,6 +656,7 @@ def shortest_paths(graph: Graph, algorithm: str,
655
656
The algorithm to be used. Currently, the following algorithms
656
657
are implemented,
657
658
'bellman_ford' -> Bellman-Ford algorithm as given in [1].
659
+ 'dijkstra' -> Dijkstra algorithm as given in [2].
658
660
source: str
659
661
The name of the source the node.
660
662
target: str
@@ -667,7 +669,7 @@ def shortest_paths(graph: Graph, algorithm: str,
667
669
668
670
(distances, predecessors): (dict, dict)
669
671
If target is not provided and algorithm used
670
- is 'bellman_ford'.
672
+ is 'bellman_ford'/'dijkstra' .
671
673
(distances[target], predecessors): (float, dict)
672
674
If target is provided and algorithm used is
673
675
'bellman_ford'.
@@ -685,11 +687,14 @@ def shortest_paths(graph: Graph, algorithm: str,
685
687
>>> G.add_edge('V1', 'V2', 11)
686
688
>>> shortest_paths(G, 'bellman_ford', 'V1')
687
689
({'V1': 0, 'V2': 11, 'V3': 21}, {'V1': None, 'V2': 'V1', 'V3': 'V2'})
690
+ >>> shortest_paths(G, 'dijkstra', 'V1')
691
+ ({'V2': 11, 'V3': 21, 'V1': 0}, {'V1': None, 'V2': 'V1', 'V3': 'V2'})
688
692
689
693
References
690
694
==========
691
695
692
696
.. [1] https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm
697
+ .. [2] https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
693
698
"""
694
699
import pydatastructs .graphs .algorithms as algorithms
695
700
func = "_" + algorithm + "_" + graph ._impl
@@ -728,6 +733,36 @@ def _bellman_ford_adjacency_list(graph: Graph, source: str, target: str) -> tupl
728
733
729
734
_bellman_ford_adjacency_matrix = _bellman_ford_adjacency_list
730
735
736
+ def _dijkstra_adjacency_list (graph : Graph , start : str , target : str ):
737
+ V = len (graph .vertices )
738
+ visited , dist , pred = dict (), dict (), dict ()
739
+ for v in graph .vertices :
740
+ visited [v ] = False
741
+ pred [v ] = None
742
+ if v != start :
743
+ dist [v ] = float ('inf' )
744
+ dist [start ] = 0
745
+ pq = PriorityQueue (implementation = 'binomial_heap' )
746
+ for vertex in dist :
747
+ pq .push (vertex , dist [vertex ])
748
+ for _ in range (V ):
749
+ u = pq .pop ()
750
+ visited [u ] = True
751
+ for v in graph .vertices :
752
+ edge_str = u + '_' + v
753
+ if (edge_str in graph .edge_weights and graph .edge_weights [edge_str ].value > 0 and
754
+ visited [v ] is False and dist [v ] > dist [u ] + graph .edge_weights [edge_str ].value ):
755
+ dist [v ] = dist [u ] + graph .edge_weights [edge_str ].value
756
+ pred [v ] = u
757
+ pq .push (v , dist [v ])
758
+
759
+ if target != "" :
760
+ return (dist [target ], pred )
761
+ return dist , pred
762
+
763
+ _dijkstra_adjacency_matrix = _dijkstra_adjacency_list
764
+
765
+
731
766
def topological_sort (graph : Graph , algorithm : str ) -> list :
732
767
"""
733
768
Performs topological sort on the given graph using given algorithm.
0 commit comments