TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
[Graph Dijkstras Shortest Path Algorithm (Dijkstra's Shortest Path)] (https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm) More...
#include <cassert>
#include <iostream>
#include <limits>
#include <memory>
#include <queue>
#include <utility>
#include <vector>
Go to the source code of this file.
Namespaces | |
namespace | graph |
Graph Algorithms. | |
Functions | |
void | graph::addEdge (std::vector< std::vector< std::pair< int, int > > > *adj, int u, int v, int w) |
Function that add edge between two nodes or vertices of graph. | |
int | graph::dijkstra (std::vector< std::vector< std::pair< int, int > > > *adj, int s, int t) |
Function runs the dijkstra algorithm for some source vertex and target vertex in the graph and returns the shortest distance of target from the source. | |
void | tests () |
int | main () |
Variables | |
constexpr int64_t | INF = std::numeric_limits<int64_t>::max() |
[Graph Dijkstras Shortest Path Algorithm (Dijkstra's Shortest Path)] (https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm)
Dijkstra's Algorithm is used to find the shortest path from a source vertex to all other reachable vertex in the graph. The algorithm initially assumes all the nodes are unreachable from the given source vertex so we mark the distances of all vertices as INF (infinity) from source vertex (INF / infinity denotes unable to reach).
in similar fashion with BFS we assume the distance of source vertex as 0 and pushes the vertex in a priority queue with it's distance. we maintain the priority queue as a min heap so that we can get the minimum element at the top of heap
Basically what we do in this algorithm is that we try to minimize the distances of all the reachable vertices from the current vertex, look at the code below to understand in better way.
Definition in file dijkstra.cpp.
int main | ( | void | ) |
Main function
Definition at line 152 of file dijkstra.cpp.
void tests | ( | ) |
Function to test the Algorithm
Definition at line 113 of file dijkstra.cpp.
|
constexpr |
Definition at line 34 of file dijkstra.cpp.