python类函数中的值返回问题

时间:2019-02-02 20:52:48

标签: python python-3.x class recursion depth-first-search

所以我在一个项目上工作,因为我必须找到最佳的可能路径...所以我现在为此使用DFS,问题在于,在递归调用中,它会打印所有可能的结果,最后最好的。但是这些值不会存储在类的“ PathList”变量列表中。

例如:Graph.py是另一个存储图形的文件

import Graph

class G:
    def __init__(self,Graph,vis):
        self.graph = Graph
        self.dist=1000000
        self.PathList= []
        self.stack=[]
        self.visited=vis

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

    def DFScall(self,v,f,dis):

        self.visited[v]=True
        self.stack.append(v)
        dis+=1
        if(v==f):
            if(dis<self.dist):
                self.PathList = self.stack
                self.dist=dis
                #print(self.PathList)
        else :
            for i in self.graph[v]:
                if self.visited[i] == False:
                    #print(self.visited[i])
                    self.DFScall(i,f,dis)

        self.stack.pop()
        self.visited[v]=False


    def DFS(self,v,f):

        self.DFScall(v,f,0)

    def returnPath(self):
        return self.PathList


if __name__== "__main__":
    graph=Graph.returnGraph()
    vis=Graph.returnNodes()
    g = G(graph,vis)
    g.DFS('S1','B8')

    Pathlist = g.returnPath()
    print(Pathlist)

输出为:

['S1', 'F0', 'E1', 'D0', 'C1', 'C2', 'B3', 'B4', 'C5', 'D4', 'D3', 'E2', 'F3', 'G2', 'H3', 'I2', 'J3', 'J4', 'I5', 'H4', 'G5', 'F4', 'E5', 'E6', 'D7', 'C6', 'B7', 'B8']
['S1', 'F0', 'E1', 'D0', 'C1', 'C2', 'B3', 'B4', 'C5', 'D4', 'D3', 'E2', 'F3', 'G2', 'H3', 'H4', 'G5', 'F4', 'E5', 'E6', 'D7', 'C6', 'B7', 'B8']
['S1', 'F0', 'E1', 'D0', 'C1', 'C2', 'B3', 'B4', 'C5', 'D4', 'D3', 'E2', 'F3', 'F4', 'E5', 'E6', 'D7', 'C6', 'B7', 'B8']
['S1', 'F0', 'E1', 'D0', 'C1', 'C2', 'B3', 'B4', 'C5', 'D4', 'E5', 'E6', 'D7', 'C6', 'B7', 'B8']
['S1', 'F0', 'E1', 'D0', 'C1', 'C2', 'B3', 'B4', 'C5', 'C6', 'D7', 'D8', 'C9', 'B8']
['S1', 'F0', 'E1', 'D0', 'C1', 'C2', 'B3', 'B4', 'C5', 'C6', 'B7', 'B8']
['S1', 'F0', 'E1', 'E2', 'D3', 'D4', 'C5', 'C6', 'B7', 'B8']
[]

最后一个列表输出是通过main调用时的输出。....在这种情况下,我该怎么做,是否需要研究。

谢谢

2 个答案:

答案 0 :(得分:0)

问题是您没有创建最佳路径的副本。您应该这样做:

self.PathList = self.stack.copy()

完整示例(为了便于阅读,对PEP8进行了更改):

import Graph


class GraphSearch:
    def __init__(self, graph, nodes):
        self.graph = graph.copy()
        self.nodes = nodes.copy()

        self.visited = None
        self.best_path_list = None
        self.current_path_list = None

    def reset_search(self):
        self.visited = {k: False for k, v in self.nodes.items()}
        self.best_path_list = list()
        self.current_path_list = list()

    def _dfs_call(self, current_node, end_node):

        self.visited[current_node] = True
        self.current_path_list.append(current_node)

        if(current_node == end_node):
            current_distance = len(self.current_path_list)
            best_distance = len(self.best_path_list)
            if(current_distance < best_distance):
                self.best_path_list = self.current_path_list.copy()
        else:
            for adj_node in self.graph[current_node]:
                if not self.visited[adj_node]:
                    self._dfs_call(adj_node, end_node)

        self.current_path_list.pop()
        self.visited[current_node] = False

    def dfs(self, current_node, end_node):
        self.reset_search()
        self._dfs_call(current_node, end_node)


if __name__ == "__main__":
    graph = Graph.return_graph()
    nodes = Graph.return_nodes()
    graph_search = GraphSearch(graph, nodes)
    start_node = 'S1'
    end_node = 'B8'
    graph_search.dfs(start_node, end_node)
    print(graph_search.best_path_list)

我没有测试代码,因为我没有测试数据。

答案 1 :(得分:0)

这是因为您实际上并没有通过这样做来复制sn1

stack

替换
self.PathList = self.stack

self.PathList = self.stack[:]