TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Toggle main menu visibility
Loading...
Searching...
No Matches
dijkstra_greedy.cpp
Go to the documentation of this file.
1
16
17
#include <cassert>
18
#include <climits>
19
#include <iostream>
20
#include <vector>
21
26
namespace
greedy_algorithms
{
31
namespace
dijkstra
{
35
class
Graph
{
36
public
:
37
int
vertexNum = 0;
38
std::vector<std::vector<int>> edges{};
39
44
explicit
Graph
(
const
int
V) {
45
// Initialize the array edges
46
this->edges = std::vector<std::vector<int>>(V, std::vector<int>(V, 0));
47
for
(
int
i = 0; i < V; i++) {
48
edges[i] = std::vector<int>(V, 0);
49
}
50
51
// Fills the array with zeros
52
for
(
int
i = 0; i < V; i++) {
53
for
(
int
j = 0; j < V; j++) {
54
edges[i][j] = 0;
55
}
56
}
57
58
this->vertexNum = V;
59
}
60
68
void
add_edge
(
int
src,
int
dst,
int
weight) {
69
this->edges[src][dst] = weight;
70
}
71
};
72
82
int
minimum_distance
(std::vector<int> mdist, std::vector<bool> vset,
int
V) {
83
int
minVal = INT_MAX, minInd = 0;
84
for
(
int
i = 0; i < V; i++) {
85
if
(!vset[i] && (mdist[i] < minVal)) {
86
minVal = mdist[i];
87
minInd = i;
88
}
89
}
90
91
return
minInd;
92
}
93
104
void
print
(std::vector<int> dist,
int
V) {
105
std::cout <<
"\nVertex Distance\n"
;
106
for
(
int
i = 0; i < V; i++) {
107
if
(dist[i] < INT_MAX) {
108
std::cout << i <<
"\t"
<< dist[i] <<
"\n"
;
109
}
110
else
{
111
std::cout << i <<
"\tINF"
<<
"\n"
;
112
}
113
}
114
}
115
124
void
dijkstra
(
Graph
graph
,
int
src) {
125
int
V =
graph
.vertexNum;
126
std::vector<int> mdist{};
// Stores updated distances to the vertex
127
std::vector<bool> vset{};
// `vset[i]` is true if the vertex `i` is included in the shortest path tree
128
129
// Initialize `mdist and `vset`. Set the distance of the source as zero
130
for
(
int
i = 0; i < V; i++) {
131
mdist[i] = INT_MAX;
132
vset[i] =
false
;
133
}
134
135
mdist[src] = 0;
136
137
// iterate to find the shortest path
138
for
(
int
count = 0; count < V - 1; count++) {
139
int
u =
minimum_distance
(mdist, vset, V);
140
141
vset[u] =
true
;
142
143
for
(
int
v = 0; v < V; v++) {
144
if
(!vset[v] &&
graph
.edges[u][v] &&
145
mdist[u] +
graph
.edges[u][v] < mdist[v]) {
146
mdist[v] = mdist[u] +
graph
.edges[u][v];
147
}
148
}
149
}
150
151
print
(mdist, V);
152
}
153
}
// namespace dijkstra
154
}
// namespace greedy_algorithms
155
160
static
void
tests
() {
161
greedy_algorithms::dijkstra::Graph
graph
(8);
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
}
194
199
int
main
() {
200
tests
();
// run self-test implementations
201
return
0;
202
}
greedy_algorithms::dijkstra::Graph
Wrapper class for storing a graph.
Definition
dijkstra_greedy.cpp:35
greedy_algorithms::dijkstra::Graph::add_edge
void add_edge(int src, int dst, int weight)
Adds an edge to the graph.
Definition
dijkstra_greedy.cpp:68
greedy_algorithms::dijkstra::Graph::Graph
Graph(const int V)
Constructs a graph.
Definition
dijkstra_greedy.cpp:44
main
int main()
Main function.
Definition
generate_parentheses.cpp:110
graph
Graph Algorithms.
greedy_algorithms::dijkstra::print
void print(std::vector< int > dist, int V)
Utility function to print the distances to vertices.
Definition
dijkstra_greedy.cpp:104
greedy_algorithms::dijkstra::minimum_distance
int minimum_distance(std::vector< int > mdist, std::vector< bool > vset, int V)
Utility function that finds the vertex with the minimum distance in mdist.
Definition
dijkstra_greedy.cpp:82
greedy_algorithms::dijkstra::dijkstra
void dijkstra(Graph graph, int src)
The main function that finds the shortest path from a given source to all other vertices using Dijkst...
Definition
dijkstra_greedy.cpp:124
greedy_algorithms
for string class
Definition
binary_addition.cpp:27
tests
Testcases to check Union of Two Arrays.
greedy_algorithms
dijkstra_greedy.cpp
Generated by
1.18.0