|
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; |
@@ -152,5 +153,73 @@ std::string DirectedGraph::dijkstrasAlgorithmOnMatrix(int beginVertex, int endVe |
152 | 153 | } |
153 | 154 |
|
154 | 155 | std::string DirectedGraph::dijkstrasAlgorithmOnList(int beginVertex, int endVertex) { |
155 | | - throw "Algorytm jeszcze nie zaimplementowany!"; |
| 156 | + if (adjacencyList.size() == 0) |
| 157 | + throw "Graf pusty!"; |
| 158 | + |
| 159 | + int numberOfVertices = adjacencyList.size(); |
| 160 | + |
| 161 | + |
| 162 | + if (beginVertex >= numberOfVertices || endVertex >= numberOfVertices) |
| 163 | + throw "Poczatkowy lub koncowy wierzcholek nie istnieje!"; |
| 164 | + |
| 165 | + // create needed arrays and assign default values |
| 166 | + vector<bool> visitedVertices; |
| 167 | + vector<unsigned long> pathLength; |
| 168 | + vector<int> previousVertex; |
| 169 | + visitedVertices.assign(numberOfVertices, false); |
| 170 | + pathLength.assign(numberOfVertices, ULONG_MAX); |
| 171 | + previousVertex.assign(numberOfVertices, -1); |
| 172 | + |
| 173 | + // assaing starting value |
| 174 | + pathLength[beginVertex] = 0; |
| 175 | + int currentVertex = beginVertex; |
| 176 | + unsigned long shortestPath; |
| 177 | + int shortestPathVertex; |
| 178 | + |
| 179 | + // run main loop for numberOfVertices times (every vertex will be visisted) |
| 180 | + for (int i = 0; i < numberOfVertices; i++) { |
| 181 | + unsigned long currentLength = pathLength[currentVertex]; |
| 182 | + |
| 183 | + for (auto& edge : adjacencyList[currentVertex]) { |
| 184 | + if (pathLength[edge.edgeEnd] > currentLength + edge.value){ |
| 185 | + pathLength[edge.edgeEnd] = currentLength + edge.value; |
| 186 | + previousVertex[edge.edgeEnd] = currentVertex; |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + visitedVertices[currentVertex] = true; |
| 191 | + |
| 192 | + shortestPath = ULONG_MAX; |
| 193 | + shortestPathVertex = -1; |
| 194 | + |
| 195 | + for (int j = 0; j < numberOfVertices; j++) { |
| 196 | + if (!visitedVertices[j]) { |
| 197 | + if (pathLength[j] < shortestPath) { |
| 198 | + shortestPath = pathLength[j]; |
| 199 | + shortestPathVertex = j; |
| 200 | + } |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + if ((i != numberOfVertices - 1) && (shortestPathVertex == -1)) |
| 205 | + throw "Graf niespojny!"; |
| 206 | + |
| 207 | + currentVertex = shortestPathVertex; |
| 208 | + |
| 209 | + } |
| 210 | + |
| 211 | + string output; |
| 212 | + output = "Najkrotsza droga z wierzch.: " + to_string(beginVertex) + " do wierzch.: " + to_string(endVertex) + " wynosi: " + to_string(pathLength[endVertex]) + ".\n"; |
| 213 | + output += "Prowadzi nastepujaca droga: "; |
| 214 | + |
| 215 | + currentVertex = endVertex; |
| 216 | + |
| 217 | + output += to_string(currentVertex); |
| 218 | + |
| 219 | + while (currentVertex != beginVertex) { |
| 220 | + currentVertex = previousVertex[currentVertex]; |
| 221 | + output += " <- " + to_string(currentVertex); |
| 222 | + } |
| 223 | + |
| 224 | + return output; |
156 | 225 | } |
0 commit comments