Skip to content

Commit 94b836b

Browse files
author
dosart
committed
Added dfs algorithm (iterative version)
1 parent f196f72 commit 94b836b

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ dist/
66
.coverage
77
coverage.xml
88
.pytest_cache/
9+
.vs/

algoritms/dfs_iterative.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""Implementation iterative depth-first search."""
2+
3+
4+
def dfs_iterative(graph):
5+
"""Visit all the vertex of graph.
6+
7+
Args:
8+
graph: graph for search
9+
10+
Returns:
11+
visited (dict): key (vertex id), value (true if vetrex is visited)
12+
"""
13+
visited = {vertex : False for vertex in graph}
14+
15+
stack = []
16+
for vertex in graph:
17+
stack.append(vertex)
18+
19+
while len(stack) > 0:
20+
vertex = stack.pop()
21+
for adjacent_vertex in vertex:
22+
if visited[adjacent_vertex] is False:
23+
stack.append(adjacent_vertex)
24+
visited[adjacent_vertex] == True

0 commit comments

Comments
 (0)