{"id":33,"date":"2014-12-24T13:40:42","date_gmt":"2014-12-24T13:40:42","guid":{"rendered":"http:\/\/theoryofprogramming.wordpress.com\/?p=33"},"modified":"2025-01-19T04:48:56","modified_gmt":"2025-01-19T04:48:56","slug":"graph-theory-basics","status":"publish","type":"post","link":"https:\/\/theoryofcoding.com\/index.php\/2014\/12\/24\/graph-theory-basics\/","title":{"rendered":"Graph Theory Basics"},"content":{"rendered":"\n<p>Hello people&#8230;! In this post, I will talk about Graph Theory Basics, what are its terminologies, types and implementations in C. Graphs are difficult to code, but they have the most interesting real-life applications. When you want to talk about the real-life applications of graphs, you just cannot resist talking about the Facebook&#8217;s Graph Search! Now, I don&#8217;t really know that algorithm, but it uses graphs to find out your closest friends, or any other associations you have with the other users.<\/p>\n\n\n\n<p>Other applications include finding the shortest paths, and by shortest path, I mean in the &#8220;universal sense&#8221;, it could be the shortest path of a Traveling Salesman, or the shortest path to win Snake and Ladder or even the shortest way to solve the Rubik&#8217;s Cube&#8230;! Amazing, right&#8230;?!<\/p>\n\n\n\n<p>You can continue to read this post here to learn about Graphs, or watch the same video on Theory of Coding&#8217;s <a href=\"https:\/\/www.youtube.com\/watch?v=NaV03tiHTQw\" data-type=\"link\" data-id=\"https:\/\/www.youtube.com\/watch?v=NaV03tiHTQw\">YouTube channel here<\/a> (contains ads). Now, let&#8217;s continue.<\/p>\n\n\n\n<p>Graphs are much clear when defined in mathematical terms. A Graph <strong>G (V, E)<\/strong>, consists of two sets,<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Set of Vertices, <strong>V<\/strong><\/li>\n\n\n\n<li>Set of Edges, <strong>E<\/strong><\/li>\n<\/ul>\n\n\n\n<p>As the name suggest, <strong>V<\/strong>, is the set of vertices or, the set of all nodes in a given graph and <strong>E<\/strong>, is the set of all the edges between these nodes, or the associations between them. So, <strong>|V|<\/strong> denotes the number of nodes in a graph and <strong>|E|<\/strong> denotes the number of edges. Let us&nbsp;take up a small example&nbsp;to understand these terms&nbsp;&#8211;<\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure data-wp-context=\"{&quot;imageId&quot;:&quot;69d8cdb770f70&quot;}\" data-wp-interactive=\"core\/image\" class=\"wp-block-image size-large wp-lightbox-container\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"849\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" data-id=\"4038\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/DirectedGraph.jpg?resize=1024%2C849&#038;ssl=1\" alt=\"\" class=\"wp-image-4038\" srcset=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/DirectedGraph.jpg?resize=1024%2C849&amp;ssl=1 1024w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/DirectedGraph.jpg?resize=300%2C249&amp;ssl=1 300w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/DirectedGraph.jpg?resize=768%2C637&amp;ssl=1 768w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/DirectedGraph.jpg?resize=1536%2C1274&amp;ssl=1 1536w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/DirectedGraph.jpg?resize=1000%2C829&amp;ssl=1 1000w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/DirectedGraph.jpg?resize=230%2C191&amp;ssl=1 230w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/DirectedGraph.jpg?resize=350%2C290&amp;ssl=1 350w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/DirectedGraph.jpg?resize=480%2C398&amp;ssl=1 480w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/DirectedGraph.jpg?w=1765&amp;ssl=1 1765w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" data-recalc-dims=\"1\" \/><button\n\t\t\tclass=\"lightbox-trigger\"\n\t\t\ttype=\"button\"\n\t\t\taria-haspopup=\"dialog\"\n\t\t\taria-label=\"Enlarge image\"\n\t\t\tdata-wp-init=\"callbacks.initTriggerButton\"\n\t\t\tdata-wp-on-async--click=\"actions.showLightbox\"\n\t\t\tdata-wp-style--right=\"state.imageButtonRight\"\n\t\t\tdata-wp-style--top=\"state.imageButtonTop\"\n\t\t>\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewBox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\" \/>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure>\n\n\n\n<figure data-wp-context=\"{&quot;imageId&quot;:&quot;69d8cdb7713ce&quot;}\" data-wp-interactive=\"core\/image\" class=\"wp-block-image size-large wp-lightbox-container\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"816\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" data-id=\"4040\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/UnidrectedGraph.jpg?resize=1024%2C816&#038;ssl=1\" alt=\"\" class=\"wp-image-4040\" srcset=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/UnidrectedGraph.jpg?resize=1024%2C816&amp;ssl=1 1024w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/UnidrectedGraph.jpg?resize=300%2C239&amp;ssl=1 300w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/UnidrectedGraph.jpg?resize=768%2C612&amp;ssl=1 768w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/UnidrectedGraph.jpg?resize=1536%2C1224&amp;ssl=1 1536w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/UnidrectedGraph.jpg?resize=1000%2C797&amp;ssl=1 1000w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/UnidrectedGraph.jpg?resize=230%2C183&amp;ssl=1 230w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/UnidrectedGraph.jpg?resize=350%2C279&amp;ssl=1 350w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/UnidrectedGraph.jpg?resize=480%2C382&amp;ssl=1 480w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/UnidrectedGraph.jpg?w=1974&amp;ssl=1 1974w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" data-recalc-dims=\"1\" \/><button\n\t\t\tclass=\"lightbox-trigger\"\n\t\t\ttype=\"button\"\n\t\t\taria-haspopup=\"dialog\"\n\t\t\taria-label=\"Enlarge image\"\n\t\t\tdata-wp-init=\"callbacks.initTriggerButton\"\n\t\t\tdata-wp-on-async--click=\"actions.showLightbox\"\n\t\t\tdata-wp-style--right=\"state.imageButtonRight\"\n\t\t\tdata-wp-style--top=\"state.imageButtonTop\"\n\t\t>\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewBox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\" \/>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure>\n<\/figure>\n\n\n\n<p>As you can see, we have two types of graphs, <strong>Directed<\/strong> and <strong>Undirected Graphs<\/strong>. In <strong>Undirected <\/strong><b>Graphs,<\/b> the direction for an edge is not defined. This is generally used to indicate that the edge is actually bi-directional in nature, i.e., one can go from V<sub>1<\/sub> \u2192 V<sub>2<\/sub>, as well as from V<sub>2<\/sub> \u2192 V<sub>1<\/sub>. This is used to represent the graph where the states (nodes) are re-doable, such as, in a Rubik&#8217;s Cube, you can go from one configuration of the cube to the other as well as the vice-versa.<\/p>\n\n\n\n<p>The other type, the <strong>Directed Graph<\/strong> restricts the&#8230; &#8220;traversal&#8221;, if you say&#8230; to only one direction. For example, in the Snakes and Ladders game, you can play dice and go from Position 5 \u2192&nbsp;Position 10, but you can&#8217;t roll the dice such that it gets you from Position 10 \u2190&nbsp;Position 5&#8230; That&#8217;s obvious&#8230;! So, it really depends on the requirement of the situation, which graph you choose. Generally, we only have to deal with graphs which are purely Directed or purely Undirected. We do not talk about the hybrid type. Some of the other type of graphs are shown below&nbsp;&#8211;<\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default wp-block-gallery-2 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure data-wp-context=\"{&quot;imageId&quot;:&quot;69d8cdb7721f9&quot;}\" data-wp-interactive=\"core\/image\" class=\"wp-block-image size-large wp-lightbox-container\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"825\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" data-id=\"4046\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/WeightedGraph.jpg?resize=1024%2C825&#038;ssl=1\" alt=\"\" class=\"wp-image-4046\" srcset=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/WeightedGraph.jpg?resize=1024%2C825&amp;ssl=1 1024w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/WeightedGraph.jpg?resize=300%2C242&amp;ssl=1 300w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/WeightedGraph.jpg?resize=768%2C619&amp;ssl=1 768w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/WeightedGraph.jpg?resize=1536%2C1238&amp;ssl=1 1536w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/WeightedGraph.jpg?resize=1000%2C806&amp;ssl=1 1000w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/WeightedGraph.jpg?resize=230%2C185&amp;ssl=1 230w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/WeightedGraph.jpg?resize=350%2C282&amp;ssl=1 350w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/WeightedGraph.jpg?resize=480%2C387&amp;ssl=1 480w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/WeightedGraph.jpg?w=1953&amp;ssl=1 1953w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" data-recalc-dims=\"1\" \/><button\n\t\t\tclass=\"lightbox-trigger\"\n\t\t\ttype=\"button\"\n\t\t\taria-haspopup=\"dialog\"\n\t\t\taria-label=\"Enlarge image\"\n\t\t\tdata-wp-init=\"callbacks.initTriggerButton\"\n\t\t\tdata-wp-on-async--click=\"actions.showLightbox\"\n\t\t\tdata-wp-style--right=\"state.imageButtonRight\"\n\t\t\tdata-wp-style--top=\"state.imageButtonTop\"\n\t\t>\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewBox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\" \/>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure>\n\n\n\n<figure data-wp-context=\"{&quot;imageId&quot;:&quot;69d8cdb7725d9&quot;}\" data-wp-interactive=\"core\/image\" class=\"wp-block-image size-large wp-lightbox-container\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"826\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" data-id=\"4045\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/DisconnectedGraph.jpg?resize=1024%2C826&#038;ssl=1\" alt=\"\" class=\"wp-image-4045\" srcset=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/DisconnectedGraph.jpg?resize=1024%2C826&amp;ssl=1 1024w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/DisconnectedGraph.jpg?resize=300%2C242&amp;ssl=1 300w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/DisconnectedGraph.jpg?resize=768%2C620&amp;ssl=1 768w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/DisconnectedGraph.jpg?resize=1536%2C1239&amp;ssl=1 1536w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/DisconnectedGraph.jpg?resize=1000%2C807&amp;ssl=1 1000w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/DisconnectedGraph.jpg?resize=230%2C186&amp;ssl=1 230w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/DisconnectedGraph.jpg?resize=350%2C282&amp;ssl=1 350w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/DisconnectedGraph.jpg?resize=480%2C387&amp;ssl=1 480w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/DisconnectedGraph.jpg?w=1813&amp;ssl=1 1813w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" data-recalc-dims=\"1\" \/><button\n\t\t\tclass=\"lightbox-trigger\"\n\t\t\ttype=\"button\"\n\t\t\taria-haspopup=\"dialog\"\n\t\t\taria-label=\"Enlarge image\"\n\t\t\tdata-wp-init=\"callbacks.initTriggerButton\"\n\t\t\tdata-wp-on-async--click=\"actions.showLightbox\"\n\t\t\tdata-wp-style--right=\"state.imageButtonRight\"\n\t\t\tdata-wp-style--top=\"state.imageButtonTop\"\n\t\t>\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewBox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\" \/>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure>\n<\/figure>\n\n\n\n<p>In <strong>Disconnected Graphs<\/strong>, there will be at least one pair of vertices V<sub>1<\/sub> and V<sub>2<\/sub>&nbsp;\u2208 <strong>V<\/strong>, which doesn&#8217;t have a path. In the above example, vertex 2 and 4 are not reachable from each other. Similarly, vertex 1 and 5 are not reachable from each other. In this graph, vertices 1, 2, 3 for a <strong>Connected Component<\/strong> and vertices 4, 5, 6, 7 form another Connected Component.<\/p>\n\n\n\n<p>In <strong>Weighted Graphs<\/strong>, the edges are given &#8220;weights&#8221;. To understand a Weighted Graph, you can think of the vertices as cities and the edges as the distance between them (so they will have some value). You could be asked the shortest path between two cities. So, in the context of a Weighted graph, the shortest path may not be the one with least edges. One must keep that in mind.<\/p>\n\n\n\n<p>There are plenty of other graph types, like <strong>Simple Graphs<\/strong> which do not contain any loops. A loop is an edge from a vertex to itself. The graphs which do contain loops are <strong>Non-Simple Graphs<\/strong>.<\/p>\n\n\n\n<p>Now, we will look at the way the graphs are implemented. There are mainly two ways to implement graphs, they are &#8211;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Adjacency Matrix<\/li>\n\n\n\n<li>Adjacency List<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Adjacency Matrix<\/h3>\n\n\n\n<p>It is a very simple representation where we take a two-dimensional matrix of the size <strong>|V+1| \u00d7&nbsp;|V+1|<\/strong>, i.e., the declaration in C would look like,<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nint adjMat&#x5B;V + 1]&#x5B;V + 1];\n<\/pre><\/pre>\n\n\n\n<p>As the arrays are zero-indexed, we generally prefer to keep the sizes <em>V + 1<\/em>. Now, initially all the values would be zeroes. We put &#8216;1&#8217; in an element of the two-dimensional array, <em>adjMat[i][j]<\/em>, if there exists an edge between V<sub>i<\/sub> \u2192 V<sub>j<\/sub>. Remember, arrays are zero-indexed. Let us take an example to understand this,<\/p>\n\n\n\n<figure data-wp-context=\"{&quot;imageId&quot;:&quot;69d8cdb772cca&quot;}\" data-wp-interactive=\"core\/image\" class=\"wp-block-image size-large wp-lightbox-container\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"347\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyMatrix_WithCode.jpg?resize=1024%2C347&#038;ssl=1\" alt=\"\" class=\"wp-image-4048\" srcset=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyMatrix_WithCode-scaled.jpg?resize=1024%2C347&amp;ssl=1 1024w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyMatrix_WithCode-scaled.jpg?resize=300%2C102&amp;ssl=1 300w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyMatrix_WithCode-scaled.jpg?resize=768%2C260&amp;ssl=1 768w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyMatrix_WithCode-scaled.jpg?resize=1536%2C520&amp;ssl=1 1536w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyMatrix_WithCode-scaled.jpg?resize=2048%2C694&amp;ssl=1 2048w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyMatrix_WithCode-scaled.jpg?resize=1000%2C339&amp;ssl=1 1000w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyMatrix_WithCode-scaled.jpg?resize=230%2C78&amp;ssl=1 230w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyMatrix_WithCode-scaled.jpg?resize=350%2C119&amp;ssl=1 350w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyMatrix_WithCode-scaled.jpg?resize=480%2C163&amp;ssl=1 480w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyMatrix_WithCode-scaled.jpg?w=2340&amp;ssl=1 2340w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" data-recalc-dims=\"1\" \/><button\n\t\t\tclass=\"lightbox-trigger\"\n\t\t\ttype=\"button\"\n\t\t\taria-haspopup=\"dialog\"\n\t\t\taria-label=\"Enlarge image\"\n\t\t\tdata-wp-init=\"callbacks.initTriggerButton\"\n\t\t\tdata-wp-on-async--click=\"actions.showLightbox\"\n\t\t\tdata-wp-style--right=\"state.imageButtonRight\"\n\t\t\tdata-wp-style--top=\"state.imageButtonTop\"\n\t\t>\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewBox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\" \/>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure>\n\n\n\n<p>I hope it is clear from the example, how we can represent the graph using an Adjacency Matrix. It is very easy to code. All you have to do is create a two-dimensional matrix and assign the values. For a weighted graph, all you have to do is assign the weights of the edges instead of assigning value 1 in the cells &#8211;<\/p>\n\n\n\n<figure data-wp-context=\"{&quot;imageId&quot;:&quot;69d8cdb7730ee&quot;}\" data-wp-interactive=\"core\/image\" class=\"wp-block-image size-large wp-lightbox-container\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"420\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyMatrix_Weighted.jpg?resize=1024%2C420&#038;ssl=1\" alt=\"\" class=\"wp-image-4052\" srcset=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyMatrix_Weighted-scaled.jpg?resize=1024%2C420&amp;ssl=1 1024w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyMatrix_Weighted-scaled.jpg?resize=300%2C123&amp;ssl=1 300w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyMatrix_Weighted-scaled.jpg?resize=768%2C315&amp;ssl=1 768w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyMatrix_Weighted-scaled.jpg?resize=1536%2C630&amp;ssl=1 1536w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyMatrix_Weighted-scaled.jpg?resize=2048%2C840&amp;ssl=1 2048w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyMatrix_Weighted-scaled.jpg?resize=1000%2C410&amp;ssl=1 1000w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyMatrix_Weighted-scaled.jpg?resize=230%2C94&amp;ssl=1 230w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyMatrix_Weighted-scaled.jpg?resize=350%2C144&amp;ssl=1 350w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyMatrix_Weighted-scaled.jpg?resize=480%2C197&amp;ssl=1 480w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyMatrix_Weighted-scaled.jpg?w=2340&amp;ssl=1 2340w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" data-recalc-dims=\"1\" \/><button\n\t\t\tclass=\"lightbox-trigger\"\n\t\t\ttype=\"button\"\n\t\t\taria-haspopup=\"dialog\"\n\t\t\taria-label=\"Enlarge image\"\n\t\t\tdata-wp-init=\"callbacks.initTriggerButton\"\n\t\t\tdata-wp-on-async--click=\"actions.showLightbox\"\n\t\t\tdata-wp-style--right=\"state.imageButtonRight\"\n\t\t\tdata-wp-style--top=\"state.imageButtonTop\"\n\t\t>\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewBox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\" \/>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Adjacency List<\/h3>\n\n\n\n<p>It is an array of pointers of size <strong>|V| + 1<\/strong>, where each pointer points to a&nbsp;linked list. The linked list holds the nodes which are adjacent to the <strong>i<sup>th<\/sup><\/strong> vertex.&nbsp;By adjacent, we mean those vertices that can be accessed from i<sup>th<\/sup> node by making a single move. It is a little complex implementation if you are uncomfortable with linked lists. But it is this implementation that we will use for Graph Search Algorithms. This is because we can easily and efficiently know which of the vertices of <strong>V<\/strong> are neighbors of a given vertex. And that is what you want, if you are to do a Graph Search. Now, let us take an example to understand the concept of Adjacency Lists.<\/p>\n\n\n\n<figure data-wp-context=\"{&quot;imageId&quot;:&quot;69d8cdb77355a&quot;}\" data-wp-interactive=\"core\/image\" class=\"wp-block-image size-large wp-lightbox-container\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"404\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyList.jpg?resize=1024%2C404&#038;ssl=1\" alt=\"\" class=\"wp-image-4056\" srcset=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyList-scaled.jpg?resize=1024%2C404&amp;ssl=1 1024w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyList-scaled.jpg?resize=300%2C118&amp;ssl=1 300w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyList-scaled.jpg?resize=768%2C303&amp;ssl=1 768w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyList-scaled.jpg?resize=1536%2C606&amp;ssl=1 1536w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyList-scaled.jpg?resize=2048%2C808&amp;ssl=1 2048w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyList-scaled.jpg?resize=1000%2C395&amp;ssl=1 1000w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyList-scaled.jpg?resize=230%2C91&amp;ssl=1 230w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyList-scaled.jpg?resize=350%2C138&amp;ssl=1 350w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyList-scaled.jpg?resize=480%2C189&amp;ssl=1 480w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyList-scaled.jpg?w=2340&amp;ssl=1 2340w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" data-recalc-dims=\"1\" \/><button\n\t\t\tclass=\"lightbox-trigger\"\n\t\t\ttype=\"button\"\n\t\t\taria-haspopup=\"dialog\"\n\t\t\taria-label=\"Enlarge image\"\n\t\t\tdata-wp-init=\"callbacks.initTriggerButton\"\n\t\t\tdata-wp-on-async--click=\"actions.showLightbox\"\n\t\t\tdata-wp-style--right=\"state.imageButtonRight\"\n\t\t\tdata-wp-style--top=\"state.imageButtonTop\"\n\t\t>\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewBox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\" \/>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure>\n\n\n\n<p>For the above illustrated graph which has 4 nodes, we create an array of size 5 (for easy 1-indexing), where each element in the array is a pointer to a linked list. Hence, the building block of our Adjacency List is an array of pointers. These pointers will each act as the head pointers of a linked list. Each linked list node is simple, it contains an integer and a next pointer.<\/p>\n\n\n\n<figure data-wp-context=\"{&quot;imageId&quot;:&quot;69d8cdb773946&quot;}\" data-wp-interactive=\"core\/image\" class=\"wp-block-image aligncenter size-medium wp-lightbox-container\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"70\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyList_ExampleVertex.jpg?resize=300%2C70&#038;ssl=1\" alt=\"\" class=\"wp-image-4058\" srcset=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyList_ExampleVertex.jpg?resize=300%2C70&amp;ssl=1 300w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyList_ExampleVertex.jpg?resize=1024%2C240&amp;ssl=1 1024w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyList_ExampleVertex.jpg?resize=768%2C180&amp;ssl=1 768w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyList_ExampleVertex.jpg?resize=1536%2C360&amp;ssl=1 1536w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyList_ExampleVertex.jpg?resize=1000%2C234&amp;ssl=1 1000w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyList_ExampleVertex.jpg?resize=230%2C54&amp;ssl=1 230w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyList_ExampleVertex.jpg?resize=350%2C82&amp;ssl=1 350w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyList_ExampleVertex.jpg?resize=480%2C113&amp;ssl=1 480w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyList_ExampleVertex.jpg?w=1809&amp;ssl=1 1809w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" data-recalc-dims=\"1\" \/><button\n\t\t\tclass=\"lightbox-trigger\"\n\t\t\ttype=\"button\"\n\t\t\taria-haspopup=\"dialog\"\n\t\t\taria-label=\"Enlarge image\"\n\t\t\tdata-wp-init=\"callbacks.initTriggerButton\"\n\t\t\tdata-wp-on-async--click=\"actions.showLightbox\"\n\t\t\tdata-wp-style--right=\"state.imageButtonRight\"\n\t\t\tdata-wp-style--top=\"state.imageButtonTop\"\n\t\t>\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewBox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\" \/>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure>\n\n\n\n<p>Like I stated before, the linked list will hold the list of vertices that are adjacent to the <strong>i<sup>th<\/sup><\/strong> vertex. From vertex 3, you can go to vertex 2 and 4. Hence the linked list corresponding to vertex 3 in the Adjacency List has elements 2 and 4.<\/p>\n\n\n\n<p>Similarly, for a weighted graph, we will just have an extra integer in the linked list node to store the weight of the edge &#8211;<\/p>\n\n\n\n<figure data-wp-context=\"{&quot;imageId&quot;:&quot;69d8cdb773d87&quot;}\" data-wp-interactive=\"core\/image\" class=\"wp-block-image size-large wp-lightbox-container\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"474\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyList_Weighted.jpg?resize=1024%2C474&#038;ssl=1\" alt=\"\" class=\"wp-image-4061\" srcset=\"https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyList_Weighted-scaled.jpg?resize=1024%2C474&amp;ssl=1 1024w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyList_Weighted-scaled.jpg?resize=300%2C139&amp;ssl=1 300w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyList_Weighted-scaled.jpg?resize=768%2C356&amp;ssl=1 768w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyList_Weighted-scaled.jpg?resize=1536%2C712&amp;ssl=1 1536w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyList_Weighted-scaled.jpg?resize=2048%2C949&amp;ssl=1 2048w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyList_Weighted-scaled.jpg?resize=1000%2C463&amp;ssl=1 1000w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyList_Weighted-scaled.jpg?resize=230%2C107&amp;ssl=1 230w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyList_Weighted-scaled.jpg?resize=350%2C162&amp;ssl=1 350w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyList_Weighted-scaled.jpg?resize=480%2C222&amp;ssl=1 480w, https:\/\/i0.wp.com\/theoryofcoding.com\/wp-content\/uploads\/2024\/01\/AdjacencyList_Weighted-scaled.jpg?w=2340&amp;ssl=1 2340w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" data-recalc-dims=\"1\" \/><button\n\t\t\tclass=\"lightbox-trigger\"\n\t\t\ttype=\"button\"\n\t\t\taria-haspopup=\"dialog\"\n\t\t\taria-label=\"Enlarge image\"\n\t\t\tdata-wp-init=\"callbacks.initTriggerButton\"\n\t\t\tdata-wp-on-async--click=\"actions.showLightbox\"\n\t\t\tdata-wp-style--right=\"state.imageButtonRight\"\n\t\t\tdata-wp-style--top=\"state.imageButtonTop\"\n\t\t>\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewBox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\" \/>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure>\n\n\n\n<p>I hope this makes it clear as to how we use the Adjacency List to implement a Graph. As I mentioned before, it is one of the most versatile implementations of the Graph Data Structure. Now, try to code the implementation in C, or any language you like. Please try this at least once by yourself so that you can get brain deep into the Graph Data Structure. It is only a linked list. And one more thing, don&#8217;t get confused between pointer-to-an-array and array-of-pointers&#8230;! So, if we put the value of <strong>|V| + 1<\/strong> in a variable &#8216;<em>vertices<\/em>&#8216;, now the declaration of our Adjacency List would be,<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nstruct node * adjacency_list&#x5B;vertices];\n<\/pre><\/pre>\n\n\n\n<p>Remember this is an &#8220;array of pointers&#8221;, the declaration for &#8220;pointer to an array&#8221; would look like,<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nstruct node (* adjcency_list)&#x5B;vertices];\n<\/pre><\/pre>\n\n\n\n<p>This is due to operator precedence. Some people make mistakes here, so, watch out! If you want to code in C++, we can have a vector where each element of the vector can hold another vector or a list, so, the declaration would be, like,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>vector&lt;list&lt;int&gt;&gt; adjacencyList(vertices + 1);<\/code><\/pre>\n\n\n\n<p>Try to code for an hour or two&#8230; If you don&#8217;t get the code, it&#8217;s ok&#8230;! I&#8217;ve put my C code below. Those who got the code right&#8230; You are fabulous&#8230;! \ud83d\ude09 &#8230; But please take a minute to go through my code, I might have written a slightly better code than yours.<\/p>\n\n\n<div class=\"su-tabs su-tabs-style-default su-tabs-mobile-stack\" data-active=\"1\" data-scroll-offset=\"0\" data-anchor-in-url=\"no\"><div class=\"su-tabs-nav\"><span class=\"\" data-url=\"\" data-target=\"blank\" tabindex=\"0\" role=\"button\">C<\/span><span class=\"\" data-url=\"\" data-target=\"blank\" tabindex=\"0\" role=\"button\">Java<\/span><span class=\"\" data-url=\"\" data-target=\"blank\" tabindex=\"0\" role=\"button\">C#<\/span><span class=\"\" data-url=\"\" data-target=\"blank\" tabindex=\"0\" role=\"button\">C++ (STL)<\/span><span class=\"\" data-url=\"\" data-target=\"blank\" tabindex=\"0\" role=\"button\">C++ (STL) (String vertices)<\/span><\/div><div class=\"su-tabs-panes\"><div class=\"su-tabs-pane su-u-clearfix su-u-trim\" data-title=\"C\">\n<script src=\"https:\/\/gist.github.com\/VamsiSangam\/4b1d76a5576836d90f9b95ba7e2ba36c.js\"><\/script><\/div>\n<div class=\"su-tabs-pane su-u-clearfix su-u-trim\" data-title=\"Java\">\n<script src=\"https:\/\/gist.github.com\/VamsiSangam\/f14d80aae0a63e6bd0134e90dcf4235d.js\"><\/script><br \/>\n<\/div>\n<div class=\"su-tabs-pane su-u-clearfix su-u-trim\" data-title=\"C#\">\n<script src=\"https:\/\/gist.github.com\/VamsiSangam\/16e8a84dd2c18a216b472aebc7b0ec01.js\"><\/script><br \/>\n<\/div>\n<div class=\"su-tabs-pane su-u-clearfix su-u-trim\" data-title=\"C++ (STL)\">\n<script src=\"https:\/\/gist.github.com\/VamsiSangam\/8fa5123ff5dcf9b41dbd30fc050e2fed.js\"><\/script><br \/>\n<\/div>\n<div class=\"su-tabs-pane su-u-clearfix su-u-trim\" data-title=\"C++ (STL) (String vertices)\">\n<script src=\"https:\/\/gist.github.com\/VamsiSangam\/d4b3b7ed4166d4d5042e8a2b358bb3f9.js\"><\/script><br \/>\n<\/div><\/div><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Comparison between Adjacency Matrix and Adjacency List<\/strong> &#8211;<\/h3>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table class=\"has-fixed-layout\"><tbody><tr><th>Adjacency Matrix<\/th><th>Adjacency List<\/th><\/tr><tr><td>For a Directed Graph, it consumes <em>O(|V|<sup>2<\/sup>)<\/em> space which is often under-utilized in the implementation. For an Undirected Graph also, it consumes <em>O(|V|<sup>2<\/sup>)<\/em> space which is also under-utilized as the generated matrix is symmetric about diagonal and values just repeat.<\/td><td>For a Directed Graph it consumes <em>O(|V| + |E|)<\/em> space which is less, and is utilized optimally. For an Undirected Graph it consumes <em>O(|V| + 2 \u2717 |E|)<\/em> space, which is <em>O(|V| + |E|)<\/em>, which is less.<\/td><\/tr><tr><td>As the memory allocation is contiguous, data access is slightly faster.<\/td><td>It is basically a Linked List, so memory allocation is not contiguous, hence traversal is slow as as compared to the traversal in an array. However, this can be eliminated if we use a Vector of C++ STL Library in the place of the Linked List. But C++ STL Vector is not recommended for graphs that keep growing.<\/td><\/tr><tr><td>Inserting an edge takes <em>O(1)<\/em> time, i.e., constant time. Therefore, inserting |E| edges take <em>O(|E|)<\/em> time, as direct access is available.<\/td><td>Inserting an edge can take <em>O(|E|)<\/em> in the worst case, which is, there is an edge between every pair of vertices, one must traverse the whole Linked List over-and-over causing insertion of |E| nodes to take <em>O(|E|<sup>2<\/sup>)<\/em> time. However, if one follows Head Insertion, inserting a node will take O(1) time, and inserting |E| nodes will take <em>O(|E|)<\/em> time.<\/td><\/tr><tr><td>Deleting an edge is very simple, we just set the corresponding element value to 0, which takes <em>O(1)<\/em> time.<\/td><td>Deleting an edge is time-taking as, one would have to traverse the Linked List, which is slow (non-contiguous). In the worst case, it takes <em>O(|E|)<\/em> time.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>With the knowledge of Adjacency Matrix and Adjacency List alone you won&#8217;t be able to do the competitive programming questions, because you do not know how to explore your graph to get the desired result. For this, you need to know the Breadth First Search (BFS) and the Depth First Search (DFS). This post is only to give an introduction. Feel free to comment your doubts..! Keep practicing&#8230;. Happy Coding&#8230;! \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello people&#8230;! In this post, I will talk about Graph Theory Basics, what are its terminologies, types and implementations in C. Graphs [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[6],"tags":[17,18,40,50],"class_list":["post-33","post","type-post","status-publish","format-standard","hentry","category-graph-theory","tag-adjacency-list","tag-adjacency-matrix","tag-data-structures","tag-graph-theory"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/posts\/33","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/comments?post=33"}],"version-history":[{"count":55,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/posts\/33\/revisions"}],"predecessor-version":[{"id":4102,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/posts\/33\/revisions\/4102"}],"wp:attachment":[{"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/media?parent=33"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/categories?post=33"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/tags?post=33"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}