How to implement depth first search for graph with a non-recursive approach

前端 未结 13 1805
情话喂你
情话喂你 2020-11-28 22:00

I have spent lots of time on this issue. However, I can only find solutions with non-recursive methods for a tree: Non recursive for tree, or a recursive method for the grap

13条回答
  •  眼角桃花
    2020-11-28 22:11

    Acutally, stack is not well able to deal with discover time and finish time, if we want to implement DFS with stack, and want to deal with discover time and finish time, we would need to resort to another recorder stack, my implementation is shown below, have test correct, below is for case-1, case-2 and case-3 graph.

    from collections import defaultdict
    
    class Graph(object):
    
        adj_list = defaultdict(list)
    
        def __init__(self, V):
            self.V = V
    
        def add_edge(self,u,v):
            self.adj_list[u].append(v)
    
        def DFS(self):
            visited = []
            instack = []
            disc = []
            fini = []
            for t in range(self.V):
                visited.append(0)
                disc.append(0)
                fini.append(0)
                instack.append(0)
    
            time = 0
            for u_ in range(self.V):
                if (visited[u_] != 1):
                    stack = []
                    stack_recorder = []
                    stack.append(u_)
                    while stack:
                        u = stack.pop()
                        visited[u] = 1
                        time+=1
                        disc[u] = time
                        print(u)
                        stack_recorder.append(u)
                        flag = 0
                        for v in self.adj_list[u]:
                            if (visited[v] != 1):
                                flag = 1
                                if instack[v]==0:
                                    stack.append(v)
                                instack[v]= 1
    
    
    
                        if flag == 0:
                            time+=1
                            temp = stack_recorder.pop()
                            fini[temp] = time
                    while stack_recorder:
                        temp = stack_recorder.pop()
                        time+=1
                        fini[temp] = time
            print(disc)
            print(fini)
    
    if __name__ == '__main__':
    
        V = 6
        G = Graph(V)
    
    #==============================================================================
    # #for case 1
    #     G.add_edge(0,1)
    #     G.add_edge(0,2)
    #     G.add_edge(1,3)
    #     G.add_edge(2,1)
    #     G.add_edge(3,2) 
    #==============================================================================
    
    #==============================================================================
    # #for case 2
    #     G.add_edge(0,1)
    #     G.add_edge(0,2)
    #     G.add_edge(1,3)
    #     G.add_edge(3,2)  
    #==============================================================================
    
    #for case 3
        G.add_edge(0,3)    
        G.add_edge(0,1)
    
        G.add_edge(1,4)
        G.add_edge(2,4)
        G.add_edge(2,5)
        G.add_edge(3,1)
        G.add_edge(4,3)
        G.add_edge(5,5)    
    
    
        G.DFS()   
    

提交回复
热议问题