-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
117 lines (97 loc) · 3.11 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <bits/stdc++.h>
using namespace std;
int NO_PARENT = -1;
// Funciton to print shortest path using parents array
void print_path(int current_vertex, vector<int> parents) {
// if source node has been processed
if (current_vertex == NO_PARENT) {
return;
}
print_path(parents[current_vertex], parents);
cout << current_vertex << "->";
}
// Funciton to print distances and shortest paths
void print_solution(int start_vertex, vector<int> distances,
vector<int> parents) {
int n_vertices = distances.size();
cout << "Vertex\t Distance\tPath";
for (int vertex_index = 0; vertex_index < n_vertices; vertex_index++) {
if (vertex_index != start_vertex) {
cout << endl
<< start_vertex << " to " << vertex_index << " \t\t ";
cout << distances[vertex_index] << "\t\t";
print_path(vertex_index, parents);
}
}
}
// Main function that implements Dijkstra's algo
void find_path(vector<vector<int>> adjacency_matrix,
int start_vertex) {
int n_vertices = adjacency_matrix[0].size();
// shortest_distance[] holds the
// the shortest distance from source to
// all vertices
vector<int> shortest_distance(n_vertices);
// added[] holds indexes of vertices that
// are included shortest distance from src to
// all veritces
vector<bool> added(n_vertices);
// Initially all distances = infinity
// And all added[] elements are false
for (int vertex_index = 0; vertex_index < n_vertices;
vertex_index++) {
shortest_distance[vertex_index] = INT_MAX;
added[vertex_index] = false;
}
// Distance from initial point to itself = 0
shortest_distance[start_vertex] = 0;
// Parent array holdsshortest path tree
vector<int> parents(n_vertices);
// The starting vertex does not
// have a parent
parents[start_vertex] = NO_PARENT;
// Main loop that fins the shortest
// distance from source to all vertices
for (int i = 1; i < n_vertices; i++) {
// Pick the minimum distance vertex
// from the set of vertices not yet
// processed. nearest_vertex is
// always equal to startNode in
// first iteration.
int nearest_vertex = -1;
int shortestDistance = INT_MAX;
for (int vertex_index = 0; vertex_index < n_vertices;
vertex_index++) {
if (!added[vertex_index] && shortest_distance[vertex_index] < shortestDistance) {
nearest_vertex = vertex_index;
shortestDistance = shortest_distance[vertex_index];
}
}
// Mark the picked vertex as processed
added[nearest_vertex] = true;
// Update dist value of the
// adjacent vertices of the
// picked vertex.
for (int vertex_index = 0; vertex_index < n_vertices;
vertex_index++) {
int edgeDistance = adjacency_matrix[nearest_vertex][vertex_index];
if (edgeDistance > 0 && ((shortestDistance + edgeDistance) < shortest_distance[vertex_index])) {
parents[vertex_index] = nearest_vertex;
shortest_distance[vertex_index] = shortestDistance + edgeDistance;
}
}
}
print_solution(start_vertex, shortest_distance, parents);
}
int main() {
vector<vector<int>> adjacency_matrix = {
{0,1,1,4,0},
{1,0,0,2,0},
{1,0,0,1,2},
{4,2,1,0,4},
{0,0,2,4,0}
};
find_path(adjacency_matrix, 1);
cout << endl;
return 0;
}