{"id":34690,"date":"2024-11-01T09:30:55","date_gmt":"2024-11-01T09:30:55","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34690"},"modified":"2024-11-01T11:26:57","modified_gmt":"2024-11-01T11:26:57","slug":"swift-coding-test-course-representation-of-graphs","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34690\/","title":{"rendered":"Swift Coding Test Course, Representation of Graphs"},"content":{"rendered":"<p><body><\/p>\n<div class=\"container\">\n<header>\n<\/header>\n<section>\n<h2>1. Introduction<\/h2>\n<p>Understanding algorithms and data structures is crucial in the field of software development. One of the problems that frequently appears in coding tests is related to graphs. A graph is a data structure composed of nodes (vertices) and edges, allowing data to be represented in various forms. In this course, we will deepen our understanding through algorithm problems related to graph representation.<\/p>\n<\/section>\n<section>\n<h2>2. Graph Representation<\/h2>\n<p>There are mainly two ways to represent a graph.<\/p>\n<ul>\n<li><strong>Adjacency Matrix:<\/strong> A two-dimensional array is used to represent the presence or absence of edges for all vertices in the graph. This method is useful when the number of vertices is small. The size of the array is O(V^2).<\/li>\n<li><strong>Adjacency List:<\/strong> This method stores the connected vertices as a list for each vertex. It is memory efficient and suitable for graphs with fewer edges. The time complexity of this method is O(V + E).<\/li>\n<\/ul>\n<\/section>\n<section>\n<h2>3. Problem Description<\/h2>\n<p>We will apply the graph representation methods through the following problem.<\/p>\n<h3>Problem: Friend Network<\/h3>\n<p>You are managing a network of N friends. Friend relationships are bidirectional, meaning if two friends are directly connected, they are friends with each other. Represent the friend relationship as a graph and write a program to check if two specific friends belong to the same friend group. A friend group includes all friends that are indirectly connected.<\/p>\n<p>Input Format:<\/p>\n<pre>\nN (number of friends)\nM (number of friend relationships)\nFor M lines, two friends A and B (1 \u2264 A, B \u2264 N) are given.\n            <\/pre>\n<p>Output Format:<\/p>\n<pre>\nPrint \"YES\" if A and B are in the same friend group, otherwise print \"NO\".\n            <\/pre>\n<\/section>\n<section>\n<h2>4. Problem Solving Process<\/h2>\n<h3>4.1. Graph Creation<\/h3>\n<p>First, we need to create a graph based on the given friend relationships. We will implement this using an adjacency list. Here is a simple Swift code for graph creation.<\/p>\n<pre><code>\nimport Foundation\n\n\/\/ Adjacency list for graph representation\nvar graph: [Int: [Int]] = [:]\n\n\/\/ Function to add friend relations\nfunc addFriendRelation(friendA: Int, friendB: Int) {\n    graph[friendA, default: []].append(friendB)\n    graph[friendB, default: []].append(friendA)\n}\n\n\/\/ Function to create graph\nfunc createGraph(N: Int, relations: [(Int, Int)]) {\n    for (A, B) in relations {\n        addFriendRelation(friendA: A, friendB: B)\n    }\n}\n            <\/code><\/pre>\n<p>The above code creates a dictionary where each friend is a key and the value is the list of connected friends.<\/p>\n<h3>4.2. Finding Friend Groups<\/h3>\n<p>Now we can use one of the graph traversal algorithms to check if two friends belong to the same group. We can use Depth-First Search (DFS) or Breadth-First Search (BFS), and we will choose DFS here. DFS allows us to explore all related friends.<\/p>\n<pre><code>\nfunc dfs(start: Int, visited: inout Set<Int>) {\n    visited.insert(start)\n    for friend in graph[start, default: []] {\n        if !visited.contains(friend) {\n            dfs(start: friend, visited: &visited)\n        }\n    }\n}\n\n\/\/ Function to check if two friends are in the same group\nfunc areInSameGroup(friendA: Int, friendB: Int) -> Bool {\n    var visited = Set<Int>()\n    dfs(start: friendA, visited: &visited)\n    return visited.contains(friendB)\n}\n            <\/code><\/pre>\n<h3>4.3. Entire Process<\/h3>\n<p>Now let&#8217;s integrate the entire process to solve the problem. Here is the code to read input, create the graph, and check the relationship of two friends.<\/p>\n<pre><code>\nlet N = Int(readLine()!)! \/\/ Number of friends\nlet M = Int(readLine()!)! \/\/ Number of friend relationships\n\nvar relations = [(Int, Int)]()\n\nfor _ in 0..<M {\n    let line = readLine()!.split(separator: \" \").map { Int($0)! }\n    relations.append((line[0], line[1]))\n}\ncreateGraph(N: N, relations: relations)\n\nlet checkLine = readLine()!.split(separator: \" \").map { Int($0)! }\nif areInSameGroup(friendA: checkLine[0], friendB: checkLine[1]) {\n    print(\"YES\")\n} else {\n    print(\"NO\")\n}\n            <\/code><\/pre>\n<p>The above code implements the logic to represent and verify friend relationships through the overall flow.<\/p>\n<\/section>\n<section>\n<h2>5. Conclusion<\/h2>\n<p>In this course, we explored how to represent graphs and how to use basic DFS algorithms to solve problems. Graphs are useful data structures that can be applied to various problems, and it is essential to become familiar with them through sufficient practice. Since such problems often appear in coding tests, I encourage you to improve your skills by solving problems multiple times.<\/p>\n<\/section>\n<footer>\n<p>\u00a9 2023 Swift Coding Test Course. All Rights Reserved.<\/p>\n<\/footer>\n<\/div>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Introduction Understanding algorithms and data structures is crucial in the field of software development. One of the problems that frequently appears in coding tests is related to graphs. A graph is a data structure composed of nodes (vertices) and edges, allowing data to be represented in various forms. In this course, we will deepen &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34690\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Swift Coding Test Course, Representation of Graphs&#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-34690","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, Representation of Graphs - \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\/34690\/\" \/>\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, Representation of Graphs - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"1. Introduction Understanding algorithms and data structures is crucial in the field of software development. One of the problems that frequently appears in coding tests is related to graphs. A graph is a data structure composed of nodes (vertices) and edges, allowing data to be represented in various forms. In this course, we will deepen &hellip; \ub354 \ubcf4\uae30 &quot;Swift Coding Test Course, Representation of Graphs&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34690\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:30:55+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:26:57+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\/34690\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34690\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Swift Coding Test Course, Representation of Graphs\",\"datePublished\":\"2024-11-01T09:30:55+00:00\",\"dateModified\":\"2024-11-01T11:26:57+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34690\/\"},\"wordCount\":454,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Swift Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34690\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34690\/\",\"name\":\"Swift Coding Test Course, Representation of Graphs - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:30:55+00:00\",\"dateModified\":\"2024-11-01T11:26:57+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34690\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34690\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34690\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Swift Coding Test Course, Representation of Graphs\"}]},{\"@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, Representation of Graphs - \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\/34690\/","og_locale":"ko_KR","og_type":"article","og_title":"Swift Coding Test Course, Representation of Graphs - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"1. Introduction Understanding algorithms and data structures is crucial in the field of software development. One of the problems that frequently appears in coding tests is related to graphs. A graph is a data structure composed of nodes (vertices) and edges, allowing data to be represented in various forms. In this course, we will deepen &hellip; \ub354 \ubcf4\uae30 \"Swift Coding Test Course, Representation of Graphs\"","og_url":"https:\/\/atmokpo.com\/w\/34690\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:30:55+00:00","article_modified_time":"2024-11-01T11:26:57+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\/34690\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34690\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Swift Coding Test Course, Representation of Graphs","datePublished":"2024-11-01T09:30:55+00:00","dateModified":"2024-11-01T11:26:57+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34690\/"},"wordCount":454,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Swift Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34690\/","url":"https:\/\/atmokpo.com\/w\/34690\/","name":"Swift Coding Test Course, Representation of Graphs - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:30:55+00:00","dateModified":"2024-11-01T11:26:57+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34690\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34690\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34690\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Swift Coding Test Course, Representation of Graphs"}]},{"@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\/34690","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=34690"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34690\/revisions"}],"predecessor-version":[{"id":34691,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34690\/revisions\/34691"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34690"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34690"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34690"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}