{"id":26885,"date":"2017-12-26T20:36:02","date_gmt":"2017-12-26T15:06:02","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26885"},"modified":"2017-12-26T20:36:02","modified_gmt":"2017-12-26T15:06:02","slug":"c-programming-biconnected-graph","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-programming-biconnected-graph\/","title":{"rendered":"C++ programming Biconnected graph"},"content":{"rendered":"<p>An undirected graph is called Biconnected if there are two vertex-disjoint paths between any two vertices. <span id=\"more-118320\"><\/span>In a Biconnected Graph, there is a simple cycle through any two vertices.<br \/>\nBy convention, two nodes connected by an edge form a biconnected graph, but this does not verify the above properties. For a graph with more than two vertices, the above properties must be there for it to be Biconnected.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p>Following are some examples.<\/p>\n<p><strong>How to find if a given graph is Biconnected or not?<\/strong><br \/>\n<em>A connected graph is Biconnected if it is connected and doesn\u2019t have any <a href=\"http:\/\/www.geeksforgeeks.org\/articulation-points-or-cut-vertices-in-a-graph\/\" target=\"_blank\" rel=\"noopener\">Articulation Point<\/a><\/em>. We mainly need to check two things in a graph.<br \/>\n1) The graph is connected.<br \/>\n2) There is not articulation point in graph.<\/p>\n<p>We start from any vertex and do DFS traversal. In DFS traversal, we check if there is any articulation point. If we don\u2019t find any articulation point, then the graph is Biconnected. Finally, we need to check whether all vertices were reachable in DFS or not. If all vertices were not reachable, then the graph is not even connected.<br \/>\nFollowing is C++ implementation of above approach.<\/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 if a given undirected graph is<br\/>\/\/ biconnected<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\/>    bool isBCUtil(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\/>    bool isBC();    \/\/ returns true if graph is Biconnected<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 returns true if there is an articulation<br\/>\/\/ point in given graph, otherwise returns false.<br\/>\/\/ This function is almost same as isAPUtil() here ( http:\/\/goo.gl\/Me9Fw )<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\/>bool Graph::isBCUtil(int u, bool visited[], int disc[],int low[],int parent[])<br\/>{<br\/>    \/\/ A static variable is used for simplicity, we can avoid use of static<br\/>    \/\/ variable by passing a pointer.<br\/>    static int time = 0;<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\/>    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 make it a child of u<br\/>        \/\/ in DFS tree and recur for it<br\/>        if (!visited[v])<br\/>        {<br\/>            children++;<br\/>            parent[v] = u;<br\/> <br\/>            \/\/ check if subgraph rooted with v has an articulation point<br\/>            if (isBCUtil(v, visited, disc, low, parent))<br\/>               return true;<br\/> <br\/>            \/\/ Check if the subtree rooted with v has a connection to<br\/>            \/\/ one of the ancestors of u<br\/>            low[u]  = min(low[u], low[v]);<br\/> <br\/>            \/\/ u is an articulation point in following cases<br\/> <br\/>            \/\/ (1) u is root of DFS tree and has two or more chilren.<br\/>            if (parent[u] == NIL &amp;&amp; children &gt; 1)<br\/>               return true;<br\/> <br\/>            \/\/ (2) If u is not root and low value of one of its child is<br\/>            \/\/ more than discovery value of u.<br\/>            if (parent[u] != NIL &amp;&amp; low[v] &gt;= disc[u])<br\/>               return true;<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\/>    return false;<br\/>}<br\/> <br\/>\/\/ The main function that returns true if graph is Biconnected, <br\/>\/\/ otherwise false. It uses recursive function isBCUtil()<br\/>bool Graph::isBC()<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, 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 if there is an articulation <br\/>    \/\/ point in given graph. We do DFS traversal starring from vertex 0<br\/>    if (isBCUtil(0, visited, disc, low, parent) == true)<br\/>        return false;<br\/> <br\/>    \/\/ Now check whether the given graph is connected or not. An undirected<br\/>    \/\/ graph is connected if all vertices are reachable from any starting <br\/>    \/\/ point (we have taken 0 as starting point)<br\/>    for (int i = 0; i &lt; V; i++)<br\/>        if (visited[i] == false)<br\/>            return false;<br\/> <br\/>    return true;<br\/>}<br\/> <br\/>\/\/ Driver program to test above function<br\/>int main()<br\/>{<br\/>    \/\/ Create graphs given in above diagrams<br\/>    Graph g1(2);<br\/>    g1.addEdge(0, 1);<br\/>    g1.isBC()? cout &lt;&lt; &quot;Yes\\n&quot; : cout &lt;&lt; &quot;No\\n&quot;;<br\/> <br\/>    Graph g2(5);<br\/>    g2.addEdge(1, 0);<br\/>    g2.addEdge(0, 2);<br\/>    g2.addEdge(2, 1);<br\/>    g2.addEdge(0, 3);<br\/>    g2.addEdge(3, 4);<br\/>    g2.addEdge(2, 4);<br\/>    g2.isBC()? cout &lt;&lt; &quot;Yes\\n&quot; : cout &lt;&lt; &quot;No\\n&quot;;<br\/> <br\/>    Graph g3(3);<br\/>    g3.addEdge(0, 1);<br\/>    g3.addEdge(1, 2);<br\/>    g3.isBC()? cout &lt;&lt; &quot;Yes\\n&quot; : cout &lt;&lt; &quot;No\\n&quot;;<br\/> <br\/>    Graph g4(5);<br\/>    g4.addEdge(1, 0);<br\/>    g4.addEdge(0, 2);<br\/>    g4.addEdge(2, 1);<br\/>    g4.addEdge(0, 3);<br\/>    g4.addEdge(3, 4);<br\/>    g4.isBC()? cout &lt;&lt; &quot;Yes\\n&quot; : cout &lt;&lt; &quot;No\\n&quot;;<br\/> <br\/>    Graph g5(3);<br\/>    g5.addEdge(0, 1);<br\/>    g5.addEdge(1, 2);<br\/>    g5.addEdge(2, 0);<br\/>    g5.isBC()? cout &lt;&lt; &quot;Yes\\n&quot; : cout &lt;&lt; &quot;No\\n&quot;;<br\/> <br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p>Output:<\/p>\n<pre>Yes\r\nYes\r\nNo\r\nNo\r\nYes<\/pre>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>C++ programming Biconnected graph &#8211; There is a simple cycle through any two vertices.A connected graph is Biconnected if it is connected and doesn\u2019t have <\/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":[78213,78214,78208,78260,78207,80229,78209,78216],"class_list":["post-26885","post","type-post","status-publish","format-standard","hentry","category-connectivity","category-graph-algorithms","tag-biconnected-components-and-articulation-points-ppt","tag-biconnected-components-of-an-undirected-graph-ppt","tag-biconnected-components-pdf","tag-biconnected-components-ppt","tag-biconnected-graph-example","tag-biconnected-graph-in-data-structure","tag-biconnectivity-algorithm","tag-what-is-articulation-point"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26885","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=26885"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26885\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26885"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26885"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26885"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}