{"id":33450,"date":"2024-11-01T09:16:41","date_gmt":"2024-11-01T09:16:41","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=33450"},"modified":"2024-11-01T11:38:38","modified_gmt":"2024-11-01T11:38:38","slug":"java-coding-test-course-topological-sorting","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/33450\/","title":{"rendered":"Java Coding Test Course, Topological Sorting"},"content":{"rendered":"<p><body><\/p>\n<p>Hello! Today we will learn about <strong>Topological Sorting<\/strong>, which frequently appears in coding tests using Java. Topological sorting is an algorithm used in Directed Acyclic Graphs (DAGs) that is very useful for determining the order of tasks considering the precedence relations between them.<\/p>\n<h2>1. What is Topological Sorting?<\/h2>\n<p>Topological sorting refers to the arrangement of vertices in a directed graph such that every vertex appears after all its preceding vertices. Generally, topological sorting is used in the following cases:<\/p>\n<ul>\n<li>When determining the order of tasks in a project (e.g., Task B can only start after Task A is completed)<\/li>\n<li>When determining the order of classes in a learning process (e.g., a prerequisite course must be taken before enrolling in the next course)<\/li>\n<\/ul>\n<h2>2. Problem Description<\/h2>\n<p>Let&#8217;s solve the following problem:<\/p>\n<pre>\n    <!-- \n    Problem: Determine Class Order\n    Given N classes, write a program that determines the order of classes based on their precedence relations.\n    - Input: \n        - First line: N (1 \u2264 N \u2264 10^6)\n        - Second line: M (1 \u2264 M \u2264 10^6)\n        - Next M lines: A B (You can take class B only after taking class A)\n        \n    - Output: Print the order in which the classes can be taken. If there is no possible order, print 0.\n    -->\n    <\/pre>\n<h2>3. Topological Sort Algorithm<\/h2>\n<p>There are two methods to perform topological sorting: one utilizing &#8216;in-degree&#8217; and the other based on &#8216;DFS&#8217;. Here we will explain the in-degree based topological sorting method.<\/p>\n<h3>3.1. In-Degree Based Topological Sort<\/h3>\n<p>The in-degree based topological sorting consists of the following steps:<\/p>\n<ol>\n<li>Calculate the in-degree of all vertices in the graph.<\/li>\n<li>Add vertices with an in-degree of 0 to the queue.<\/li>\n<li>Remove vertices one by one from the queue and decrease the in-degree of all vertices connected to it by 1.<\/li>\n<li>Add vertices whose in-degree becomes 0 to the queue.<\/li>\n<li>Repeat the above steps until the queue is empty.<\/li>\n<\/ol>\n<h2>4. Java Code Implementation<\/h2>\n<p>Let&#8217;s implement the topological sorting algorithm in Java. Below is the Java code to solve the problem.<\/p>\n<pre><code>\nimport java.util.*;\n\npublic class TopologicalSort {\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        int N = scanner.nextInt(); \/\/ Number of classes\n        int M = scanner.nextInt(); \/\/ Number of relations\n\n        List<List<Integer>> graph = new ArrayList<>();\n        int[] inDegree = new int[N + 1];\n        \n        for (int i = 0; i <= N; i++) {\n            graph.add(new ArrayList<>());\n        }\n        \n        \/\/ Process graph input\n        for (int i = 0; i < M; i++) {\n            int A = scanner.nextInt();\n            int B = scanner.nextInt();\n            graph.get(A).add(B);\n            inDegree[B]++;\n        }\n        \n        \/\/ Add nodes with in-degree 0 to the queue\n        Queue<Integer> queue = new LinkedList<>();\n        for (int i = 1; i <= N; i++) {\n            if (inDegree[i] == 0) {\n                queue.add(i);\n            }\n        }\n\n        List<Integer> result = new ArrayList<>();\n        \n        while (!queue.isEmpty()) {\n            int current = queue.poll();\n            result.add(current);\n            for (int next : graph.get(current)) {\n                inDegree[next]--; \/\/ Decrease in-degree\n                if (inDegree[next] == 0) {\n                    queue.add(next);\n                }\n            }\n        }\n        \n        \/\/ Check if the order is determined\n        if (result.size() == N) {\n            for (int i : result) {\n                System.out.print(i + \" \");\n            }\n        } else {\n            System.out.println(0);\n        }\n        \n        scanner.close();\n    }\n}\n    <\/Integer><\/Integer><\/List<Integer><\/code><\/pre>\n<h2>5. Code Explanation<\/h2>\n<p>The above Java code works as follows:<\/p>\n<ol>\n<li>It uses a Scanner to read input and retrieves the number of classes N and the number of relations M.<\/li>\n<li>It initializes an array to store the graph and in-degrees. The graph information for each class is stored as a list.<\/li>\n<li>It receives M relations, adds the relations to the graph, and updates the in-degrees.<\/li>\n<li>It starts by adding nodes with an in-degree of 0 to the queue.<\/li>\n<li>It removes vertices from the queue, decreasing the in-degrees of all vertices connected to it by 1.<\/li>\n<li>If any vertex\u2019s in-degree becomes 0 during this process, it adds it to the queue.<\/li>\n<li>It handles exceptional situations and ultimately prints the classes in the possible order.<\/li>\n<\/ol>\n<h2>6. Example Test<\/h2>\n<p>Let&#8217;s test with the following input:<\/p>\n<pre><code>\n5 4\n1 2\n1 3\n2 4\n3 4\n    <\/code><\/pre>\n<p>This example includes 5 classes and 4 relations, so it can be executed appropriately. The correct output will be the possible order in which the classes can be taken.<\/p>\n<h2>7. Time Complexity<\/h2>\n<p>The time complexity of topological sorting is O(V + E), where V is the number of vertices (N) and E is the number of edges (M), so it operates efficiently even for large graphs.<\/p>\n<h2>8. Conclusion<\/h2>\n<p>Today we learned about the topological sorting algorithm with Java. This algorithm can be applied effectively in various fields and frequently appears in coding tests, so be sure to practice. Topological sorting will be a great help in solving complex problems!<\/p>\n<footer>\n<p>\u00a9 2023 Java Coding Test Course<\/p>\n<\/footer>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello! Today we will learn about Topological Sorting, which frequently appears in coding tests using Java. Topological sorting is an algorithm used in Directed Acyclic Graphs (DAGs) that is very useful for determining the order of tasks considering the precedence relations between them. 1. What is Topological Sorting? Topological sorting refers to the arrangement of &hellip; <a href=\"https:\/\/atmokpo.com\/w\/33450\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Java Coding Test Course, Topological Sorting&#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":[139],"tags":[],"class_list":["post-33450","post","type-post","status-publish","format-standard","hentry","category-java-coding-test"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Java Coding Test Course, Topological Sorting - \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\/33450\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java Coding Test Course, Topological Sorting - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Hello! Today we will learn about Topological Sorting, which frequently appears in coding tests using Java. Topological sorting is an algorithm used in Directed Acyclic Graphs (DAGs) that is very useful for determining the order of tasks considering the precedence relations between them. 1. What is Topological Sorting? Topological sorting refers to the arrangement of &hellip; \ub354 \ubcf4\uae30 &quot;Java Coding Test Course, Topological Sorting&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/33450\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:16:41+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:38:38+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=\"1\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/33450\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33450\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Java Coding Test Course, Topological Sorting\",\"datePublished\":\"2024-11-01T09:16:41+00:00\",\"dateModified\":\"2024-11-01T11:38:38+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33450\/\"},\"wordCount\":494,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Java Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/33450\/\",\"url\":\"https:\/\/atmokpo.com\/w\/33450\/\",\"name\":\"Java Coding Test Course, Topological Sorting - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:16:41+00:00\",\"dateModified\":\"2024-11-01T11:38:38+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33450\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/33450\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/33450\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java Coding Test Course, Topological Sorting\"}]},{\"@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":"Java Coding Test Course, Topological Sorting - \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\/33450\/","og_locale":"ko_KR","og_type":"article","og_title":"Java Coding Test Course, Topological Sorting - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Hello! Today we will learn about Topological Sorting, which frequently appears in coding tests using Java. Topological sorting is an algorithm used in Directed Acyclic Graphs (DAGs) that is very useful for determining the order of tasks considering the precedence relations between them. 1. What is Topological Sorting? Topological sorting refers to the arrangement of &hellip; \ub354 \ubcf4\uae30 \"Java Coding Test Course, Topological Sorting\"","og_url":"https:\/\/atmokpo.com\/w\/33450\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:16:41+00:00","article_modified_time":"2024-11-01T11:38:38+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":"1\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/33450\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/33450\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Java Coding Test Course, Topological Sorting","datePublished":"2024-11-01T09:16:41+00:00","dateModified":"2024-11-01T11:38:38+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/33450\/"},"wordCount":494,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Java Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/33450\/","url":"https:\/\/atmokpo.com\/w\/33450\/","name":"Java Coding Test Course, Topological Sorting - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:16:41+00:00","dateModified":"2024-11-01T11:38:38+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/33450\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/33450\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/33450\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Java Coding Test Course, Topological Sorting"}]},{"@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\/33450","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=33450"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33450\/revisions"}],"predecessor-version":[{"id":33451,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33450\/revisions\/33451"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=33450"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=33450"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=33450"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}