{"id":246,"date":"2025-06-01T15:35:07","date_gmt":"2025-06-01T10:05:07","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=246"},"modified":"2025-06-01T15:35:09","modified_gmt":"2025-06-01T10:05:09","slug":"is-graph-bipartite","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/is-graph-bipartite\/","title":{"rendered":"Is Graph Bipartite? | Leetcode 785 | Easy 2-Color DFS Solution in Python"},"content":{"rendered":"\n<p>Learn how to test if an undirected graph is bipartite by coloring it with two colors. Simple DFS idea, commented Python code, dry run, and Big-O analysis.<\/p>\n\n\n\n<p>Here&#8217;s the [<strong><a href=\"https:\/\/leetcode.com\/problems\/is-graph-bipartite\/description\/\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">Problem Link<\/span><\/mark><\/a><\/strong>] to begin with.<\/p>\n\n\n<div style=\"max-width: -moz-fit-content; \" class=\"wp-block-ub-table-of-contents-block ub_table-of-contents ub_table-of-contents-collapsed\" id=\"ub_table-of-contents-251180e9-ec5c-4dce-bc96-d3a484dc5032\" data-linktodivider=\"false\" data-showtext=\"show\" data-hidetext=\"hide\" data-scrolltype=\"auto\" data-enablesmoothscroll=\"true\" data-initiallyhideonmobile=\"false\" data-initiallyshow=\"false\"><div class=\"ub_table-of-contents-header-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-header\" style=\"text-align: left; \">\n\t\t\t\t<div class=\"ub_table-of-contents-title\">Contents:<\/div>\n\t\t\t\t<div class=\"ub_table-of-contents-header-toggle\">\n\t\t\t<div class=\"ub_table-of-contents-toggle\" style=\"\">\n\t\t\t\u00a0[<a class=\"ub_table-of-contents-toggle-link\" href=\"#\" style=\"\">show<\/a>]\n\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t<\/div>\n\t\t<\/div><div class=\"ub_table-of-contents-extra-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-container ub_table-of-contents-1-column ub-hide\">\n\t\t\t\t<ul style=\"\"><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/is-graph-bipartite\/#0-what-does-the-problem-ask\" style=\"\">What does the problem ask?<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/is-graph-bipartite\/#1-examples\" style=\"\">Examples<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/is-graph-bipartite\/#2-intuition-amp-approach\" style=\"\">Intuition &amp; Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/is-graph-bipartite\/#3-code\" style=\"\">Code<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/is-graph-bipartite\/#4-code-explanation-step-by-step\" style=\"\">Code explanation (step-by-step)<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/is-graph-bipartite\/#5-dry-run-on-the-first-example\" style=\"\">Dry run on the first example<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/is-graph-bipartite\/#6-complexity\" style=\"\">Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/is-graph-bipartite\/#7-conclusion\" style=\"\">Conclusion<\/a><\/li><\/ul>\n\t\t\t<\/div>\n\t\t<\/div><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"0-what-does-the-problem-ask\">What does the problem ask?<\/h2>\n\n\n\n<p>You get an <strong>undirected graph<\/strong> as an adjacency list <code>graph<\/code>.<br>Your job is to say <strong>true<\/strong> if you can color every node using only <strong>two colors<\/strong> such that <strong>no two connected nodes share the same color<\/strong>.<br>If this is impossible, return <strong>false<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"1-examples\">Examples<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><code>graph<\/code> (adjacency list)<\/th><th>Result<\/th><th>Reason<\/th><\/tr><\/thead><tbody><tr><td><code>[[1,3], [0,2], [1,3], [0,2]]<\/code><\/td><td><strong>true<\/strong><\/td><td>You can color nodes <code>(0,2)<\/code> with color 0 and <code>(1,3)<\/code> with color 1.<\/td><\/tr><tr><td><code>[[1,2,3], [0,2], [0,1,3], [0,2]]<\/code><\/td><td><strong>false<\/strong><\/td><td>Triangle <code>(0,1,2)<\/code> forces a conflict, needs 3 colors.<\/td><\/tr><tr><td><code>[[],[]]<\/code><\/td><td><strong>true<\/strong><\/td><td>Two isolated nodes are always bipartite.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"2-intuition-amp-approach\">Intuition &amp; Approach<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Think in colors.<\/strong><br>Bipartite means we can split nodes into two groups (colors). Adjacent nodes must land in different groups.<\/li>\n\n\n\n<li><strong>Pick a start node \u2192 give it color 0.<\/strong><br>Then travel through its neighbors with DFS.<\/li>\n\n\n\n<li><strong>Color neighbors with the opposite color (1 \u2212 current).<\/strong>\n<ul class=\"wp-block-list\">\n<li>If a neighbor already has a color <strong>and it matches ours<\/strong>, we found a clash \u2192 graph is <strong>not bipartite<\/strong>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Handle disconnected parts.<\/strong><br>The graph may have many separate components, so repeat the DFS for every still-uncolored node.<\/li>\n\n\n\n<li><strong>If no clashes appear, the graph is bipartite.<\/strong><\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"3-code\">Code<\/h2>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class Solution:\n    # Helper DFS to try coloring one component\n    def dfs(self, current_node, visited, graph, color):\n        visited[current_node] = color          # paint current node\n        for adjNode in graph[current_node]:    # go through all neighbors\n            if visited[adjNode] != -1:         # neighbor already colored\n                if visited[adjNode] == color:  # same color \u2192 clash!\n                    return False\n            else:\n                # color neighbor with opposite color (1 - color)\n                ans = self.dfs(adjNode, visited, graph, 1 - color)\n                if ans == False:               # clash found deeper\n                    return False\n        return True                            # this branch is fine\n\n    def isBipartite(self, graph: List[List[int]]) -&gt; bool:\n        total_nodes = len(graph)\n        visited = [-1] * total_nodes           # -1 means &quot;no color yet&quot;\n        for index in range(0, total_nodes):\n            if visited[index] == -1:           # unvisited component\n                ans = self.dfs(index, visited, graph, 0)  # start with color 0\n                if ans == False:               # clash in this component\n                    return False\n        return True                            # all components are okay\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\"># Helper DFS to try coloring one component<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">dfs<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">current_node<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">visited<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">graph<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">color<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        visited[current_node] = color          <\/span><span style=\"color: #6A9955\"># paint current node<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> adjNode <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> graph[current_node]:    <\/span><span style=\"color: #6A9955\"># go through all neighbors<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> visited[adjNode] != -<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">:         <\/span><span style=\"color: #6A9955\"># neighbor already colored<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> visited[adjNode] == color:  <\/span><span style=\"color: #6A9955\"># same color \u2192 clash!<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                    <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">False<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">else<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #6A9955\"># color neighbor with opposite color (1 - color)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                ans = <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.dfs(adjNode, visited, graph, <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\"> - color)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> ans == <\/span><span style=\"color: #569CD6\">False<\/span><span style=\"color: #D4D4D4\">:               <\/span><span style=\"color: #6A9955\"># clash found deeper<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                    <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">False<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">True<\/span><span style=\"color: #D4D4D4\">                            <\/span><span style=\"color: #6A9955\"># this branch is fine<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">isBipartite<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">graph<\/span><span style=\"color: #D4D4D4\">: List[List[<\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">]]) -&gt; <\/span><span style=\"color: #4EC9B0\">bool<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        total_nodes = <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(graph)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        visited = [-<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">] * total_nodes           <\/span><span style=\"color: #6A9955\"># -1 means &quot;no color yet&quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> index <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">, total_nodes):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> visited[index] == -<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">:           <\/span><span style=\"color: #6A9955\"># unvisited component<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                ans = <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.dfs(index, visited, graph, <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">)  <\/span><span style=\"color: #6A9955\"># start with color 0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> ans == <\/span><span style=\"color: #569CD6\">False<\/span><span style=\"color: #D4D4D4\">:               <\/span><span style=\"color: #6A9955\"># clash in this component<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                    <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">False<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">True<\/span><span style=\"color: #D4D4D4\">                            <\/span><span style=\"color: #6A9955\"># all components are okay<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"4-code-explanation-step-by-step\">Code explanation (step-by-step)<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong><code>visited<\/code> array<\/strong><br><em>Size = number of nodes<\/em>\n<ul class=\"wp-block-list\">\n<li><code>-1<\/code> \u2192 not colored yet<\/li>\n\n\n\n<li><code>0<\/code> or <code>1<\/code> \u2192 the chosen color<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Main loop<\/strong> goes through every node:\n<ul class=\"wp-block-list\">\n<li>If the node is not colored, we launch DFS from it.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>DFS routine<\/strong>\n<ul class=\"wp-block-list\">\n<li>Colors current node.<\/li>\n\n\n\n<li>Checks all neighbors:\n<ul class=\"wp-block-list\">\n<li>If neighbor has same color \u2192 immediately return <code>False<\/code>.<\/li>\n\n\n\n<li>If neighbor is uncolored \u2192 recursively color it with <code>1 - color<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>If recursion finishes with no clashes \u2192 return <code>True<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>If <strong>any<\/strong> component returns <code>False<\/code>, the full graph is not bipartite.<br>Otherwise return <code>True<\/code>.<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"5-dry-run-on-the-first-example\">Dry run on the first example<\/h2>\n\n\n\n<p><code>graph = [[1,3], [0,2], [1,3], [0,2]]<\/code><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Step<\/th><th>Action<\/th><th><code>visited<\/code><\/th><\/tr><\/thead><tbody><tr><td>Start at node 0, color 0<\/td><td><code>[0, -1, -1, -1]<\/code><\/td><td><\/td><\/tr><tr><td>Visit neighbor 1 \u2192 color 1<\/td><td><code>[0, 1, -1, -1]<\/code><\/td><td><\/td><\/tr><tr><td>From node 1 visit neighbor 2 \u2192 color 0<\/td><td><code>[0, 1, 0, -1]<\/code><\/td><td><\/td><\/tr><tr><td>From node 2 visit neighbor 3 \u2192 color 1<\/td><td><code>[0, 1, 0, 1]<\/code><\/td><td><\/td><\/tr><tr><td>All further neighbors already colored with opposite color \u2192 no clash<\/td><td>finished<\/td><td><\/td><\/tr><tr><td>Loop checks node 1, 2, 3 (already colored)<\/td><td>done<\/td><td><\/td><\/tr><tr><td><strong>Return <code>True<\/code><\/strong><\/td><td><\/td><td><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"6-complexity\">Complexity<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Measure<\/th><th>Value<\/th><\/tr><\/thead><tbody><tr><td><strong>Time<\/strong><\/td><td><strong>O(V + E)<\/strong><\/td><\/tr><tr><td><strong>Space<\/strong><\/td><td><strong>O(V)<\/strong><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><em>Explanation<\/em><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We touch every vertex and every edge once during DFS.<\/li>\n\n\n\n<li><code>visited<\/code> array and recursion stack need up to <code>V<\/code> entries.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"7-conclusion\">Conclusion<\/h2>\n\n\n\n<p>A graph is bipartite if you can 2-color it with no clashes.<br>Depth-First Search plus an array that stores each node\u2019s color is a quick and clear way to test this. If a conflict appears at any step, we know right away the graph needs more than two colors; otherwise it is bipartite.<\/p>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/www.codeanddebug.in\/course\/zero-to-hero-python-dsa\">Join our Advance DSA COURSE<\/a><\/div>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><em>For any changes to the article, kindly email at <a href=\"mailto:code@codeanddebug.in\">code@codeanddebug.in<\/a> or contact us at <a href=\"tel:+91-9712928220\">+91-9712928220<\/a>.<\/em><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to test if an undirected graph is bipartite by coloring it with two colors. Simple DFS idea, commented Python code, dry run, and Big-O analysis. Here&#8217;s the [Problem Link] to begin with. What does the problem ask? You get an undirected graph as an adjacency list graph.Your job is to say true if<\/p>\n","protected":false},"author":1,"featured_media":248,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,6],"tags":[17,19],"class_list":{"0":"post-246","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-intermediate","9":"tag-graph","10":"tag-medium"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/06\/bipartite-graph-featured-image.png","author_info":{"display_name":"codeanddebug","author_link":"https:\/\/codeanddebug.in\/blog\/author\/codeanddebug\/"},"_links":{"self":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/246","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/comments?post=246"}],"version-history":[{"count":2,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/246\/revisions"}],"predecessor-version":[{"id":252,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/246\/revisions\/252"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/248"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=246"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=246"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=246"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}