-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFS.py
More file actions
26 lines (24 loc) · 728 Bytes
/
DFS.py
File metadata and controls
26 lines (24 loc) · 728 Bytes
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
def dfs(gr,node,returnset=False,processed=False):
# EXEMPLARY DFS IMPLEMENTATION
visited = dict()
for nd in gr.nodes:
visited[nd] = 0
def DFSutil(current):
# here we can add processing of node
visited[current] = 1
if gr.hasneib(current):
for nei in gr[current]:
if (not processed or nei not in processed) and not visited[nei]:
DFSutil(nei)
DFSutil(node)
if returnset:
res = set()
else:
res = []
for key in visited.keys():
if visited[key] == 1:
if returnset:
res.add(key)
else:
res.append(key)
return res