{"id":34374,"date":"2024-11-01T09:27:25","date_gmt":"2024-11-01T09:27:25","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34374"},"modified":"2024-11-01T11:41:33","modified_gmt":"2024-11-01T11:41:33","slug":"javascript-coding-test-course-determine-bipartite-graph","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34374\/","title":{"rendered":"JavaScript Coding Test Course, Determine Bipartite Graph"},"content":{"rendered":"<p><body><\/p>\n<p>Graph problems frequently appear in coding tests. A bipartite graph refers to a graph that can be divided into two sets, where adjacent vertices belong to different sets. In this lecture, we will learn about the algorithm to determine whether a graph is bipartite.<\/p>\n<h2>Problem Definition<\/h2>\n<p>Write a function to determine whether the given undirected graph is bipartite. A bipartite graph must divide the vertices into two groups, with no edges existing between vertices in the same group.<\/p>\n<h3>Input<\/h3>\n<ul>\n<li>Number of vertices <code>n<\/code> (1 \u2264 <code>n<\/code> \u2264 1000)<\/li>\n<li>Number of edges <code>m<\/code> (0 \u2264 <code>m<\/code> \u2264 1000)<\/li>\n<li>Information on <code>m<\/code> edges (pairs of integers <code>u, v<\/code> indicating that vertex <code>u<\/code> is connected to vertex <code>v<\/code>)<\/li>\n<\/ul>\n<h3>Output<\/h3>\n<p>If the graph is a bipartite graph, print <code>true<\/code>, otherwise print <code>false<\/code>.<\/p>\n<h2>Approach to the Problem<\/h2>\n<p>To determine if a graph is bipartite, we can use a graph coloring technique. We color each vertex with two colors, such as &#8216;0&#8217; and &#8216;1&#8217;, and if adjacent vertices are colored the same, the graph is not bipartite.<\/p>\n<p>The implementation method is as follows:<\/p>\n<ul>\n<li>Represent the graph as an adjacency list<\/li>\n<li>Create an array to store the color of each vertex<\/li>\n<li>Use breadth-first search (BFS) or depth-first search (DFS) to visit and color the vertices<\/li>\n<li>If adjacent vertices have the same color, return that it is not a bipartite graph<\/li>\n<\/ul>\n<h2>Code Implementation<\/h2>\n<p>Let&#8217;s implement a function to determine whether a graph is bipartite according to the steps above using JavaScript.<\/p>\n<pre><code>\nfunction isBipartiteGraph(n, edges) {\n    \/\/ Represent the graph as an adjacency list\n    const graph = Array.from({length: n}, () => []);\n    edges.forEach(([u, v]) => {\n        graph[u].push(v);\n        graph[v].push(u); \/\/ Since it is an undirected graph, add in both directions\n    });\n\n    const colors = Array(n).fill(-1); \/\/ Store the color of each vertex (-1: unvisited, 0: first color, 1: second color)\n\n    const bfs = (start) => {\n        const queue = [start];\n        colors[start] = 0; \/\/ Color the starting vertex with the first color\n\n        while (queue.length) {\n            const node = queue.shift();\n\n            for (const neighbor of graph[node]) {\n                if (colors[neighbor] === -1) {\n                    \/\/ If the adjacent vertex is unvisited, color it and add to the queue\n                    colors[neighbor] = 1 - colors[node];\n                    queue.push(neighbor);\n                } else if (colors[neighbor] === colors[node]) {\n                    \/\/ If the adjacent vertex's color is the same as the current node, it is not a bipartite graph\n                    return false;\n                }\n            }\n        }\n        return true;\n    };\n\n    \/\/ Check all vertices and perform BFS if there are connected components\n    for (let i = 0; i < n; i++) {\n        if (colors[i] === -1) {\n            if (!bfs(i)) return false;\n        }\n    }\n    \n    return true;\n}\n\n\/\/ Test case\nconst n = 4;\nconst edges = [[0, 1], [0, 2], [1, 3], [2, 3]];\nconsole.log(isBipartiteGraph(n, edges)); \/\/ false\n    <\/code><\/pre>\n<h2>Algorithm Analysis<\/h2>\n<p>The above algorithm uses breadth-first search (BFS) to traverse all the vertices of the graph. Consequently, the time complexity is O(n + m) where <code>n<\/code> is the number of vertices and <code>m<\/code> is the number of edges. This is because we visit each vertex and edge once.<\/p>\n<p>The space complexity is also O(n + m) required to store the adjacency list and color array.<\/p>\n<h2>Conclusion<\/h2>\n<p>In this lecture, we learned about the algorithm to determine whether a graph is bipartite. With this algorithm, we can ascertain if a graph problem involves a bipartite graph. Given its applicability to various problems, a thorough understanding is beneficial.<\/p>\n<p>Furthermore, the solution to this problem can also be implemented similarly using DFS in addition to BFS. Understand the unique characteristics of the algorithm and try solving various derivative problems based on that.<\/p>\n<h2>Additional Practice Problems<\/h2>\n<p>To help your understanding, try solving the following problems:<\/p>\n<ul>\n<li>Symmetric Bipartite Graph Determination: Determine whether the given edge information constitutes a bipartite graph when provided symmetrically.<\/li>\n<li>Maximum Matching in Bipartite Graph: Implement an algorithm to find the maximum matching in a given bipartite graph.<\/li>\n<\/ul>\n<h2>References<\/h2>\n<p>For a detailed explanation of bipartite graphs, please refer to the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Bipartite_graph\" target=\"_blank\" rel=\"noopener\">Bipartite Graph<\/a> article on Wikipedia.<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Graph problems frequently appear in coding tests. A bipartite graph refers to a graph that can be divided into two sets, where adjacent vertices belong to different sets. In this lecture, we will learn about the algorithm to determine whether a graph is bipartite. Problem Definition Write a function to determine whether the given undirected &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34374\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;JavaScript Coding Test Course, Determine Bipartite Graph&#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":[141],"tags":[],"class_list":["post-34374","post","type-post","status-publish","format-standard","hentry","category-javascript-coding-test"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>JavaScript Coding Test Course, Determine Bipartite Graph - \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\/34374\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JavaScript Coding Test Course, Determine Bipartite Graph - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Graph problems frequently appear in coding tests. A bipartite graph refers to a graph that can be divided into two sets, where adjacent vertices belong to different sets. In this lecture, we will learn about the algorithm to determine whether a graph is bipartite. Problem Definition Write a function to determine whether the given undirected &hellip; \ub354 \ubcf4\uae30 &quot;JavaScript Coding Test Course, Determine Bipartite Graph&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34374\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:27:25+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:41:33+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=\"3\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/34374\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34374\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"JavaScript Coding Test Course, Determine Bipartite Graph\",\"datePublished\":\"2024-11-01T09:27:25+00:00\",\"dateModified\":\"2024-11-01T11:41:33+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34374\/\"},\"wordCount\":432,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Javascript Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34374\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34374\/\",\"name\":\"JavaScript Coding Test Course, Determine Bipartite Graph - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:27:25+00:00\",\"dateModified\":\"2024-11-01T11:41:33+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34374\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34374\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34374\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript Coding Test Course, Determine Bipartite Graph\"}]},{\"@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":"JavaScript Coding Test Course, Determine Bipartite Graph - \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\/34374\/","og_locale":"ko_KR","og_type":"article","og_title":"JavaScript Coding Test Course, Determine Bipartite Graph - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Graph problems frequently appear in coding tests. A bipartite graph refers to a graph that can be divided into two sets, where adjacent vertices belong to different sets. In this lecture, we will learn about the algorithm to determine whether a graph is bipartite. Problem Definition Write a function to determine whether the given undirected &hellip; \ub354 \ubcf4\uae30 \"JavaScript Coding Test Course, Determine Bipartite Graph\"","og_url":"https:\/\/atmokpo.com\/w\/34374\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:27:25+00:00","article_modified_time":"2024-11-01T11:41:33+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":"3\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/34374\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34374\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"JavaScript Coding Test Course, Determine Bipartite Graph","datePublished":"2024-11-01T09:27:25+00:00","dateModified":"2024-11-01T11:41:33+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34374\/"},"wordCount":432,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Javascript Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34374\/","url":"https:\/\/atmokpo.com\/w\/34374\/","name":"JavaScript Coding Test Course, Determine Bipartite Graph - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:27:25+00:00","dateModified":"2024-11-01T11:41:33+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34374\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34374\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34374\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"JavaScript Coding Test Course, Determine Bipartite Graph"}]},{"@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\/34374","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=34374"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34374\/revisions"}],"predecessor-version":[{"id":34375,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34374\/revisions\/34375"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34374"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34374"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34374"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}