{"id":34038,"date":"2024-11-01T09:23:24","date_gmt":"2024-11-01T09:23:24","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34038"},"modified":"2024-11-01T10:53:54","modified_gmt":"2024-11-01T10:53:54","slug":"c-coding-test-course-determine-bipartite-graph","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34038\/","title":{"rendered":"C# Coding Test Course, Determine Bipartite Graph"},"content":{"rendered":"<p><body><\/p>\n<h2>1. Introduction<\/h2>\n<p>\n        Graph theory is an important area in computer science and algorithms, utilized to solve various real-world problems.<br \/>\n        In this article, we will introduce the concept of &#8216;bipartite graphs&#8217; and the algorithmic problem of determining them,<br \/>\n        detailing how to solve it using C#. A bipartite graph is one in which the vertices of the graph are divided into two sets,<br \/>\n        ensuring that no two vertices within the same set are connected. Such graphs play a significant role in various applications.<br \/>\n        For example, they are frequently used to represent connections between two different types of objects or in bipartite matching problems.\n    <\/p>\n<h2>2. Problem Description<\/h2>\n<p>\n        In this problem, you are required to implement an algorithm to determine whether a given graph is a bipartite graph.<br \/>\n        For instance, the number of vertices <code>V<\/code> and the number of edges <code>E<\/code> are provided, along with<br \/>\n        <code>V<\/code> vertices and <code>E<\/code> edges connecting them. If the given graph is a bipartite graph, you should output &#8220;YES&#8221;; otherwise, output &#8220;NO&#8221;.\n    <\/p>\n<h3>Input Format<\/h3>\n<pre>\n        The first line contains the number of vertices <code>V<\/code> and the number of edges <code>E<\/code>. \n        The next <code>E<\/code> lines contain two integers representing the endpoints of each edge.\n    <\/pre>\n<h3>Output Format<\/h3>\n<pre>\n        If the given graph is a bipartite graph, output \"YES\"; otherwise, output \"NO\".\n    <\/pre>\n<h2>3. Algorithmic Approach<\/h2>\n<p>\n        To solve this problem, we will use either BFS (Breadth-First Search) or DFS (Depth-First Search).<br \/>\n        The method we will employ for determining whether a graph is bipartite involves coloring each vertex with two different colors.<br \/>\n        Specifically, one set will be marked with color 1 and the other with color 2. If two adjacent vertices are colored the same, it indicates that the graph is not bipartite.\n    <\/p>\n<h3>Algorithm Steps<\/h3>\n<ol>\n<li>Create a data structure to represent the graph in an adjacency list format.<\/li>\n<li>Visit the vertices using BFS or DFS and determine the colors of the vertices.<\/li>\n<li>Check if adjacent vertices have the same color to determine bipartiteness.<\/li>\n<li>After visiting all vertices, output the final result.<\/li>\n<\/ol>\n<h2>4. C# Code Implementation<\/h2>\n<p>\n        Now, based on the described algorithm, we will implement the code in C#.<br \/>\n        Below is the C# code for determining whether a graph is bipartite.\n    <\/p>\n<pre>\nusing System;\nusing System.Collections.Generic;\n\nclass Program\n{\n    static List<int>[] graph;\n    static int[] color;\n\n    static void Main(string[] args)\n    {\n        string[] input = Console.ReadLine().Split();\n        int V = int.Parse(input[0]);\n        int E = int.Parse(input[1]);\n\n        graph = new List<int>[V + 1];\n        for (int i = 0; i <= V; i++)\n            graph[i] = new List<int>();\n\n        for (int i = 0; i < E; i++)\n        {\n            input = Console.ReadLine().Split();\n            int u = int.Parse(input[0]);\n            int v = int.Parse(input[1]);\n            graph[u].Add(v);\n            graph[v].Add(u);\n        }\n\n        color = new int[V + 1];\n\n        bool isBipartite = true;\n        for (int i = 1; i <= V; i++)\n        {\n            if (color[i] == 0)\n                isBipartite &#038;= BFS(i);\n        }\n\n        Console.WriteLine(isBipartite ? \"YES\" : \"NO\");\n    }\n\n    static bool BFS(int start)\n    {\n        Queue<int> queue = new Queue<int>();\n        queue.Enqueue(start);\n        color[start] = 1;  \/\/ Color the starting vertex\n\n        while (queue.Count > 0)\n        {\n            int node = queue.Dequeue();\n            foreach (int neighbor in graph[node])\n            {\n                if (color[neighbor] == 0)  \/\/ If not colored yet\n                {\n                    color[neighbor] = 3 - color[node];  \/\/ Color with different color\n                    queue.Enqueue(neighbor);\n                }\n                else if (color[neighbor] == color[node])  \/\/ If the adjacent vertices have the same color\n                {\n                    return false;\n                }\n            }\n        }\n        return true;\n    }\n}\n    <\/int><\/int><\/int><\/int><\/int><\/pre>\n<h2>5. Code Explanation<\/h2>\n<p>\n        I will explain the key components and logic used in the C# code above.<\/p>\n<h3>Data Structures<\/h3>\n<p>\n        &#8211; <code>List<int>[] graph<\/int><\/code>: Used to represent the graph in an adjacency list format.<br \/>\n        It stores the list of connected vertices using the vertex numbers as keys.<br \/>\n        <br \/>\n        &#8211; <code>int[] color<\/code>: Stores the colors of each vertex. 0 indicates uncolored, while 1 and 2 represent the two different colors.\n    <\/p>\n<h3>Main Method<\/h3>\n<p>\n        &#8211; Receives the number of vertices and edges as input, based on which the graph is constructed.<br \/>\n        <br \/>\n        &#8211; Iterates through all vertices, calling BFS to check if the graph is bipartite.<br \/>\n        After checking all given vertices, the result is printed.\n    <\/p>\n<h3>BFS Method<\/h3>\n<p>\n        &#8211; The BFS method performs breadth-first search using a queue.<br \/>\n        It colors the starting vertex and colors adjacent vertices with a different color if they are not colored yet.<br \/>\n        <br \/>\n        &#8211; If an already colored vertex is found to be colored the same as the current vertex, it returns false, indicating that the graph is not bipartite.\n    <\/p>\n<h2>6. Complexity Analysis<\/h2>\n<p>\n        The time complexity of this algorithm is O(V + E).<br \/>\n        Here, V is the number of vertices and E is the number of edges, as the algorithm explores all vertices and edges of the graph.<br \/>\n        Thus, it can operate efficiently given the input size.\n    <\/p>\n<h2>7. Conclusion<\/h2>\n<p>\n        This article explained the definition of bipartite graphs and how to solve the problem of determining them using C#.<br \/>\n        I hope it helped in understanding the basics of graph theory and the BFS\/DFS algorithms, enabling practical applications.<br \/>\n        Moreover, I encourage you to enhance your understanding of graph algorithms by solving various problems.\n    <\/p>\n<h2>8. Additional References<\/h2>\n<ul>\n<li><a href=\"https:\/\/en.wikipedia.org\/wiki\/Bipartite_graph\">Wikipedia: Bipartite Graph<\/a><\/li>\n<li><a href=\"https:\/\/www.geeksforgeeks.org\/bipartite-graph\/\">GeeksforGeeks: Bipartite Graph Check<\/a><\/li>\n<li><a href=\"https:\/\/www.hackerrank.com\/domains\/tutorials\/10-days-of-java\">HackerRank: 10 Days of Java<\/a><\/li>\n<\/ul>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Introduction Graph theory is an important area in computer science and algorithms, utilized to solve various real-world problems. In this article, we will introduce the concept of &#8216;bipartite graphs&#8217; and the algorithmic problem of determining them, detailing how to solve it using C#. A bipartite graph is one in which the vertices of the &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34038\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;C# 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":[90],"tags":[],"class_list":["post-34038","post","type-post","status-publish","format-standard","hentry","category-c-coding-test-tutorials"],"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, 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\/34038\/\" \/>\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, Determine Bipartite Graph - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"1. Introduction Graph theory is an important area in computer science and algorithms, utilized to solve various real-world problems. In this article, we will introduce the concept of &#8216;bipartite graphs&#8217; and the algorithmic problem of determining them, detailing how to solve it using C#. A bipartite graph is one in which the vertices of the &hellip; \ub354 \ubcf4\uae30 &quot;C# Coding Test Course, Determine Bipartite Graph&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34038\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:23:24+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T10:53:54+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=\"2\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/34038\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34038\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"C# Coding Test Course, Determine Bipartite Graph\",\"datePublished\":\"2024-11-01T09:23:24+00:00\",\"dateModified\":\"2024-11-01T10:53:54+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34038\/\"},\"wordCount\":581,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"C# Coding Test Tutorials\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34038\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34038\/\",\"name\":\"C# Coding Test Course, Determine Bipartite Graph - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:23:24+00:00\",\"dateModified\":\"2024-11-01T10:53:54+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34038\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34038\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34038\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C# 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":"C# 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\/34038\/","og_locale":"ko_KR","og_type":"article","og_title":"C# Coding Test Course, Determine Bipartite Graph - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"1. Introduction Graph theory is an important area in computer science and algorithms, utilized to solve various real-world problems. In this article, we will introduce the concept of &#8216;bipartite graphs&#8217; and the algorithmic problem of determining them, detailing how to solve it using C#. A bipartite graph is one in which the vertices of the &hellip; \ub354 \ubcf4\uae30 \"C# Coding Test Course, Determine Bipartite Graph\"","og_url":"https:\/\/atmokpo.com\/w\/34038\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:23:24+00:00","article_modified_time":"2024-11-01T10:53:54+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":"2\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/34038\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34038\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"C# Coding Test Course, Determine Bipartite Graph","datePublished":"2024-11-01T09:23:24+00:00","dateModified":"2024-11-01T10:53:54+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34038\/"},"wordCount":581,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["C# Coding Test Tutorials"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34038\/","url":"https:\/\/atmokpo.com\/w\/34038\/","name":"C# Coding Test Course, Determine Bipartite Graph - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:23:24+00:00","dateModified":"2024-11-01T10:53:54+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34038\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34038\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34038\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"C# 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\/34038","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=34038"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34038\/revisions"}],"predecessor-version":[{"id":34039,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34038\/revisions\/34039"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34038"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34038"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34038"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}