{"id":34278,"date":"2024-11-01T09:26:18","date_gmt":"2024-11-01T09:26:18","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34278"},"modified":"2024-11-01T10:57:53","modified_gmt":"2024-11-01T10:57:53","slug":"c-coding-test-course-bipartite-graph-checking","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34278\/","title":{"rendered":"C++ Coding Test Course, Bipartite Graph Checking"},"content":{"rendered":"<p><body><\/p>\n<p>Hello! Today we will learn about <strong>bipartite graphs<\/strong>. A bipartite graph is a graph in which the set of vertices can be divided into two subsets, and there are no edges between vertices within the same subset. This topic is one of the frequently posed questions in coding tests. In the next session, we will explore the algorithms and coding needed to determine if a graph is bipartite.<\/p>\n<h2>Problem Description<\/h2>\n<p>The problem is to determine whether the given undirected graph is bipartite. Starting from a vertex, we need to check whether we can visit all vertices while satisfying the bipartite graph conditions.<\/p>\n<h3>Problem Example<\/h3>\n<p>Given the following graph, determine whether it is a bipartite graph.<\/p>\n<pre>\nVertices: 6\nEdges: \n1 - 2\n1 - 3\n2 - 4\n2 - 5\n3 - 6\n<\/pre>\n<p>Looking at the graph above, we can see if it can be divided into two sets. Set A is {1, 4, 5, 6} and Set B is {2, 3}. This graph is indeed a bipartite graph.<\/p>\n<h2>Problem Solving Strategy<\/h2>\n<p>One method we can use to confirm if a graph is bipartite is <strong>Breadth-First Search (BFS)<\/strong> or <strong>Depth-First Search (DFS)<\/strong>. Both methods can be employed to traverse the graph while coloring each vertex (or grouping them) to verify the bipartite graph conditions.<\/p>\n<h3>Algorithm Steps<\/h3>\n<ol>\n<li>Represent the graph in the form of an adjacency list.<\/li>\n<li>Run a loop to visit all nodes.<\/li>\n<li>Color the current vertex to divide it into two groups.<\/li>\n<li>Check if adjacent vertices have the same color.<\/li>\n<li>Repeat until all nodes have been visited or until all reachable nodes are visited.<\/li>\n<\/ol>\n<h2>C++ Code Implementation<\/h2>\n<p>Let&#8217;s take a look at the C++ code to determine if this undirected graph is bipartite.<\/p>\n<pre><code>\n#include &lt;iostream&gt;\n#include &lt;vector&gt;\n#include &lt;queue&gt;\n\nusing namespace std;\n\nconst int MAX = 1000; \/\/ Maximum number of vertices\nvector&lt;int&gt; graph[MAX];\nint color[MAX]; \/\/ Color setting: -1 is unvisited, 0 and 1 are groups\n\nbool isBipartite(int start) {\n    queue&lt;int&gt; q;\n    q.push(start);\n    color[start] = 0; \/\/ Color the starting vertex\n\n    while (!q.empty()) {\n        int v = q.front();\n        q.pop();\n\n        for (int neighbor : graph[v]) {\n            \/\/ If the neighboring vertex has not been visited, color it.\n            if (color[neighbor] == -1) {\n                color[neighbor] = 1 - color[v]; \/\/ Assign a different color than the current vertex\n                q.push(neighbor);\n            }\n            \/\/ If the neighboring vertex has the same color as the current vertex, it is not a bipartite graph.\n            else if (color[neighbor] == color[v]) {\n                return false; \/\/ Violates the bipartite graph conditions\n            }\n        }\n    }\n    return true;\n}\n\nint main() {\n    int v, e;\n    cout &lt;&lt; \"Enter the number of vertices and edges: \";\n    cin &gt;&gt; v &gt;&gt; e;\n\n    \/\/ Initialize the graph\n    for (int i = 0; i &lt; MAX; i++) {\n        color[i] = -1;\n    }\n\n    cout &lt;&lt; \"Input edges (format: a b): \" &lt;&lt; endl;\n    for (int i = 0; i &lt; e; i++) {\n        int a, b;\n        cin &gt;&gt; a &gt;&gt; b;\n        graph[a].push_back(b);\n        graph[b].push_back(a); \/\/ Undirected graph\n    }\n\n    bool result = true;\n    for (int i = 1; i &lt;= v; i++) {\n        if (color[i] == -1) {\n            \/\/ If this vertex is unvisited, check if it is bipartite\n            if (!isBipartite(i)) {\n                result = false;\n                break;\n            }\n        }\n    }\n\n    if (result) {\n        cout &lt;&lt; \"This graph is a bipartite graph.\" &lt;&lt; endl;\n    } else {\n        cout &lt;&lt; \"This graph is not a bipartite graph.\" &lt;&lt; endl;\n    }\n\n    return 0;\n}\n<\/code><\/pre>\n<h2>Code Explanation<\/h2>\n<p>This code takes the vertices and edges of the given graph as input and determines whether the graph is bipartite. The <strong>isBipartite<\/strong> function checks if all adjacent vertices are of different colors.<\/p>\n<h3>Variable and Structure Explanation<\/h3>\n<ul>\n<li><code>graph[MAX]<\/code>: Adjacency list representation of the graph.<\/li>\n<li><code>color[MAX]<\/code>: An array representing the color of each vertex.<\/li>\n<li><code>queue&lt;int&gt; q<\/code>: The queue used for BFS.<\/li>\n<\/ul>\n<h3>Input Example and Output Result<\/h3>\n<p>When running the code, an example of inputting edges is as follows.<\/p>\n<pre>\nEnter the number of vertices and edges: 6 5\nInput edges (format: a b): \n1 2\n1 3\n2 4\n2 5\n3 6\n<\/pre>\n<p>When given the above input, the output will be as follows.<\/p>\n<pre>\nThis graph is a bipartite graph.\n<\/pre>\n<h2>Conclusion<\/h2>\n<p>In conclusion, we learned how to determine if a graph is bipartite using C++. Understanding the basic principle of solving problems using BFS or DFS can be applied to various graph problems. I hope this helps your preparation for coding tests!<\/p>\n<div class=\"note\">\n<strong>Note:<\/strong> This problem has a time complexity of O(V + E) for large graphs, which allows for a reasonably checkable time. I recommend experimenting multiple times with various test cases.\n<\/div>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello! Today we will learn about bipartite graphs. A bipartite graph is a graph in which the set of vertices can be divided into two subsets, and there are no edges between vertices within the same subset. This topic is one of the frequently posed questions in coding tests. In the next session, we will &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34278\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;C++ Coding Test Course, Bipartite Graph Checking&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[111],"tags":[],"class_list":["post-34278","post","type-post","status-publish","format-standard","hentry","category-c-coding-test-tutorials-2"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C++ Coding Test Course, Bipartite Graph Checking - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/34278\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++ Coding Test Course, Bipartite Graph Checking - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Hello! Today we will learn about bipartite graphs. A bipartite graph is a graph in which the set of vertices can be divided into two subsets, and there are no edges between vertices within the same subset. This topic is one of the frequently posed questions in coding tests. In the next session, we will &hellip; \ub354 \ubcf4\uae30 &quot;C++ Coding Test Course, Bipartite Graph Checking&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34278\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:26:18+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T10:57:53+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"4\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/34278\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34278\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"C++ Coding Test Course, Bipartite Graph Checking\",\"datePublished\":\"2024-11-01T09:26:18+00:00\",\"dateModified\":\"2024-11-01T10:57:53+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34278\/\"},\"wordCount\":425,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"C++ Coding Test Tutorials\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34278\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34278\/\",\"name\":\"C++ Coding Test Course, Bipartite Graph Checking - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:26:18+00:00\",\"dateModified\":\"2024-11-01T10:57:53+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34278\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34278\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34278\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ Coding Test Course, Bipartite Graph Checking\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C++ Coding Test Course, Bipartite Graph Checking - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/34278\/","og_locale":"ko_KR","og_type":"article","og_title":"C++ Coding Test Course, Bipartite Graph Checking - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Hello! Today we will learn about bipartite graphs. A bipartite graph is a graph in which the set of vertices can be divided into two subsets, and there are no edges between vertices within the same subset. This topic is one of the frequently posed questions in coding tests. In the next session, we will &hellip; \ub354 \ubcf4\uae30 \"C++ Coding Test Course, Bipartite Graph Checking\"","og_url":"https:\/\/atmokpo.com\/w\/34278\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:26:18+00:00","article_modified_time":"2024-11-01T10:57:53+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"4\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/34278\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34278\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"C++ Coding Test Course, Bipartite Graph Checking","datePublished":"2024-11-01T09:26:18+00:00","dateModified":"2024-11-01T10:57:53+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34278\/"},"wordCount":425,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["C++ Coding Test Tutorials"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34278\/","url":"https:\/\/atmokpo.com\/w\/34278\/","name":"C++ Coding Test Course, Bipartite Graph Checking - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:26:18+00:00","dateModified":"2024-11-01T10:57:53+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34278\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34278\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34278\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"C++ Coding Test Course, Bipartite Graph Checking"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34278","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=34278"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34278\/revisions"}],"predecessor-version":[{"id":34279,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34278\/revisions\/34279"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34278"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34278"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}