TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
dijkstra_greedy.cpp File Reference

Dijkstra algorithm implementation More...

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

Go to the source code of this file.

Classes

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

Namespaces

namespace  greedy_algorithms
 for string class
 
namespace  greedy_algorithms::dijkstra
 Functions for the Dijkstra algorithm implementation.
 

Functions

int greedy_algorithms::dijkstra::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::dijkstra::print (std::vector< int > dist, int V)
 Utility function to print the distances to vertices.
 
void greedy_algorithms::dijkstra::dijkstra (Graph graph, int src)
 The main function that finds the shortest path from a 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

Definition in file dijkstra_greedy.cpp.

Function Documentation

◆ main()

int main ( void )

Main function.

Returns
0 on exit

Definition at line 199 of file dijkstra_greedy.cpp.

199 {
200 tests(); // run self-test implementations
201 return 0;
202}
static void tests()
Self-test implementations.

◆ tests()

static void tests ( )
static

Self-test implementations.

Returns
void

Definition at line 160 of file dijkstra_greedy.cpp.

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