{"id":34884,"date":"2024-11-01T09:33:07","date_gmt":"2024-11-01T09:33:07","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34884"},"modified":"2024-11-01T11:26:07","modified_gmt":"2024-11-01T11:26:07","slug":"swift-coding-test-course-kevin-bacons-6-degrees-of-separation","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34884\/","title":{"rendered":"Swift Coding Test Course, Kevin Bacon&#8217;s 6 Degrees of Separation"},"content":{"rendered":"<p><body><\/p>\n<h2>Problem Description<\/h2>\n<p>\n        &#8220;The Six Degrees of Kevin Bacon&#8221; is a theory that all people can be connected to the actor Kevin Bacon through a maximum of six degrees of separation.<br \/>\n        In this problem, based on a series of movie lists and information about the actors who starred in those movies, you need to calculate the degree of connection between each actor and Kevin Bacon.<br \/>\n        You are required to calculate the shortest distance to Kevin Bacon for each actor from the given movie and actor information,<br \/>\n        and find the actor with the shortest distance.\n    <\/p>\n<h3>Problem Definition<\/h3>\n<p>\n        The input provided is as follows.<br \/>\n        &#8211; The first line contains the number of actors N (1 \u2264 N \u2264 100) and the number of movies M (1 \u2264 M \u2264 1000).<br \/>\n        &#8211; Following this, M lines provide a list of actors appearing in each movie, separated by spaces.<br \/>\n        Each line lists the names of the actors appearing in a movie.<\/p>\n<p>        The output should be the number of the actor closest to Kevin Bacon and that distance.\n    <\/p>\n<h2>Approach to the Problem<\/h2>\n<p>\n        To solve the problem, we will use a graph traversal algorithm.<br \/>\n        Each actor will be a node, and if two actors appeared in the same movie, we connect them with an edge.<br \/>\n        Then, we will use BFS (Breadth-First Search) to calculate the shortest path to Kevin Bacon.<br \/>\n        The algorithm proceeds in the following order.\n    <\/p>\n<ol>\n<li>Create the graph. Define the relationships based on the movies the actors appeared in, treating each actor as a vertex.<\/li>\n<li>Use BFS to calculate the distance to Kevin Bacon.<\/li>\n<li>Compare the distances for all actors to find the shortest distance and the corresponding actor.<\/li>\n<\/ol>\n<h2>Example Input<\/h2>\n<pre>\n    5 3\n    A B C\n    B D\n    C D E\n    <\/pre>\n<h2>Example Output<\/h2>\n<pre>\n    1 2\n    <\/pre>\n<h3>Code Implementation<\/h3>\n<p>\n        Here is the algorithm implemented in Swift.<br \/>\n        It creates a graph and measures the distance from each actor to Kevin Bacon using BFS.\n    <\/p>\n<pre>\n        import Foundation\n\n        func findKevinBaconNumber(N: Int, M: Int, movies: [[String]]) -> (Int, Int) {\n            var graph = [String: [String]]()\n            var distance = [String: Int]()\n            \n            \/\/ Create the graph\n            for movie in movies {\n                for i in 0..<movie.count {\n                    for j in 0..<movie.count {\n                        if i != j {\n                            graph[movie[i], default: []].append(movie[j])\n                            graph[movie[j], default: []].append(movie[i])\n                        }\n                    }\n                }\n            }\n            \n            for actor in graph.keys {\n                distance[actor] = Int.max\n            }\n            \n            \/\/ BFS function\n            func bfs(start: String) {\n                var queue = [start]\n                var visited = Set<String>()\n                var dist = 0\n                \n                while !queue.isEmpty {\n                    let size = queue.count\n                    for _ in 0..<size {\n                        let actor = queue.removeFirst()\n                        if visited.contains(actor) { continue }\n                        visited.insert(actor)\n                        \n                        for neighbor in graph[actor, default: []] {\n                            if !visited.contains(neighbor) {\n                                queue.append(neighbor)\n                                distance[neighbor] = min(distance[neighbor]!, dist + 1)\n                            }\n                        }\n                    }\n                    dist += 1\n                }\n            }\n            \n            for actor in graph.keys {\n                bfs(start: actor)\n            }\n            \n            let result = distance.min { $0.value < $1.value } ?? (String(Int.max), Int.max)\n            return (Int(result.key) ?? 0, result.value)\n        }\n\n    <\/pre>\n<h2>Conclusion<\/h2>\n<p>\n        Today we covered an algorithm problem themed around \"The Six Degrees of Kevin Bacon\".<br \/>\n        Through this problem, we learned how to utilize graph theory and BFS.<br \/>\n        I hope this has been helpful in preparing for coding tests.<br \/>\n        We plan to continue with more problems and solutions, so please stay tuned.\n    <\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Description &#8220;The Six Degrees of Kevin Bacon&#8221; is a theory that all people can be connected to the actor Kevin Bacon through a maximum of six degrees of separation. In this problem, based on a series of movie lists and information about the actors who starred in those movies, you need to calculate the &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34884\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Swift Coding Test Course, Kevin Bacon&#8217;s 6 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":[129],"tags":[],"class_list":["post-34884","post","type-post","status-publish","format-standard","hentry","category-swift-coding-test"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Swift Coding Test Course, Kevin Bacon&#039;s 6 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\/34884\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Swift Coding Test Course, Kevin Bacon&#039;s 6 Degrees of Separation - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Problem Description &#8220;The Six Degrees of Kevin Bacon&#8221; is a theory that all people can be connected to the actor Kevin Bacon through a maximum of six degrees of separation. In this problem, based on a series of movie lists and information about the actors who starred in those movies, you need to calculate the &hellip; \ub354 \ubcf4\uae30 &quot;Swift Coding Test Course, Kevin Bacon&#8217;s 6 Degrees of Separation&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34884\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:33:07+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:26:07+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\/34884\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34884\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Swift Coding Test Course, Kevin Bacon&#8217;s 6 Degrees of Separation\",\"datePublished\":\"2024-11-01T09:33:07+00:00\",\"dateModified\":\"2024-11-01T11:26:07+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34884\/\"},\"wordCount\":356,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Swift Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34884\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34884\/\",\"name\":\"Swift Coding Test Course, Kevin Bacon's 6 Degrees of Separation - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:33:07+00:00\",\"dateModified\":\"2024-11-01T11:26:07+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34884\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34884\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34884\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Swift Coding Test Course, Kevin Bacon&#8217;s 6 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":"Swift Coding Test Course, Kevin Bacon's 6 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\/34884\/","og_locale":"ko_KR","og_type":"article","og_title":"Swift Coding Test Course, Kevin Bacon's 6 Degrees of Separation - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Problem Description &#8220;The Six Degrees of Kevin Bacon&#8221; is a theory that all people can be connected to the actor Kevin Bacon through a maximum of six degrees of separation. In this problem, based on a series of movie lists and information about the actors who starred in those movies, you need to calculate the &hellip; \ub354 \ubcf4\uae30 \"Swift Coding Test Course, Kevin Bacon&#8217;s 6 Degrees of Separation\"","og_url":"https:\/\/atmokpo.com\/w\/34884\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:33:07+00:00","article_modified_time":"2024-11-01T11:26:07+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\/34884\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34884\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Swift Coding Test Course, Kevin Bacon&#8217;s 6 Degrees of Separation","datePublished":"2024-11-01T09:33:07+00:00","dateModified":"2024-11-01T11:26:07+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34884\/"},"wordCount":356,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Swift Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34884\/","url":"https:\/\/atmokpo.com\/w\/34884\/","name":"Swift Coding Test Course, Kevin Bacon's 6 Degrees of Separation - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:33:07+00:00","dateModified":"2024-11-01T11:26:07+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34884\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34884\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34884\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Swift Coding Test Course, Kevin Bacon&#8217;s 6 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\/34884","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=34884"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34884\/revisions"}],"predecessor-version":[{"id":34885,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34884\/revisions\/34885"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34884"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34884"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34884"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}