{"id":34962,"date":"2024-11-01T09:34:03","date_gmt":"2024-11-01T09:34:03","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34962"},"modified":"2024-11-01T11:45:34","modified_gmt":"2024-11-01T11:45:34","slug":"kotlin-coding-test-course-representation-of-graphs","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34962\/","title":{"rendered":"kotlin coding test course, representation of graphs"},"content":{"rendered":"<p><body><\/p>\n<p>Hello! Today, we will take a detailed look at how to represent graphs, which frequently appear in coding tests, using Kotlin. Graphs are an important data structure in various fields and have essential fundamental functions for solving a range of problems. In this article, we will explain how to represent a graph and how to use that representation to solve problems.<\/p>\n<h2>What is a Graph?<\/h2>\n<p>A graph is a data structure composed of nodes (vertices) and edges. A node represents an object, and an edge represents the relationship between nodes. Graphs can be classified into directed and undirected graphs based on directionality, and into weighted and unweighted graphs based on the presence of weights.<\/p>\n<h3>Ways to Represent a Graph<\/h3>\n<p>Graphs can be represented in various ways, with the two most common methods being the Adjacency Matrix and the Adjacency List.<\/p>\n<h4>1. Adjacency Matrix<\/h4>\n<p>An adjacency matrix is a method of representing a graph using a 2D array of size N x N. N is the number of nodes in the graph, and each element of the matrix indicates whether nodes are connected. For example, if node A is connected to node B, then matrix[A][B] will be 1.<\/p>\n<pre class=\"code\">\n    fun createAdjacencyMatrix(vertices: Int, edges: List<Pair<Int, Int>>): Array<Array<Int>> {\n        val matrix = Array(vertices) { Array(vertices) { 0 } }\n        for (edge in edges) {\n            val (from, to) = edge\n            matrix[from][to] = 1\n            matrix[to][from] = 1  \/\/ For undirected graphs\n        }\n        return matrix\n    }\n<\/pre>\n<h4>2. Adjacency List<\/h4>\n<p>An adjacency list is a method that uses a list to store connected nodes for each node. This method is memory efficient and advantageous for graphs with fewer nodes or edges.<\/p>\n<pre class=\"code\">\n    fun createAdjacencyList(vertices: Int, edges: List<Pair<Int, Int>>): List<MutableList<Int>> {\n        val list = MutableList(vertices) { mutableListOf<Int>() }\n        for (edge in edges) {\n            val (from, to) = edge\n            list[from].add(to)\n            list[to].add(from)  \/\/ For undirected graphs\n        }\n        return list\n    }\n<\/pre>\n<h2>Problem: Friend Relationship Network<\/h2>\n<p>The following is a problem that represents a graph of friendships.<\/p>\n<blockquote>\n<p>Problem: Given N people and their friendships, write a program to check whether a friendship exists between two people. Friend relationships are represented as undirected graphs. The numbers of the two people are given sequentially from 0 to N-1.<\/p>\n<\/blockquote>\n<h2>Problem-Solving Process<\/h2>\n<h3>1. Understand the Input Format<\/h3>\n<p>First, we need to understand the given data. The input is as follows:<\/p>\n<ul>\n<li>The first line contains the number of people N.<\/li>\n<li>The second line contains the number of friendships M.<\/li>\n<li>Subsequently, M lines contain two integers a and b representing each friendship.<\/li>\n<\/ul>\n<h3>2. Choose a Data Structure<\/h3>\n<p>To store the given friendships, we will use an adjacency list. This allows for easy exploration of friendships.<\/p>\n<h3>3. Create the Graph<\/h3>\n<pre class=\"code\">\n    fun main() {\n        val reader = BufferedReader(InputStreamReader(System.`in`))\n        val n = reader.readLine().toInt()\n        val m = reader.readLine().toInt()\n\n        val edges = mutableListOf<Pair<Int, Int>>()\n        for (i in 0 until m) {\n            val (a, b) = reader.readLine().split(\" \").map { it.toInt() }\n            edges.add(Pair(a, b))\n        }\n\n        val adjacencyList = createAdjacencyList(n, edges)\n    }\n<\/pre>\n<h3>4. Check Friendship<\/h3>\n<p>The method to check whether two specific people are friends is to use DFS (Depth First Search) or BFS (Breadth First Search) to verify connectivity. Here, we will implement it using DFS.<\/p>\n<pre class=\"code\">\n    fun isConnected(adjList: List<List<Int>>, start: Int, target: Int, visited: BooleanArray): Boolean {\n        if (start == target) return true\n        visited[start] = true\n        \n        for (neighbor in adjList[start]) {\n            if (!visited[neighbor]) {\n                if (isConnected(adjList, neighbor, target, visited)) {\n                    return true\n                }\n            }\n        }\n        return false\n    }\n\n    fun main() {\n        \/\/ Continuing from the previous code\n        val query = reader.readLine().split(\" \").map { it.toInt() }\n        val (x, y) = query\n\n        val visited = BooleanArray(n) { false }\n        val result = isConnected(adjacencyList, x, y, visited)\n        println(if (result) \"YES\" else \"NO\")\n    }\n<\/pre>\n<h2>Conclusion<\/h2>\n<p>In this lesson, we learned about the basic concepts and methods of representing graphs, and how to solve the friend relationship network problem using these representations. Graphs are essential for solving various problems, and these graph representation and traversal techniques are very useful in algorithm tests.<\/p>\n<p>Having implemented examples using Kotlin, I encourage you to practice by solving various graph problems yourself. Next time, we will discuss the differences between BFS and DFS, as well as various problem-solving strategies utilizing them. I hope this aids you in your learning journey!<\/p>\n<footer>\n<p>Author: [Your Name]<\/p>\n<p>Date: [Date]<\/p>\n<\/footer>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello! Today, we will take a detailed look at how to represent graphs, which frequently appear in coding tests, using Kotlin. Graphs are an important data structure in various fields and have essential fundamental functions for solving a range of problems. In this article, we will explain how to represent a graph and how to &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34962\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;kotlin 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":[106],"tags":[],"class_list":["post-34962","post","type-post","status-publish","format-standard","hentry","category----en"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>kotlin 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\/34962\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"kotlin coding test course, representation of graphs - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Hello! Today, we will take a detailed look at how to represent graphs, which frequently appear in coding tests, using Kotlin. Graphs are an important data structure in various fields and have essential fundamental functions for solving a range of problems. In this article, we will explain how to represent a graph and how to &hellip; \ub354 \ubcf4\uae30 &quot;kotlin coding test course, representation of graphs&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34962\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:34:03+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:45:34+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\/34962\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34962\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"kotlin coding test course, representation of graphs\",\"datePublished\":\"2024-11-01T09:34:03+00:00\",\"dateModified\":\"2024-11-01T11:45:34+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34962\/\"},\"wordCount\":494,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Kotlin coding test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34962\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34962\/\",\"name\":\"kotlin coding test course, representation of graphs - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:34:03+00:00\",\"dateModified\":\"2024-11-01T11:45:34+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34962\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34962\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34962\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"kotlin 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":"kotlin 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\/34962\/","og_locale":"ko_KR","og_type":"article","og_title":"kotlin coding test course, representation of graphs - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Hello! Today, we will take a detailed look at how to represent graphs, which frequently appear in coding tests, using Kotlin. Graphs are an important data structure in various fields and have essential fundamental functions for solving a range of problems. In this article, we will explain how to represent a graph and how to &hellip; \ub354 \ubcf4\uae30 \"kotlin coding test course, representation of graphs\"","og_url":"https:\/\/atmokpo.com\/w\/34962\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:34:03+00:00","article_modified_time":"2024-11-01T11:45:34+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\/34962\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34962\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"kotlin coding test course, representation of graphs","datePublished":"2024-11-01T09:34:03+00:00","dateModified":"2024-11-01T11:45:34+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34962\/"},"wordCount":494,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Kotlin coding test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34962\/","url":"https:\/\/atmokpo.com\/w\/34962\/","name":"kotlin coding test course, representation of graphs - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:34:03+00:00","dateModified":"2024-11-01T11:45:34+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34962\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34962\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34962\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"kotlin 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\/34962","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=34962"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34962\/revisions"}],"predecessor-version":[{"id":34963,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34962\/revisions\/34963"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34962"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34962"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34962"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}