- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
就像我之前遇到的一个问题一样,我正在尝试创建一个广度优先搜索算法,该算法采用图形并输出顶点访问顺序。它需要一个邻接矩阵(表示图形)作为输入,这是我目前所拥有的。
import sys
import Queue
# Input has to be adjacency matrix or list
graphAL2 = {0 : [1,2,3],
1 : [0,3,4],
2 : [0,4,5],
3 : [0,1,5],
4 : [1,2],
5 : [2,3] }
# NEED TO FIX:
# - Final graphAL2v print is only displaying key values as 1, not iterating
# through graph and visiting each vertex
def main():
count = 0
graphAL2v = {}
for key, value in graphAL2.items():
graphAL2v[key] = 0
print(graphAL2v)
for key in graphAL2v: # each vertex v in V
if graphAL2v[key] == 0: # is marked with 0
bfs(key, count, graphAL2, graphAL2v)
print(graphAL2v)
def bfs(v, count, graphal, graphv):
count = count + 1
print('Visiting', v)
# Mark v with count and initialize queue with v
graphv[v] = count
visited = Queue.Queue()
while not visited.empty(): #queue not empty:
print('queue is not empty')
for element in graphal[v]: # each vertex w in V adjacent to front vertex
if element == 0:
count = count + 1
# mark w with count
graphal[v] = count
visited.put()
visited.get()
if __name__ == '__main__':
sys.exit(main())
我遇到的问题是我的输出
{0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0}
('Visiting', 0)
('Visiting', 1)
('Visiting', 2)
('Visiting', 3)
('Visiting', 4)
('Visiting', 5)
{0: 1, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1}
将列表中所有顶点的访问顺序显示为 1,而当它遍历“图形”时应该将每个顶点的访问顺序显示为不同的数字。我相信这个错误源于 bfs() 函数的 while 循环。有关尝试修复代码以便获得所需输出的任何建议吗?我对 Python 中的队列也不太熟悉,因此不胜感激。
最佳答案
您的代码中有很多问题 -
首先,您永远不会将任何东西放入您创建的队列中,因此它始终是空的,您需要将 v
放入队列中,然后放在 while
循环,即起点。
其次,在 for
循环中,您正在检查 element == 0
,这是错误的,您需要检查 graphv[element ] == 0
,即元素是否已经被访问过。
第三,在for
循环中,你需要设置graphv[element] = count
,这表示你已经激活了element
.
您没有使用 visited.put()
将任何内容放入队列中,您需要将要放入队列中的元素作为参数传递。
从Queue取回元素时,需要将其赋值回v
,否则v
永远不会改变,v
表示正在迭代的当前元素。
示例代码-
import sys
import Queue
# Input has to be adjacency matrix or list
graphAL2 = {0 : [1,2,3],
1 : [0,3,4],
2 : [0,4,5],
3 : [0,1,5],
4 : [1,2],
5 : [2,3] }
# NEED TO FIX:
# - Final graphAL2v print is only displaying key values as 1, not iterating
# through graph and visiting each vertex
def main():
count = 0
graphAL2v = {}
for key, value in graphAL2.items():
graphAL2v[key] = 0
print(graphAL2v)
for key in graphAL2v: # each vertex v in V
if graphAL2v[key] == 0: # is marked with 0
bfs(key, count, graphAL2, graphAL2v)
print(graphAL2v)
def bfs(v, count, graphal, graphv):
count = count + 1
print('Visiting', v)
# Mark v with count and initialize queue with v
graphv[v] = count
visited = Queue.Queue()
visited.put(v)
while not visited.empty(): #queue not empty:
print('queue is not empty')
for element in graphal[v]: # each vertex w in V adjacent to front vertex
if graphv[element] == 0:
count = count + 1
# mark w with count
graphv[element] = count
visited.put(element)
v = visited.get()
return count
if __name__ == '__main__':
sys.exit(main())
演示(经过上述更改后)-
{0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0}
Visiting 0
queue is not empty
queue is not empty
queue is not empty
queue is not empty
queue is not empty
queue is not empty
{0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6}
关于python - 广度优先搜索算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32624274/
所以我有一个有向图,我添加了顶点和边。该图表示机场和它们之间的航类。当我运行广度优先或深度优先搜索以找到两个机场之间的路径时,我第一次得到了正确的答案,但是当我第二次使用完全相同的机场运行它时,它找不
我是一名优秀的程序员,十分优秀!