Skip to content

Commit 264cdfa

Browse files
authored
Add doctest for shortest path
1 parent b7039ea commit 264cdfa

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

graphs/breadth_first_search_shortest_path.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ def shortest_path(self, target_vertex):
4242
1.) No path is found. The string is a human readable message to indicate this.
4343
2.) The shortest path is found. The string is in the form `v1(->v2->v3->...->vn)`,
4444
where v1 is the source vertex and vn is the target vertex, if it exists separately.
45+
46+
>>> g = Graph(graph, "G")
47+
>>> g.breath_first_search()
48+
49+
Case 1 - No path is found.
50+
>>> assert(g.shortest_path("Foo") == "No path from vertex:G to vertex:Foo")
51+
52+
Case 2 - The path is found.
53+
>>> assert(g.shortest_path("D") == "G->C->A->B->D")
54+
>>> assert(g.shortest_path("G") == "G")
4555
"""
4656
if target_vertex == self.source_vertex:
4757
return f"{self.source_vertex}"
@@ -52,6 +62,8 @@ def shortest_path(self, target_vertex):
5262

5363

5464
if __name__ == "__main__":
65+
import doctest
66+
doctest.testmod()
5567
g = Graph(graph, "G")
5668
g.breath_first_search()
5769
print(g.shortest_path("D"))

0 commit comments

Comments
 (0)