Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用宽度优先搜索PYTHON使用字典检测运行时错误最短路径查找器_Python_Python 3.x_Dictionary_Graph Theory_Breadth First Search - Fatal编程技术网

使用宽度优先搜索PYTHON使用字典检测运行时错误最短路径查找器

使用宽度优先搜索PYTHON使用字典检测运行时错误最短路径查找器,python,python-3.x,dictionary,graph-theory,breadth-first-search,Python,Python 3.x,Dictionary,Graph Theory,Breadth First Search,我有一个在字典中表示的无向图,没有边标签或权重。。。。只是边缘。 我使用搜索来获得最短路径,但总是得到运行时错误。 但是,代码在给定的测试用例上正确运行。 这是我的完整代码: #Using breadth first search def bfs_paths(graph, start, goal): queue = [(start, [start])] while queue: (vertex, path) = queue.pop(0) for

我有一个在字典中表示的无向图,没有边标签或权重。。。。只是边缘。 我使用搜索来获得最短路径,但总是得到运行时错误。 但是,代码在给定的测试用例上正确运行。 这是我的完整代码:

#Using breadth first search
def bfs_paths(graph, start, goal):
    queue = [(start, [start])]
    while queue:
        (vertex, path) = queue.pop(0)

        for next in graph[vertex] - set(path):
            if next == goal:
            yield path + [next]
        else:
            queue.append((next, path + [next]))
def shortest_path(graph, start, goal):
    try:
        return next(bfs_paths(graph, start, goal))
    except StopIteration:
        return None

counter = int(input())
for i in range(counter) :
    N , M = input().strip().split()
    N , M = [int(N),int(M)]
    conn = input().strip().split()
# initialize the graph with dictionary .
    graph ={x : set() for x in range(N) if conn[x-1] != '1'}
    for cell_conn in range(M) :
        cell_0 ,cell_1= input().strip().split()
        cell_0 ,cell_1=[int(cell_0) ,int(cell_1)]
    if conn[cell_0-1] != '1'  and conn[cell_1-1] != '1' :
        graph[cell_0].add(cell_1)
    path_list = ""
    for cell in range(1,N):
        if conn[cell-1] != '1' :
             path_conn_number = shortest_path(graph, 0, cell )
           if  path_conn_number != None :
               path_list += str(len(path_conn_number)-1) + ' '
           else :
               path_list += '-1' + ' '
        else :
           path_list += '-1' + ' '
    print(path_list)
下面是问题的细节。

注:比赛已经结束,但我想知道是什么问题?! 什么是导致此错误的测试用例