Skip to content
This repository was archived by the owner on May 27, 2019. It is now read-only.

Commit 131b88e

Browse files
committed
Fix Dijkstra's algorithm for matrix
When there was no path to any vertex algorithm printed error message. Now it shows that there is no path available.
1 parent 294f34c commit 131b88e

1 file changed

Lines changed: 35 additions & 9 deletions

File tree

DirectedGraph.cpp

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -289,25 +289,51 @@ std::string DirectedGraph::dijkstrasAlgorithmOnMatrix(int beginVertex, int endVe
289289
}
290290

291291
if ((i != numberOfVertices - 1) && (shortestPathVertex == -1))
292-
throw "Graf niespojny!";
292+
break;
293293

294294
currentVertex = shortestPathVertex;
295295

296296
}
297297

298298
if (print) {
299299
string output;
300-
output = "Najkrotsza droga z wierzch.: " + to_string(beginVertex) + " do wierzch.: " + to_string(endVertex) +
301-
" wynosi: " + to_string(pathLength[endVertex]) + ".\n";
302-
output += "Prowadzi nastepujaca droga: ";
300+
shortestPath = pathLength[endVertex];
303301

304-
currentVertex = endVertex;
302+
if (shortestPath == ULONG_MAX) {
303+
output += "Droga pomiedzy wybranymi wierzcholkami nie istnieje!\n\n"
304+
} else {
305+
output =
306+
"Najkrotsza droga z wierzch.: " + to_string(beginVertex) + " do wierzch.: " + to_string(endVertex) +
307+
" wynosi: " + to_string(shortestPath) + ".\n";
308+
output += "Prowadzi nastepujaca droga: ";
305309

306-
output += to_string(currentVertex);
310+
currentVertex = endVertex;
307311

308-
while (currentVertex != beginVertex) {
309-
currentVertex = previousVertex[currentVertex];
310-
output += " <- " + to_string(currentVertex);
312+
output += to_string(currentVertex);
313+
314+
while (currentVertex != beginVertex) {
315+
currentVertex = previousVertex[currentVertex];
316+
output += " <- " + to_string(currentVertex);
317+
}
318+
319+
output += "\n\n";
320+
}
321+
322+
output += "Najkrotsza droga do innych wierzcholkow: \n";
323+
output += "W | Dlugosc\n";
324+
output += "------------------";
325+
for (int i = 0; i < numberOfVertices; i++) {
326+
shortestPath = pathLength[i];
327+
328+
output += to_string(i) + " | ";
329+
330+
if (shortestPath == ULONG_MAX) {
331+
output += "BRAK";
332+
} else {
333+
output += to_string(shortestPath);
334+
}
335+
336+
output += "\n";
311337
}
312338

313339
return output;

0 commit comments

Comments
 (0)