{"id":34342,"date":"2024-11-01T09:27:04","date_gmt":"2024-11-01T09:27:04","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34342"},"modified":"2024-11-01T10:57:37","modified_gmt":"2024-11-01T10:57:37","slug":"c-coding-test-course-kevin-bacons-six-degrees-of-separation-2","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34342\/","title":{"rendered":"C++ Coding Test Course, Kevin Bacon&#8217;s Six Degrees of Separation"},"content":{"rendered":"<p><body><\/p>\n<article>\n<header>\n<p>October 3, 2023<\/p>\n<p>Author: AI Writer<\/p>\n<\/header>\n<section>\n<h2>Introduction<\/h2>\n<p>The Kevin Bacon rule of six degrees states that any person is connected to Kevin Bacon within a maximum of six degrees. This is an intriguing concept that explains relationships in social networks and is a popular topic in various fields. In this article, we will transform this rule into an algorithmic problem and detail the solution process using C++.<\/p>\n<p>The problem we will tackle is &#8220;Finding the person who can reach Kevin Bacon the fastest.&#8221; Through research, we will calculate the degree of connection to Kevin Bacon for each person when given the friendship relations among several people.<\/p>\n<\/section>\n<section>\n<h2>Problem Definition<\/h2>\n<p>The objective is to calculate each person&#8217;s Kevin Bacon number within a given network and find the person with the lowest number. To do this, we need to input the number of people and their friendship relations.<\/p>\n<h3>Problem Description<\/h3>\n<p>The input is provided in the following format:<\/p>\n<ul>\n<li>The first line contains the number of people N (1 \u2264 N \u2264 100)<\/li>\n<li>From the second line onward, each line consists of two integers A, B representing the friendship relation (A and B are friends).<\/li>\n<\/ul>\n<p>The output should be the number of the person closest to Kevin Bacon. If there are multiple, output the smallest number.<\/p>\n<\/section>\n<section>\n<h2>Problem Solving Approach<\/h2>\n<p>To solve this problem, we will use graph traversal techniques. We will treat people as vertices and friendships as edges, applying BFS (Breadth-First Search). We will calculate the shortest distance from each person to Kevin Bacon and find the minimum among them.<\/p>\n<h3>Algorithm<\/h3>\n<ol>\n<li>Construct the graph as an adjacency list based on the input.<\/li>\n<li>Calculate each person&#8217;s Kevin Bacon number using BFS.<\/li>\n<li>Compare the calculated Kevin Bacon numbers to find the minimum.<\/li>\n<\/ol>\n<\/section>\n<section>\n<h2>C++ Implementation<\/h2>\n<p>Now, let\u2019s implement the C++ code based on the algorithm above.<\/p>\n<pre><code>\n#include &lt;iostream&gt;\n#include &lt;vector&gt;\n#include &lt;queue&gt;\n#include &lt;limits&gt; \nusing namespace std;\n\nint main() {\n    int N, M;\n    cin &gt;&gt; N &gt;&gt; M; \/\/ Number of people N, number of friendship relations M\n    vector&lt;vector&lt;int&gt;&gt; graph(N + 1); \/\/ Adjacency list\n\n    for (int i = 0; i &lt; M; i++) {\n        int A, B;\n        cin &gt;&gt; A &gt;&gt; B; \/\/ Friendship relation A, B\n        graph[A].push_back(B);\n        graph[B].push_back(A); \/\/ Undirected graph\n    }\n\n    int min_bacon = numeric_limits&lt;int&gt;::max();\n    int answer = 0;\n\n    for (int i = 1; i &lt;= N; i++) {\n        vector&lt;int&gt; distance(N + 1, -1); \/\/ Initialize distances for each person\n        queue&lt;int&gt; q;\n        distance[i] = 0; \/\/ Distance from self is 0\n        q.push(i); \/\/ Add self to the queue\n\n        while (!q.empty()) {\n            int current = q.front();\n            q.pop();\n            for (int neighbor : graph[current]) {\n                if (distance[neighbor] == -1) {\n                    distance[neighbor] = distance[current] + 1; \/\/ Calculate distance\n                    q.push(neighbor); \/\/ Add friend to explore\n                }\n            }\n        }\n\n        int bacon_count = 0;\n        for (int d : distance) {\n            if (d != -1) {\n                bacon_count += d; \/\/ Sum Kevins Bacon numbers\n            }\n        }\n\n        if (bacon_count &lt; min_bacon) {\n            min_bacon = bacon_count; \/\/ Update minimum\n            answer = i; \/\/ Record person number\n        }\n    }\n\n    cout &lt;&lt; answer &lt;&lt; endl; \/\/ Output result\n    return 0;\n}\n            <\/code><\/pre>\n<\/section>\n<section>\n<h2>Code Explanation<\/h2>\n<p>The above C++ code includes the following key steps:<\/p>\n<ol>\n<li><strong>Input Processing:<\/strong> Read N and M, and store friendship relations in an adjacency list format.<\/li>\n<li><strong>BFS Implementation:<\/strong> Perform BFS for each person to calculate distances.<\/li>\n<li><strong>Result Calculation:<\/strong> Update the Kevin Bacon number based on calculated distances and ultimately find the minimum.<\/li>\n<\/ol>\n<\/section>\n<section>\n<h2>Complexity Analysis<\/h2>\n<p>The time complexity of this algorithm is O(N + M). This is because each person explores their friends through BFS, and each edge is explored only once. This is sufficiently efficient for the input range of the given problem.<\/p>\n<\/section>\n<section>\n<h2>Conclusion<\/h2>\n<p>In this lecture, we explored how to solve an algorithm problem utilizing the Kevin Bacon rule of six degrees in C++. We analyzed relationships within a network using graph traversal techniques like BFS. Such types of problems frequently appear in coding tests, making it crucial to practice varied problems to enhance proficiency.<\/p>\n<p>Finally, the problem-solving process presented here will not only aid in preparation for coding tests but also help address various issues that may arise in actual programming more systematically.<\/p>\n<\/section>\n<footer>\n<p>We hope you found this lecture beneficial and look forward to more problem-solving in the future!<\/p>\n<\/footer>\n<\/article>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>October 3, 2023 Author: AI Writer Introduction The Kevin Bacon rule of six degrees states that any person is connected to Kevin Bacon within a maximum of six degrees. This is an intriguing concept that explains relationships in social networks and is a popular topic in various fields. In this article, we will transform this &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34342\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;C++ Coding Test Course, Kevin Bacon&#8217;s Six Degrees of Separation&#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-34342","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, Kevin Bacon&#039;s Six Degrees of Separation - \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\/34342\/\" \/>\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, Kevin Bacon&#039;s Six Degrees of Separation - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"October 3, 2023 Author: AI Writer Introduction The Kevin Bacon rule of six degrees states that any person is connected to Kevin Bacon within a maximum of six degrees. This is an intriguing concept that explains relationships in social networks and is a popular topic in various fields. In this article, we will transform this &hellip; \ub354 \ubcf4\uae30 &quot;C++ Coding Test Course, Kevin Bacon&#8217;s Six Degrees of Separation&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34342\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:27:04+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T10:57:37+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\/34342\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34342\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"C++ Coding Test Course, Kevin Bacon&#8217;s Six Degrees of Separation\",\"datePublished\":\"2024-11-01T09:27:04+00:00\",\"dateModified\":\"2024-11-01T10:57:37+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34342\/\"},\"wordCount\":499,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"C++ Coding Test Tutorials\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34342\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34342\/\",\"name\":\"C++ Coding Test Course, Kevin Bacon's Six Degrees of Separation - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:27:04+00:00\",\"dateModified\":\"2024-11-01T10:57:37+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34342\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34342\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34342\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ Coding Test Course, Kevin Bacon&#8217;s Six Degrees of Separation\"}]},{\"@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, Kevin Bacon's Six Degrees of Separation - \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\/34342\/","og_locale":"ko_KR","og_type":"article","og_title":"C++ Coding Test Course, Kevin Bacon's Six Degrees of Separation - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"October 3, 2023 Author: AI Writer Introduction The Kevin Bacon rule of six degrees states that any person is connected to Kevin Bacon within a maximum of six degrees. This is an intriguing concept that explains relationships in social networks and is a popular topic in various fields. In this article, we will transform this &hellip; \ub354 \ubcf4\uae30 \"C++ Coding Test Course, Kevin Bacon&#8217;s Six Degrees of Separation\"","og_url":"https:\/\/atmokpo.com\/w\/34342\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:27:04+00:00","article_modified_time":"2024-11-01T10:57:37+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\/34342\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34342\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"C++ Coding Test Course, Kevin Bacon&#8217;s Six Degrees of Separation","datePublished":"2024-11-01T09:27:04+00:00","dateModified":"2024-11-01T10:57:37+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34342\/"},"wordCount":499,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["C++ Coding Test Tutorials"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34342\/","url":"https:\/\/atmokpo.com\/w\/34342\/","name":"C++ Coding Test Course, Kevin Bacon's Six Degrees of Separation - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:27:04+00:00","dateModified":"2024-11-01T10:57:37+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34342\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34342\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34342\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"C++ Coding Test Course, Kevin Bacon&#8217;s Six Degrees of Separation"}]},{"@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\/34342","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=34342"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34342\/revisions"}],"predecessor-version":[{"id":34343,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34342\/revisions\/34343"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34342"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34342"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34342"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}