|
4 | 4 |
|
5 | 5 | #include <cmath> |
6 | 6 | #include <random> |
| 7 | +#include <climits> |
7 | 8 | #include "DirectedGraph.h" |
8 | 9 |
|
9 | 10 | using namespace std; |
@@ -75,17 +76,21 @@ void DirectedGraph::generate(int numberOfVertices, int density, int range) { |
75 | 76 | } |
76 | 77 |
|
77 | 78 | string DirectedGraph::runAlgorithm(char index, char arg1, int arg2, int arg3) { |
| 79 | + string output; |
| 80 | + |
78 | 81 | if (index == 1) { |
79 | 82 | if (arg1 == 0) { |
80 | | - dijkstrasAlgorithmOnMatrix(); |
| 83 | + output = dijkstrasAlgorithmOnMatrix(arg2, arg3); |
81 | 84 | } else if (arg1 == 1) { |
82 | | - dijkstrasAlgorithmOnList(); |
| 85 | + output = dijkstrasAlgorithmOnList(arg2, arg3); |
83 | 86 | } else { |
84 | 87 | throw "Nieznany blad!"; // should never be thrown |
85 | 88 | } |
86 | 89 | } else { |
87 | 90 | throw "Algorytm nie istnieje!"; |
88 | 91 | } |
| 92 | + |
| 93 | + return output; |
89 | 94 | } |
90 | 95 |
|
91 | 96 | void DirectedGraph::test() { |
@@ -143,10 +148,152 @@ void DirectedGraph::loadRawDataToList(std::vector<int> rawData) { |
143 | 148 |
|
144 | 149 | // private |
145 | 150 |
|
146 | | -void DirectedGraph::dijkstrasAlgorithmOnMatrix() { |
147 | | - throw "Algorytm jeszcze nie zaimplementowany!"; |
| 151 | +std::string DirectedGraph::dijkstrasAlgorithmOnMatrix(int beginVertex, int endVertex) { |
| 152 | + if (incidenceMatrix.size() == 0) |
| 153 | + throw "Graf pusty!"; |
| 154 | + |
| 155 | + int numberOfVertices = incidenceMatrix[0].size(); |
| 156 | + |
| 157 | + |
| 158 | + if (beginVertex >= numberOfVertices || endVertex >= numberOfVertices) |
| 159 | + throw "Poczatkowy lub koncowy wierzcholek nie istnieje!"; |
| 160 | + |
| 161 | + // create needed arrays and assign default values |
| 162 | + vector<bool> visitedVertices; |
| 163 | + vector<unsigned long> pathLength; |
| 164 | + vector<int> previousVertex; |
| 165 | + visitedVertices.assign(numberOfVertices, false); |
| 166 | + pathLength.assign(numberOfVertices, ULONG_MAX); |
| 167 | + previousVertex.assign(numberOfVertices, -1); |
| 168 | + |
| 169 | + // assaing starting value |
| 170 | + pathLength[beginVertex] = 0; |
| 171 | + int currentVertex = beginVertex; |
| 172 | + unsigned long shortestPath; |
| 173 | + int shortestPathVertex; |
| 174 | + |
| 175 | + // run main loop for numberOfVertices times (every vertex will be visisted) |
| 176 | + for (int i = 0; i < numberOfVertices; i++) { |
| 177 | + unsigned long currentLength = pathLength[currentVertex]; |
| 178 | + |
| 179 | + for (auto& row : incidenceMatrix) { |
| 180 | + if (row[currentVertex] > 0) { |
| 181 | + for (int j = 0; j < numberOfVertices; j++) { |
| 182 | + if (row[j] < 0) { |
| 183 | + if (pathLength[j] > currentLength + row[currentVertex]) { |
| 184 | + pathLength[j] = currentLength + row[currentVertex]; |
| 185 | + previousVertex[j] = currentVertex; |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + visitedVertices[currentVertex] = true; |
| 193 | + |
| 194 | + shortestPath = ULONG_MAX; |
| 195 | + shortestPathVertex = -1; |
| 196 | + |
| 197 | + for (int j = 0; j < numberOfVertices; j++) { |
| 198 | + if (!visitedVertices[j]) { |
| 199 | + if (pathLength[j] < shortestPath) { |
| 200 | + shortestPath = pathLength[j]; |
| 201 | + shortestPathVertex = j; |
| 202 | + } |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + if ((i != numberOfVertices - 1) && (shortestPathVertex == -1)) |
| 207 | + throw "Graf niespojny!"; |
| 208 | + |
| 209 | + currentVertex = shortestPathVertex; |
| 210 | + |
| 211 | + } |
| 212 | + |
| 213 | + string output; |
| 214 | + output = "Najkrotsza droga z wierzch.: " + to_string(beginVertex) + " do wierzch.: " + to_string(endVertex) + " wynosi: " + to_string(pathLength[endVertex]) + ".\n"; |
| 215 | + output += "Prowadzi nastepujaca droga: "; |
| 216 | + |
| 217 | + currentVertex = endVertex; |
| 218 | + |
| 219 | + output += to_string(currentVertex); |
| 220 | + |
| 221 | + while (currentVertex != beginVertex) { |
| 222 | + currentVertex = previousVertex[currentVertex]; |
| 223 | + output += " <- " + to_string(currentVertex); |
| 224 | + } |
| 225 | + |
| 226 | + return output; |
148 | 227 | } |
149 | 228 |
|
150 | | -void DirectedGraph::dijkstrasAlgorithmOnList() { |
151 | | - throw "Algorytm jeszcze nie zaimplementowany!"; |
| 229 | +std::string DirectedGraph::dijkstrasAlgorithmOnList(int beginVertex, int endVertex) { |
| 230 | + if (adjacencyList.size() == 0) |
| 231 | + throw "Graf pusty!"; |
| 232 | + |
| 233 | + int numberOfVertices = adjacencyList.size(); |
| 234 | + |
| 235 | + |
| 236 | + if (beginVertex >= numberOfVertices || endVertex >= numberOfVertices) |
| 237 | + throw "Poczatkowy lub koncowy wierzcholek nie istnieje!"; |
| 238 | + |
| 239 | + // create needed arrays and assign default values |
| 240 | + vector<bool> visitedVertices; |
| 241 | + vector<unsigned long> pathLength; |
| 242 | + vector<int> previousVertex; |
| 243 | + visitedVertices.assign(numberOfVertices, false); |
| 244 | + pathLength.assign(numberOfVertices, ULONG_MAX); |
| 245 | + previousVertex.assign(numberOfVertices, -1); |
| 246 | + |
| 247 | + // assaing starting value |
| 248 | + pathLength[beginVertex] = 0; |
| 249 | + int currentVertex = beginVertex; |
| 250 | + unsigned long shortestPath; |
| 251 | + int shortestPathVertex; |
| 252 | + |
| 253 | + // run main loop for numberOfVertices times (every vertex will be visisted) |
| 254 | + for (int i = 0; i < numberOfVertices; i++) { |
| 255 | + unsigned long currentLength = pathLength[currentVertex]; |
| 256 | + |
| 257 | + for (auto& edge : adjacencyList[currentVertex]) { |
| 258 | + if (pathLength[edge.edgeEnd] > currentLength + edge.value){ |
| 259 | + pathLength[edge.edgeEnd] = currentLength + edge.value; |
| 260 | + previousVertex[edge.edgeEnd] = currentVertex; |
| 261 | + } |
| 262 | + } |
| 263 | + |
| 264 | + visitedVertices[currentVertex] = true; |
| 265 | + |
| 266 | + shortestPath = ULONG_MAX; |
| 267 | + shortestPathVertex = -1; |
| 268 | + |
| 269 | + for (int j = 0; j < numberOfVertices; j++) { |
| 270 | + if (!visitedVertices[j]) { |
| 271 | + if (pathLength[j] < shortestPath) { |
| 272 | + shortestPath = pathLength[j]; |
| 273 | + shortestPathVertex = j; |
| 274 | + } |
| 275 | + } |
| 276 | + } |
| 277 | + |
| 278 | + if ((i != numberOfVertices - 1) && (shortestPathVertex == -1)) |
| 279 | + throw "Graf niespojny!"; |
| 280 | + |
| 281 | + currentVertex = shortestPathVertex; |
| 282 | + |
| 283 | + } |
| 284 | + |
| 285 | + string output; |
| 286 | + output = "Najkrotsza droga z wierzch.: " + to_string(beginVertex) + " do wierzch.: " + to_string(endVertex) + " wynosi: " + to_string(pathLength[endVertex]) + ".\n"; |
| 287 | + output += "Prowadzi nastepujaca droga: "; |
| 288 | + |
| 289 | + currentVertex = endVertex; |
| 290 | + |
| 291 | + output += to_string(currentVertex); |
| 292 | + |
| 293 | + while (currentVertex != beginVertex) { |
| 294 | + currentVertex = previousVertex[currentVertex]; |
| 295 | + output += " <- " + to_string(currentVertex); |
| 296 | + } |
| 297 | + |
| 298 | + return output; |
152 | 299 | } |
0 commit comments