-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdfs.py
More file actions
21 lines (17 loc) · 650 Bytes
/
dfs.py
File metadata and controls
21 lines (17 loc) · 650 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from backtrack import backtrack_path
def dfs_recurs(graph, start_node, end_node, visited=set()):
if visited is None:
visited = set()
order = []
if start_node not in visited:
order.append(start_node)
visited.add(start_node)
for node in graph[start_node]:
if node not in visited:
order.extend(dfs_recurs(graph, node, end_node, visited))
if end_node in order:
break
return order
def dfs(graph, start_node, end_node):
order = dfs_recurs(graph, start_node, end_node, set())
return order, backtrack_path(start_node, end_node, order, graph)