{"id":33794,"date":"2024-11-01T09:20:28","date_gmt":"2024-11-01T09:20:28","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=33794"},"modified":"2024-11-01T12:26:44","modified_gmt":"2024-11-01T12:26:44","slug":"%ed%8c%8c%ec%9d%b4%ec%8d%ac-%ec%bd%94%eb%94%a9-%ed%85%8c%ec%8a%a4%ed%8a%b8-%ea%b0%95%ec%a2%8c-%ec%bc%80%eb%b9%88-%eb%b2%a0%ec%9d%b4%ec%bb%a8%ec%9d%98-6%eb%8b%a8%ea%b3%84-%eb%b2%95%ec%b9%99","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/33794\/","title":{"rendered":"Python coding test course, Six Degrees of Kevin Bacon"},"content":{"rendered":"<p>Hello! Today we will learn about one of the frequently asked algorithm problems in coding tests called the &#8220;Kevin Bacon&#8217;s Six Degrees of Separation.&#8221; This problem can be solved by utilizing various concepts from graph theory and offers a great opportunity to practice basic search algorithms like Breadth-First Search (BFS) and Depth-First Search (DFS).<\/p>\n<h2>1. Problem Description<\/h2>\n<p>Kevin Bacon&#8217;s Six Degrees of Separation is a famous social network theory stating that there is a connection through relationships between any two people within six steps. This is a problem to implement this theory in code.<\/p>\n<p>The problem is as follows:<\/p>\n<pre>\nGiven N users and the relationships between them,\ncalculate the Kevin Bacon score for each user,\nand output the user with the lowest score.\nThe score is the sum of the shortest distances to that user.\n<\/pre>\n<h2>2. Input Format<\/h2>\n<p>The first line contains the number of users N (1 \u2264 N \u2264 100) and the number of relationships M (0 \u2264 M \u2264 4,900).<\/p>\n<p>The next M lines contain two integers a and b, indicating that users a and b are friends with each other.<\/p>\n<h2>3. Output Format<\/h2>\n<p>Print the number of the user with the lowest Kevin Bacon score. In case of a tie, print the user with the smallest number.<\/p>\n<h2>4. Problem Solving Process<\/h2>\n<p>To solve this problem, follow these steps:<\/p>\n<h3>4.1. Graph Creation<\/h3>\n<p>First, create a graph representing each user&#8217;s friendships in the form of an adjacency list. This graph can be represented using a dictionary or list.<\/p>\n<pre>\ngraph = {i: [] for i in range(1, N + 1)}\nfor _ in range(M):\n    a, b = map(int, input().split())\n    graph[a].append(b)\n    graph[b].append(a)\n<\/pre>\n<h3>4.2. Implement BFS or DFS<\/h3>\n<p>For each user, calculate the shortest distance to other users using BFS or DFS. Since BFS guarantees the shortest path, it is more suitable for this problem.<\/p>\n<pre>\ndef bfs(start, graph):\n    queue = [start]\n    visited = {start: 0}\n    \n    while queue:\n        current = queue.pop(0)\n        \n        for neighbor in graph[current]:\n            if neighbor not in visited:\n                visited[neighbor] = visited[current] + 1\n                queue.append(neighbor)\n    return sum(visited.values())\n<\/pre>\n<h3>4.3. Score Calculation<\/h3>\n<p>Calculate the Kevin Bacon score for all users. Use the BFS results to store the scores and find the lowest score.<\/p>\n<pre>\nmin_score = float('inf')\nuser_with_min_score = -1\nfor user in range(1, N + 1):\n    score = bfs(user, graph)\n    if score < min_score:\n        min_score = score\n        user_with_min_score = user\n    elif score == min_score:\n        user_with_min_score = min(user_with_min_score, user)\n<\/pre>\n<h2>5. Full Code<\/h2>\n<p>Now, let's write the full code that integrates the above processes.<\/p>\n<pre>\nfrom collections import deque\n\ndef bfs(start, graph):\n    queue = deque([start])\n    visited = {start: 0}\n    \n    while queue:\n        current = queue.popleft()\n        \n        for neighbor in graph[current]:\n            if neighbor not in visited:\n                visited[neighbor] = visited[current] + 1\n                queue.append(neighbor)\n    return sum(visited.values())\n\ndef find_kevin_bacon(graph, n):\n    min_score = float('inf')\n    user_with_min_score = -1\n    \n    for user in range(1, n + 1):\n        score = bfs(user, graph)\n        if score < min_score:\n            min_score = score\n            user_with_min_score = user\n        elif score == min_score:\n            user_with_min_score = min(user_with_min_score, user)\n    \n    return user_with_min_score\n\n# Input\nN, M = map(int, input().split())\ngraph = {i: [] for i in range(1, N + 1)}\nfor _ in range(M):\n    a, b = map(int, input().split())\n    graph[a].append(b)\n    graph[b].append(a)\n\n# Output result\nresult = find_kevin_bacon(graph, N)\nprint(result)\n<\/pre>\n<h2>6. Code Explanation<\/h2>\n<p>The code takes input values for N and M, constructs a graph based on friendships, and then calculates the Kevin Bacon score for each user using BFS. Finally, it outputs the number of the user with the lowest score. This problem provides a great practice for understanding graph theory and the BFS algorithm.<\/p>\n<h2>7. Performance Analysis<\/h2>\n<p>The time complexity of this algorithm is O(V + E), where V is the number of vertices (users) and E is the number of edges (relationships). Assuming N is 100, the worst-case scenario could have 4900 relationships, resulting in approximately 495,000 searches during 100 BFS calls. This is within a range that can be sufficiently handled within the given time.<\/p>\n<h2>8. Conclusion<\/h2>\n<p>This problem utilizing Kevin Bacon's Six Degrees of Separation provides a good opportunity to solidify the fundamentals of graph theory and learn how to apply BFS. Practicing various variations of this problem can further enhance your algorithmic thinking. We wish you continued success in preparing for coding tests through diverse problem-solving!<\/p>\n<p>Thank you!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello! Today we will learn about one of the frequently asked algorithm problems in coding tests called the &#8220;Kevin Bacon&#8217;s Six Degrees of Separation.&#8221; This problem can be solved by utilizing various concepts from graph theory and offers a great opportunity to practice basic search algorithms like Breadth-First Search (BFS) and Depth-First Search (DFS). 1. &hellip; <a href=\"https:\/\/atmokpo.com\/w\/33794\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Python coding test course, Six Degrees of Kevin Bacon&#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":[145],"tags":[],"class_list":["post-33794","post","type-post","status-publish","format-standard","hentry","category-python-coding-test"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python coding test course, Six Degrees of Kevin Bacon - \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\/33794\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python coding test course, Six Degrees of Kevin Bacon - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Hello! Today we will learn about one of the frequently asked algorithm problems in coding tests called the &#8220;Kevin Bacon&#8217;s Six Degrees of Separation.&#8221; This problem can be solved by utilizing various concepts from graph theory and offers a great opportunity to practice basic search algorithms like Breadth-First Search (BFS) and Depth-First Search (DFS). 1. &hellip; \ub354 \ubcf4\uae30 &quot;Python coding test course, Six Degrees of Kevin Bacon&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/33794\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:20:28+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T12:26:44+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=\"4\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/33794\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33794\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Python coding test course, Six Degrees of Kevin Bacon\",\"datePublished\":\"2024-11-01T09:20:28+00:00\",\"dateModified\":\"2024-11-01T12:26:44+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33794\/\"},\"wordCount\":449,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Python Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/33794\/\",\"url\":\"https:\/\/atmokpo.com\/w\/33794\/\",\"name\":\"Python coding test course, Six Degrees of Kevin Bacon - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:20:28+00:00\",\"dateModified\":\"2024-11-01T12:26:44+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33794\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/33794\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/33794\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python coding test course, Six Degrees of Kevin Bacon\"}]},{\"@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":"Python coding test course, Six Degrees of Kevin Bacon - \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\/33794\/","og_locale":"ko_KR","og_type":"article","og_title":"Python coding test course, Six Degrees of Kevin Bacon - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Hello! Today we will learn about one of the frequently asked algorithm problems in coding tests called the &#8220;Kevin Bacon&#8217;s Six Degrees of Separation.&#8221; This problem can be solved by utilizing various concepts from graph theory and offers a great opportunity to practice basic search algorithms like Breadth-First Search (BFS) and Depth-First Search (DFS). 1. &hellip; \ub354 \ubcf4\uae30 \"Python coding test course, Six Degrees of Kevin Bacon\"","og_url":"https:\/\/atmokpo.com\/w\/33794\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:20:28+00:00","article_modified_time":"2024-11-01T12:26:44+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":"4\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/33794\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/33794\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Python coding test course, Six Degrees of Kevin Bacon","datePublished":"2024-11-01T09:20:28+00:00","dateModified":"2024-11-01T12:26:44+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/33794\/"},"wordCount":449,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Python Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/33794\/","url":"https:\/\/atmokpo.com\/w\/33794\/","name":"Python coding test course, Six Degrees of Kevin Bacon - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:20:28+00:00","dateModified":"2024-11-01T12:26:44+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/33794\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/33794\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/33794\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Python coding test course, Six Degrees of Kevin Bacon"}]},{"@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\/33794","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=33794"}],"version-history":[{"count":2,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33794\/revisions"}],"predecessor-version":[{"id":38049,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33794\/revisions\/38049"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=33794"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=33794"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=33794"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}