{"id":27063,"date":"2018-01-02T21:58:23","date_gmt":"2018-01-02T16:28:23","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27063"},"modified":"2018-01-02T21:58:23","modified_gmt":"2018-01-02T16:28:23","slug":"c-programming-bridges-graph","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-programming-bridges-graph\/","title":{"rendered":"C++ programming 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<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[ad type=&#8221;banner&#8221;]\n<p>C++ programming :<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\">\/\/ A C++ program to find bridges in a given undirected graph<br\/>#include&lt;iostream&gt;<br\/>#include &lt;list&gt;<br\/>#define NIL -1<br\/>using namespace std;<br\/> <br\/>\/\/ A class that represents an undirected graph<br\/>class Graph<br\/>{<br\/>    int V;    \/\/ No. of vertices<br\/>    list&lt;int&gt; *adj;    \/\/ A dynamic array of adjacency lists<br\/>    void bridgeUtil(int v, bool visited[], int disc[], int low[],<br\/>                    int parent[]);<br\/>public:<br\/>    Graph(int V);   \/\/ Constructor<br\/>    void addEdge(int v, int w);   \/\/ to add an edge to graph<br\/>    void bridge();    \/\/ prints all bridges<br\/>};<br\/> <br\/>Graph::Graph(int V)<br\/>{<br\/>    this-&gt;V = V;<br\/>    adj = new list&lt;int&gt;[V];<br\/>}<br\/> <br\/>void Graph::addEdge(int v, int w)<br\/>{<br\/>    adj[v].push_back(w);<br\/>    adj[w].push_back(v);  \/\/ Note: the graph is undirected<br\/>}<br\/> <br\/>\/\/ A recursive function that finds and prints bridges using<br\/>\/\/ 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 Graph::bridgeUtil(int u, bool visited[], int disc[], <br\/>                                  int low[], int parent[])<br\/>{<br\/>    \/\/ A static variable is used for simplicity, we can <br\/>    \/\/ avoid use of static variable by passing a pointer.<br\/>    static int time = 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\/>    list&lt;int&gt;::iterator i;<br\/>    for (i = adj[u].begin(); i != adj[u].end(); ++i)<br\/>    {<br\/>        int v = *i;  \/\/ v is current adjacent of u<br\/> <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]  = 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 <br\/>            \/\/ is a bridge<br\/>            if (low[v] &gt; disc[u])<br\/>              cout &lt;&lt; u &lt;&lt;&quot; &quot; &lt;&lt; v &lt;&lt; endl;<br\/>        }<br\/> <br\/>        \/\/ Update low value of u for parent function calls.<br\/>        else if (v != parent[u])<br\/>            low[u]  = min(low[u], disc[v]);<br\/>    }<br\/>}<br\/> <br\/>\/\/ DFS based function to find all bridges. It uses recursive <br\/>\/\/ function bridgeUtil()<br\/>void Graph::bridge()<br\/>{<br\/>    \/\/ Mark all the vertices as not visited<br\/>    bool *visited = new bool[V];<br\/>    int *disc = new int[V];<br\/>    int *low = new int[V];<br\/>    int *parent = new int[V];<br\/> <br\/>    \/\/ Initialize parent and visited 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\/>\/\/ Driver program to test above function<br\/>int main()<br\/>{<br\/>    \/\/ Create graphs given in above diagrams<br\/>    cout &lt;&lt; &quot;\\nBridges in first graph \\n&quot;;<br\/>    Graph g1(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\/> <br\/>    cout &lt;&lt; &quot;\\nBridges in second graph \\n&quot;;<br\/>    Graph g2(4);<br\/>    g2.addEdge(0, 1);<br\/>    g2.addEdge(1, 2);<br\/>    g2.addEdge(2, 3);<br\/>    g2.bridge();<br\/> <br\/>    cout &lt;&lt; &quot;\\nBridges in third graph \\n&quot;;<br\/>    Graph g3(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\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p>Output:<\/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[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>C++ programming Bridges in a graph,An edge in an un directed connected graph is a bridge if 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],"tags":[79021,80897,83824,83825,83829,83819,83820,83823,83828,83822,80896,83827,83826,83821],"class_list":["post-27063","post","type-post","status-publish","format-standard","hentry","category-connectivity","category-graph-algorithms","tag-articulation-points-and-bridges","tag-articulation-points-in-a-graph","tag-bridge-design-pattern-c","tag-bridge-design-pattern-sourcemaking","tag-bridge-dfs","tag-bridge-pattern-c-example","tag-bridge-pattern-real-world-example","tag-c-patterns","tag-check-if-edge-is-bridge","tag-composite-pattern-c","tag-cut-vertex-graph-theory","tag-find-bridges-in-directed-graph","tag-source-making-bridge","tag-when-to-use-bridge-pattern"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27063","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=27063"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27063\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27063"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27063"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27063"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}