Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.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_Topological Sort - Fatal编程技术网

Python 使用所有给定的边查找路径

Python 使用所有给定的边查找路径,python,topological-sort,Python,Topological Sort,我有一张边的列表。我需要解码从源节点到接收节点的路径。在我的路径中可能有循环,但我应该只使用每个边一次。在我的列表中,我可能不止一次拥有相同的优势,这意味着在我的道路上,我应该不止一次地通过它 假设我的边列表如下所示: [(1, 16), (9, 3), (8, 9), (15, 8), (5, 1), (8, 15), (3, 5)] 因此,我的路径是: 8->15->8->9->3->5->1->16 equivalent to [8,15,8,9

我有一张边的列表。我需要解码从源节点到接收节点的路径。在我的路径中可能有循环,但我应该只使用每个边一次。在我的列表中,我可能不止一次拥有相同的优势,这意味着在我的道路上,我应该不止一次地通过它

假设我的边列表如下所示:

[(1, 16), (9, 3), (8, 9), (15, 8), (5, 1), (8, 15), (3, 5)]
因此,我的路径是:

8->15->8->9->3->5->1->16 equivalent to [8,15,8,9,3,5,1,16]
我知道接收节点和源节点。(在上面的示例中,我知道8是源,16是汇)下面是另一个示例,同一个边有多个用法:

[(1,2),(2,1),(2,3),(1,2)]
路径是:

1->2->1->2->3 equivalent to [1,2,1,2,3]
基本上,这是一种拓扑排序,但在拓扑排序中我们没有循环。我有以下代码,但它不使用循环中的节点

def find_all_paths(graph, start, end):
    path  = []
    paths = []
    queue = [(start, end, path)]
    while queue:
        start, end, path = queue.pop()
        print 'PATH', path

        path = path + [start]
        if start == end:
            paths.append(path)
        for node in set(graph[start]).difference(path):
            queue.append((node, end, path))
return paths

简单地说,您可能需要在边上进行多次传递,以使用所有边组合路径

包含的代码基于以下假设:

  • 存在一种解决办法。也就是说,所有顶点都属于基础图的单个连接组件,并且
  • in_degree
    =
    out_degree
    用于除2个顶点以外的所有顶点或所有顶点。在后一种情况下,其中一个顶点的
    In_degree
    -
    out_degree
    =1,另一个顶点的
    In_degree
    -
    out_degree
    =1
此外,即使在这些条件下,也不一定存在唯一的解决方案来解决利用所有边查找从源到汇的路径的问题。此代码仅查找一个解决方案,而不是所有解决方案。(存在多个解决方案的示例是“雏菊”
[(1,2)、(2,1)、(1,3)、(3,1)、(1,4)、(4,1)、(1,5)、(5,1)]
,其中开始和结束相同。)

其思想是为由边的起始节点索引的路径创建一个包含所有边的字典,然后在将边添加到路径时从字典中删除边。我们不想在第一次遍历时获取路径中的所有边,而是将字典检查多次,直到所有边都被使用。第一个过程创建从源到接收器的路径。后续过程添加到循环中

警告:几乎没有一致性检查或验证。如果开始不是边缘的有效源,则返回的“路径”将断开连接

"""
This is a basic implementatin of Hierholzer's algorithm as applied to the case of a 
directed graph with perhaps multiple identical edges.
"""

import collections

def node_dict(edge_list):
    s_dict = collections.defaultdict(list)
    for edge in edge_list:
        s_dict[edge[0]].append(edge)
    return s_dict

def get_a_path(n_dict,start):
    """
    INPUT:  A dictionary whose keys are nodes 'a' and whose values are lists of 
    allowed directed edges (a,b) from 'a' to 'b', along with a start WHICH IS 
    ASSUMED TO BE IN THE DICTIONARY.

    OUTPUT:  An ordered list of initial nodes and an ordered list of edges 
    representing a path starting at start and ending when there are no other 
    allowed edges that can be traversed from the final node in the last edge.

    NOTE:  This function modifies the dictionary n_dict!
    """

    cur_edge = n_dict[start][0]
    n_dict[start].remove(cur_edge)

    trail = [cur_edge[0]]
    path = [cur_edge]
    cur_node = cur_edge[1]

    while len(n_dict[cur_node]) > 0:
        cur_edge = n_dict[cur_node][0]
        n_dict[cur_node].remove(cur_edge)
        trail.append(cur_edge[0])
        path.append(cur_edge)
        cur_node = cur_edge[1]

    return trail, path


def find_a_path_with_all_edges(edge_list,start):
    """
    INPUT:  A list of edges given by ordered pairs (a,b) and a starting node.

    OUTPUT:  A list of nodes and an associated list of edges representing a path 
    where each edge is represented once and if the input had a valid Eulerian 
    trail starting from start, then the lists give a valid path through all of 
    the edges.

    EXAMPLES:

        In [2]: find_a_path_with_all_edges([(1,2),(2,1),(2,3),(1,2)],1)
        Out[2]: ([1, 2, 1, 2, 3], [(1, 2), (2, 1), (1, 2), (2, 3)])

        In [3]: find_a_path_with_all_edges([(1, 16), (9, 3), (8, 9), (15, 8), (5, 1), (8, 15), (3, 5)],8)
        Out[3]: 
        ([8, 15, 8, 9, 3, 5, 1, 16],
         [(8, 15), (15, 8), (8, 9), (9, 3), (3, 5), (5, 1), (1, 16)])
    """

    s_dict = node_dict(edge_list)
    trail, path_check = get_a_path(s_dict,start)

    #Now add in edges that were missed in the first pass...
    while max([len(s_dict[x]) for x in s_dict]) > 0:
        #Note:  there may be a node in a loop we don't have on trail yet
        add_nodes = [x for x in trail if len(s_dict[x])>0]
        if len(add_nodes) > 0:
            skey = add_nodes[0]
        else:
            print "INVALID EDGE LIST!!!"
            break

        temp,ptemp = get_a_path(s_dict,skey)
        i = trail.index(skey)
        if i == 0:
            trail = temp + trail
            path_check = ptemp + path_check
        else:
            trail = trail[:i] + temp + trail[i:]
            path_check = path_check[:i] + ptemp + path_check[i:]

    #Add the final node to trail.
    trail.append(path_check[-1][1])
    return trail, path_check

你试过什么?你遇到了什么特别的问题,或者你只是想让别人帮你编码?你需要更具体一些有一些图形包和解决方案!有没有试过?e、 python图形!不,我试图做很长时间,我可以生成路径,但他们不考虑我的循环。我试图提取将形成循环的边,然后尝试开发考虑循环的路径,但是我找不到一个工作良好的代码来帮助我找到所有可能的循环!是的,当然,我都试过了。我遇到了一个优化问题,我用Gurobipy解决了这个问题,我正在使用networkx作为我的网络数据库。最后,我想出了这组边,它们将构成我的路径。但我无法使用这些库生成这些边的路径。这是必须的。它列出了一个线性时间算法。