{"id":34494,"date":"2024-11-01T09:28:37","date_gmt":"2024-11-01T09:28:37","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34494"},"modified":"2024-11-01T11:41:02","modified_gmt":"2024-11-01T11:41:02","slug":"javascript-coding-test-course-topological-sort","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34494\/","title":{"rendered":"JavaScript Coding Test Course, Topological Sort"},"content":{"rendered":"<article>\n<p>\n        In modern software development environments, algorithms play a crucial role. Let&#8217;s take a look at topological sorting,<br \/>\n        which is one of the problems frequently encountered in coding tests. Topological sorting is a technique for<br \/>\n        ordering all nodes in a directed graph by considering the direction of edges. It is primarily used to<br \/>\n        express dependencies between tasks.\n<\/p>\n<h2>Problem Description<\/h2>\n<p>\n        Let&#8217;s explore a problem that receives input as follows. Given the precedence between the tasks,<br \/>\n        the problem is to output the order in which all tasks can be completed using topological sorting.\n<\/p>\n<p>\n<strong>Example Problem:<\/strong><br \/>\n        There are N tasks, and each task is identified by a number from 1 to N.<br \/>\n        M edges are given, which define the precedence between the tasks.<br \/>\n        Check if the given tasks can be processed through topological sorting and output that order.\n<\/p>\n<p>\n<strong>Input Example:<\/strong><br \/>\n        6 6<br \/>\n        6 5<br \/>\n        5 4<br \/>\n        4 3<br \/>\n        2 5<br \/>\n        3 1<br \/>\n        1 2<\/p>\n<p><strong>Output Example:<\/strong><br \/>\n        6 5 4 3 1 2\n<\/p>\n<h2>Problem Solving Process<\/h2>\n<h3>1. Understanding the Problem<\/h3>\n<p>\n        First, we need to understand what topological sorting is and what is required in this problem.<br \/>\n        Topological sorting is the process of ordering each node in a directed graph while respecting the direction of all edges.<br \/>\n        Based on the direction of the given edges, we can define the order in which each task should precede.<br \/>\n        A graph that allows for topological sorting must be acyclic (Directed Acyclic Graph, DAG).\n    <\/p>\n<h3>2. Approach to Solve the Problem<\/h3>\n<p>\n        The basic approach to solving the problem is as follows:\n    <\/p>\n<ol>\n<li>Represent the precedence between the given tasks as a graph.<\/li>\n<li>Calculate the indegree for each task.<\/li>\n<li>Add tasks with an indegree of 0 to a queue.<\/li>\n<li>Process tasks one by one from the queue and decrease the indegree of tasks connected to it.<br \/>\n            Tasks that become 0 indegree are added back to the queue.<\/li>\n<li>Repeat until all tasks are processed.<\/li>\n<li>Output the result of the topological sorting.<\/li>\n<\/ol>\n<h3>3. Implementation in JavaScript<\/h3>\n<p>\n        Now, let&#8217;s implement the JavaScript code according to the above steps.<br \/>\n        The code below performs topological sorting based on the given input.\n    <\/p>\n<pre>\n        <code>\n        function topologicalSort(N, edges) {\n            const graph = {};\n            const indegree = new Array(N + 1).fill(0);\n            const result = [];\n\n            \/\/ Create graph and initialize indegree\n            edges.forEach(([u, v]) =&gt; {\n                if (!graph[u]) graph[u] = [];\n                graph[u].push(v);\n                indegree[v]++;\n            });\n\n            const queue = [];\n            \n            \/\/ Add tasks with indegree of 0\n            for (let i = 1; i &lt;= N; i++) {\n                if (indegree[i] === 0) {\n                    queue.push(i);\n                }\n            }\n\n            while (queue.length &gt; 0) {\n                const node = queue.shift();\n                result.push(node);\n                \n                \/\/ Decrease indegree of connected nodes\n                if (graph[node]) {\n                    graph[node].forEach(neighbor =&gt; {\n                        indegree[neighbor]--;\n                        if (indegree[neighbor] === 0) {\n                            queue.push(neighbor);\n                        }\n                    });\n                }\n            }\n\n            \/\/ Check if topological sorting was possible.\n            if (result.length !== N) {\n                return \"A cycle exists.\";\n            }\n\n            return result;\n        }\n\n        \/\/ Input Example\n        const N = 6;\n        const edges = [\n            [6, 5],\n            [5, 4],\n            [4, 3],\n            [2, 5],\n            [3, 1],\n            [1, 2],\n        ];\n        console.log(topologicalSort(N, edges));\n        <\/code>\n    <\/pre>\n<h3>4. Code Explanation<\/h3>\n<p>\n        I will now explain how to implement topological sorting through the above code.\n    <\/p>\n<ul>\n<li>\n<strong>Graph Construction:<\/strong><br \/>\n            The graph is created in the form of an adjacency list based on the given list of edges.<br \/>\n            The indegree of each node is recorded, indicating how many edges depend on that node.\n        <\/li>\n<li>\n<strong>Finding Nodes with Indegree of 0:<\/strong><br \/>\n            Check all nodes and add those with an indegree of 0 to the queue.\n        <\/li>\n<li>\n<strong>Processing via BFS:<\/strong><br \/>\n            Process nodes one by one from the queue and reduce the indegree of connected nodes.<br \/>\n            If a node&#8217;s indegree becomes 0, add it to the queue.\n        <\/li>\n<li>\n<strong>Check the Length of the Result:<\/strong><br \/>\n            If all tasks are processed, the length of the result array should be the same as the number of nodes,<br \/>\n            indicating that topological sorting has been successfully performed.\n        <\/li>\n<\/ul>\n<h3>5. Conclusion and Lessons Learned<\/h3>\n<p>\n        Topological sorting is very useful when tasks need to be performed in a specific order based on dependencies.<br \/>\n        Through this tutorial, we learned the fundamental idea of topological sorting and how to implement it in JavaScript.<br \/>\n        Having opportunities to utilize various data structures and algorithms is essential for successful performance in coding tests.\n    <\/p>\n<p>\n        Since there are many scenarios in real problems that require topological sorting,<br \/>\n        it is important to understand the characteristics of each problem and solve it using the appropriate data structures and algorithms.<br \/>\n        Keep practicing various problems to improve your skills!\n    <\/p>\n<\/article>\n","protected":false},"excerpt":{"rendered":"<p>In modern software development environments, algorithms play a crucial role. Let&#8217;s take a look at topological sorting, which is one of the problems frequently encountered in coding tests. Topological sorting is a technique for ordering all nodes in a directed graph by considering the direction of edges. It is primarily used to express dependencies between &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34494\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;JavaScript Coding Test Course, Topological Sort&#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":[141],"tags":[],"class_list":["post-34494","post","type-post","status-publish","format-standard","hentry","category-javascript-coding-test"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>JavaScript Coding Test Course, Topological Sort - \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\/34494\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JavaScript Coding Test Course, Topological Sort - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"In modern software development environments, algorithms play a crucial role. Let&#8217;s take a look at topological sorting, which is one of the problems frequently encountered in coding tests. Topological sorting is a technique for ordering all nodes in a directed graph by considering the direction of edges. It is primarily used to express dependencies between &hellip; \ub354 \ubcf4\uae30 &quot;JavaScript Coding Test Course, Topological Sort&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34494\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:28:37+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:41:02+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\/34494\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34494\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"JavaScript Coding Test Course, Topological Sort\",\"datePublished\":\"2024-11-01T09:28:37+00:00\",\"dateModified\":\"2024-11-01T11:41:02+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34494\/\"},\"wordCount\":554,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Javascript Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34494\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34494\/\",\"name\":\"JavaScript Coding Test Course, Topological Sort - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:28:37+00:00\",\"dateModified\":\"2024-11-01T11:41:02+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34494\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34494\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34494\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript Coding Test Course, Topological Sort\"}]},{\"@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":"JavaScript Coding Test Course, Topological Sort - \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\/34494\/","og_locale":"ko_KR","og_type":"article","og_title":"JavaScript Coding Test Course, Topological Sort - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"In modern software development environments, algorithms play a crucial role. Let&#8217;s take a look at topological sorting, which is one of the problems frequently encountered in coding tests. Topological sorting is a technique for ordering all nodes in a directed graph by considering the direction of edges. It is primarily used to express dependencies between &hellip; \ub354 \ubcf4\uae30 \"JavaScript Coding Test Course, Topological Sort\"","og_url":"https:\/\/atmokpo.com\/w\/34494\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:28:37+00:00","article_modified_time":"2024-11-01T11:41:02+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\/34494\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34494\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"JavaScript Coding Test Course, Topological Sort","datePublished":"2024-11-01T09:28:37+00:00","dateModified":"2024-11-01T11:41:02+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34494\/"},"wordCount":554,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Javascript Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34494\/","url":"https:\/\/atmokpo.com\/w\/34494\/","name":"JavaScript Coding Test Course, Topological Sort - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:28:37+00:00","dateModified":"2024-11-01T11:41:02+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34494\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34494\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34494\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"JavaScript Coding Test Course, Topological Sort"}]},{"@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\/34494","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=34494"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34494\/revisions"}],"predecessor-version":[{"id":34495,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34494\/revisions\/34495"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34494"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34494"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34494"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}