TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Toggle main menu visibility
Loading...
Searching...
No Matches
dijkstra.cpp
Go to the documentation of this file.
1
26
#include <cassert>
27
#include <iostream>
28
#include <limits>
29
#include <memory>
30
#include <queue>
31
#include <utility>
32
#include <vector>
33
34
constexpr
int64_t
INF
= std::numeric_limits<int64_t>::max();
35
40
41
namespace
graph
{
48
void
addEdge
(std::vector<std::vector<std::pair<int, int>>> *adj,
int
u,
int
v,
49
int
w) {
50
(*adj)[u - 1].push_back(std::make_pair(v - 1, w));
51
// (*adj)[v - 1].push_back(std::make_pair(u - 1, w));
52
}
53
66
int
dijkstra
(std::vector<std::vector<std::pair<int, int>>> *adj,
int
s,
int
t) {
68
int
n = adj->size();
69
71
std::vector<int64_t> dist(n,
INF
);
72
76
std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int>>,
77
std::greater<std::pair<int, int>>>
78
pq;
79
81
pq.push(std::make_pair(0, s));
82
84
dist[s] = 0;
85
86
while
(!pq.empty()) {
88
int
currentNode = pq.top().second;
89
91
int
currentDist = pq.top().first;
92
93
pq.pop();
94
97
for
(std::pair<int, int> edge : (*adj)[currentNode]) {
99
if
(currentDist + edge.second < dist[edge.first]) {
100
dist[edge.first] = currentDist + edge.second;
101
pq.push(std::make_pair(dist[edge.first], edge.first));
102
}
103
}
104
}
105
if
(dist[t] !=
INF
) {
106
return
dist[t];
107
}
108
return
-1;
109
}
110
}
// namespace graph
111
113
void
tests
() {
114
std::cout <<
"Initiatinig Predefined Tests..."
<< std::endl;
115
std::cout <<
"Initiating Test 1..."
<< std::endl;
116
std::vector<std::vector<std::pair<int, int>>> adj1(
117
4, std::vector<std::pair<int, int>>());
118
graph::addEdge
(&adj1, 1, 2, 1);
119
graph::addEdge
(&adj1, 4, 1, 2);
120
graph::addEdge
(&adj1, 2, 3, 2);
121
graph::addEdge
(&adj1, 1, 3, 5);
122
123
int
s = 1, t = 3;
124
assert(
graph::dijkstra
(&adj1, s - 1, t - 1) == 3);
125
std::cout <<
"Test 1 Passed..."
<< std::endl;
126
127
s = 4, t = 3;
128
std::cout <<
"Initiating Test 2..."
<< std::endl;
129
assert(
graph::dijkstra
(&adj1, s - 1, t - 1) == 5);
130
std::cout <<
"Test 2 Passed..."
<< std::endl;
131
132
std::vector<std::vector<std::pair<int, int>>> adj2(
133
5, std::vector<std::pair<int, int>>());
134
graph::addEdge
(&adj2, 1, 2, 4);
135
graph::addEdge
(&adj2, 1, 3, 2);
136
graph::addEdge
(&adj2, 2, 3, 2);
137
graph::addEdge
(&adj2, 3, 2, 1);
138
graph::addEdge
(&adj2, 2, 4, 2);
139
graph::addEdge
(&adj2, 3, 5, 4);
140
graph::addEdge
(&adj2, 5, 4, 1);
141
graph::addEdge
(&adj2, 2, 5, 3);
142
graph::addEdge
(&adj2, 3, 4, 4);
143
144
s = 1, t = 5;
145
std::cout <<
"Initiating Test 3..."
<< std::endl;
146
assert(
graph::dijkstra
(&adj2, s - 1, t - 1) == 6);
147
std::cout <<
"Test 3 Passed..."
<< std::endl;
148
std::cout <<
"All Test Passed..."
<< std::endl << std::endl;
149
}
150
152
int
main
() {
153
// running predefined tests
154
tests
();
155
156
int
vertices = int(), edges = int();
157
std::cout <<
"Enter the number of vertices : "
;
158
std::cin >> vertices;
159
std::cout <<
"Enter the number of edges : "
;
160
std::cin >> edges;
161
162
std::vector<std::vector<std::pair<int, int>>> adj(
163
vertices, std::vector<std::pair<int, int>>());
164
165
int
u = int(), v = int(), w = int();
166
while
(edges--) {
167
std::cin >> u >> v >> w;
168
graph::addEdge
(&adj, u, v, w);
169
}
170
171
int
s = int(), t = int();
172
std::cin >> s >> t;
173
int
dist =
graph::dijkstra
(&adj, s - 1, t - 1);
174
if
(dist == -1) {
175
std::cout <<
"Target not reachable from source"
<< std::endl;
176
}
else
{
177
std::cout <<
"Shortest Path Distance : "
<< dist << std::endl;
178
}
179
return
0;
180
}
INF
constexpr int64_t INF
for assert
Definition
bidirectional_dijkstra.cpp:24
main
int main()
Main function.
Definition
generate_parentheses.cpp:110
graph
Graph Algorithms.
graph::addEdge
void addEdge(std::vector< std::vector< int > > *adj, int u, int v)
Function that add edge between two nodes or vertices of graph.
Definition
connected_components.cpp:46
graph::dijkstra
int dijkstra(std::vector< std::vector< std::pair< int, int > > > *adj, int s, int t)
Function runs the dijkstra algorithm for some source vertex and target vertex in the graph and return...
Definition
dijkstra.cpp:66
tests
Testcases to check Union of Two Arrays.
graph
dijkstra.cpp
Generated by
1.18.0