{"id":34268,"date":"2024-11-01T09:26:13","date_gmt":"2024-11-01T09:26:13","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34268"},"modified":"2024-11-01T10:57:55","modified_gmt":"2024-11-01T10:57:55","slug":"c-coding-test-course-solving-the-traveling-salesman-problem","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34268\/","title":{"rendered":"C++ Coding Test Course, Solving the Traveling Salesman Problem"},"content":{"rendered":"<p><body><\/p>\n<article>\n<header>\n<p>Author: [Author Name]<\/p>\n<p>Publication Date: [Publication Date]<\/p>\n<\/header>\n<section>\n<h2>Problem Definition<\/h2>\n<p>\n                The &#8220;Traveling Salesman Problem&#8221; is the problem of finding the shortest path that visits all given cities and returns to the starting point.<br \/>\n                This problem is significant in computer science and optimization theory and can be applied to various real-world problems.<br \/>\n                The Traveling Salesman Problem is known to be NP-complete, and we will explore methods to solve it through effective algorithms.\n            <\/p>\n<\/section>\n<section>\n<h2>Problem Description<\/h2>\n<pre>\n                There are N given cities, and the distances between each pair of cities are provided. The salesman must \n                visit each city exactly once and return to the starting city. \n                Find the path that visits all cities at the minimum cost.\n                \n                Input:\n                - First line: Number of cities N (1 \u2264 N \u2264 20)\n                - Second line: Distance information in the form of an N x N matrix. \n                  (The i-th row and j-th column of the matrix represents the distance from city i to city j)\n                \n                Output:\n                - Minimum cost\n            <\/pre>\n<\/section>\n<section>\n<h2>Problem Approach<\/h2>\n<p>\n                To solve this problem, we will use Dynamic Programming and Bit Masking techniques.<br \/>\n                Dynamic Programming will help us solve subproblems, and Bit Masking will allow us to manage the visiting status of cities.<br \/>\n                When there are N cities, the visiting status of each city can be represented by bits.<br \/>\n                For example, with N=4, 0000 represents a state where no city has been visited, and 1111 represents a state where all cities have been visited.<br \/>\n                This allows us to utilize previously computed values to calculate the minimum cost for each subproblem.\n            <\/p>\n<\/section>\n<section>\n<h2>Algorithm Explanation<\/h2>\n<p>\n                1. **Bit Masking Setup**:<br \/>\n                   Each city&#8217;s state is represented by bits. For example, with 4 cities, we can represent from 0b0000 to 0b1111.<br \/>\n                   This bitmask can be used to track the visited cities.<br \/>\n                <br \/>\n                2. **Recursive Call**:<br \/>\n                   Takes the current city and the bitmask of visited cities as arguments and calculates the minimum cost to visit all cities.<br \/>\n                <br \/>\n                3. **Memoization**:<br \/>\n                   Stores previously calculated results to reduce redundant calculations. The state is stored as `(current city, visited cities bitmask)`.<br \/>\n                <br \/>\n                4. **Final Path Calculation**:<br \/>\n                   When all cities have been visited, adds the cost to return to the starting city and returns the minimum path.\n            <\/p>\n<\/section>\n<section>\n<h2>C++ Code Implementation<\/h2>\n<pre>\n                #include <iostream>\n                #include <vector>\n                #include <cstring>\n                #include <climits>\n\n                using namespace std;\n\n                int N; \/\/ Number of cities\n                int dist[20][20]; \/\/ Distance matrix\n                int dp[1 &lt;&lt; 20][20]; \/\/ Memoization\n\n                int tsp(int mask, int pos) {\n                    \/\/ If all cities have been visited, return to start city\n                    if (mask == (1 &lt;&lt; N) - 1) {\n                        return dist[pos][0]; \/\/ Distance to starting city\n                    }\n\n                    \/\/ Return immediately if the result is already calculated\n                    if (dp[mask][pos] != -1) {\n                        return dp[mask][pos];\n                    }\n\n                    int ans = INT_MAX; \/\/ Initialize to infinity\n                    for (int city = 0; city &lt; N; city++) {\n                        \/\/ If the city has not been visited, move to the next city\n                        if ((mask &amp; (1 &lt;&lt; city)) == 0) {\n                            int newAns = dist[pos][city] + tsp(mask | (1 &lt;&lt; city), city);\n                            ans = min(ans, newAns);\n                        }\n                    }\n\n                    return dp[mask][pos] = ans; \/\/ Save the result\n                }\n\n                int main() {\n                    \/\/ Input the number of cities\n                    cout &lt;&lt; \"Enter the number of cities: \";\n                    cin &gt;&gt; N;\n\n                    \/\/ Input distance matrix\n                    cout &lt;&lt; \"Enter the distance matrix:\" &lt;&lt; endl;\n                    for (int i = 0; i &lt; N; i++) {\n                        for (int j = 0; j &lt; N; j++) {\n                            cin &gt;&gt; dist[i][j];\n                        }\n                    }\n\n                    \/\/ Initialize the memoization array\n                    memset(dp, -1, sizeof(dp));\n\n                    \/\/ Calculate and output the minimum cost\n                    int result = tsp(1, 0); \/\/ Start by visiting the first city\n                    cout &lt;&lt; \"Minimum cost: \" &lt;&lt; result &lt;&lt; endl;\n\n                    return 0;\n                }\n            <\/climits><\/cstring><\/vector><\/iostream><\/pre>\n<\/section>\n<section>\n<h2>Conclusion<\/h2>\n<p>\n                The Traveling Salesman Problem is one of the important examples in algorithms and data structures,<br \/>\n                and can be effectively solved using Dynamic Programming techniques.<br \/>\n                Through this problem, we can understand how Bit Masking and Memoization combine to provide a powerful solution.<br \/>\n                As this problem frequently appears in coding interviews, it is crucial to enhance proficiency through ample practice.\n            <\/p>\n<\/section>\n<\/article>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Author: [Author Name] Publication Date: [Publication Date] Problem Definition The &#8220;Traveling Salesman Problem&#8221; is the problem of finding the shortest path that visits all given cities and returns to the starting point. This problem is significant in computer science and optimization theory and can be applied to various real-world problems. The Traveling Salesman Problem is &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34268\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;C++ Coding Test Course, Solving the Traveling Salesman Problem&#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-34268","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, Solving the Traveling Salesman Problem - \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\/34268\/\" \/>\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, Solving the Traveling Salesman Problem - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Author: [Author Name] Publication Date: [Publication Date] Problem Definition The &#8220;Traveling Salesman Problem&#8221; is the problem of finding the shortest path that visits all given cities and returns to the starting point. This problem is significant in computer science and optimization theory and can be applied to various real-world problems. The Traveling Salesman Problem is &hellip; \ub354 \ubcf4\uae30 &quot;C++ Coding Test Course, Solving the Traveling Salesman Problem&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34268\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:26:13+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T10:57: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=\"3\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/34268\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34268\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"C++ Coding Test Course, Solving the Traveling Salesman Problem\",\"datePublished\":\"2024-11-01T09:26:13+00:00\",\"dateModified\":\"2024-11-01T10:57:55+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34268\/\"},\"wordCount\":332,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"C++ Coding Test Tutorials\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34268\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34268\/\",\"name\":\"C++ Coding Test Course, Solving the Traveling Salesman Problem - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:26:13+00:00\",\"dateModified\":\"2024-11-01T10:57:55+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34268\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34268\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34268\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ Coding Test Course, Solving the Traveling Salesman Problem\"}]},{\"@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, Solving the Traveling Salesman Problem - \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\/34268\/","og_locale":"ko_KR","og_type":"article","og_title":"C++ Coding Test Course, Solving the Traveling Salesman Problem - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Author: [Author Name] Publication Date: [Publication Date] Problem Definition The &#8220;Traveling Salesman Problem&#8221; is the problem of finding the shortest path that visits all given cities and returns to the starting point. This problem is significant in computer science and optimization theory and can be applied to various real-world problems. The Traveling Salesman Problem is &hellip; \ub354 \ubcf4\uae30 \"C++ Coding Test Course, Solving the Traveling Salesman Problem\"","og_url":"https:\/\/atmokpo.com\/w\/34268\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:26:13+00:00","article_modified_time":"2024-11-01T10:57: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":"3\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/34268\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34268\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"C++ Coding Test Course, Solving the Traveling Salesman Problem","datePublished":"2024-11-01T09:26:13+00:00","dateModified":"2024-11-01T10:57:55+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34268\/"},"wordCount":332,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["C++ Coding Test Tutorials"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34268\/","url":"https:\/\/atmokpo.com\/w\/34268\/","name":"C++ Coding Test Course, Solving the Traveling Salesman Problem - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:26:13+00:00","dateModified":"2024-11-01T10:57:55+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34268\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34268\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34268\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"C++ Coding Test Course, Solving the Traveling Salesman Problem"}]},{"@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\/34268","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=34268"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34268\/revisions"}],"predecessor-version":[{"id":34269,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34268\/revisions\/34269"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34268"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34268"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34268"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}