dijkstra c++ geeksforgeeks using set
C++
// A C++ program for Dijkstra's single source shortest path algorithm.
// The program is for adjacency matrix representation of the graph
#include <limits.h>
#include <stdio.h>
// Number of vertices in the graph
#define V 9
// A utility function to find the vertex with minimum distance value, from
// the set of vertices not yet included in shortest path tree
int minDistance(int dist[], bool sptSet[])
{
// Initialize min value
int min = INT_MAX, min_index;
for (int v = 0; v < V; v++)
if (sptSet[v] == false && dist[v] <= min)
min = dist[v], min_index = v;
return min_index;
}
// A utility function to print the constructed distance array
void printSolution(int dist[])
{
printf("Vertex \t\t Distance from Source\n");
for (int i = 0; i < V; i++)
printf("%d \t\t %d\n", i, dist[i]);
}
// Function that implements Dijkstra's single source shortest path algorithm
// for a graph represented using adjacency matrix representation
void dijkstra(int graph[V][V], int src)
{
int dist[V]; // The output array. dist[i] will hold the shortest
// distance from src to i
bool sptSet[V]; // sptSet[i] will be true if vertex i is included in shortest
// path tree or shortest distance from src to i is finalized
// Initialize all distances as INFINITE and stpSet[] as false
for (int i = 0; i < V; i++)
dist[i] = INT_MAX, sptSet[i] = false;
// Distance of source vertex from itself is always 0
dist[src] = 0;
// Find shortest path for all vertices
for (int count = 0; count < V - 1; count++) {
// Pick the minimum distance vertex from the set of vertices not
// yet processed. u is always equal to src in the first iteration.
int u = minDistance(dist, sptSet);
// Mark the picked vertex as processed
sptSet[u] = true;
// Update dist value of the adjacent vertices of the picked vertex.
for (int v = 0; v < V; v++)
// Update dist[v] only if is not in sptSet, there is an edge from
// u to v, and total weight of path from src to v through u is
// smaller than current value of dist[v]
if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX
&& dist[u] + graph[u][v] < dist[v])
dist[v] = dist[u] + graph[u][v];
}
// print the constructed distance array
printSolution(dist);
}
// driver program to test above function
int main()
{
/* Let us create the example graph discussed above */
int graph[V][V] = { { 0, 4, 0, 0, 0, 0, 0, 8, 0 },
{ 4, 0, 8, 0, 0, 0, 0, 11, 0 },
{ 0, 8, 0, 7, 0, 4, 0, 0, 2 },
{ 0, 0, 7, 0, 9, 14, 0, 0, 0 },
{ 0, 0, 0, 9, 0, 10, 0, 0, 0 },
{ 0, 0, 4, 14, 10, 0, 2, 0, 0 },
{ 0, 0, 0, 0, 0, 2, 0, 1, 6 },
{ 8, 11, 0, 0, 0, 0, 1, 0, 7 },
{ 0, 0, 2, 0, 0, 0, 6, 7, 0 } };
dijkstra(graph, 0);
return 0;
}
Also in C++:
- Title
- root to leaf path print
- Category
- C++
- Title
- c++ create object
- Category
- C++
- Title
- how read a shader from another file c++
- Category
- C++
- Title
- c++ assert
- Category
- C++
- Title
- gta san andreas
- Category
- C++
- Title
- how to initialize a vector in c++
- Category
- C++
- Title
- switch statement c++
- Category
- C++
- Title
- declaring 2d vector in c++
- Category
- C++
- Title
- c++ std::copy to cout
- Category
- C++
- Title
- bool function in c++
- Category
- C++
- Title
- opencv compile c++
- Category
- C++
- Title
- heap in cpp stl
- Category
- C++
- Title
- c++ empty stream
- Category
- C++
- Title
- how to delete a node c++
- Category
- C++
- Title
- C++ remove element from set
- Category
- C++
- Title
- calling by reference c++
- Category
- C++
- Title
- c++ compare char array
- Category
- C++
- Title
- object slicing in c++
- Category
- C++
- Title
- time function c++
- Category
- C++
- Title
- varint index
- Category
- C++
- Title
- c++ compare char
- Category
- C++
- Title
- c++ class inheritance
- Category
- C++
- Title
- how initilaize deffult value to c++ class
- Category
- C++
- Title
- how to convert n space separated integers in c++
- Category
- C++
- Title
- cannot jump from switch statement to this case label c++
- Category
- C++
- Title
- capitalize first letter c++
- Category
- C++
- Title
- list conda environments
- Category
- C++
- Title
- generate random double c++
- Category
- C++
- Title
- c++ yes no question
- Category
- C++
- Title
- how to get the prime number in c++ where time complexity is 0(log n)
- Category
- C++
- Title
- C++ string format ctime
- Category
- C++
- Title
- c++ uint32_t
- Category
- C++
- Title
- minmax_element c++
- Category
- C++
- Title
- how to find the size of a character array in c++
- Category
- C++
- Title
- chess perft 5
- Category
- C++
- Title
- linear search in c++
- Category
- C++
- Title
- iostream library in cpp
- Category
- C++
- Title
- variabvles in c++
- Category
- C++
- Title
- lambda operator in c++
- Category
- C++
- Title
- how to convert a string to a double c++
- Category
- C++
- Title
- how to get input from the console in c++
- Category
- C++
- Title
- Operator overloading in C++ Programming
- Category
- C++
- Title
- inconsequential meaning
- Category
- C++
- Title
- C++ Syntax
- Category
- C++
- Title
- how to iterate trough a vector in c++
- Category
- C++
- Title
- how to print nth palindrome number in c++
- Category
- C++
- Title
- how to check datatype of a variable in c++
- Category
- C++
- Title
- mark occurances of elements in array cpp
- Category
- C++
- Title
- system("pause") note working c++
- Category
- C++
- Title
- fill c++
- Category
- C++
- Title
- add a timer c++
- Category
- C++
- Title
- recursion in cpp with reference
- Category
- C++
- Title
- clear file before writing c++
- Category
- C++
- Title
- max three values c++
- Category
- C++
- Title
- c++ triple
- Category
- C++
- Title
- memcmp in cpp
- Category
- C++
- Title
- how to execute c++ program in cmd
- Category
- C++
- Title
- c++ loop trhought object
- Category
- C++
- Title
- how to sort a vector in reverse c++
- Category
- C++
- Title
- how to sort vector in c++
- Category
- C++
- Title
- what does count function do in hashmap
- Category
- C++
- Title
- sub string of vector c++
- Category
- C++
- Title
- c++ client service ros
- Category
- C++
- Title
- c++ program for matrix addition
- Category
- C++
- Title
- length of array in cpp
- Category
- C++
- Title
- c++ do you not inherit constructor
- Category
- C++
- Title
- printf c++
- Category
- C++
- Title
- how to iterate through array in c++
- Category
- C++
- Title
- cut by delimiter c++
- Category
- C++
- Title
- tuple c++
- Category
- C++
- Title
- c++ typeid get type name
- Category
- C++
- Title
- repeating character in c++
- Category
- C++
- Title
- print matrix c++
- Category
- C++
- Title
- string input
- Category
- C++
- Title
- command line options in c++
- Category
- C++
- Title
- residuo en lenguaje c
- Category
- C++
- Title
- c++ cout int
- Category
- C++
- Title
- flushing output in c++
- Category
- C++
- Title
- variabili in c++
- Category
- C++
- Title
- compile c++ program
- Category
- C++
- Title
- border radius layout android xml
- Category
- C++
- Title
- stack function in cpp
- Category
- C++
- Title
- msdn parse command line
- Category
- C++
- Title
- c++ calculator program using switch case
- Category
- C++
- Title
- else if c++
- Category
- C++
- Title
- UPARAM(ref)
- Category
- C++
- Title
- min and max heap in cpp
- Category
- C++
- Title
- compare function in sort c++ stl
- Category
- C++
- Title
- what is time complexity of min_element()
- Category
- C++
- Title
- do while loop c++
- Category
- C++
- Title
- cpp nan value
- Category
- C++
- Title
- new class * [] c++
- Category
- C++
- Title
- tree traversal c++ in order
- Category
- C++
- Title
- max in c++
- Category
- C++
- Title
- c++ clear stream
- Category
- C++
- Title
- how to run c++ file mingw cmd
- Category
- C++
- Title
- if not defined c++
- Category
- C++
- Title
- stringstream in c++
- Category
- C++
- Title
- mkdir boost filesystem
- Category
- C++
- Title
- sleep system function linux c++
- Category
- C++