{"id":17705,"date":"2023-08-25T06:33:04","date_gmt":"2023-08-25T06:33:04","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=17705"},"modified":"2024-02-08T05:47:05","modified_gmt":"2024-02-08T05:47:05","slug":"dijkstras-algorithm","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/dijkstras-algorithm\/","title":{"rendered":"Dijkstra\u2019s algorithm"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1692944924737-Topic%20%2846%29.jpg\" alt=\"\" \/><\/p>\n<p>Dijkstra&#8217;s algorithm, named after Dutch computer scientist Edsger W. Dijkstra, is a fundamental algorithm used in graph theory and computer science for finding the shortest path between nodes in a graph with non-negative edge weights. Originally designed to solve the single-source shortest path problem, Dijkstra&#8217;s algorithm efficiently determines the shortest path from a designated starting node to all other nodes in the graph. Its simplicity and effectiveness make it a widely used tool in various applications, including network routing protocols, transportation networks, and data processing systems.<\/p>\n<h2>How Dijkstra algorithm Work?<\/h2>\n<p>The algorithm works by maintaining a set of vertices that have already been visited, and a set of vertices that have not yet been visited. The algorithm starts at the source vertex and adds it to the set of visited vertices. Then, it repeatedly finds the vertex in the set of unvisited vertices that has the shortest known path to the source vertex, and adds that vertex to the set of visited vertices. This process continues until all vertices have been visited.<br \/>\n<img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1692944924726-1-01%20%2871%29.png\" alt=\"\" \/><\/p>\n<p>The Dijkstra algorithm is a greedy algorithm, which means that it makes the best possible choice at each step, without considering the future consequences of its decisions. In the case of Dijkstra algorithm, the best possible choice is to add the vertex with the shortest known path to the source vertex to the set of visited vertices.<\/p>\n<p>The Dijkstra algorithm is a very efficient algorithm for finding the shortest paths in a graph. Its time complexity is O(V log V), where V is the number of vertices in the graph.<\/p>\n<h2>Example of Dijkstra algorithm<\/h2>\n<p>Here is an example of Dijkstra algorithm that is discussed below.<\/p>\n<p><strong>Step 1 :<\/strong> Start with a weighted graph.<br \/>\n<img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1692944924712-1-02%20%2846%29.png\" alt=\"\" \/><\/p>\n<p><strong>Step 2 :<\/strong> Choose a starting vertex and assign infinity path values to all other devices.<br \/>\n<img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1692944924705-1-03%20%2819%29.png\" alt=\"\" \/><\/p>\n<p><strong>Step 3 :<\/strong> Go to each vertex and update its path length.<br \/>\n<img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1692944924690-1-04%20%2813%29.png\" alt=\"\" \/><\/p>\n<p><strong>Step 4 :<\/strong> If the path length of the adjacent vertex is less than new path length, don&#8217;t update it.<br \/>\n<img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1692944924685-1-05%20%2812%29.png\" alt=\"\" \/><\/p>\n<p><strong>Step 5 :<\/strong> Avoid updating path lengths of already visited vertices.<br \/>\n<img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1692944924668-1-06%20%288%29.png\" alt=\"\" \/><\/p>\n<p><strong>Step 6 :<\/strong> After each iteration, we pick the unvisited vertex with the least path length. So we choose 5.<br \/>\n<img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1692944924660-1-07%20%285%29.png\" alt=\"\" \/><\/p>\n<p><strong>Step 7 :<\/strong> Notice how the rightmost vertex has its path length updated twice.<br \/>\n<img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1692944924646-1-08%20%282%29.png\" alt=\"\" \/><\/p>\n<p><strong>Step 8 :<\/strong>  Repeat until all the vertices have been visited.<br \/>\n<img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1692944924610-1-09%20%281%29.png\" alt=\"\" \/><\/p>\n<h2>Pseudocode for Dijkstra algorithm<\/h2>\n<p>The following is a pseudocode for Dijkstra algorithm:<\/p>\n<pre><code>procedure Dijkstra(G, s)\n    for each vertex v in G\n        dist[v] := infinity\n        prev[v] := nil\n    dist[s] := 0\n    Q := {s}\n    while Q is not empty\n        u := vertex in Q with minimum dist[u]\n        remove u from Q\n        for each vertex v adjacent to u\n            if dist[v] &gt; dist[u] + weight(u, v)\n                dist[v] := dist[u] + weight(u, v)\n                prev[v] := u<\/code><\/pre>\n<p>In this pseudocode, <code>G<\/code> is the graph, <code>s<\/code> is the source vertex, <code>dist[v]<\/code> is the shortest known path from <code>s<\/code> to vertex <code>v<\/code>, and <code>prev[v]<\/code> is the predecessor of vertex <code>v<\/code> in the shortest path from <code>s<\/code> to <code>v<\/code>.<\/p>\n<h2>Code Implementation of Dijkstra algorithm<\/h2>\n<p>Here is a code implementation of the Dijkstra algorithm in C++.<\/p>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_17713 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_17713 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_17713 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_17713 .wpsm_nav-tabs > li.active > a, #tab_container_17713 .wpsm_nav-tabs > li.active > a:hover, #tab_container_17713 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_17713 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_17713 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_17713 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_17713 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_17713 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_17713 .wpsm_nav-tabs > li > a:hover , #tab_container_17713 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_17713 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_17713 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_17713 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_17713 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_17713 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_17713 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_17713 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_17713 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_17713 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_17713 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_17713 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_17713\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_17713\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_17713_1\" aria-controls=\"tabs_desc_17713_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>CPP<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_17713\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_17713_1\">\r\n\t\t\t\t\t\t\t\t<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\">#include &lt;iostream&gt;\r\n#include &lt;vector&gt;\r\n#include &lt;queue&gt;\r\n\r\nusing namespace std;\r\n\r\n\/\/ A structure to represent a weighted edge\r\nstruct Edge {\r\n  int src;\r\n  int dest;\r\n  int weight;\r\n};\r\n\r\n\/\/ A class to represent a graph\r\nclass Graph {\r\nprivate:\r\n  int V; \/\/ number of vertices\r\n  vector&lt;vector&lt;Edge&gt;&gt; adj; \/\/ adjacency list\r\n\r\npublic:\r\n  Graph(int V) {\r\n    this-&gt;V = V;\r\n    adj.resize(V);\r\n  }\r\n\r\n  void addEdge(int src, int dest, int weight) {\r\n    Edge edge{src, dest, weight};\r\n    adj[src].push_back(edge);\r\n  }\r\n\r\n  \/\/ Dijkstra's algorithm\r\n  void dijkstra(int src, vector&lt;int&gt;&amp; dist) {\r\n    \/\/ Initialize the distance array\r\n    dist.resize(V, INT_MAX);\r\n\r\n    \/\/ Create a priority queue to store the vertices\r\n    priority_queue&lt;pair&lt;int, int&gt;, vector&lt;pair&lt;int, int&gt;&gt;, greater&lt;pair&lt;int, int&gt;&gt;&gt; pq;\r\n\r\n    \/\/ Add the source vertex to the priority queue\r\n    pq.push({0, src});\r\n\r\n    \/\/ Mark the source vertex as visited\r\n    dist[src] = 0;\r\n\r\n    while (!pq.empty()) {\r\n      \/\/ Get the vertex with the shortest distance\r\n      int u = pq.top().second;\r\n      pq.pop();\r\n\r\n      \/\/ Mark the vertex as visited\r\n      visited[u] = true;\r\n\r\n      \/\/ For each unvisited neighbor of u\r\n      for (Edge edge : adj[u]) {\r\n        int v = edge.dest;\r\n\r\n        \/\/ If the distance to v is greater than the distance to u plus the weight of the edge\r\n        if (dist[v] &gt; dist[u] + edge.weight) {\r\n          \/\/ Update the distance to v\r\n          dist[v] = dist[u] + edge.weight;\r\n\r\n          \/\/ Add v to the priority queue\r\n          pq.push({dist[v], v});\r\n        }\r\n      }\r\n    }\r\n  }\r\n};\r\n\r\nint main() {\r\n  \/\/ Create a graph\r\n  Graph g(5);\r\n\r\n  \/\/ Add edges to the graph\r\n  g.addEdge(0, 1, 1);\r\n  g.addEdge(0, 2, 4);\r\n  g.addEdge(1, 2, 2);\r\n  g.addEdge(1, 3, 7);\r\n  g.addEdge(2, 3, 3);\r\n  g.addEdge(2, 4, 5);\r\n  g.addEdge(3, 4, 2);\r\n\r\n  \/\/ Initialize the distance array\r\n  vector&lt;int&gt; dist(g.V, INT_MAX);\r\n\r\n  \/\/ Run Dijkstra's algorithm\r\n  g.dijkstra(0, dist);\r\n\r\n  \/\/ Print the shortest distances from vertex 0 to all other vertices\r\n  for (int i = 0; i &lt; g.V; i++) {\r\n    cout &lt;&lt; \"Shortest distance from vertex 0 to vertex \" &lt;&lt; i &lt;&lt; \" is \" &lt;&lt; dist[i] &lt;&lt; endl;\r\n  }\r\n\r\n  return 0;\r\n}\r\n<\/pre>\r\n&nbsp;\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_17713 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_17713 a\"),jQuery(\"#tab-content_17713\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<p><strong>Explanation :<\/strong>  This code first creates a graph object with 5 vertices. Then, it adds edges to the graph. Next, it initializes the distance array to INT_MAX, which represents infinity. Finally, it runs Dijkstra&#8217;s algorithm and prints the shortest distances from vertex 0 to all other vertices.<\/p>\n<h2>Applications of Dijkstra algorithm<\/h2>\n<p>Dijkstra algorithm is extremely versatile and has a wide range of applications. Among its most common applications are:<\/p>\n<ul>\n<li>Routing in computer networks<\/li>\n<li>Finding the shortest path between two points in a road network<\/li>\n<li>Finding the shortest path between two genes in a DNA sequence<\/li>\n<li>Finding the shortest path between two products in a supply chain<\/li>\n<li>Finding the shortest path between two servers in a cloud computing environment<\/li>\n<\/ul>\n<p>Dijkstra algorithm is also used in a variety of other applications, such as:<\/p>\n<ul>\n<li>Finding the best route for a robot to navigate through a factory<\/li>\n<li>Finding the best way to schedule tasks in a manufacturing process<\/li>\n<li>Finding the best way to allocate resources in a computer system<\/li>\n<\/ul>\n<h2>Complexity of Dijkstra algorithm<\/h2>\n<p>The time complexity of Dijkstra algorithm is O(V log V), where V is the number of vertices in the graph. The space complexity of the algorithm is O(V), where V is the number of vertices in the graph.<\/p>\n<h2>Some Variations of Dijkstra algorithm<\/h2>\n<p>There are many variations of Dijkstra algorithm. Some of the most common variations include:<\/p>\n<ul>\n<li>The Bellman-Ford algorithm, which is a more general algorithm that can be used to find the shortest paths in graphs with negative weights.<\/li>\n<li>The Floyd-Warshall algorithm, which is a more efficient algorithm for finding the shortest paths in graphs with dense connectivity.<\/li>\n<\/ul>\n<p>The algorithm is a more sophisticated algorithm that can be used to find the shortest paths in graphs with obstacles.<\/p>\n<p><strong>Conclusion<\/strong><br \/>\nIn conclusion, Dijkstra&#8217;s algorithm stands as a cornerstone in the field of graph theory and computer science, offering an elegant solution to the single-source shortest path problem. Its efficiency and versatility have made it a vital component in the development of various real-world applications, facilitating optimal routing, resource allocation, and decision-making processes. As technology continues to evolve, Dijkstra&#8217;s algorithm remains a timeless algorithm, embodying the essence of computational efficiency and problem-solving prowess.<\/p>\n<h2>Frequently Asked Questions (FAQs) related to the Dijkstra Algorithm<\/h2>\n<p>Here are some frequently asked questions about Dijkstra algorithm.<\/p>\n<p><strong>1. What is the single-source shortest path problem, and how does Dijkstra&#8217;s algorithm solve it?<\/strong><br \/>\nThe single-source shortest path problem involves finding the shortest paths from a designated starting node to all other nodes in a graph. Dijkstra&#8217;s algorithm efficiently solves this problem by iteratively exploring the graph from the starting node, updating the shortest path distances to each node as it progresses.<\/p>\n<p><strong>2. What are the key characteristics of Dijkstra&#8217;s algorithm?<\/strong><br \/>\nDijkstra&#8217;s algorithm is known for its simplicity, efficiency, and effectiveness in finding shortest paths in graphs with non-negative edge weights. It employs a greedy approach, selecting the node with the smallest tentative distance at each step and updating the shortest path distances accordingly.<\/p>\n<p><strong>3. What is the time complexity of Dijkstra&#8217;s algorithm?<\/strong><br \/>\nThe time complexity of Dijkstra&#8217;s algorithm depends on the implementation and the data structures used. In the simplest form, with an adjacency matrix representation, the time complexity is O(V^2), where V is the number of vertices in the graph. However, with a more efficient implementation using a priority queue, the time complexity can be reduced to O((V + E) log V), where E is the number of edges.<\/p>\n<p><strong>4. Does Dijkstra&#8217;s algorithm work for graphs with negative edge weights?<\/strong><br \/>\nNo, Dijkstra&#8217;s algorithm does not work for graphs with negative edge weights. This is because it relies on the assumption that all edge weights are non-negative. If negative edge weights are present, Dijkstra&#8217;s algorithm may produce incorrect results or enter into an infinite loop.<\/p>\n<p><strong>5. How can I implement Dijkstra&#8217;s algorithm in my programming projects?<\/strong><br \/>\nDijkstra&#8217;s algorithm can be implemented in various programming languages using different data structures such as arrays, priority queues, or adjacency lists. There are numerous online resources, tutorials, and libraries available to help with the implementation of Dijkstra&#8217;s algorithm in different programming environments.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Dijkstra&#8217;s algorithm, named after Dutch computer scientist Edsger W. Dijkstra, is a fundamental algorithm used in graph theory and computer science for finding the shortest path between nodes in a graph with non-negative edge weights. Originally designed to solve the single-source shortest path problem, Dijkstra&#8217;s algorithm efficiently determines the shortest path from a designated starting [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[191],"tags":[],"class_list":["post-17705","post","type-post","status-publish","format-standard","hentry","category-data-structure"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Dijkstra\u2019s algorithm<\/title>\n<meta name=\"description\" content=\"Dijkstra\u2019s algorithm is an algorithm for finding the shortest paths between nodes in a weighted graph.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prepbytes.com\/blog\/dijkstras-algorithm\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Dijkstra\u2019s algorithm\" \/>\n<meta property=\"og:description\" content=\"Dijkstra\u2019s algorithm is an algorithm for finding the shortest paths between nodes in a weighted graph.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/dijkstras-algorithm\/\" \/>\n<meta property=\"og:site_name\" content=\"PrepBytes Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prepbytes0211\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-08-25T06:33:04+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-02-08T05:47:05+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1692944924737-Topic%20%2846%29.jpg\" \/>\n<meta name=\"author\" content=\"Prepbytes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prepbytes\" \/>\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:\/\/prepbytes.com\/blog\/dijkstras-algorithm\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/dijkstras-algorithm\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"Dijkstra\u2019s algorithm\",\"datePublished\":\"2023-08-25T06:33:04+00:00\",\"dateModified\":\"2024-02-08T05:47:05+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/dijkstras-algorithm\/\"},\"wordCount\":1130,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/dijkstras-algorithm\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1692944924737-Topic%20%2846%29.jpg\",\"articleSection\":[\"Data Structure\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/dijkstras-algorithm\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/dijkstras-algorithm\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/dijkstras-algorithm\/\",\"name\":\"Dijkstra\u2019s algorithm\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/dijkstras-algorithm\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/dijkstras-algorithm\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1692944924737-Topic%20%2846%29.jpg\",\"datePublished\":\"2023-08-25T06:33:04+00:00\",\"dateModified\":\"2024-02-08T05:47:05+00:00\",\"description\":\"Dijkstra\u2019s algorithm is an algorithm for finding the shortest paths between nodes in a weighted graph.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/dijkstras-algorithm\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/dijkstras-algorithm\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/dijkstras-algorithm\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1692944924737-Topic%20%2846%29.jpg\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1692944924737-Topic%20%2846%29.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/dijkstras-algorithm\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Data Structure\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/data-structure\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Dijkstra\u2019s algorithm\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/43.205.93.38\/#website\",\"url\":\"http:\/\/43.205.93.38\/\",\"name\":\"PrepBytes Blog\",\"description\":\"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING\",\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/43.205.93.38\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/43.205.93.38\/#organization\",\"name\":\"Prepbytes\",\"url\":\"http:\/\/43.205.93.38\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"contentUrl\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"width\":160,\"height\":160,\"caption\":\"Prepbytes\"},\"image\":{\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/prepbytes0211\/\",\"https:\/\/www.instagram.com\/prepbytes\/\",\"https:\/\/www.linkedin.com\/company\/prepbytes\/\",\"https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\",\"name\":\"Prepbytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"caption\":\"Prepbytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Dijkstra\u2019s algorithm","description":"Dijkstra\u2019s algorithm is an algorithm for finding the shortest paths between nodes in a weighted graph.","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:\/\/prepbytes.com\/blog\/dijkstras-algorithm\/","og_locale":"en_US","og_type":"article","og_title":"Dijkstra\u2019s algorithm","og_description":"Dijkstra\u2019s algorithm is an algorithm for finding the shortest paths between nodes in a weighted graph.","og_url":"https:\/\/prepbytes.com\/blog\/dijkstras-algorithm\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2023-08-25T06:33:04+00:00","article_modified_time":"2024-02-08T05:47:05+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1692944924737-Topic%20%2846%29.jpg","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/dijkstras-algorithm\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/dijkstras-algorithm\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"Dijkstra\u2019s algorithm","datePublished":"2023-08-25T06:33:04+00:00","dateModified":"2024-02-08T05:47:05+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/dijkstras-algorithm\/"},"wordCount":1130,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/dijkstras-algorithm\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1692944924737-Topic%20%2846%29.jpg","articleSection":["Data Structure"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/dijkstras-algorithm\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/dijkstras-algorithm\/","url":"https:\/\/prepbytes.com\/blog\/dijkstras-algorithm\/","name":"Dijkstra\u2019s algorithm","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/dijkstras-algorithm\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/dijkstras-algorithm\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1692944924737-Topic%20%2846%29.jpg","datePublished":"2023-08-25T06:33:04+00:00","dateModified":"2024-02-08T05:47:05+00:00","description":"Dijkstra\u2019s algorithm is an algorithm for finding the shortest paths between nodes in a weighted graph.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/dijkstras-algorithm\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/dijkstras-algorithm\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/dijkstras-algorithm\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1692944924737-Topic%20%2846%29.jpg","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1692944924737-Topic%20%2846%29.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/dijkstras-algorithm\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"Data Structure","item":"https:\/\/prepbytes.com\/blog\/category\/data-structure\/"},{"@type":"ListItem","position":3,"name":"Dijkstra\u2019s algorithm"}]},{"@type":"WebSite","@id":"http:\/\/43.205.93.38\/#website","url":"http:\/\/43.205.93.38\/","name":"PrepBytes Blog","description":"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING","publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/43.205.93.38\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/43.205.93.38\/#organization","name":"Prepbytes","url":"http:\/\/43.205.93.38\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/","url":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","contentUrl":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","width":160,"height":160,"caption":"Prepbytes"},"image":{"@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/prepbytes0211\/","https:\/\/www.instagram.com\/prepbytes\/","https:\/\/www.linkedin.com\/company\/prepbytes\/","https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA"]},{"@type":"Person","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e","name":"Prepbytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","caption":"Prepbytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/17705","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=17705"}],"version-history":[{"count":3,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/17705\/revisions"}],"predecessor-version":[{"id":18816,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/17705\/revisions\/18816"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=17705"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=17705"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=17705"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}