对于给定的图G =(V,E)和边e∈E,设计一个O(n + m)时算法,如果存在,则查找包含e的最短周期

时间:2017-02-14 16:25:05

标签: algorithm graph

尝试理解图表,并且非常努力。我知道如何找到最短的路径,但不确定如何找到最短的周期并仍然在O(n + m)时间内完成?

1 个答案:

答案 0 :(得分:0)

  

包含e的最短周期

BFS非常适合。一个循环将成为目标。时间复杂度是一样的。

你想要这样的东西(从维基百科编辑):

Cycle-With-Breadth-First-Search(Graph g, Edge e):

    remove e from E
    root is b where e = (a,b)
    create empty set S
    create empty queue Q      

    root.parent = a
    Q.enqueueEdges(root)                      

    while Q is not empty:

        if current = a
            return current

        current = Q.dequeue()
        for each node n that is adjacent to current:
            if n is not in S:
                add n to S
                n.parent = current
                Q.enqueue(n)

有关周期和BFS的更多信息,请阅读此链接 https://stackoverflow.com/a/4464388/6782134