TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
[Bidirectional Dijkstra Shortest Path Algorithm] (https://www.coursera.org/learn/algorithms-on-graphs/lecture/7ml18/bidirectional-dijkstra) More...
#include <cassert>
#include <cstdint>
#include <iostream>
#include <limits>
#include <queue>
#include <utility>
#include <vector>
Go to the source code of this file.
Namespaces | |
namespace | graph |
Graph Algorithms. | |
namespace | bidirectional_dijkstra |
Functions for [Bidirectional Dijkstra Shortest Path] (https://www.coursera.org/learn/algorithms-on-graphs/lecture/7ml18/bidirectional-dijkstra) algorithm. | |
Functions | |
void | graph::bidirectional_dijkstra::addEdge (std::vector< std::vector< std::pair< uint64_t, uint64_t > > > *adj1, std::vector< std::vector< std::pair< uint64_t, uint64_t > > > *adj2, uint64_t u, uint64_t v, uint64_t w) |
Function that add edge between two nodes or vertices of graph. | |
uint64_t | graph::bidirectional_dijkstra::Shortest_Path_Distance (const std::vector< uint64_t > &workset_, const std::vector< std::vector< uint64_t > > &distance_) |
This function returns the shortest distance from the source to the target if there is path between vertices 's' and 't'. | |
int | graph::bidirectional_dijkstra::Bidijkstra (std::vector< std::vector< std::pair< uint64_t, uint64_t > > > *adj1, std::vector< std::vector< std::pair< uint64_t, uint64_t > > > *adj2, uint64_t s, uint64_t 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. | |
static void | tests () |
Function to test the provided algorithm above. | |
int | main () |
Main function. | |
Variables | |
constexpr int64_t | INF = std::numeric_limits<int64_t>::max() |
for assert | |
[Bidirectional Dijkstra Shortest Path Algorithm] (https://www.coursera.org/learn/algorithms-on-graphs/lecture/7ml18/bidirectional-dijkstra)
This is basically the same Dijkstra Algorithm but faster because it goes from the source to the target and from target to the source and stops when finding a vertex visited already by the direct search or the reverse one. Here some simulations of it: https://www.youtube.com/watch?v=DINCL5cd_w0&t=24s
Definition in file bidirectional_dijkstra.cpp.
void graph::bidirectional_dijkstra::addEdge | ( | std::vector< std::vector< std::pair< uint64_t, uint64_t > > > * | adj1, |
std::vector< std::vector< std::pair< uint64_t, uint64_t > > > * | adj2, | ||
uint64_t | u, | ||
uint64_t | v, | ||
uint64_t | w ) |
Function that add edge between two nodes or vertices of graph.
adj1 | adjacency list for the direct search |
adj2 | adjacency list for the reverse search |
u | any node or vertex of graph |
v | any node or vertex of graph |
Definition at line 46 of file bidirectional_dijkstra.cpp.
int graph::bidirectional_dijkstra::Bidijkstra | ( | std::vector< std::vector< std::pair< uint64_t, uint64_t > > > * | adj1, |
std::vector< std::vector< std::pair< uint64_t, uint64_t > > > * | adj2, | ||
uint64_t | s, | ||
uint64_t | 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.
adj1 | input graph |
adj2 | input graph reversed |
s | source vertex |
t | target vertex |
n denotes the number of vertices in graph
setting all the distances initially to INF
creating a a vector of min heap using priority queue pq[0] contains the min heap for the direct search pq[1] contains the min heap for the reverse search
first element of pair contains the distance second element of pair contains the vertex
vector for store the nodes or vertices in the shortest path
vector for store the nodes or vertices visited
pushing the source vertex 's' with 0 distance in pq[0] min heap
marking the distance of source as 0
pushing the target vertex 't' with 0 distance in pq[1] min heap
marking the distance of target as 0
direct search
second element of pair denotes the node / vertex
first element of pair denotes the distance
for all the reachable vertex from the currently exploring vertex we will try to minimize the distance
minimizing distances
check if currentNode has already been visited
reversed search
second element of pair denotes the node / vertex
first element of pair denotes the distance
for all the reachable vertex from the currently exploring vertex we will try to minimize the distance
minimizing distances
check if currentNode has already been visited
Definition at line 87 of file bidirectional_dijkstra.cpp.
int main | ( | void | ) |
Main function.
Definition at line 249 of file bidirectional_dijkstra.cpp.
uint64_t graph::bidirectional_dijkstra::Shortest_Path_Distance | ( | const std::vector< uint64_t > & | workset_, |
const std::vector< std::vector< uint64_t > > & | distance_ ) |
This function returns the shortest distance from the source to the target if there is path between vertices 's' and 't'.
workset_ | vertices visited in the search |
distance_ | vector of distances from the source to the target and from the target to the source |
Definition at line 62 of file bidirectional_dijkstra.cpp.
|
static |
Function to test the provided algorithm above.
Definition at line 200 of file bidirectional_dijkstra.cpp.
|
constexpr |
for assert
for io operations for variable INF for the priority_queue of distances for make_pair function for store the graph, the distances, and the path
Definition at line 24 of file bidirectional_dijkstra.cpp.