mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-24 13:31:07 +00:00
7aaf79cc23
* Update dfs.py * Add type hints, rearrange doc-strings and comments * fixup! Format Python code with psf/black push * dfs -> depth_first_search Co-Authored-By: Christian Clauss <cclauss@me.com> * dfs -> depth_first_search * Add doctest for DFS * fixup! Format Python code with psf/black push * Rename dfs.py to depth_first_search_dictionary.py * updating DIRECTORY.md * Rename depth_first_search_dictionary.py to depth_first_search_dfs.py * updating DIRECTORY.md * Rename depth_first_search.py to depth_first_search_2.py * updating DIRECTORY.md * Rename depth_first_search_dfs.py to depth_first_search.py * updating DIRECTORY.md Co-authored-by: John Law <johnlaw.po@gmail.com> Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
60 lines
1.8 KiB
Python
60 lines
1.8 KiB
Python
"""The DFS function simply calls itself recursively for every unvisited child of
|
|
its argument. We can emulate that behaviour precisely using a stack of iterators.
|
|
Instead of recursively calling with a node, we'll push an iterator to the node's
|
|
children onto the iterator stack. When the iterator at the top of the stack
|
|
terminates, we'll pop it off the stack.
|
|
|
|
Pseudocode:
|
|
all nodes initially unexplored
|
|
mark s as explored
|
|
for every edge (s, v):
|
|
if v unexplored:
|
|
DFS(G, v)
|
|
"""
|
|
|
|
from typing import Set, Dict
|
|
|
|
|
|
def depth_first_search(graph: Dict, start: str) -> Set[int]:
|
|
"""Depth First Search on Graph
|
|
|
|
:param graph: directed graph in dictionary format
|
|
:param vertex: starting vectex as a string
|
|
:returns: the trace of the search
|
|
>>> G = { "A": ["B", "C", "D"], "B": ["A", "D", "E"],
|
|
... "C": ["A", "F"], "D": ["B", "D"], "E": ["B", "F"],
|
|
... "F": ["C", "E", "G"], "G": ["F"] }
|
|
>>> start = "A"
|
|
>>> output_G = list({'A', 'B', 'C', 'D', 'E', 'F', 'G'})
|
|
>>> all(x in output_G for x in list(depth_first_search(G, "A")))
|
|
True
|
|
>>> all(x in output_G for x in list(depth_first_search(G, "G")))
|
|
True
|
|
"""
|
|
explored, stack = set(start), [start]
|
|
while stack:
|
|
v = stack.pop()
|
|
# one difference from BFS is to pop last element here instead of first one
|
|
for w in graph[v]:
|
|
if w not in explored:
|
|
explored.add(w)
|
|
stack.append(w)
|
|
return explored
|
|
|
|
|
|
G = {
|
|
"A": ["B", "C", "D"],
|
|
"B": ["A", "D", "E"],
|
|
"C": ["A", "F"],
|
|
"D": ["B", "D"],
|
|
"E": ["B", "F"],
|
|
"F": ["C", "E", "G"],
|
|
"G": ["F"],
|
|
}
|
|
|
|
if __name__ == "__main__":
|
|
import doctest
|
|
|
|
doctest.testmod()
|
|
print(depth_first_search(G, "A"))
|