【问题标题】:How to store results of recursive function instead of printing如何存储递归函数的结果而不是打印
【发布时间】:2021-03-18 23:35:39
【问题描述】:

下面的代码打印了图中的 Hamilton 路径(Hamiltonian 路径是只访问图的每个顶点一次的路径)。由于函数printAllHamiltonianPaths(g, start, visited, path, N) 的输出是非类型的,我无法存储它。我想将函数的输出存储为列表,然后在其他地方使用它。我不确定应该对功能进行哪些更改。

代码如下:

class Graph:
 
    # Constructor
    def __init__(self, edges, N):
 
        # A List of Lists to represent an adjacency list
        self.adjList = [[] for _ in range(N)]
 
        # add edges to the undirected graph
        for (src, dest) in edges:
            self.adjList[src].append(dest)
            self.adjList[dest].append(src)
 
 
def printAllHamiltonianPaths(g, v, visited, path, N):
 
    # if all the vertices are visited, then hamiltonian path exists
    if len(path) == N:
        # print hamiltonian path
        print(path)
        return 
 
    # Check if every edge starting from vertex v leads to a solution or not
    for w in g.adjList[v]:
 
        # process only unvisited vertices as hamiltonian
        # path visits each vertex exactly once
        if not visited[w]:
            visited[w] = True
            path.append(w)
 
            # check if adding vertex w to the path leads to solution or not
            printAllHamiltonianPaths(g, w, visited, path, N)
 
            # Backtrack
            visited[w] = False
            path.pop()
if __name__ == '__main__':
 
    # List of graph edges as per above diagram
    edges = [(0, 2), (0, 3), (1, 3), (1, 4), (2, 4), (3, 4)]
    
    # Set number of vertices in the graph
    N = 5
 
    # create a graph from edges
    g = Graph(edges, N)
 
    # starting node
    start = 0
 
    # add starting node to the path
    path = [start]
 
    # mark start node as visited
    visited = [False] * N
    visited[start] = True
 
    a=printAllHamiltonianPaths(g, start, visited, path, N)         

【问题讨论】:

  • 在你的 printAll... 函数最后添加return path 行代码?
  • 我做到了。 'if len(path) == N: return pass' 但仍然输出非类型。

标签: python list function recursion graph


【解决方案1】:

你可以把你的函数变成一个生成器(如果实际结果很大,这也很方便):

def hamiltonianPaths(g, v, visited, path, N):
    # if all the vertices are visited, then hamiltonian path exists
    if len(path) == N:
        # print hamiltonian path
        yield path.copy()
        return 

    # Check if every edge starting from vertex v leads to a solution or not
    for w in g.adjList[v]:
 
        # process only unvisited vertices as hamiltonian
        # path visits each vertex exactly once
        if not visited[w]:
            visited[w] = True
            path.append(w)
 
            # check if adding vertex w to the path leads to solution or not
            yield from hamiltonianPaths(g, w, visited, path, N)
 
            # Backtrack
            visited[w] = False
            path.pop()

用法:a = list(hamiltonianPaths(g, start, visited, path, N))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-17
    • 2021-12-02
    • 2014-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多