文章转载处:https://blog.csdn.net/qq_40276310/article/details/80668401
一、算法原理
首先,我们给定一个二叉树图如下:
1、宽度优先搜索:
宽度优先搜索算法(Breadth First Search,BSF),思想是:
换句话说,广度优先搜索遍历图的过程是以v为起点,由近至远,依次访问和v有路径相通且路 径长度为1,2…的顶点。
如上图的BFS访问顺序为:A->B->C->D->E->F
2、深度优先搜索:
图的深度优先搜索(Depth First Search, DFS),和树的前序遍历非常类似。
它的思想:
如上图的BFS访问顺序为:A->B->D->E->C->F
2、关键代码的编写
(1)首先定义一个类,创建一个二叉树图
# 首先定义一个创建图的类,使用邻接矩阵
class Graph(object):
def __init__(self, *args, **kwargs):
self.order = [] # visited order
self.neighbor = {}
def add_node(self, node):
key, val = node
if not isinstance(val, list):
print('节点输入时应该为一个线性表') # 避免不正确的输入
self.neighbor[key] = val
(2)宽度优先算法的实现
# 宽度优先算法的实现
def BFS(self, root):
#首先判断根节点是否为空节点
if root != None:
search_queue = deque()
search_queue.append(root)
visited = []
else:
print('root is None')
return -1
while search_queue:
person = search_queue.popleft()
self.order.append(person)
if (not person in visited) and (person in self.neighbor.keys()):
search_queue += self.neighbor[person]
visited.append(person)
(3)深度优先算法的实现
# 深度优先算法的实现
def DFS(self, root):
# 首先判断根节点是否为空节点
if root != None:
search_queue = deque()
search_queue.append(root)
visited = []
else:
print('root is None')
return -1
while search_queue:
person = search_queue.popleft()
self.order.append(person)
if (not person in visited) and (person in self.neighbor.keys()):
tmp = self.neighbor[person]
tmp.reverse()
for index in tmp:
search_queue.appendleft(index)
visited.append(person)
# -*- coding: utf-8 -*-
from collections import deque # 线性表的模块
# 首先定义一个创建图的类,使用邻接矩阵
class Graph(object):
def __init__(self, *args, **kwargs):
self.order = [] # visited order
self.neighbor = {}
def add_node(self, node):
key, val = node
if not isinstance(val, list):
print('节点输入时应该为一个线性表') # 避免不正确的输入
self.neighbor[key] = val
# 宽度优先算法的实现
def BFS(self, root):
#首先判断根节点是否为空节点
if root != None:
search_queue = deque()
search_queue.append(root)
visited = []
else:
print('root is None')
return -1
while search_queue:
person = search_queue.popleft()
self.order.append(person)
if (not person in visited) and (person in self.neighbor.keys()):
search_queue += self.neighbor[person]
visited.append(person)
# 深度优先算法的实现
def DFS(self, root):
# 首先判断根节点是否为空节点
if root != None:
search_queue = deque()
search_queue.append(root)
visited = []
else:
print('root is None')
return -1
while search_queue:
person = search_queue.popleft()
self.order.append(person)
if (not person in visited) and (person in self.neighbor.keys()):
tmp = self.neighbor[person]
tmp.reverse()
for index in tmp:
search_queue.appendleft(index)
visited.append(person)
def clear(self):
self.order = []
def node_print(self):
for index in self.order:
print(index, end=' ')
if __name__ == '__main__':
# 创建一个二叉树图
g = Graph()
g.add_node(('A', ['B', 'C']))
g.add_node(('B', ['D', 'E']))
g.add_node(('C', ['F']))
# 进行宽度优先搜索
g.BFS('A')
print('宽度优先搜索:')
print(' ', end=' ')
g.node_print()
g.clear()
# 进行深度优先搜索
print('\n\n深度优先搜索:')
print(' ', end=' ')
g.DFS('A')
g.node_print()
print()