{"id":34034,"date":"2024-11-01T09:23:20","date_gmt":"2024-11-01T09:23:20","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34034"},"modified":"2024-11-01T10:53:55","modified_gmt":"2024-11-01T10:53:55","slug":"c-coding-test-course-bellman-ford","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34034\/","title":{"rendered":"C# Coding Test Course, Bellman-Ford"},"content":{"rendered":"<p><body><\/p>\n<p>As you often solve algorithm problems, you will encounter various shortest path problems. Among them, the &#8220;Bellman-Ford algorithm&#8221; is a useful algorithm for finding the shortest path in a graph that includes edges with negative weights. In this lecture, we will gain a deep understanding of the Bellman-Ford algorithm and solve a problem using it.<\/p>\n<h2>Introduction to the Bellman-Ford Algorithm<\/h2>\n<p>The Bellman-Ford algorithm is an algorithm for finding the shortest path from the starting point to all vertices in a given graph. This algorithm has the following characteristics:<\/p>\n<ul>\n<li>It can find the shortest path even in graphs with edges that have negative weights.<\/li>\n<li>If a negative cycle exists, the shortest path cannot be defined.<\/li>\n<li>The time complexity is O(VE), where V is the number of vertices and E is the number of edges.<\/li>\n<\/ul>\n<h3>How the Bellman-Ford Algorithm Works<\/h3>\n<p>This algorithm works as follows:<\/p>\n<ol>\n<li>Initialize the distance values of all vertices to infinity. However, set the distance value of the starting vertex to 0.<\/li>\n<li>For all edges, update the distance value if it can be moved through that edge.<\/li>\n<li>Repeat this process V &#8211; 1 times because the longest path can consist of V &#8211; 1 edges.<\/li>\n<li>Finally, check once more for all edges to see if a negative cycle exists.<\/li>\n<\/ol>\n<h2>Problem Description<\/h2>\n<h3>Problem: Finding the Shortest Path<\/h3>\n<p>Given the number of vertices N and edges M in a graph, find the shortest path from the starting vertex S to all other vertices. There may be edges with negative weights, and if a negative cycle exists, print &#8220;Negative Cycle&#8221;.<\/p>\n<pre><code>Input\n3 3 1\n1 2 2\n1 3 3\n2 3 -5\n\nOutput\n1 0 2\n<\/code><\/pre>\n<h3>Explanation of the Input Example<\/h3>\n<p>The above input example represents the following graph:<\/p>\n<p>Number of vertices: 3<\/p>\n<p>Number of edges: 3<\/p>\n<p>Starting vertex: 1<\/p>\n<ul>\n<li>1 \u2194 2 : Weight 2<\/li>\n<li>1 \u2194 3 : Weight 3<\/li>\n<li>2 \u2194 3 : Weight -5<\/li>\n<\/ul>\n<h2>Code Implementation<\/h2>\n<pre><code class=\"language-csharp\">using System;\nusing System.Collections.Generic;\n\nclass Program\n{\n    static void Main()\n    {\n        \/\/ Input\n        string[] input = Console.ReadLine().Split();\n        int N = int.Parse(input[0]); \/\/ Number of vertices\n        int M = int.Parse(input[1]); \/\/ Number of edges\n        int S = int.Parse(input[2]); \/\/ Starting vertex\n\n        \/\/ Initialize graph\n        List<Tuple<int, int, int>> edges = new List<Tuple<int, int, int>>();\n        for (int i = 0; i < M; i++)\n        {\n            input = Console.ReadLine().Split();\n            int u = int.Parse(input[0]); \/\/ Starting vertex\n            int v = int.Parse(input[1]); \/\/ Destination vertex\n            int w = int.Parse(input[2]); \/\/ Weight\n            edges.Add(Tuple.Create(u, v, w));\n        }\n\n        \/\/ Initialize distance array\n        int[] distance = new int[N + 1];\n        Array.Fill(distance, int.MaxValue);\n        distance[S] = 0;\n\n        \/\/ Bellman-Ford algorithm\n        for (int i = 1; i <= N - 1; i++)\n        {\n            foreach (var edge in edges)\n            {\n                int u = edge.Item1;\n                int v = edge.Item2;\n                int w = edge.Item3;\n                if (distance[u] != int.MaxValue &#038;&#038; distance[u] + w < distance[v])\n                {\n                    distance[v] = distance[u] + w;\n                }\n            }\n        }\n\n        \/\/ Check for negative cycle\n        bool hasNegativeCycle = false;\n        foreach (var edge in edges)\n        {\n            int u = edge.Item1;\n            int v = edge.Item2;\n            int w = edge.Item3;\n            if (distance[u] != int.MaxValue &#038;&#038; distance[u] + w < distance[v])\n            {\n                hasNegativeCycle = true;\n                break;\n            }\n        }\n\n        \/\/ Output result\n        if (hasNegativeCycle)\n        {\n            Console.WriteLine(\"Negative Cycle\");\n        }\n        else\n        {\n            for (int i = 1; i <= N; i++)\n            {\n                Console.WriteLine(distance[i] == int.MaxValue ? \"INF\" : distance[i].ToString());\n            }\n        }\n    }\n}\n<\/code><\/pre>\n<h3>Code Explanation<\/h3>\n<p>The above code implements the Bellman-Ford algorithm. Let me explain the main parts of the code.<\/p>\n<ul>\n<li><code>List&lt;Tuple&lt;int, int, int&gt;&gt; edges<\/code>: A list to store edge information.<\/li>\n<li><code>int[] distance<\/code>: Stores the shortest distance to each vertex. The initial value is set to infinity, and the starting vertex's distance is initialized to 0.<\/li>\n<li>In the core loop of the Bellman-Ford algorithm, distances are updated by checking each edge.<\/li>\n<li>At the end, edges are checked once more to verify if a negative cycle exists.<\/li>\n<\/ul>\n<h2>Conclusion<\/h2>\n<p>In this lecture, we examined the concept of the Bellman-Ford algorithm and the process of solving a problem using it. By deeply understanding the principles of the algorithm and implementing it in code, you can enhance your ability to apply it in coding tests.<\/p>\n<p>The Bellman-Ford algorithm is also used in various practical shortest path problems, so be sure to understand it thoroughly and practice to prepare for coding tests.<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>As you often solve algorithm problems, you will encounter various shortest path problems. Among them, the &#8220;Bellman-Ford algorithm&#8221; is a useful algorithm for finding the shortest path in a graph that includes edges with negative weights. In this lecture, we will gain a deep understanding of the Bellman-Ford algorithm and solve a problem using it. &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34034\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;C# Coding Test Course, Bellman-Ford&#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-34034","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, Bellman-Ford - \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\/34034\/\" \/>\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, Bellman-Ford - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"As you often solve algorithm problems, you will encounter various shortest path problems. Among them, the &#8220;Bellman-Ford algorithm&#8221; is a useful algorithm for finding the shortest path in a graph that includes edges with negative weights. In this lecture, we will gain a deep understanding of the Bellman-Ford algorithm and solve a problem using it. &hellip; \ub354 \ubcf4\uae30 &quot;C# Coding Test Course, Bellman-Ford&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34034\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:23:20+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T10:53:55+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\/34034\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34034\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"C# Coding Test Course, Bellman-Ford\",\"datePublished\":\"2024-11-01T09:23:20+00:00\",\"dateModified\":\"2024-11-01T10:53:55+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34034\/\"},\"wordCount\":436,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"C# Coding Test Tutorials\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34034\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34034\/\",\"name\":\"C# Coding Test Course, Bellman-Ford - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:23:20+00:00\",\"dateModified\":\"2024-11-01T10:53:55+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34034\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34034\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34034\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C# Coding Test Course, Bellman-Ford\"}]},{\"@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, Bellman-Ford - \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\/34034\/","og_locale":"ko_KR","og_type":"article","og_title":"C# Coding Test Course, Bellman-Ford - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"As you often solve algorithm problems, you will encounter various shortest path problems. Among them, the &#8220;Bellman-Ford algorithm&#8221; is a useful algorithm for finding the shortest path in a graph that includes edges with negative weights. In this lecture, we will gain a deep understanding of the Bellman-Ford algorithm and solve a problem using it. &hellip; \ub354 \ubcf4\uae30 \"C# Coding Test Course, Bellman-Ford\"","og_url":"https:\/\/atmokpo.com\/w\/34034\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:23:20+00:00","article_modified_time":"2024-11-01T10:53:55+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\/34034\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34034\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"C# Coding Test Course, Bellman-Ford","datePublished":"2024-11-01T09:23:20+00:00","dateModified":"2024-11-01T10:53:55+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34034\/"},"wordCount":436,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["C# Coding Test Tutorials"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34034\/","url":"https:\/\/atmokpo.com\/w\/34034\/","name":"C# Coding Test Course, Bellman-Ford - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:23:20+00:00","dateModified":"2024-11-01T10:53:55+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34034\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34034\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34034\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"C# Coding Test Course, Bellman-Ford"}]},{"@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\/34034","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=34034"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34034\/revisions"}],"predecessor-version":[{"id":34035,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34034\/revisions\/34035"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34034"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34034"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34034"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}