{"id":49627,"date":"2025-07-08T15:03:49","date_gmt":"2025-07-08T09:33:49","guid":{"rendered":"https:\/\/www.iquanta.in\/blog\/?p=49627"},"modified":"2025-07-08T15:03:51","modified_gmt":"2025-07-08T09:33:51","slug":"graph-implementation-in-c-using-adjacency-list-and-matrix","status":"publish","type":"post","link":"https:\/\/www.iquanta.in\/blog\/graph-implementation-in-c-using-adjacency-list-and-matrix\/","title":{"rendered":"Graph Implementation in C++ using Adjacency List and Matrix"},"content":{"rendered":"\n<p>Graphs are the most fundamental concept in data structures and in this blog we will be covering graph implementation in C++ using both adjacency matrix and adjacency list. In Graph, every node is connected with each other through edges that maintains the relationship between each other.<\/p>\n\n\n\n<p>Graph is a hierarchical model that maintains network between nodes in a direct and undirected manner. There are different types of graph in data structure. So let us begin to understand the introduction and types of graph representation.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/chat.whatsapp.com\/B6weknl7133BQXjPva0pgB\"><img fetchpriority=\"high\" decoding=\"async\" width=\"875\" height=\"142\" src=\"https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/image-117.png\" alt=\"graph implementation in C++\" class=\"wp-image-49616\" srcset=\"https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/image-117.png 875w, https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/image-117-300x49.png 300w, https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/image-117-768x125.png 768w, https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/image-117-150x24.png 150w, https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/image-117-696x113.png 696w\" sizes=\"(max-width: 875px) 100vw, 875px\" \/><\/a><\/figure><\/div>\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_77 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.iquanta.in\/blog\/graph-implementation-in-c-using-adjacency-list-and-matrix\/#What_is_a_Graph_Data_Structure\" >What is a Graph Data Structure?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.iquanta.in\/blog\/graph-implementation-in-c-using-adjacency-list-and-matrix\/#Types_of_Graphs_Representation\" >Types of Graphs Representation<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.iquanta.in\/blog\/graph-implementation-in-c-using-adjacency-list-and-matrix\/#Graph_Implementation_in_C_using_Adjacency_Matrix\" >Graph Implementation in C++ using Adjacency Matrix<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.iquanta.in\/blog\/graph-implementation-in-c-using-adjacency-list-and-matrix\/#Graph_Implementation_in_C_using_Adjacency_List\" >Graph Implementation in C++ using Adjacency List<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.iquanta.in\/blog\/graph-implementation-in-c-using-adjacency-list-and-matrix\/#FAQs\" >FAQs<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.iquanta.in\/blog\/graph-implementation-in-c-using-adjacency-list-and-matrix\/#What_is_the_difference_between_adjacency_matrix_and_adjacency_list\" >What is the difference between adjacency matrix and adjacency list?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.iquanta.in\/blog\/graph-implementation-in-c-using-adjacency-list-and-matrix\/#Which_is_better_for_a_large_graph_adjacency_matrix_or_adjacency_list\" >Which is better for a large graph adjacency matrix or adjacency list?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.iquanta.in\/blog\/graph-implementation-in-c-using-adjacency-list-and-matrix\/#Can_I_use_the_same_code_for_directed_graphs\" >Can I use the same code for directed graphs?<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"h-what-is-a-graph-data-structure\"><span class=\"ez-toc-section\" id=\"What_is_a_Graph_Data_Structure\"><\/span><strong>What is a Graph Data Structure?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Graphs are the important concept in computer science domain because it is worked on a hierarchical model. In Graph data structure there are two components that are nodes and edges where nodes are connected with each other through edges.<\/p>\n\n\n\n<p>There are different types of <a href=\"https:\/\/www.iquanta.in\/blog\/graph-data-structure-its-types-and-representation\/\">graph in data structure<\/a> which includes directed graph, undirected graph, weighted graph, cyclic graph, acyclic graph, complete graph, connected and disconnected graph. <\/p>\n\n\n\n<p>Let&#8217;s understand graph easily through the visual representation which is given below in diagram. In this diagram A, B, C and D all are nodes connected through the edges and this is the example of undirected graph (consists of no direction).<\/p>\n\n\n\n<p><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img decoding=\"async\" width=\"211\" height=\"161\" src=\"https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/Untitled-Diagram-3.jpg\" alt=\"graph implementtaion in C++\" class=\"wp-image-49633\" style=\"width:305px;height:auto\" srcset=\"https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/Untitled-Diagram-3.jpg 211w, https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/Untitled-Diagram-3-80x60.jpg 80w, https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/Untitled-Diagram-3-150x114.jpg 150w\" sizes=\"(max-width: 211px) 100vw, 211px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-types-of-graphs-representation\"><span class=\"ez-toc-section\" id=\"Types_of_Graphs_Representation\"><\/span><strong>Types of Graphs Representation<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>There are two ways we can represent graphs : <\/p>\n\n\n\n<ol>\n<li>Adjacency Matrix <\/li>\n\n\n\n<li>Adjacency List <\/li>\n<\/ol>\n\n\n\n<p>The first one is adjacency matrix in which we are showing relationship between vertices and edges. We are representing adjacency matrix through table format in that through rows we are representing the vertices and through column we are represents edges that connects vertices to each other. <\/p>\n\n\n\n<p>Adjacency matrix comparatively represents the working of a mathematical concept &#8220;matrix&#8221; itself. In Matrix, entire field is consider to be a grid of rows and column and if we want to represent any element in a matrix then we can do so with the help of the rows and column number representation.<\/p>\n\n\n\n<p>Let&#8217;s better understand the graph representation in C++ using both Adjacency Matrix and List in the next section.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img decoding=\"async\" width=\"940\" height=\"903\" src=\"https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/image-121.png\" alt=\"Adjacency matrix\" class=\"wp-image-49643\" style=\"width:782px;height:auto\" srcset=\"https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/image-121.png 940w, https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/image-121-300x288.png 300w, https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/image-121-768x738.png 768w, https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/image-121-437x420.png 437w, https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/image-121-150x144.png 150w, https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/image-121-696x669.png 696w\" sizes=\"(max-width: 940px) 100vw, 940px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-graph-implementation-in-c-using-adjacency-matrix\"><span class=\"ez-toc-section\" id=\"Graph_Implementation_in_C_using_Adjacency_Matrix\"><\/span><strong>Graph Implementation in C++ using Adjacency Matrix<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>A graph can be represented in different ways. One of the simplest methods is using an adjacency matrix. This method is easy to understand and is ideal for dense graphs.<\/p>\n\n\n\n<p>In an adjacency matrix, we use a 2D array where each cell shows whether there is a connection between two nodes. If there is an edge from node i to node j the matrix entry adj[i][j] is set to 1. If there is no edge it stays 0.<\/p>\n\n\n\n<p>Here is a basic<a href=\"https:\/\/www.iquanta.in\/blog\/graph-data-structure-its-types-and-representation\/\"> graph<\/a> implementation in C++ using an adjacency matrix:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/chat.whatsapp.com\/B6weknl7133BQXjPva0pgB\"><img loading=\"lazy\" decoding=\"async\" width=\"875\" height=\"142\" src=\"https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/image-118.png\" alt=\"\" class=\"wp-image-49638\" srcset=\"https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/image-118.png 875w, https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/image-118-300x49.png 300w, https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/image-118-768x125.png 768w, https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/image-118-150x24.png 150w, https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/image-118-696x113.png 696w\" sizes=\"(max-width: 875px) 100vw, 875px\" \/><\/a><\/figure><\/div>\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nclass Graph {\nprivate:\n    int numVertices;\n    int** adjMatrix;\n\npublic:\n    Graph(int vertices) {\n        numVertices = vertices;\n        adjMatrix = new int*&#091;numVertices];\n        for (int i = 0; i &lt; numVertices; i++) {\n            adjMatrix&#091;i] = new int&#091;numVertices];\n            for (int j = 0; j &lt; numVertices; j++)\n                adjMatrix&#091;i]&#091;j] = 0;\n        }\n    }\n\n    void addEdge(int i, int j) {\n        adjMatrix&#091;i]&#091;j] = 1;\n        adjMatrix&#091;j]&#091;i] = 1;\n    }\n\n    void removeEdge(int i, int j) {\n        adjMatrix&#091;i]&#091;j] = 0;\n        adjMatrix&#091;j]&#091;i] = 0;\n    }\n\n    void display() {\n        for (int i = 0; i &lt; numVertices; i++) {\n            for (int j = 0; j &lt; numVertices; j++) {\n                cout &lt;&lt; adjMatrix&#091;i]&#091;j] &lt;&lt; \" \";\n            }\n            cout &lt;&lt; endl;\n        }\n    }\n\n    ~Graph() {\n        for (int i = 0; i &lt; numVertices; i++)\n            delete&#091;] adjMatrix&#091;i];\n        delete&#091;] adjMatrix;\n    }\n};\n\nint main() {\n    Graph g(5);\n    g.addEdge(0, 1);\n    g.addEdge(0, 4);\n    g.addEdge(1, 2);\n    g.addEdge(1, 3);\n    g.addEdge(1, 4);\n    g.addEdge(2, 3);\n    g.addEdge(3, 4);\n\n    cout &lt;&lt; \"Adjacency Matrix:\" &lt;&lt; endl;\n    g.display();\n\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<p>This program creates a graph with 5 nodes and adds connections between them. The display function prints the adjacency matrix so you can easily see which nodes are connected.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-graph-implementation-in-c-using-adjacency-list\"><span class=\"ez-toc-section\" id=\"Graph_Implementation_in_C_using_Adjacency_List\"><\/span><strong>Graph Implementation in C++ using Adjacency List<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Another popular way to represent a graph data structure is using an adjacency list. This method is more memory efficient than an adjacency matrix especially when the graph is sparse.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"950\" height=\"614\" src=\"https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/image-122.png\" alt=\"adjacency list\" class=\"wp-image-49644\" style=\"width:817px;height:auto\" srcset=\"https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/image-122.png 950w, https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/image-122-300x194.png 300w, https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/image-122-768x496.png 768w, https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/image-122-650x420.png 650w, https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/image-122-150x97.png 150w, https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/image-122-696x450.png 696w\" sizes=\"(max-width: 950px) 100vw, 950px\" \/><\/figure><\/div>\n\n\n<p>In an adjacency list each node stores a list of all the nodes that is connected to. This saves space because we only store edges that actually exist. Here is a simple graph implementation in C++ using an adjacency list:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\n#include &lt;vector&gt;\nusing namespace std;\n\nclass Graph {\nprivate:\n    int numVertices;\n    vector&lt;vector&lt;int&gt;&gt; adjList;\n\npublic:\n    Graph(int vertices) {\n        numVertices = vertices;\n        adjList.resize(numVertices);\n    }\n\n    void addEdge(int u, int v) {\n        adjList&#091;u].push_back(v);\n        adjList&#091;v].push_back(u);\n    }\n\n    void display() {\n        for (int i = 0; i &lt; numVertices; i++) {\n            cout &lt;&lt; i &lt;&lt; \": \";\n            for (int neighbor : adjList&#091;i]) {\n                cout &lt;&lt; neighbor &lt;&lt; \" \";\n            }\n            cout &lt;&lt; endl;\n        }\n    }\n};\n\nint main() {\n    Graph g(5);\n    g.addEdge(0, 1);\n    g.addEdge(0, 4);\n    g.addEdge(1, 2);\n    g.addEdge(1, 3);\n    g.addEdge(1, 4);\n    g.addEdge(2, 3);\n    g.addEdge(3, 4);\n\n    cout &lt;&lt; \"Adjacency List:\" &lt;&lt; endl;\n    g.display();\n\n    return 0;\n}\n<\/code><\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/chat.whatsapp.com\/B6weknl7133BQXjPva0pgB\"><img loading=\"lazy\" decoding=\"async\" width=\"875\" height=\"142\" src=\"https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/image-119.png\" alt=\"\" class=\"wp-image-49639\" srcset=\"https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/image-119.png 875w, https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/image-119-300x49.png 300w, https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/image-119-768x125.png 768w, https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/image-119-150x24.png 150w, https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/image-119-696x113.png 696w\" sizes=\"(max-width: 875px) 100vw, 875px\" \/><\/a><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-faqs\"><span class=\"ez-toc-section\" id=\"FAQs\"><\/span><strong>FAQs<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-what-is-the-difference-between-adjacency-matrix-and-adjacency-list\"><span class=\"ez-toc-section\" id=\"What_is_the_difference_between_adjacency_matrix_and_adjacency_list\"><\/span><strong>What is the difference between adjacency matrix and adjacency list?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>An adjacency matrix uses a 2D array to show all possible connections between nodes. An adjacency list uses linked lists or vectors to store only the existing edges. The list saves more memory when the graph has fewer edges.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-which-is-better-for-a-large-graph-adjacency-matrix-or-adjacency-list\"><span class=\"ez-toc-section\" id=\"Which_is_better_for_a_large_graph_adjacency_matrix_or_adjacency_list\"><\/span><strong>Which is better for a large graph adjacency matrix or adjacency list?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>For large graphs with fewer edges an adjacency list is better because it uses less memory.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-can-i-use-the-same-code-for-directed-graphs\"><span class=\"ez-toc-section\" id=\"Can_I_use_the_same_code_for_directed_graphs\"><\/span><strong>Can I use the same code for directed graphs?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Yes you  can use the same code for the directed graphs.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Graphs are the most fundamental concept in data structures and in this blog we will be covering graph implementation in C++ using both adjacency matrix and adjacency list. In Graph, every node is connected with each other through edges that maintains the relationship between each other. Graph is a hierarchical model that maintains network between [&hellip;]<\/p>\n","protected":false},"author":560,"featured_media":53454,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1075,1073],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.4 (Yoast SEO v21.9.1) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Graph Implementation in C++ using Adjacency List and Matrix - iQuanta<\/title>\n<meta name=\"description\" content=\"In this blog we will be talking about the graph implementation in C++ using adjacency list and adjacency matrix.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.iquanta.in\/blog\/graph-implementation-in-c-using-adjacency-list-and-matrix\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Graph Implementation in C++ using Adjacency List and Matrix\" \/>\n<meta property=\"og:description\" content=\"In this blog we will be talking about the graph implementation in C++ using adjacency list and adjacency matrix.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.iquanta.in\/blog\/graph-implementation-in-c-using-adjacency-list-and-matrix\/\" \/>\n<meta property=\"og:site_name\" content=\"iQuanta\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/facebook.com\/iquanta.in\" \/>\n<meta property=\"article:published_time\" content=\"2025-07-08T09:33:49+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-07-08T09:33:51+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/WhatsApp-Image-2025-07-08-at-2.36.51-PM.jpeg\" \/>\n\t<meta property=\"og:image:width\" content=\"1600\" \/>\n\t<meta property=\"og:image:height\" content=\"900\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Nidhi Goswami\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Nidhi Goswami\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.iquanta.in\/blog\/graph-implementation-in-c-using-adjacency-list-and-matrix\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.iquanta.in\/blog\/graph-implementation-in-c-using-adjacency-list-and-matrix\/\"},\"author\":{\"name\":\"Nidhi Goswami\",\"@id\":\"https:\/\/www.iquanta.in\/blog\/#\/schema\/person\/ec8c8c25d0526dd86557b6fed064f7f3\"},\"headline\":\"Graph Implementation in C++ using Adjacency List and Matrix\",\"datePublished\":\"2025-07-08T09:33:49+00:00\",\"dateModified\":\"2025-07-08T09:33:51+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.iquanta.in\/blog\/graph-implementation-in-c-using-adjacency-list-and-matrix\/\"},\"wordCount\":638,\"publisher\":{\"@id\":\"https:\/\/www.iquanta.in\/blog\/#organization\"},\"articleSection\":[\"DSA and Competitive Programming\",\"iSkills\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.iquanta.in\/blog\/graph-implementation-in-c-using-adjacency-list-and-matrix\/\",\"url\":\"https:\/\/www.iquanta.in\/blog\/graph-implementation-in-c-using-adjacency-list-and-matrix\/\",\"name\":\"Graph Implementation in C++ using Adjacency List and Matrix - iQuanta\",\"isPartOf\":{\"@id\":\"https:\/\/www.iquanta.in\/blog\/#website\"},\"datePublished\":\"2025-07-08T09:33:49+00:00\",\"dateModified\":\"2025-07-08T09:33:51+00:00\",\"description\":\"In this blog we will be talking about the graph implementation in C++ using adjacency list and adjacency matrix.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.iquanta.in\/blog\/graph-implementation-in-c-using-adjacency-list-and-matrix\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.iquanta.in\/blog\/graph-implementation-in-c-using-adjacency-list-and-matrix\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.iquanta.in\/blog\/graph-implementation-in-c-using-adjacency-list-and-matrix\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.iquanta.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Graph Implementation in C++ using Adjacency List and Matrix\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.iquanta.in\/blog\/#website\",\"url\":\"https:\/\/www.iquanta.in\/blog\/\",\"name\":\"iQuanta | Cat Preparation Online\",\"description\":\"Building Learning Networks\",\"publisher\":{\"@id\":\"https:\/\/www.iquanta.in\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.iquanta.in\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.iquanta.in\/blog\/#organization\",\"name\":\"IQuanta\",\"url\":\"https:\/\/www.iquanta.in\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.iquanta.in\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2018\/08\/IQuanta-1.png\",\"contentUrl\":\"https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2018\/08\/IQuanta-1.png\",\"width\":525,\"height\":200,\"caption\":\"IQuanta\"},\"image\":{\"@id\":\"https:\/\/www.iquanta.in\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/facebook.com\/iquanta.in\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.iquanta.in\/blog\/#\/schema\/person\/ec8c8c25d0526dd86557b6fed064f7f3\",\"name\":\"Nidhi Goswami\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.iquanta.in\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/21d234d87afd924b217d26b25a3cf1ee?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/21d234d87afd924b217d26b25a3cf1ee?s=96&d=mm&r=g\",\"caption\":\"Nidhi Goswami\"},\"url\":\"https:\/\/www.iquanta.in\/blog\/author\/nidhigoswami\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Graph Implementation in C++ using Adjacency List and Matrix - iQuanta","description":"In this blog we will be talking about the graph implementation in C++ using adjacency list and adjacency matrix.","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:\/\/www.iquanta.in\/blog\/graph-implementation-in-c-using-adjacency-list-and-matrix\/","og_locale":"en_US","og_type":"article","og_title":"Graph Implementation in C++ using Adjacency List and Matrix","og_description":"In this blog we will be talking about the graph implementation in C++ using adjacency list and adjacency matrix.","og_url":"https:\/\/www.iquanta.in\/blog\/graph-implementation-in-c-using-adjacency-list-and-matrix\/","og_site_name":"iQuanta","article_publisher":"https:\/\/facebook.com\/iquanta.in","article_published_time":"2025-07-08T09:33:49+00:00","article_modified_time":"2025-07-08T09:33:51+00:00","og_image":[{"width":1600,"height":900,"url":"https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2025\/05\/WhatsApp-Image-2025-07-08-at-2.36.51-PM.jpeg","type":"image\/jpeg"}],"author":"Nidhi Goswami","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Nidhi Goswami","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.iquanta.in\/blog\/graph-implementation-in-c-using-adjacency-list-and-matrix\/#article","isPartOf":{"@id":"https:\/\/www.iquanta.in\/blog\/graph-implementation-in-c-using-adjacency-list-and-matrix\/"},"author":{"name":"Nidhi Goswami","@id":"https:\/\/www.iquanta.in\/blog\/#\/schema\/person\/ec8c8c25d0526dd86557b6fed064f7f3"},"headline":"Graph Implementation in C++ using Adjacency List and Matrix","datePublished":"2025-07-08T09:33:49+00:00","dateModified":"2025-07-08T09:33:51+00:00","mainEntityOfPage":{"@id":"https:\/\/www.iquanta.in\/blog\/graph-implementation-in-c-using-adjacency-list-and-matrix\/"},"wordCount":638,"publisher":{"@id":"https:\/\/www.iquanta.in\/blog\/#organization"},"articleSection":["DSA and Competitive Programming","iSkills"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.iquanta.in\/blog\/graph-implementation-in-c-using-adjacency-list-and-matrix\/","url":"https:\/\/www.iquanta.in\/blog\/graph-implementation-in-c-using-adjacency-list-and-matrix\/","name":"Graph Implementation in C++ using Adjacency List and Matrix - iQuanta","isPartOf":{"@id":"https:\/\/www.iquanta.in\/blog\/#website"},"datePublished":"2025-07-08T09:33:49+00:00","dateModified":"2025-07-08T09:33:51+00:00","description":"In this blog we will be talking about the graph implementation in C++ using adjacency list and adjacency matrix.","breadcrumb":{"@id":"https:\/\/www.iquanta.in\/blog\/graph-implementation-in-c-using-adjacency-list-and-matrix\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.iquanta.in\/blog\/graph-implementation-in-c-using-adjacency-list-and-matrix\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.iquanta.in\/blog\/graph-implementation-in-c-using-adjacency-list-and-matrix\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.iquanta.in\/blog\/"},{"@type":"ListItem","position":2,"name":"Graph Implementation in C++ using Adjacency List and Matrix"}]},{"@type":"WebSite","@id":"https:\/\/www.iquanta.in\/blog\/#website","url":"https:\/\/www.iquanta.in\/blog\/","name":"iQuanta | Cat Preparation Online","description":"Building Learning Networks","publisher":{"@id":"https:\/\/www.iquanta.in\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.iquanta.in\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.iquanta.in\/blog\/#organization","name":"IQuanta","url":"https:\/\/www.iquanta.in\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.iquanta.in\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2018\/08\/IQuanta-1.png","contentUrl":"https:\/\/www.iquanta.in\/blog\/wp-content\/uploads\/2018\/08\/IQuanta-1.png","width":525,"height":200,"caption":"IQuanta"},"image":{"@id":"https:\/\/www.iquanta.in\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/facebook.com\/iquanta.in"]},{"@type":"Person","@id":"https:\/\/www.iquanta.in\/blog\/#\/schema\/person\/ec8c8c25d0526dd86557b6fed064f7f3","name":"Nidhi Goswami","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.iquanta.in\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/21d234d87afd924b217d26b25a3cf1ee?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/21d234d87afd924b217d26b25a3cf1ee?s=96&d=mm&r=g","caption":"Nidhi Goswami"},"url":"https:\/\/www.iquanta.in\/blog\/author\/nidhigoswami\/"}]}},"_links":{"self":[{"href":"https:\/\/www.iquanta.in\/blog\/wp-json\/wp\/v2\/posts\/49627"}],"collection":[{"href":"https:\/\/www.iquanta.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.iquanta.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.iquanta.in\/blog\/wp-json\/wp\/v2\/users\/560"}],"replies":[{"embeddable":true,"href":"https:\/\/www.iquanta.in\/blog\/wp-json\/wp\/v2\/comments?post=49627"}],"version-history":[{"count":8,"href":"https:\/\/www.iquanta.in\/blog\/wp-json\/wp\/v2\/posts\/49627\/revisions"}],"predecessor-version":[{"id":53460,"href":"https:\/\/www.iquanta.in\/blog\/wp-json\/wp\/v2\/posts\/49627\/revisions\/53460"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.iquanta.in\/blog\/wp-json\/wp\/v2\/media\/53454"}],"wp:attachment":[{"href":"https:\/\/www.iquanta.in\/blog\/wp-json\/wp\/v2\/media?parent=49627"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.iquanta.in\/blog\/wp-json\/wp\/v2\/categories?post=49627"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.iquanta.in\/blog\/wp-json\/wp\/v2\/tags?post=49627"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}