Skip to content

Commit 456eb66

Browse files
authored
"feat: add graph algorithem"
1 parent ff0d7c8 commit 456eb66

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

pydatastructs/graphs/algorithms.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,29 @@ def shortest_paths(graph: Graph, algorithm: str, source: str, target: str="", **
718718
(distances[target], predecessors): (float, dict)
719719
If target is provided and algorithm used is
720720
'bellman_ford'/'dijkstra'/'A_star'.
721+
722+
Examples
723+
========
724+
>>> from pydatastructs import Graph, AdjacencyListGraphNode
725+
>>> from pydatastructs import shortest_paths
726+
>>> V1 = AdjacencyListGraphNode("V1")
727+
>>> V2 = AdjacencyListGraphNode("V2")
728+
>>> V3 = AdjacencyListGraphNode("V3")
729+
>>> G = Graph(V1, V2, V3)
730+
>>> G.add_edge('V2', 'V3', 10)
731+
>>> G.add_edge('V1', 'V2', 11)
732+
>>> shortest_paths(G, 'bellman_ford', 'V1')
733+
({'V1': 0, 'V2': 11, 'V3': 21}, {'V1': None, 'V2': 'V1', 'V3': 'V2'})
734+
>>> shortest_paths(G, 'dijkstra', 'V1')
735+
({'V2': 11, 'V3': 21, 'V1': 0}, {'V1': None, 'V2': 'V1', 'V3': 'V2'})
736+
>>> shortest_paths(G, 'A_star', 'V1', 'V3')
737+
(21, {'V1': None, 'V2': 'V1', 'V3': 'V2'})
738+
739+
References
740+
==========
741+
.. [1] https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm
742+
.. [2] https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
743+
.. [3] https://en.wikipedia.org/wiki/A*_search_algorithm
721744
"""
722745
raise_if_backend_is_not_python(
723746
shortest_paths, kwargs.get('backend', Backend.PYTHON))

0 commit comments

Comments
 (0)