2022年5月15日 星期日

dag

 graph = {

    'A':['B','C'],

    'B':['D','E'],

    'C':['E'],

    'D':['F'],

    'E':['F'],

    'F':[]    

    }


visited = set()

visiteds = []


def dfs(graph,visited,node):

    visiteds.append(node)

    if node not in visited:

        visited.add(node)

        print(node,end=' ')

        for neighbor in graph[node]:

            dfs(graph,visited,neighbor)


dfs(graph,visited,'A')

print(visiteds)

dag = set([i for i in visiteds if visiteds.count(i)>1])

print(dag)

沒有留言:

張貼留言