Skip to content

Commit 8877bf7

Browse files
Added Dijkstra algorithm for shortest paths (#301)
1 parent 7138dc4 commit 8877bf7

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

pydatastructs/graphs/algorithms.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
DisjointSetForest, PriorityQueue)
1111
from pydatastructs.graphs.graph import Graph
1212
from pydatastructs.linear_data_structures.algorithms import merge_sort_parallel
13+
from pydatastructs import PriorityQueue
1314

1415
__all__ = [
1516
'breadth_first_search',
@@ -655,6 +656,7 @@ def shortest_paths(graph: Graph, algorithm: str,
655656
The algorithm to be used. Currently, the following algorithms
656657
are implemented,
657658
'bellman_ford' -> Bellman-Ford algorithm as given in [1].
659+
'dijkstra' -> Dijkstra algorithm as given in [2].
658660
source: str
659661
The name of the source the node.
660662
target: str
@@ -667,7 +669,7 @@ def shortest_paths(graph: Graph, algorithm: str,
667669
668670
(distances, predecessors): (dict, dict)
669671
If target is not provided and algorithm used
670-
is 'bellman_ford'.
672+
is 'bellman_ford'/'dijkstra'.
671673
(distances[target], predecessors): (float, dict)
672674
If target is provided and algorithm used is
673675
'bellman_ford'.
@@ -685,11 +687,14 @@ def shortest_paths(graph: Graph, algorithm: str,
685687
>>> G.add_edge('V1', 'V2', 11)
686688
>>> shortest_paths(G, 'bellman_ford', 'V1')
687689
({'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'})
688692
689693
References
690694
==========
691695
692696
.. [1] https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm
697+
.. [2] https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
693698
"""
694699
import pydatastructs.graphs.algorithms as algorithms
695700
func = "_" + algorithm + "_" + graph._impl
@@ -728,6 +733,36 @@ def _bellman_ford_adjacency_list(graph: Graph, source: str, target: str) -> tupl
728733

729734
_bellman_ford_adjacency_matrix = _bellman_ford_adjacency_list
730735

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+
731766
def topological_sort(graph: Graph, algorithm: str) -> list:
732767
"""
733768
Performs topological sort on the given graph using given algorithm.

pydatastructs/graphs/tests/test_algorithms.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,8 @@ def _test_shortest_paths(ds, algorithm):
290290

291291
_test_shortest_paths("List", 'bellman_ford')
292292
_test_shortest_paths("Matrix", 'bellman_ford')
293+
_test_shortest_paths("List", 'dijkstra')
294+
_test_shortest_paths("Matrix", 'dijkstra')
293295

294296
def test_topological_sort():
295297

0 commit comments

Comments
 (0)