-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.cpp
90 lines (81 loc) · 2.55 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
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
const int INF = 0x7ffffff;
/* find the smallest distance not yet checked */
int choose(const vector<int> &distance, const vector<bool> &found) {
int min = INF;
int minPos = -1;
for (int i = 0; i < distance.size(); ++i) {
if (distance[i] < min && !found[i]) {
min = distance[i];
minPos = i;
}
}
return minPos;
}
/*
adjMat is the adjacency matrix
distance[i] represents the shortest path from vertex src to i
found[i] holds a 0 if the shortest path from vertex i has not been found and a 1 if it has
path[i] represents the precursor node of i
*/
void shortestPath(int src, vector<vector<int>> &adjMat, vector<int> &distance, vector<bool> &found, vector<int> &path) {
int vertexNum = adjMat.size();
for (int i = 0; i < vertexNum; ++i) {
found[i] = false;
path[i] = src;
distance[i] = adjMat[src][i];
}
found[src] = true;
distance[src] = 0;
for (int i = 0; i < vertexNum - 2; ++i) {
int dest = choose(distance, found);
found[dest] = true;
for (int j = 0; j < vertexNum; ++j) {
if (!found[j] ) {
if (distance[dest] + adjMat[dest][j] < distance[j]) {
distance[j] = distance[dest] + adjMat[dest][j];
path[j] = dest;
}
}
}
}
}
void searchPath(vector<int> &path, vector<int> &distance, int src, int dest) {
cout << "Weight: " << distance[dest] << endl;
stack<int> pathStack;
pathStack.push(dest);
int p = path[dest];
while (p != src) {
pathStack.push(p);
p = path[p];
}
cout << "Path: " << src;
while (!pathStack.empty()) {
cout << " -> " << pathStack.top();
pathStack.pop();
}
cout << endl;
}
int main() {
int vertexNum = 7;
int src = 0;
int dest = 6;
vector<vector<int>> adjMat = {
{ 0, 12, 14, 16, INF, INF, INF},
{ 12, 0, INF, 7, 10, INF, INF},
{ 14, INF, 0, 9, INF, 8, INF},
{ 16, 7, 9, 0, 6, 2, INF},
{INF, 10, INF, 6, 0, 5, 3},
{INF, INF, 8, 2, 5, 0, 4},
{INF, INF, INF, INF, 3, 4, 0}
};
vector<int> distance(vertexNum);
vector<bool> found(vertexNum);
vector<int> path(vertexNum);
shortestPath(src, adjMat, distance, found, path);
searchPath(path, distance, src, dest);
return 0;
}