{"id":34146,"date":"2024-11-01T09:24:44","date_gmt":"2024-11-01T09:24:44","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34146"},"modified":"2024-11-01T10:58:25","modified_gmt":"2024-11-01T10:58:25","slug":"c-coding-test-course-representation-of-graphs-2","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34146\/","title":{"rendered":"C++ Coding Test Course, Representation of Graphs"},"content":{"rendered":"<p><body><\/p>\n<p>\n        In modern algorithm problem solving, graphs are a very important and fundamental data structure. In this article, we will explain the representation of graphs and learn how to solve related problems using C++.\n    <\/p>\n<h2>What is a graph?<\/h2>\n<p>\n        A graph is a data structure composed of vertices and edges, which is useful for expressing various relationships. For instance, graphs can be utilized in various scenarios such as relationships between users in a social network, connections between cities in a road network, and links between web pages.\n    <\/p>\n<h2>Methods of graph representation<\/h2>\n<p>\n        Graphs are generally represented in two ways: Adjacency Matrix and Adjacency List. Each method has its own advantages and disadvantages, so it is important to choose the appropriate method according to the nature of the problem.\n    <\/p>\n<h3>1. Adjacency Matrix<\/h3>\n<p>\n        An adjacency matrix is a way to represent a graph using a two-dimensional array. The size of the matrix is V x V (where V is the number of vertices), and each element of the matrix (i, j) indicates whether there is an edge between vertex i and vertex j.\n    <\/p>\n<pre><code>\n        \/\/ Declaration of adjacency matrix in C++\n        const int MAX = 100; \/\/ Maximum number of vertices\n        int adj[MAX][MAX]; \/\/ Adjacency matrix\n        int V; \/\/ Number of vertices\n        <\/code><\/pre>\n<p>\n        The advantage of an adjacency matrix is that it allows you to check the existence of a specific edge in O(1) time. However, in terms of memory, it uses the square of V, making it inefficient when there are many vertices but few edges.\n    <\/p>\n<h3>2. Adjacency List<\/h3>\n<p>\n        An adjacency list is a method that uses a list of vertices connected to each vertex. This is more suitable for sparse graphs.\n    <\/p>\n<pre><code>\n        \/\/ Declaration of adjacency list in C++\n        #include <vector>\n        #include <iostream>\n        using namespace std;\n\n        vector<int> adj[MAX]; \/\/ Adjacency list\n        int V; \/\/ Number of vertices\n        <\/int><\/iostream><\/vector><\/code><\/pre>\n<p>\n        The adjacency list uses memory more efficiently and requires O(E) time to traverse the list of edges.\n    <\/p>\n<h2>Problem: Finding Connected Components<\/h2>\n<p>\n        In this problem, you need to find all components that are interconnected in the given directed graph. In other words, you need to identify the set of vertices that can be reached starting from a single vertex.\n    <\/p>\n<h3>Problem Description<\/h3>\n<p>\n        Given the number of vertices V and the number of edges E, with each edge provided as a pair of two vertices, write a program to find and output all connected components in this graph.\n    <\/p>\n<h3>Input Format<\/h3>\n<ul>\n<li>First line: Number of vertices V (1 \u2264 V \u2264 1000)<\/li>\n<li>Second line: Number of edges E (0 \u2264 E \u2264 10000)<\/li>\n<li>Next E lines: Each line represents an edge in the form (u, v)<\/li>\n<\/ul>\n<h3>Output Format<\/h3>\n<p>\n        Output the number of connected components and the vertices of each component in ascending order.\n    <\/p>\n<h2>Problem-solving Process<\/h2>\n<p>\n        To solve the problem, we will use an adjacency list to store the graph and find connected components using depth-first search (DFS).\n    <\/p>\n<h3>Step 1: Constructing the Adjacency List<\/h3>\n<p>\n        First, we need to take the input and construct the adjacency list. We will input the number of vertices and edges and complete the list representing the relationships through each edge.\n    <\/p>\n<h3>Step 2: Finding Connected Components with DFS<\/h3>\n<p>\n        Whenever a search occurs, we mark the visited vertex, and repeat this until there are no more vertices to visit, thereby finding the components. The implementation of DFS can be easily done recursively.\n    <\/p>\n<pre><code>\n        #include <iostream>\n        #include <vector>\n        #include <algorithm>\n\n        using namespace std;\n\n        vector<int> adj[1001]; \/\/ Adjacency list\n        bool visited[1001]; \/\/ Visit check\n        vector<vector<int>> components; \/\/ Store connected components\n\n        void dfs(int v, vector<int> &component) {\n            visited[v] = true; \/\/ Mark as visited\n            component.push_back(v); \/\/ Add to the component\n\n            for (int u : adj[v]) {\n                if (!visited[u]) {\n                    dfs(u, component); \/\/ Recursion\n                }\n            }\n        }\n\n        int main() {\n            int V, E;\n            cin >> V >> E;\n\n            for (int i = 0; i < E; i++) {\n                int u, v;\n                cin >> u >> v; \/\/ Input edge\n                adj[u].push_back(v); \/\/ Construct adjacency list\n                adj[v].push_back(u); \/\/ Assuming an undirected graph\n            }\n\n            \/\/ Finding connected components for each vertex\n            for (int i = 1; i <= V; i++) {\n                if (!visited[i]) {\n                    vector<int> component;\n                    dfs(i, component); \/\/ DFS search\n                    components.push_back(component); \/\/ Store found component\n                }\n            }\n\n            \/\/ Output results\n            cout << components.size() << \"\\n\"; \/\/ Number of connected components\n            for (const auto &#038;component : components) {\n                sort(component.begin(), component.end()); \/\/ Sort the component\n                for (int v : component) {\n                    cout << v << \" \"; \/\/ Output vertices\n                }\n                cout << \"\\n\";\n            }\n\n            return 0;\n        }\n        <\/int><\/int><\/vector<int><\/int><\/algorithm><\/vector><\/iostream><\/code><\/pre>\n<p>\n        The above code implements the logic to find and output connected components from the input graph. It sorts and outputs each component in ascending order.\n    <\/p>\n<h2>Conclusion<\/h2>\n<p>\n        We have examined the representation of graphs and the process of solving problems using DFS. Graphs play an important role in various algorithm problems, and learning them is fundamental to achieving good results in coding tests. I hope this course enhances your basic understanding of graphs and your problem-solving skills.\n    <\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In modern algorithm problem solving, graphs are a very important and fundamental data structure. In this article, we will explain the representation of graphs and learn how to solve related problems using C++. What is a graph? A graph is a data structure composed of vertices and edges, which is useful for expressing various relationships. &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34146\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;C++ 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":[111],"tags":[],"class_list":["post-34146","post","type-post","status-publish","format-standard","hentry","category-c-coding-test-tutorials-2"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C++ 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\/34146\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++ Coding Test Course, Representation of Graphs - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"In modern algorithm problem solving, graphs are a very important and fundamental data structure. In this article, we will explain the representation of graphs and learn how to solve related problems using C++. What is a graph? A graph is a data structure composed of vertices and edges, which is useful for expressing various relationships. &hellip; \ub354 \ubcf4\uae30 &quot;C++ Coding Test Course, Representation of Graphs&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34146\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:24:44+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T10:58:25+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\/34146\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34146\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"C++ Coding Test Course, Representation of Graphs\",\"datePublished\":\"2024-11-01T09:24:44+00:00\",\"dateModified\":\"2024-11-01T10:58:25+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34146\/\"},\"wordCount\":570,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"C++ Coding Test Tutorials\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34146\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34146\/\",\"name\":\"C++ Coding Test Course, Representation of Graphs - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:24:44+00:00\",\"dateModified\":\"2024-11-01T10:58:25+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34146\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34146\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34146\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ 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":"C++ 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\/34146\/","og_locale":"ko_KR","og_type":"article","og_title":"C++ Coding Test Course, Representation of Graphs - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"In modern algorithm problem solving, graphs are a very important and fundamental data structure. In this article, we will explain the representation of graphs and learn how to solve related problems using C++. What is a graph? A graph is a data structure composed of vertices and edges, which is useful for expressing various relationships. &hellip; \ub354 \ubcf4\uae30 \"C++ Coding Test Course, Representation of Graphs\"","og_url":"https:\/\/atmokpo.com\/w\/34146\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:24:44+00:00","article_modified_time":"2024-11-01T10:58:25+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\/34146\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34146\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"C++ Coding Test Course, Representation of Graphs","datePublished":"2024-11-01T09:24:44+00:00","dateModified":"2024-11-01T10:58:25+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34146\/"},"wordCount":570,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["C++ Coding Test Tutorials"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34146\/","url":"https:\/\/atmokpo.com\/w\/34146\/","name":"C++ Coding Test Course, Representation of Graphs - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:24:44+00:00","dateModified":"2024-11-01T10:58:25+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34146\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34146\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34146\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"C++ 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\/34146","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=34146"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34146\/revisions"}],"predecessor-version":[{"id":34147,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34146\/revisions\/34147"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34146"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34146"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34146"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}