-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdijkstra.cpp
More file actions
144 lines (126 loc) · 3.38 KB
/
Copy pathdijkstra.cpp
File metadata and controls
144 lines (126 loc) · 3.38 KB
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
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#define INFY 9999999
// Define the number of nodes in the field
#define N_NODES 5
using namespace std;
struct Node
{
int id;
int f;
int J;
};
bool isAdiacent(int id1, int id2, int graph[][N_NODES])
{
if (graph[id1 - 1][id2 - 1] != 0)
{
// A link has been found
return true;
}
return false;
}
int p(int j, int k, int graph[][N_NODES]) // Function that return the cost of a link
{
return graph[j - 1][k - 1];
}
void setup(Node nodes[], vector<int> &ids, int graph[][N_NODES], vector<Node> &S, vector<Node> &T)
{
for (int i = 0; i < N_NODES; i++)
{
Node temp;
temp.id = ids[i];
if (ids[i] == 1)
{
// It's the first node
temp.f = 0;
temp.J = 0;
S.push_back(temp);
}
else if (isAdiacent(ids[i], 1, graph))
{
temp.f = p(1, ids[i], graph);
temp.J = 1;
T.push_back(temp);
}
else
{
temp.f = INFY;
T.push_back(temp);
}
nodes[i] = temp;
}
}
void setupIds(vector<int> &ids)
{
for (int i = 0; i < N_NODES; i++)
{
ids.push_back(i + 1);
}
}
int main()
{
// Define the matrix
int graph[N_NODES][N_NODES] = {};
vector<int> ids;
Node nodes[N_NODES] = {};
vector<Node> S; // Starting Node (1)
vector<Node> T; // Other nodes (2, 3)
setupIds(ids);
// Graph creation
for (int i = 0; i < N_NODES; i++)
{
for (int j = i + 1; j < N_NODES; j++)
{
if (ids[i] != ids[j])
{
// One of the main hypothesys of Dijkstra's Algorithm is that costs NEEDS to be > 0 (not negative)
do
{
cout << "What is the cost of the connection between: " << ids[i] << " and " << ids[j] << endl;
cin >> graph[i][j];
} while (graph[i][j] < 0);
// It should be specular (non-oriented graph)
graph[j][i] = graph[i][j];
}
}
}
// Setup of algorithm
setup(nodes, ids, graph, S, T);
// Body of the algorithm
while (T.size() > 0)
{
// Find the minimum in T (by f)
Node min = T[0];
vector<Node>::iterator idMin;
idMin = T.begin();
// Search for the min element in the T vector
for (int i = 1; i < T.size(); i++)
{
if (T[i].f < min.f)
{
min = T[i];
idMin = T.begin() + i;
}
}
// Erase the node from the T vector and add it in the S vector (has been analysed)
T.erase(idMin);
S.push_back(min);
if (T.size() == 0)
break;
// We assign a permanent label to the nodes adiacent to the min one
for (int i = 0; i < T.size(); i++)
{
if (isAdiacent(T[i].id, min.id, graph) && (T[i].f > min.f + p(T[i].id, min.id, graph)))
{
T[i].f = min.f + p(T[i].id, min.id, graph);
T[i].J = min.id;
}
}
}
// Final Infos
for (int i = 0; i < S.size(); i++)
cout << "To reach NodeID " << S[i].id << ", the shortest trip goes through NodeID " << S[i].J << " and has a cost of " << S[i].f << endl;
return 0;
}