Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
dijkstra.cpp File Reference

Dijkstra algorithm implementation More...

#include <cassert>
#include <climits>
#include <iostream>
#include <vector>
Include dependency graph for dijkstra.cpp:

Classes

class  greedy_algorithms::Graph
 Wrapper class for storing a graph. More...
 

Namespaces

namespace  greedy_algorithms
 for std::vector
 

Functions

int greedy_algorithms::minimum_distance (std::vector< int > mdist, std::vector< bool > vset, int V)
 Utility function that finds the vertex with the minimum distance in mdist.
 
void greedy_algorithms::print (std::vector< int > dist, int V)
 Utility function to print the distances to vertices.
 
void greedy_algorithms::dijkstra (Graph graph, int src)
 The main function that finds the shortest path from given source to all other vertices using Dijkstra's Algorithm.
 
static void tests ()
 Self-test implementations.
 
int main ()
 Main function.
 

Detailed Description

Dijkstra algorithm implementation

Quote from Wikipedia.

Dijkstra's algorithm is an algorithm for finding the shortest paths between nodes in a weighted graph, which may represent, for example, road networks. It was conceived by computer scientist Edsger W. Dijkstra in 1956 and published three years later.

Author
David Leal
Arpan Jain

Function Documentation

◆ main()

int main ( void  )

Main function.

Returns
0 on exit
196 {
197 tests(); // run self-test implementations
198 return 0;
199}
void tests()
Definition dijkstra.cpp:113
Here is the call graph for this function:

◆ tests()

static void tests ( )
static

Self-test implementations.

Returns
void
157 {
159
160 // 1st test.
161 graph.add_edge(6, 2, 4);
162 graph.add_edge(2, 6, 4);
163
164 assert(graph.edges[6][2] == 4);
165
166 // 2nd test.
167 graph.add_edge(0, 1, 1);
168 graph.add_edge(1, 0, 1);
169
170 assert(graph.edges[0][1] == 1);
171
172 // 3rd test.
173 graph.add_edge(0, 2, 7);
174 graph.add_edge(2, 0, 7);
175 graph.add_edge(1, 2, 1);
176 graph.add_edge(2, 1, 1);
177
178 assert(graph.edges[0][2] == 7);
179
180 // 4th test.
181 graph.add_edge(1, 3, 3);
182 graph.add_edge(3, 1, 3);
183 graph.add_edge(1, 4, 2);
184 graph.add_edge(4, 1, 2);
185 graph.add_edge(2, 3, 2);
186
187 assert(graph.edges[1][3] == 3);
188
189 std::cout << "All tests have successfully passed!\n";
190}
Wrapper class for storing a graph.
Definition dijkstra.cpp:33
Graph Algorithms.