forked from navneet-bhole95/Code-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dijsktra.cpp
170 lines (136 loc) · 2.82 KB
/
Dijsktra.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <iostream>
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
class Dijsktra {
private:
int **matrix;
int size, source, dest;
int *min_dis;
bool *fixed;
public:
void input() {
cout << "Enter size" << endl;
cin >> size;
initialize();
make_adj_mat();
cout << "Enter source and destination:" << endl;
cin >> source >> dest;
}
void calc_shortest(int point) {
fixed[point] = true;
if(point == dest) return;
else {
if(point == source) min_dis[point] = 0;
for (int i = 0; i < size; ++i) {
if (matrix[point][i] != 0 && !fixed[i]) {
if (min_dis[i] != -1){
min_dis[i] = min(min_dis[i], min_dis[point] + matrix[point][i]);
}
else {
min_dis[i] = min_dis[point] + matrix[point][i];
}
}
}
point = get_next();
calc_shortest(point);
}
}
int get_next() {
int min = 9999;
int point = -1;
for (int i = 0; i < size; ++i) {
if (!fixed[i] && min_dis[i] != -1) {
if (min_dis[i] < min ) {
min = min_dis[i];
point = i;
}
}
}
return point;
}
void make_adj_mat() {
for (int i = 0; i < size; i++) {
cout << "Enter connections of node " << i <<
" (vert, weight). Put -1 at end." << endl;
while(1) {
int vert, weight;
cin >> vert;
if (vert == -1) break;
cin >> weight;
matrix[i][vert] = weight;
}
}
}
void initialize() {
min_dis = new int[size];
for (int i = 0; i < size; i++){
min_dis[i] = -1;
}
fixed = new bool[10];
for (int i = 0; i < size; ++i){
fixed[i] = false;
}
matrix = new int *[size];
for (int i = 0; i < size; i++){
matrix[i] = new int[10];
}
for(int i = 0; i < size; i++) {
for(int j = 0; j < size; j++) {
matrix[i][j] = 0;
}
}
}
void print_min() {
for (int i = 0; i < size; ++i) {
cout << "Min dis of " << i <<
" from source is " << min_dis[i] << endl;
}
}
void print_mat() {
for(int i = 0; i < size; i++) {
for(int j = 0; j < size; j++) {
cout << matrix[i][j] << "\t";
}
cout << endl;
}
}
void print_path() {
stack<int> path;
int point = dest;
path.push(point);
while(point != source) {
for (int i = 0; i < size; ++i){
int inter_pt_dis = matrix[i][point];
if (inter_pt_dis != 0 && min_dis[i] + inter_pt_dis == min_dis[point]){
point = i;
path.push(point);
break;
}
}
}
while(!path.empty()) {
cout << path.top() << " ";
path.pop();
}
cout << endl;
}
int get_source() {
return source;
}
~Dijsktra() {
delete(matrix);
}
};
int main() {
Dijsktra dijsktra;
dijsktra.input();
cout << "\nThe adjacency matrix is:" << endl;
dijsktra.print_mat();
dijsktra.calc_shortest(dijsktra.get_source());
cout << "\nThe minimum distaces:" << endl;
dijsktra.print_min();
cout << "\nThe path is:" << endl;
dijsktra.print_path();
return 0;
}