简体   繁体   English

使用 Tarjan 算法在有向图中查找桥的特例

[英]The special case for using Tarjan's algorithm to find bridges in directed graphs

I am trying to get better understanding of Tarjan's algorithm for finding SCC, articulation points and bridges.我试图更好地理解 Tarjan 用于查找 SCC、关节点和桥梁的算法。 I am considering a special case where the graph contains only 2 nodes with edges 0->1 and 1->0.我正在考虑一种特殊情况,其中图形仅包含 2 个边为 0->1 和 1->0 的节点。 The following code will output [0,1] as a bridge.下面的代码将output[0,1]作为桥梁。

class Solution(object):
    def criticalConnections(self, n, connections):
        """
        :type n: int
        :type connections: List[List[int]]
        :rtype: List[List[int]]
        """
        g = defaultdict(set)
        pre = [-1]*n
        low = [-1]*n
        cnt = [0]
        for c in connections:
            g[c[0]].add(c[1]) # undirected graph, connect 
            g[c[1]].add(c[0]) # in both directions
        ans = []
        def dfs(edge):            
            v, w = edge
            pre[w] = cnt[0]
            low[w] = pre[w]
            cnt[0] += 1
            for i in g[w]:
                if i == v: continue # we don't want to go back through the same path.
                                    # if we go back is because we found another way back                   
                if pre[i] == -1:
                    dfs((w,i))          
                    # low[i] > pre[w] indicates no back edge to
                    # w's ancesters; otherwise, low[i] will be 
                    # < pre[w]+1 since back edge makes low[i] smaller           
                    if low[i] > pre[w]: 
                    #print(low[i], pre[w]+1, (w,i))                       
                        ans.append([w,i])                
                    low[w] = min(low[w], low[i]) # low[i] might be an ancestor of w
                else: # if i was already discovered means that we found an ancestor
                    low[w] = min(low[w], pre[i]) # finds the ancestor with the least 
                                                 # discovery time
               
                
        dfs((-1,0))
        
        return ans
print(Solution().criticalConnections(2, [[0,1],[1,0]]))

However, from many discussions online, after removing node 1, node 0 can still be considered as connected (to itself) which means edge 0->1 is not a bridge.然而,从网上的许多讨论来看,在删除节点 1 之后,节点 0 仍然可以被认为是连接的(到它自己),这意味着边 0->1 不是桥。 Am I missing something here?我在这里错过了什么吗? Or Tarjan's algorithm is not suitable for this kind of degenerate graph with 2 nodes?或者 Tarjan 的算法不适合这种有 2 个节点的退化图?

A bridge in a directed graph is an edge whose deletion increases the graph's number of strongly connected components, and the number connected components when the graph is undirected.有向图中的桥是一条边,删除它会增加图中强连通分量的数量,以及当图是无向时连通分量的数量。 So when you remove any edge in your graph then the number of strongly connected components increases so the output of this code is correct in this case.因此,当您删除图中的任何边时,强连通分量的数量就会增加,因此此代码的 output 在这种情况下是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM