-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDFS_BFS.py
32 lines (32 loc) · 959 Bytes
/
DFS_BFS.py
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
# DFS
def DFS(graph, node):
# DFS使用栈实现先进后出
# 需要注意DFS在系统上是有限制的,不是无限制的,比如win只支持3000左右(实验测试知道的)
stack = []
stack.append(node)
Find_Path = set()
Find_Path.add(node)
while(len(stack) > 0):
vertex = stack.pop()
nodes = graph[vertex]
for node in nodes:
if node not in Find_Path:
stack.append(node)
Find_Path.add(node)
print(vertex,end=" ")
DFS(graph,'A')
# BFS
def BFS(graph, node):
queue = []#BFS使用栈实现先进先出
queue.append(node)
Find_Path = set()
Find_Path.add(node)
while(len(queue) > 0):
vertex = queue.pop(0)
nodes = graph[vertex]
for node in nodes:
if node not in Find_Path:
queue.append(node)
Find_Path.add(node)
print(vertex,end=" ")
BFS(graph,'A')