3

Given an undirected Graph, how can I find all the bridges? I've only found Tarjan's algorithm which seems rather complicated.

It seems there should be multiple linear time solutions, but I can't find anything.

Jakub Arnold
  • 79,807
  • 86
  • 218
  • 314
  • Tarjan's algorithm is for finding strongly connected component (SCC) in a *directed* graph, not sure how you want to apply it here (not sure it is not possible though) – amit Mar 07 '15 at 16:53
  • http://www.geeksforgeeks.org/bridge-in-a-graph/ – Dawid Drozd Jan 28 '16 at 21:21

1 Answers1

12

Tarjan's algorithm was the first bridge finding algorithm in an undirected graph that ran in linear time. However a simpler algorithm exists and you can have a look at its implementation here.

    private int bridges;      // number of bridges
    private int cnt;          // counter
    private int[] pre;        // pre[v] = order in which dfs examines v
    private int[] low;        // low[v] = lowest preorder of any vertex connected to v

    public Bridge(Graph G) {
        low = new int[G.V()];
        pre = new int[G.V()];
        for (int v = 0; v < G.V(); v++) low[v] = -1;
        for (int v = 0; v < G.V(); v++) pre[v] = -1;

        for (int v = 0; v < G.V(); v++)
            if (pre[v] == -1)
                dfs(G, v, v);
    }

    public int components() { return bridges + 1; }

    private void dfs(Graph G, int u, int v) {
        pre[v] = cnt++;
        low[v] = pre[v];
        for (int w : G.adj(v)) {
            if (pre[w] == -1) {
                dfs(G, v, w);
                low[v] = Math.min(low[v], low[w]);
                if (low[w] == pre[w]) {
                    StdOut.println(v + "-" + w + " is a bridge");
                    bridges++;
                }
            }

            // update low number - ignore reverse of edge leading to v
            else if (w != u)
                low[v] = Math.min(low[v], pre[w]);
        }
    }

The algorithm does the job by maintaining 2 arrays pre and low. pre holds the pre-order traversal numbering for the nodes. So pre[0] = 2 means that vertex 0 was discovered in the 3rd dfs call. And low[u] holds the smallest pre-order number of any vertex that is reachable from u.

The algorithm detects a bridge whenever for an edge u--v, where u comes first in the preorder numbering, low[v]==pre[v]. This is because if we remove the edge between u--v, v can't reach any vertex that comes before u. Hence removing the edge would split the graph into 2 separate graphs.

For a more elaborate explanation you can also have a look at this answer .

Community
  • 1
  • 1
Nikunj Banka
  • 10,091
  • 15
  • 68
  • 105
  • 1
    Hi Nikunj, I just wonder why you would call this Tarjan's algorithm? Though the algorithm you posted shares the same `low` and `pre` arrays as the original Tarjan, Tarjan is used to find SCC in directed graph. – Gang Fang Dec 03 '19 at 04:54
  • 1
    hey, I was just wondering does this Tarjan's bridge finding algorithm have time complexity of O(V)? Because, although I cant find the algorithim elsewere without being told I need to be in russia, as far as I understand the algorithim is ment to be O(V) or some just say linear time... But this doesnt look like it is.. SO is this not the real algorithm? – user3038404 Feb 03 '20 at 15:48