-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdfs.py
More file actions
132 lines (111 loc) · 4.15 KB
/
dfs.py
File metadata and controls
132 lines (111 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
from termcolor import colored
class DFS:
"""
Problem Type: Graph Traversal, Depth-First Search (DFS)
Problem Statement:
Given a graph represented as an adjacency list and a starting node, perform a depth-first search (DFS) traversal of the graph.
Parameters:
graph (Dict[Any, List[Any]]): The graph represented as an adjacency list.
start (Any): The starting node for the DFS traversal.
visited (Set[Any], optional): The set of already visited nodes. Defaults to None.
Returns:
None: The function prints the nodes in the order they are visited during the DFS traversal.
Example:
graph = {
'A': ['B', 'C'],
'B': ['D', 'E'],
'C': ['F'],
'D': [],
'E': ['G'],
'F': []
}
dfs = DFS(graph)
dfs.dfs('A') -> A B D E F C
Diagram:
A
/ \
B C
/ \ \
D E F
\
G
"""
def __init__(self, graph):
# Initialize the graph and the visited set
self.graph = graph
self.visited = set()
def __iter__(self):
# Initialize the stack with the starting node
self.stack = [self.start]
return self
def __next__(self):
# If the stack is empty, stop the iteration
if not self.stack:
raise StopIteration
# Pop a node from the stack
node = self.stack.pop()
# If the node has not been visited, mark it as visited and add its neighbors to the stack
if node not in self.visited:
self.visited.add(node)
self.stack.extend(reversed(self.graph[node]))
return node
# If the node has already been visited, continue to the next node
return self.__next__()
def dfs(self, start):
"""
Problem Type: Graph Traversal, Depth-First Search (DFS)
Depth-First Search (DFS) is an algorithm for traversing or searching tree or graph data structures.
The algorithm starts at the root node (selecting some arbitrary node as the root in the case of a graph)
and explores as far as possible along each branch before backtracking.
The DFS algorithm works as follows:
1. Initialize a stack and add the starting node to it.
2. Initialize a set to keep track of visited nodes.
3. Loop until the stack is empty:
a. Pop a node from the stack.
b. If the node has not been visited:
i. Print the node.
ii. Mark the node as visited.
iii. Add all unvisited neighbors of the node to the stack.
Problem Statement:
Given a graph represented as an adjacency list and a starting node, perform a depth-first search (DFS) traversal of the graph.
Parameters:
start (Any): The starting node for the DFS traversal.
Returns:
None: The function prints the nodes in the order they are visited during the DFS traversal.
Example:
graph = {
'A': ['B', 'C'],
'B': ['D', 'E'],
'C': ['F'],
'D': [],
'E': ['G'],
'F': []
}
dfs.dfs('A') -> A B D E G C F
"""
# Set the starting node
self.start = start
# Iterate through the nodes using the custom iterator
for node in self:
# Print the visited node
if node != self.start:
print(",", end="", flush=True)
print(colored(node, 'blue'), end="", flush=True)
print()
def __repr__(self):
# Return a string representation of the DFS object
return f"\n{colored('DFS', 'red')}\n{colored('-'*100, 'red')}\n{colored('graph', 'red')}= {self.graph},\n{colored('visited', 'red')}= {self.visited},\n{colored('start', 'red')}= {self.start}\n{colored('-'*100, 'red')}"
# Example usage:
graph = {
'A': ['B', 'C'],
'B': ['D', 'E'],
'C': ['F'],
'D': [],
'E': ['G'],
'F': [],
'G': []
}
print(colored("\nDFS Traversal:", 'green'))
dfs = DFS(graph)
dfs.dfs('A')
print(f"\n{colored('Representation:', 'magenta')} {dfs}")