Skip to content

adding dijsktra algorithm #301

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion pydatastructs/graphs/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
DisjointSetForest, PriorityQueue)
from pydatastructs.graphs.graph import Graph
from pydatastructs.linear_data_structures.algorithms import merge_sort_parallel
from pydatastructs import PriorityQueue

__all__ = [
'breadth_first_search',
Expand Down Expand Up @@ -655,6 +656,7 @@ def shortest_paths(graph: Graph, algorithm: str,
The algorithm to be used. Currently, the following algorithms
are implemented,
'bellman_ford' -> Bellman-Ford algorithm as given in [1].
'dijkstra' -> Dijkstra algorithm as given in [2].
source: str
The name of the source the node.
target: str
Expand All @@ -667,7 +669,7 @@ def shortest_paths(graph: Graph, algorithm: str,

(distances, predecessors): (dict, dict)
If target is not provided and algorithm used
is 'bellman_ford'.
is 'bellman_ford'/'dijkstra'.
(distances[target], predecessors): (float, dict)
If target is provided and algorithm used is
'bellman_ford'.
Expand All @@ -685,11 +687,14 @@ def shortest_paths(graph: Graph, algorithm: str,
>>> G.add_edge('V1', 'V2', 11)
>>> shortest_paths(G, 'bellman_ford', 'V1')
({'V1': 0, 'V2': 11, 'V3': 21}, {'V1': None, 'V2': 'V1', 'V3': 'V2'})
>>> shortest_paths(G, 'dijkstra', 'V1')
({'V2': 11, 'V3': 21, 'V1': 0}, {'V1': None, 'V2': 'V1', 'V3': 'V2'})

References
==========

.. [1] https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm
.. [2] https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
"""
import pydatastructs.graphs.algorithms as algorithms
func = "_" + algorithm + "_" + graph._impl
Expand Down Expand Up @@ -728,6 +733,36 @@ def _bellman_ford_adjacency_list(graph: Graph, source: str, target: str) -> tupl

_bellman_ford_adjacency_matrix = _bellman_ford_adjacency_list

def _dijkstra_adjacency_list(graph: Graph, start: str, target: str):
V = len(graph.vertices)
visited, dist, pred = dict(), dict(), dict()
for v in graph.vertices:
visited[v] = False
pred[v] = None
if v != start:
dist[v] = float('inf')
dist[start] = 0
pq = PriorityQueue(implementation='binomial_heap')
for vertex in dist:
pq.push(vertex, dist[vertex])
for _ in range(V):
u = pq.pop()
visited[u] = True
for v in graph.vertices:
edge_str = u + '_' + v
if (edge_str in graph.edge_weights and graph.edge_weights[edge_str].value > 0 and
visited[v] is False and dist[v] > dist[u] + graph.edge_weights[edge_str].value):
dist[v] = dist[u] + graph.edge_weights[edge_str].value
pred[v] = u
pq.push(v, dist[v])

if target != "":
return (dist[target], pred)
return dist, pred

_dijkstra_adjacency_matrix = _dijkstra_adjacency_list


def topological_sort(graph: Graph, algorithm: str) -> list:
"""
Performs topological sort on the given graph using given algorithm.
Expand Down
2 changes: 2 additions & 0 deletions pydatastructs/graphs/tests/test_algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,8 @@ def _test_shortest_paths(ds, algorithm):

_test_shortest_paths("List", 'bellman_ford')
_test_shortest_paths("Matrix", 'bellman_ford')
_test_shortest_paths("List", 'dijkstra')
_test_shortest_paths("Matrix", 'dijkstra')

def test_topological_sort():

Expand Down