简体   繁体   English

在python中使用BFS的Unweighted-Single-Source-Shortest-Path

[英]Unweighted-Single-Source-Shortest-Path using BFS in python

Im new at python. 我是python的新手。 I try to get the Unweighted-Single-Source-Shortest-Path using BFS. 我尝试使用BFS获得Unweighted-Single-Source-Shortest-Path。

from queue import *


def ussp(graph, s):
    len_graph = len(graph)
    prev = [[]*len_graph for i in range(len_graph)]
    visited = [False for i in range(len_graph)]
    queue2 = Queue
    dist = [[float('inf')] for i in range(len_graph)]
    queue2.put_nowait(graph[s], 0)  # starting with s
    visited[s] = True
    dist[s] = 0

    # modified BFS alg.
    while queue2.empty() == False:
        h = queue2.get_nowait()
        for i in len(graph[h]):
            if visited[graph[h][i]] == False:
                visited[graph[h][i]] = True
                dist[graph[h][i]] = dist[h] + 1
                prev[graph[h][i]] = h
                queue2.put_nowait(graph[h][i], 0)
    print(dist)


graph2 = {1: [2, 3, 5], 2: [4, 6, 1], 3: [5, 1], 4: [6], 5: [2], 6: [1, 7], 7: [2]}
ussp(graph2, 1)

Thats what I got for now. 那就是我现在得到的。 I was pretty sure it should work, but it does not at all. 我非常确定它应该可以工作,但是根本不起作用。 It doesn't even get compiled. 它甚至没有被编译。 Im pretty new with lists, arrays and queues in python, too. 我在python中使用列表,数组和队列也很新。 Would be kind if you could help me. 如果您能帮助我,会很高兴。 Thanks in advance 提前致谢

First, I added in a destination parameter to your function signature. 首先,我在函数签名中添加了一个目标参数。 Assuming you wanted to find the shortest path from node 1 to node 7, the program below works. 假设您想找到从节点1到节点7的最短路径,下面的程序将起作用。 I also added some python boilerplate since you said you are new to python. 我还添加了一些python样板,因为您说过您是python的新手。

import sys
from queue import Queue as Q

def ussp(graph, s, d):
    len_graph = len(graph)

    prev = [ -1 for i in range(len_graph)]
    visited = [False for i in range(len_graph)]
    q = Q()
    dist = [sys.maxsize for i in range(len_graph)]
    q.put(s, False)
    visited[s-1] = True
    dist[s-1] = 0

    # modified BFS alg.
    while q.empty() == False:
        h = q.get_nowait()
        for i in range(len(graph[h])):
            if visited[graph[h][i]-1] == False:
                visited[graph[h][i]-1] = True
                dist[graph[h][i]-1] = dist[h-1] + 1
                prev[graph[h][i]-1] = h
                q.put_nowait(graph[h][i])

    path = []
    crawl = d # destination
    path.append(crawl)
    while (prev[crawl-1] != -1):
        path.append(prev[crawl-1])
        crawl = prev[crawl-1]

    print(list(reversed(path)))


def main():
    graph2 = {1: [2, 3, 5], 2: [4, 6, 1], 3: [5, 1], 4: [6], 5: [2], 6: [1, 7], 7: [2]}
    ussp(graph2, 1, 7)

if __name__ == '__main__':
    main()

This line is the cause of your error: 此行是导致您出错的原因:

queue2 = Queue

You're setting queue2 equal to the Queue class, instead of an instance of the Queue class. 你设置queue2等于Queue ,而不是一个实例类, Queue类。

Change that line to this: 将该行更改为此:

queue2 = Queue()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM