Skip to content

Commit 398d00b

Browse files
authored
Add doctest for DFS
1 parent 07c21ee commit 398d00b

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

graphs/dfs.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ def depth_first_search(graph: Dict, start: str) -> Set[int]:
2121
:param graph: directed graph in dictionary format
2222
:param vertex: starting vectex as a string
2323
:returns: the trace of the search
24+
>>> G = { "A": ["B", "C", "D"], "B": ["A", "D", "E"],
25+
... "C": ["A", "F"], "D": ["B", "D"], "E": ["B", "F"],
26+
... "F": ["C", "E", "G"], "G": ["F"] }
27+
>>> start = "A"
28+
>>> output_G = list({'A', 'B', 'C', 'D', 'E', 'F', 'G'})
29+
>>> all(x in output_G for x in list(depth_first_search(G, "A")))
30+
True
31+
>>> all(x in output_G for x in list(depth_first_search(G, "G")))
32+
True
2433
"""
2534
explored, stack = set(start), [start]
2635
while stack:
@@ -33,14 +42,11 @@ def depth_first_search(graph: Dict, start: str) -> Set[int]:
3342
return explored
3443

3544

36-
G = {
37-
"A": ["B", "C"],
38-
"B": ["A", "D", "E"],
39-
"C": ["A", "F"],
40-
"D": ["B"],
41-
"E": ["B", "F"],
42-
"F": ["C", "E"],
43-
}
45+
G = { "A": ["B", "C", "D"], "B": ["A", "D", "E"],
46+
"C": ["A", "F"], "D": ["B", "D"], "E": ["B", "F"],
47+
"F": ["C", "E", "G"], "G": ["F"] }
4448

4549
if __name__ == "__main__":
50+
import doctest
51+
doctest.testmod()
4652
print(depth_first_search(G, "A"))

0 commit comments

Comments
 (0)