{"id":27102,"date":"2018-01-03T21:24:07","date_gmt":"2018-01-03T15:54:07","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27102"},"modified":"2018-01-03T21:24:07","modified_gmt":"2018-01-03T15:54:07","slug":"java-programming-bridges-graph","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/java-programming-bridges-graph\/","title":{"rendered":"JAVA programming &#8211; Bridges in a graph"},"content":{"rendered":"<p>An edge in an undirected connected graph is a bridge iff removing it disconnects the graph. <span id=\"more-118009\"><\/span>For a disconnected undirected graph, definition is similar, a bridge is an edge removing which increases number of connected components.<br \/>\nLike <a href=\"http:\/\/www.geeksforgeeks.org\/articulation-points-or-cut-vertices-in-a-graph\/\" target=\"_blank\" rel=\"noopener noreferrer\">Articulation Points<\/a>,bridges represent vulnerabilities in a connected network and are useful for designing reliable networks. For example, in a wired computer network, an articulation point indicates the critical computers and a bridge indicates the critical wires or connections.<\/p>\n<p>Following are some example graphs with bridges highlighted with red color.<\/p>\n<p><strong>How to find all bridges in a given graph?<\/strong><br \/>\nA simple approach is to one by one remove all edges and see if removal of a edge causes disconnected graph. Following are steps of simple approach for connected graph.<\/p>\n<p>1) For every edge (u, v), do following<br \/>\n\u2026..a) Remove (u, v) from graph<br \/>\n..\u2026b) See if the graph remains connected (We can either use BFS or DFS)<br \/>\n\u2026..c) Add (u, v) back to the graph.<\/p>\n<p>Time complexity of above method is O(E*(V+E)) for a graph represented using adjacency list. Can we do better?<\/p>\n[ad type=&#8221;banner&#8221;]\n<p><strong>A O(V+E) algorithm to find all Bridges<\/strong><br \/>\nThe idea is similar to <a href=\"http:\/\/www.geeksforgeeks.org\/articulation-points-or-cut-vertices-in-a-graph\/\" target=\"_blank\" rel=\"noopener noreferrer\">O(V+E) algorithm for Articulation Points<\/a>. We do DFS traversal of the given graph. In DFS tree an edge (u, v) (u is parent of v in DFS tree) is bridge if there does not exit any other alternative to reach u or an ancestor of u from subtree rooted with v. As discussed in the <a href=\"http:\/\/www.geeksforgeeks.org\/articulation-points-or-cut-vertices-in-a-graph\/\" target=\"_blank\" rel=\"noopener noreferrer\">previous post<\/a>, the value low[v] indicates earliest visited vertex reachable from subtree rooted with v. <em>The condition for an edge (u, v) to be a bridge is, \u201clow[v] &gt; disc[u]\u201d<\/em>.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">\/\/ A Java program to find bridges in a given undirected graph<br\/>import java.io.*;<br\/>import java.util.*;<br\/>import java.util.LinkedList;<br\/> <br\/>\/\/ This class represents a undirected graph using adjacency list<br\/>\/\/ representation<br\/>class Graph<br\/>{<br\/>    private int V;   \/\/ No. of vertices<br\/> <br\/>    \/\/ Array  of lists for Adjacency List Representation<br\/>    private LinkedList&lt;Integer&gt; adj[];<br\/>    int time = 0;<br\/>    static final int NIL = -1;<br\/> <br\/>    \/\/ Constructor<br\/>    Graph(int v)<br\/>    {<br\/>        V = v;<br\/>        adj = new LinkedList[v];<br\/>        for (int i=0; i&lt;v; ++i)<br\/>            adj[i] = new LinkedList();<br\/>    }<br\/> <br\/>    \/\/ Function to add an edge into the graph<br\/>    void addEdge(int v, int w)<br\/>    {<br\/>        adj[v].add(w);  \/\/ Add w to v&#039;s list.<br\/>        adj[w].add(v);  \/\/Add v to w&#039;s list<br\/>    }<br\/> <br\/>    \/\/ A recursive function that finds and prints bridges<br\/>    \/\/ using DFS traversal<br\/>    \/\/ u --&gt; The vertex to be visited next<br\/>    \/\/ visited[] --&gt; keeps tract of visited vertices<br\/>    \/\/ disc[] --&gt; Stores discovery times of visited vertices<br\/>    \/\/ parent[] --&gt; Stores parent vertices in DFS tree<br\/>    void bridgeUtil(int u, boolean visited[], int disc[],<br\/>                    int low[], int parent[])<br\/>    {<br\/> <br\/>        \/\/ Count of children in DFS Tree<br\/>        int children = 0;<br\/> <br\/>        \/\/ Mark the current node as visited<br\/>        visited[u] = true;<br\/> <br\/>        \/\/ Initialize discovery time and low value<br\/>        disc[u] = low[u] = ++time;<br\/> <br\/>        \/\/ Go through all vertices aadjacent to this<br\/>        Iterator&lt;Integer&gt; i = adj[u].iterator();<br\/>        while (i.hasNext())<br\/>        {<br\/>            int v = i.next();  \/\/ v is current adjacent of u<br\/> <br\/>            \/\/ If v is not visited yet, then make it a child<br\/>            \/\/ of u in DFS tree and recur for it.<br\/>            \/\/ If v is not visited yet, then recur for it<br\/>            if (!visited[v])<br\/>            {<br\/>                parent[v] = u;<br\/>                bridgeUtil(v, visited, disc, low, parent);<br\/> <br\/>                \/\/ Check if the subtree rooted with v has a<br\/>                \/\/ connection to one of the ancestors of u<br\/>                low[u]  = Math.min(low[u], low[v]);<br\/> <br\/>                \/\/ If the lowest vertex reachable from subtree<br\/>                \/\/ under v is below u in DFS tree, then u-v is<br\/>                \/\/ a bridge<br\/>                if (low[v] &gt; disc[u])<br\/>                    System.out.println(u+&quot; &quot;+v);<br\/>            }<br\/> <br\/>            \/\/ Update low value of u for parent function calls.<br\/>            else if (v != parent[u])<br\/>                low[u]  = Math.min(low[u], disc[v]);<br\/>        }<br\/>    }<br\/> <br\/> <br\/>    \/\/ DFS based function to find all bridges. It uses recursive<br\/>    \/\/ function bridgeUtil()<br\/>    void bridge()<br\/>    {<br\/>        \/\/ Mark all the vertices as not visited<br\/>        boolean visited[] = new boolean[V];<br\/>        int disc[] = new int[V];<br\/>        int low[] = new int[V];<br\/>        int parent[] = new int[V];<br\/> <br\/> <br\/>        \/\/ Initialize parent and visited, and ap(articulation point)<br\/>        \/\/ arrays<br\/>        for (int i = 0; i &lt; V; i++)<br\/>        {<br\/>            parent[i] = NIL;<br\/>            visited[i] = false;<br\/>        }<br\/> <br\/>        \/\/ Call the recursive helper function to find Bridges<br\/>        \/\/ in DFS tree rooted with vertex &#039;i&#039;<br\/>        for (int i = 0; i &lt; V; i++)<br\/>            if (visited[i] == false)<br\/>                bridgeUtil(i, visited, disc, low, parent);<br\/>    }<br\/> <br\/>    public static void main(String args[])<br\/>    {<br\/>        \/\/ Create graphs given in above diagrams<br\/>        System.out.println(&quot;Bridges in first graph &quot;);<br\/>        Graph g1 = new Graph(5);<br\/>        g1.addEdge(1, 0);<br\/>        g1.addEdge(0, 2);<br\/>        g1.addEdge(2, 1);<br\/>        g1.addEdge(0, 3);<br\/>        g1.addEdge(3, 4);<br\/>        g1.bridge();<br\/>        System.out.println();<br\/> <br\/>        System.out.println(&quot;Bridges in Second graph&quot;);<br\/>        Graph g2 = new Graph(4);<br\/>        g2.addEdge(0, 1);<br\/>        g2.addEdge(1, 2);<br\/>        g2.addEdge(2, 3);<br\/>        g2.bridge();<br\/>        System.out.println();<br\/> <br\/>        System.out.println(&quot;Bridges in Third graph &quot;);<br\/>        Graph g3 = new Graph(7);<br\/>        g3.addEdge(0, 1);<br\/>        g3.addEdge(1, 2);<br\/>        g3.addEdge(2, 0);<br\/>        g3.addEdge(1, 3);<br\/>        g3.addEdge(1, 4);<br\/>        g3.addEdge(1, 6);<br\/>        g3.addEdge(3, 5);<br\/>        g3.addEdge(4, 5);<br\/>        g3.bridge();<br\/>    }<br\/>}<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\nOutput:<\/p>\n<pre>Bridges in first graph\r\n3 4\r\n0 3\r\n\r\nBridges in second graph\r\n2 3\r\n1 2\r\n0 1\r\n\r\nBridges in third graph\r\n1 6<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>JAVA programming &#8211; Bridges in a graph &#8211; An edge in an undirected connected graph is a bridge iff removing it disconnects the graph.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[78385,73906,2139],"tags":[83836,83837,83830,83833,83831,83834,83835,83832],"class_list":["post-27102","post","type-post","status-publish","format-standard","hentry","category-connectivity","category-graph-algorithms","category-java","tag-articulation-point-program-in-c","tag-articulation-point-using-dfs-algorithm","tag-articulation-points-or-cut-vertices-in-a-graph","tag-articulation-points-java","tag-consider-the-following-graph-l-and-find-the-bridges-if-any","tag-cut-vertex-and-bridges","tag-find-all-nodes-in-a-graph-that-remove-any-1-of-them-would-cause-the-graph-to-disconnect","tag-show-how-to-compute-all-the-bridges-of-g-in-oe-time"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27102","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=27102"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27102\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}