{"id":33598,"date":"2024-11-01T09:18:19","date_gmt":"2024-11-01T09:18:19","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=33598"},"modified":"2024-11-01T11:47:28","modified_gmt":"2024-11-01T11:47:28","slug":"python-coding-test-course-representation-of-graphs","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/33598\/","title":{"rendered":"Python Coding Test Course, Representation of Graphs"},"content":{"rendered":"<p><body><\/p>\n<p>The graph is a mathematical object composed of <strong>vertices<\/strong> and <strong>edges<\/strong>.<br \/>\n    Graphs play a vital role in data structures and are an efficient way to solve various complex problems.<br \/>\n    In this article, we will explain the basic concepts of graphs and cover how to represent and explore graphs using Python.<\/p>\n<h2>1. Basic Concepts of Graphs<\/h2>\n<p>A graph consists of nodes and the edges connecting those nodes. Graphs can be classified into two forms:<\/p>\n<ul>\n<li><strong>Directed Graph<\/strong>: A graph where edges have direction. That is, when an edge points from A to B, there may not be an edge from B to A.<\/li>\n<li><strong>Undirected Graph<\/strong>: A graph where edges do not have direction. An edge connecting A and B exists in both directions.<\/li>\n<\/ul>\n<h2>2. Representation Methods of Graphs<\/h2>\n<p>Graphs can be represented in various ways. The most common methods are:<\/p>\n<ol>\n<li><strong>Adjacency List<\/strong>: Represents the graph by maintaining a list of vertices connected to each vertex. This method is memory efficient.<\/li>\n<li><strong>Adjacency Matrix<\/strong>: Represents all vertices of the graph in a matrix form. Each element of the matrix indicates whether two vertices are connected.<\/li>\n<\/ol>\n<h2>3. Problem Solving: Representation of Graphs<\/h2>\n<p>Now, let&#8217;s solve a practical problem of representing a graph.<\/p>\n<h3>Problem Description<\/h3>\n<blockquote>\n<p>Write a program that receives the number of vertices and the information of edges, and represents the graph in both adjacency list and adjacency matrix forms based on the given information.<\/p>\n<p>Input format:<\/p>\n<ul>\n<li>The first line contains the number of vertices N (1 \u2264 N \u2264 100).<\/li>\n<li>The second line contains the number of edges M (1 \u2264 M \u2264 1000).<\/li>\n<li>From the third line, the edge information (A, B) is given over M lines. A and B are integers from 1 to N, indicating they are connected.<\/li>\n<\/ul>\n<p>Output format:<\/p>\n<p>The first line should output the adjacency list, and the second line should output the adjacency matrix. Each vertex starts from 1.<\/p>\n<\/blockquote>\n<h3>4. Steps to Solve the Problem<\/h3>\n<p>The steps to solve the above problem are as follows:<\/p>\n<h4>4.1. Input Processing<\/h4>\n<p>First, receive the vertex and edge information from the user. Use the <code>input()<\/code> function in Python to receive input and convert it to the appropriate format.<\/p>\n<h4>4.2. Create Adjacency List<\/h4>\n<p>The adjacency list uses a list of lists to store the connected vertices for each vertex. Since the vertex numbers start from 1, an empty list is added in advance to match the list&#8217;s index.<\/p>\n<h4>4.3. Create Adjacency Matrix<\/h4>\n<p>The adjacency matrix uses a 2D array to store the connection status between vertices. The initial value is set to 0, and if an edge exists, it is set to 1. In the case of an undirected graph, when there is a connection A-B, both (A, B) and (B, A) in the matrix should be updated.<\/p>\n<h4>4.4. Output the Results<\/h4>\n<p>Finally, output the created adjacency list and adjacency matrix.<\/p>\n<h3>5. Code Implementation<\/h3>\n<pre><code>def graph_representation():\n    # Input\n    N = int(input(\"Enter the number of vertices (1 \u2264 N \u2264 100): \"))\n    M = int(input(\"Enter the number of edges (1 \u2264 M \u2264 1000): \"))\n    \n    # Initialize adjacency list\n    adj_list = [[] for _ in range(N + 1)]\n    \n    # Initialize adjacency matrix\n    adj_matrix = [[0] * (N + 1) for _ in range(N + 1)]\n    \n    # Input edge information\n    for _ in range(M):\n        A, B = map(int, input(\"Enter edge information (A B): \").split())\n        adj_list[A].append(B)\n        adj_list[B].append(A)  # Undirected graph\n        adj_matrix[A][B] = 1\n        adj_matrix[B][A] = 1  # Undirected graph\n    \n    # Output adjacency list\n    print(\"Adjacency List:\")\n    for i in range(1, N + 1):\n        print(f\"{i}: {adj_list[i]}\")\n    \n    # Output adjacency matrix\n    print(\"Adjacency Matrix:\")\n    for i in range(1, N + 1):\n        print(\" \".join(map(str, adj_matrix[i][1:])))\n        \n# Function call\ngraph_representation()<\/code><\/pre>\n<h3>6. Code Explanation<\/h3>\n<p>The above Python code consists of the following procedures:<\/p>\n<ul>\n<li><strong>Input Processing<\/strong>: Receives the number of vertices and edges, and gets the information for each edge.<\/li>\n<li><strong>Initialize Adjacency List<\/strong>: Creates an empty list according to the number of vertices N.<\/li>\n<li><strong>Initialize Adjacency Matrix<\/strong>: Initializes a matrix of size N x N to 0.<\/li>\n<li><strong>Input Edge Information and Update List\/Matrix<\/strong>: Updates the adjacency list and matrix based on the input A, B in a loop.<\/li>\n<li><strong>Output Results<\/strong>: Outputs the adjacency list and adjacency matrix respectively.<\/li>\n<\/ul>\n<h3>7. Example Execution<\/h3>\n<p>For example, if we have a graph with 5 vertices and 6 edges, the input and output would be as follows:<\/p>\n<pre><code>Enter the number of vertices (1 \u2264 N \u2264 100): 5\nEnter the number of edges (1 \u2264 M \u2264 1000): 6\nEnter edge information (A B): 1 2\nEnter edge information (A B): 1 3\nEnter edge information (A B): 2 4\nEnter edge information (A B): 3 4\nEnter edge information (A B): 4 5\nEnter edge information (A B): 2 5\nAdjacency List:\n1: [2, 3]\n2: [1, 4, 5]\n3: [1, 4]\n4: [2, 3, 5]\n5: [2, 4]\nAdjacency Matrix:\n0 1 1 0 0\n1 0 0 1 1\n1 0 0 1 0\n0 1 1 0 1\n0 1 0 1 0<\/code><\/pre>\n<h3>8. Conclusion<\/h3>\n<p>In this lecture, we learned about the concept of graphs and various representation methods. We also learned how to create adjacency lists and adjacency matrices through a problem, enhancing our understanding of the basic structure of graphs. There are many more problems to tackle, such as graph traversal (DFS, BFS), so I hope you build upon this knowledge and move to the next level.<\/p>\n<p>Try solving various graph problems while studying algorithms. Thank you!<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The graph is a mathematical object composed of vertices and edges. Graphs play a vital role in data structures and are an efficient way to solve various complex problems. In this article, we will explain the basic concepts of graphs and cover how to represent and explore graphs using Python. 1. Basic Concepts of Graphs &hellip; <a href=\"https:\/\/atmokpo.com\/w\/33598\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Python 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":[145],"tags":[],"class_list":["post-33598","post","type-post","status-publish","format-standard","hentry","category-python-coding-test"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python 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\/33598\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Coding Test Course, Representation of Graphs - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"The graph is a mathematical object composed of vertices and edges. Graphs play a vital role in data structures and are an efficient way to solve various complex problems. In this article, we will explain the basic concepts of graphs and cover how to represent and explore graphs using Python. 1. Basic Concepts of Graphs &hellip; \ub354 \ubcf4\uae30 &quot;Python Coding Test Course, Representation of Graphs&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/33598\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:18:19+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:47:28+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=\"4\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/33598\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33598\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Python Coding Test Course, Representation of Graphs\",\"datePublished\":\"2024-11-01T09:18:19+00:00\",\"dateModified\":\"2024-11-01T11:47:28+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33598\/\"},\"wordCount\":638,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Python Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/33598\/\",\"url\":\"https:\/\/atmokpo.com\/w\/33598\/\",\"name\":\"Python Coding Test Course, Representation of Graphs - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:18:19+00:00\",\"dateModified\":\"2024-11-01T11:47:28+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33598\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/33598\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/33598\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python 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":"Python 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\/33598\/","og_locale":"ko_KR","og_type":"article","og_title":"Python Coding Test Course, Representation of Graphs - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"The graph is a mathematical object composed of vertices and edges. Graphs play a vital role in data structures and are an efficient way to solve various complex problems. In this article, we will explain the basic concepts of graphs and cover how to represent and explore graphs using Python. 1. Basic Concepts of Graphs &hellip; \ub354 \ubcf4\uae30 \"Python Coding Test Course, Representation of Graphs\"","og_url":"https:\/\/atmokpo.com\/w\/33598\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:18:19+00:00","article_modified_time":"2024-11-01T11:47:28+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":"4\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/33598\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/33598\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Python Coding Test Course, Representation of Graphs","datePublished":"2024-11-01T09:18:19+00:00","dateModified":"2024-11-01T11:47:28+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/33598\/"},"wordCount":638,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Python Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/33598\/","url":"https:\/\/atmokpo.com\/w\/33598\/","name":"Python Coding Test Course, Representation of Graphs - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:18:19+00:00","dateModified":"2024-11-01T11:47:28+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/33598\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/33598\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/33598\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Python 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\/33598","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=33598"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33598\/revisions"}],"predecessor-version":[{"id":33599,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33598\/revisions\/33599"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=33598"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=33598"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=33598"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}