Как реализовано возвращение в эту функцию ?? Python - PullRequest
0 голосов
/ 21 апреля 2020

Добрый вечер, у меня проблема с кодом, который я пытаюсь реализовать (Attachment), я пытаюсь получить все пути из одной точки в другую (через узлы), алгоритм решает ее и возвращает распечатанный в консоли , но когда я реализую код для возврата набора со всеми возможными комбинациями, он возвращает пустые значения. введите описание изображения здесь

from collections import defaultdict

# This class represents a directed graph
# using adjacency list representation


class Graph:
    '''A recursive function to print all paths from 'u' to 'd'.
        visited[] keeps track of vertices in current path.
        path[] stores actual vertices and path_index is current
        index in path[]'''

    def __init__(self, vertices):
        # No. of vertices
        self.V = vertices

        # default dictionary to store graph
        self.graph = defaultdict(list)
        self.resul = []

    # function to add an edge to graph

    def addEdge(self, u, v):
        self.graph[u].append(v)
        # print(self.graph[u])

    def AllPathsUtil(self, u, d, visited, path):
        # Mark the current node as visited and store in path
        visited[u] = True
        path.append(u)   # [4,1,0]

        # If current vertex is same as destination, then print
        # current path[]
        if u == d:
            g = path
            return g

        else:
            # If current vertex is not destination
            # Recur for all the vertices adjacent to this vertex
            for i in self.graph[u]:
                if visited[i] == False:  # [T,T,F,T,F,F,F]
                    self.AllPathsUtil(i, d, visited, path)

        # Remove current vertex from path[] and mark it as unvisited
        path.pop()
        visited[u] = False

    # Prints all paths from 's' to 'd'

    def AllPaths(self, s, d):
        # Mark all the vertices as not visited
        visited = [False]*(self.V)

        # Create an array to store paths
        path = []

        # Call the recursive helper function to print all paths
        self.AllPathsUtil(s, d, visited, path)



g = Graph(7)
g.addEdge(4, 1)
g.addEdge(1, 4)
g.addEdge(1, 2)  # {1: 4,2}
g.addEdge(2, 1)
g.addEdge(2, 3)
g.addEdge(3, 2)
g.addEdge(2, 5)
g.addEdge(5, 2)
g.addEdge(2, 0)
g.addEdge(0, 2)
g.addEdge(1, 0)
g.addEdge(0, 1)
g.addEdge(0, 6)
g.addEdge(6, 0)

s = 4
d = 6
print("Following are all different paths from %d to %d :" % (s, d))
g.AllPaths(s, d)
...