"""
procedure BFS(G,v) is
let Q be a queue
Q.enqueue(v)
label v as discovered
while Q is not empty
v ← Q.dequeue()
procedure(v)
for all edges from v to w in G.adjacentEdges(v) do
if w is not labeled as discovered
Q.enqueue(w)
label w as discovered
"""
def procedure(v):
pass
def BFS(G,v0):
""" 广度优先搜索 """
q, s = [], set()
q.extend(v0)
s.add(v0)
while q: # 当队列q非空
v = q.pop(0)
procedure(v)
for w in G[v]: # 对图G中顶点v的所有邻近点w
if w not in s: # 如果顶点 w 没被发现
q.extend(w)
s.add(w) # 记录w已被发现
""""
Pseudocode[edit]
Input: A graph G and a vertex v of G
Output: All vertices reachable from v labeled as discovered
A recursive implementation of DFS:[5]
1 procedure DFS(G,v):
2 label v as discovered
3 for all edges from v to w in G.adjacentEdges(v) do
4 if vertex w is not labeled as discovered then
5 recursively call DFS(G,w)
A non-recursive implementation of DFS:[6]
1 procedure DFS-iterative(G,v):
2 let S be a stack
3 S.push(v)
4 while S is not empty
5 v = S.pop()
6 if v is not labeled as discovered:
7 label v as discovered
8 for all edges from v to w in G.adjacentEdges(v) do
9 S.push(w)
"""
def DFS(G,v0):
S = []
S.append(v0)
label = set()
while S:
v = S.pop()
if v not in label:
label.add(v)
procedure(v)
for w in G[v]:
S.append(w)
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有