编程语言
首页 > 编程语言> > 广度优先搜索算法

广度优先搜索算法

作者:互联网

像我先前遇到的先前问题一样,我正在尝试创建一个广度优先搜索算法,该算法采用图形并输出顶点访问顺序.它以一个邻接矩阵(代表该图)作为输入,这就是我到目前为止所拥有的.

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中的队列,因此可以提供任何帮助.

解决方法:

您的代码中有很多问题-

>首先,您永远不会在创建的队列中放入任何内容,因此它始终为空,您需要在while循环之前将v放入队列中,这是起点.
>其次,在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}

标签:breadth-first-search,queue,python,algorithm,recursion
来源: https://codeday.me/bug/20191119/2038317.html