@@ -21,6 +21,15 @@ def depth_first_search(graph: Dict, start: str) -> Set[int]:
21
21
:param graph: directed graph in dictionary format
22
22
:param vertex: starting vectex as a string
23
23
: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
24
33
"""
25
34
explored , stack = set (start ), [start ]
26
35
while stack :
@@ -33,14 +42,11 @@ def depth_first_search(graph: Dict, start: str) -> Set[int]:
33
42
return explored
34
43
35
44
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" ] }
44
48
45
49
if __name__ == "__main__" :
50
+ import doctest
51
+ doctest .testmod ()
46
52
print (depth_first_search (G , "A" ))
0 commit comments