@@ -80,9 +80,9 @@ string DirectedGraph::runAlgorithm(char index, char arg1, int arg2, int arg3) {
8080
8181 if (index == 1 ) {
8282 if (arg1 == 0 ) {
83- output = dijkstrasAlgorithmOnMatrix (arg2, arg3);
83+ output = dijkstrasAlgorithmOnMatrix (arg2, arg3, true );
8484 } else if (arg1 == 1 ) {
85- output = dijkstrasAlgorithmOnList (arg2, arg3);
85+ output = dijkstrasAlgorithmOnList (arg2, arg3, true );
8686 } else {
8787 throw " Nieznany blad!" ; // should never be thrown
8888 }
@@ -148,7 +148,7 @@ void DirectedGraph::loadRawDataToList(std::vector<int> rawData) {
148148
149149// private
150150
151- std::string DirectedGraph::dijkstrasAlgorithmOnMatrix (int beginVertex, int endVertex) {
151+ std::string DirectedGraph::dijkstrasAlgorithmOnMatrix (int beginVertex, int endVertex, bool print ) {
152152 if (incidenceMatrix.size () == 0 )
153153 throw " Graf pusty!" ;
154154
@@ -210,23 +210,28 @@ std::string DirectedGraph::dijkstrasAlgorithmOnMatrix(int beginVertex, int endVe
210210
211211 }
212212
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: " ;
213+ if (print) {
214+ string output;
215+ output = " Najkrotsza droga z wierzch.: " + to_string (beginVertex) + " do wierzch.: " + to_string (endVertex) +
216+ " wynosi: " + to_string (pathLength[endVertex]) + " .\n " ;
217+ output += " Prowadzi nastepujaca droga: " ;
218+
219+ currentVertex = endVertex;
216220
217- currentVertex = endVertex ;
221+ output += to_string (currentVertex) ;
218222
219- output += to_string (currentVertex);
223+ while (currentVertex != beginVertex) {
224+ currentVertex = previousVertex[currentVertex];
225+ output += " <- " + to_string (currentVertex);
226+ }
220227
221- while (currentVertex != beginVertex) {
222- currentVertex = previousVertex[currentVertex];
223- output += " <- " + to_string (currentVertex);
228+ return output;
224229 }
225230
226- return output ;
231+ return " " ;
227232}
228233
229- std::string DirectedGraph::dijkstrasAlgorithmOnList (int beginVertex, int endVertex) {
234+ std::string DirectedGraph::dijkstrasAlgorithmOnList (int beginVertex, int endVertex, bool print ) {
230235 if (adjacencyList.size () == 0 )
231236 throw " Graf pusty!" ;
232237
@@ -282,18 +287,23 @@ std::string DirectedGraph::dijkstrasAlgorithmOnList(int beginVertex, int endVert
282287
283288 }
284289
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: " ;
290+ if (print) {
291+ string output;
292+ output = " Najkrotsza droga z wierzch.: " + to_string (beginVertex) + " do wierzch.: " + to_string (endVertex) +
293+ " wynosi: " + to_string (pathLength[endVertex]) + " .\n " ;
294+ output += " Prowadzi nastepujaca droga: " ;
295+
296+ currentVertex = endVertex;
288297
289- currentVertex = endVertex ;
298+ output += to_string (currentVertex) ;
290299
291- output += to_string (currentVertex);
300+ while (currentVertex != beginVertex) {
301+ currentVertex = previousVertex[currentVertex];
302+ output += " <- " + to_string (currentVertex);
303+ }
292304
293- while (currentVertex != beginVertex) {
294- currentVertex = previousVertex[currentVertex];
295- output += " <- " + to_string (currentVertex);
305+ return output;
296306 }
297307
298- return output ;
308+ return " " ;
299309}
0 commit comments