{"id":33382,"date":"2024-11-01T09:16:02","date_gmt":"2024-11-01T09:16:02","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=33382"},"modified":"2024-11-01T11:38:56","modified_gmt":"2024-11-01T11:38:56","slug":"java-coding-test-course-finding-the-order-of-building","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/33382\/","title":{"rendered":"Java Coding Test Course, Finding the Order of Building"},"content":{"rendered":"<p><body><\/p>\n<article>\n<header>\n<p>\n                In this article, we will solve an algorithm problem to prepare for a Java coding test. The topic is &#8220;Finding the Order of Building Construction.&#8221; This problem combines graph traversal algorithms and sorting algorithms that can be utilized in various fields. I will explain the theoretical background and code implementation methods needed to solve the problem in detail.\n            <\/p>\n<\/header>\n<section>\n<h2>Problem Description<\/h2>\n<p>\n                You are planning to construct several buildings in a city. Each building has an order based on its dependencies with one another. In other words, if building A must be constructed before building B, then the dependency is represented as A -> B. You need to solve the problem of determining the construction order of all buildings according to these rules.\n            <\/p>\n<p>\n                Below is the number of buildings N and the dependency information given as input. You need to output the construction order of all buildings.<br \/>\n                For example, let&#8217;s assume the following dependencies are given:\n            <\/p>\n<pre>\n            5 4\n            2 1\n            3 1\n            4 2\n            5 3\n            <\/pre>\n<p>\n                This input indicates that there are 5 buildings and the following dependencies exist.<br \/>\n                Building 1 must be constructed after building 2, building 1 after building 3, building 2 after building 4, and building 3 after building 5.<br \/>\n                In this case, a possible construction order of the buildings could be:\n            <\/p>\n<pre>\n            1, 2, 3, 4, 5 or 1, 2, 3, 5, 4, etc.\n            <\/pre>\n<\/section>\n<section>\n<h2>Strategy for Problem Solving<\/h2>\n<p>\n                To solve this problem, we can utilize the concept of topological sorting from graph theory. Topological sorting is the act of arranging all vertices (nodes) in a directed graph according to the direction of the edges. In this case, if there is an edge A -> B, A must come before B.\n            <\/p>\n<p>\n                The main steps of the algorithm are as follows:\n            <\/p>\n<ol>\n<li>Implement the graph in the form of an adjacency list.<\/li>\n<li>Calculate the in-degree of each node.<\/li>\n<li>Add the nodes with an in-degree of 0 to a queue to start processing.<\/li>\n<li>While removing nodes from the queue, record the order and decrease the in-degree of the other connected nodes. If any node&#8217;s in-degree becomes 0, add it back to the queue.<\/li>\n<li>Repeat until all nodes are processed.<\/li>\n<\/ol>\n<\/section>\n<section>\n<h2>Java Implementation Code<\/h2>\n<p>\n                Based on the algorithm described above, let&#8217;s implement the code in Java. Below is the Java code that performs topological sorting.\n            <\/p>\n<pre>\n                <code>\npublic class BuildingOrder {\n    import java.util.*;\n\n    public static void main(String[] args) {\n        Scanner scanner = new Scanner(System.in);\n        \n        \/\/ Receiving input for buildings and dependencies\n        int N = scanner.nextInt(); \/\/ Number of buildings\n        int M = scanner.nextInt(); \/\/ Number of dependencies\n        \n        List<list<integer>&gt; graph = new ArrayList&lt;&gt;();\n        int[] inDegree = new int[N + 1]; \/\/ In-degree\n        \n        for (int i = 0; i &lt;= N; i++) {\n            graph.add(new ArrayList&lt;&gt;());\n        }\n        \n        for (int i = 0; i &lt; M; i++) {\n            int a = scanner.nextInt();\n            int b = scanner.nextInt();\n            graph.get(a).add(b);\n            inDegree[b]++;\n        }\n        \n        \/\/ Implementing the process to perform topological sorting\n        StringBuilder result = new StringBuilder();\n        Queue<integer> queue = new LinkedList&lt;&gt;();\n\n        \/\/ Adding nodes with in-degree of 0 to the queue\n        for (int i = 1; i &lt;= N; i++) {\n            if (inDegree[i] == 0) {\n                queue.offer(i);\n            }\n        }\n        \n        while (!queue.isEmpty()) {\n            int current = queue.poll();\n            result.append(current).append(\" \");\n\n            for (int neighbor : graph.get(current)) {\n                inDegree[neighbor]--;\n                if (inDegree[neighbor] == 0) {\n                    queue.offer(neighbor);\n                }\n            }\n        }\n\n        \/\/ Outputting the result\n        System.out.println(\"Construction order of buildings: \");\n        System.out.println(result.toString().trim());\n        scanner.close();\n    }\n}\n                <\/integer><\/list<integer><\/code>\n            <\/pre>\n<p>\n                The above code first receives the number of buildings and their dependencies, initializes the graph and in-degrees based on this input, and then determines the construction order through topological sorting and prints it.\n            <\/p>\n<\/section>\n<section>\n<h2>Input and Output Examples<\/h2>\n<h3>Example Input<\/h3>\n<pre>\n            5 4\n            2 1\n            3 1\n            4 2\n            5 3\n            <\/pre>\n<h3>Example Output<\/h3>\n<pre>\n            Construction order of buildings: \n            1 2 3 4 5 or 1 2 3 5 4\n            <\/pre>\n<\/section>\n<section>\n<h2>Advanced Learning<\/h2>\n<p>\n                The topological sorting algorithm can be used in many real-world situations. For example, it is useful for project schedule management, grammar parsing in compilers, and solving problems with precedence relationships among multiple tasks. Understanding and effectively utilizing such algorithms can greatly aid in problem-solving not only for coding tests but also across various fields.\n            <\/p>\n<p>\n                Practice Problems:<br \/>\n                <br \/>1. Use topological sorting to determine the construction order for the following graph.<br \/>\n                <br \/>2. Implement the following dependencies and visualize the graph to think about what issues might arise.\n            <\/p>\n<\/section>\n<footer>\n<p>\n                The problem of finding the order of building construction discussed in this article will enhance your understanding of Java programming and algorithms, and will be highly beneficial for applying this knowledge in real situations. Wishing you successful outcomes in your coding tests.\n            <\/p>\n<\/footer>\n<\/article>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will solve an algorithm problem to prepare for a Java coding test. The topic is &#8220;Finding the Order of Building Construction.&#8221; This problem combines graph traversal algorithms and sorting algorithms that can be utilized in various fields. I will explain the theoretical background and code implementation methods needed to solve the &hellip; <a href=\"https:\/\/atmokpo.com\/w\/33382\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Java Coding Test Course, Finding the Order of Building&#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-33382","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, Finding the Order of Building - \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\/33382\/\" \/>\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, Finding the Order of Building - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"In this article, we will solve an algorithm problem to prepare for a Java coding test. The topic is &#8220;Finding the Order of Building Construction.&#8221; This problem combines graph traversal algorithms and sorting algorithms that can be utilized in various fields. I will explain the theoretical background and code implementation methods needed to solve the &hellip; \ub354 \ubcf4\uae30 &quot;Java Coding Test Course, Finding the Order of Building&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/33382\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:16:02+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:38:56+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\/33382\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33382\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Java Coding Test Course, Finding the Order of Building\",\"datePublished\":\"2024-11-01T09:16:02+00:00\",\"dateModified\":\"2024-11-01T11:38:56+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33382\/\"},\"wordCount\":527,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Java Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/33382\/\",\"url\":\"https:\/\/atmokpo.com\/w\/33382\/\",\"name\":\"Java Coding Test Course, Finding the Order of Building - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:16:02+00:00\",\"dateModified\":\"2024-11-01T11:38:56+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33382\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/33382\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/33382\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java Coding Test Course, Finding the Order of Building\"}]},{\"@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, Finding the Order of Building - \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\/33382\/","og_locale":"ko_KR","og_type":"article","og_title":"Java Coding Test Course, Finding the Order of Building - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"In this article, we will solve an algorithm problem to prepare for a Java coding test. The topic is &#8220;Finding the Order of Building Construction.&#8221; This problem combines graph traversal algorithms and sorting algorithms that can be utilized in various fields. I will explain the theoretical background and code implementation methods needed to solve the &hellip; \ub354 \ubcf4\uae30 \"Java Coding Test Course, Finding the Order of Building\"","og_url":"https:\/\/atmokpo.com\/w\/33382\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:16:02+00:00","article_modified_time":"2024-11-01T11:38:56+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\/33382\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/33382\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Java Coding Test Course, Finding the Order of Building","datePublished":"2024-11-01T09:16:02+00:00","dateModified":"2024-11-01T11:38:56+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/33382\/"},"wordCount":527,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Java Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/33382\/","url":"https:\/\/atmokpo.com\/w\/33382\/","name":"Java Coding Test Course, Finding the Order of Building - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:16:02+00:00","dateModified":"2024-11-01T11:38:56+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/33382\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/33382\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/33382\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Java Coding Test Course, Finding the Order of Building"}]},{"@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\/33382","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=33382"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33382\/revisions"}],"predecessor-version":[{"id":33383,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33382\/revisions\/33383"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=33382"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=33382"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=33382"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}