|
| 1 | +// |
| 2 | +// Created by barto on 19.05.18. |
| 3 | +// |
| 4 | + |
| 5 | +#include <cmath> |
| 6 | +#include <random> |
| 7 | +#include <climits> |
| 8 | +#include "DirectedGraph.h" |
| 9 | + |
| 10 | +using namespace std; |
| 11 | + |
| 12 | +// public |
| 13 | + |
| 14 | +DirectedGraph::DirectedGraph() : Graph("Graf skierowany", 1) {} |
| 15 | + |
| 16 | +std::string DirectedGraph::getAvailableAlgorithms() { |
| 17 | + std::string output = ""; |
| 18 | + |
| 19 | + output += "6. Algorytm Dijkstry (SP) [arg1 <- rodzaj reprezentacji (m - macierz, l - lista); arg2 <- wierzch. pocz.; arg3 <- wierzch. konc.]\n"; |
| 20 | + |
| 21 | + return output; |
| 22 | +} |
| 23 | + |
| 24 | +void DirectedGraph::generate(int numberOfVertices, int density, int range) { |
| 25 | + double dens = (double)density / 100; |
| 26 | + dens *= numberOfVertices * (numberOfVertices - 1); |
| 27 | + int numberOfEdges = round(dens); |
| 28 | + |
| 29 | + // prepare matrix and list |
| 30 | + incidenceMatrix.clear(); |
| 31 | + adjacencyList.clear(); |
| 32 | + |
| 33 | + incidenceMatrix.resize(numberOfEdges); |
| 34 | + adjacencyList.resize(numberOfVertices); |
| 35 | + |
| 36 | + for (auto& row : incidenceMatrix) { |
| 37 | + row.assign(numberOfVertices, 0); |
| 38 | + } |
| 39 | + |
| 40 | + // declare rands |
| 41 | + std::random_device rd; |
| 42 | + std::mt19937 mt(rd()); |
| 43 | + std::uniform_int_distribution<int> randomVertex(0, numberOfVertices - 1); |
| 44 | + // std::uniform_int_distribution<int> randomEdge(0, numberOfEdges - 1); |
| 45 | + // std::uniform_int_distribution<int> randomValue(1, 15); |
| 46 | + std::uniform_int_distribution<int> randomValue(1, range); |
| 47 | + |
| 48 | + int beginningVertex = 0; |
| 49 | + int endVertex = 0; |
| 50 | + int value; |
| 51 | + bool edgeBeginningNotAvailable = true; |
| 52 | + bool edgeEndNotAvailable = true; |
| 53 | + |
| 54 | + // generate rand edges |
| 55 | + for (int i = 0; i < numberOfEdges; i++) { |
| 56 | + // random beggining |
| 57 | + edgeBeginningNotAvailable = true; |
| 58 | + while (edgeBeginningNotAvailable){ |
| 59 | + beginningVertex = randomVertex(mt); |
| 60 | + edgeBeginningNotAvailable = !edgeBeginningAvailable(beginningVertex); |
| 61 | + }; |
| 62 | + // random end |
| 63 | + edgeEndNotAvailable = true; |
| 64 | + while (edgeEndNotAvailable){ |
| 65 | + endVertex = randomVertex(mt); |
| 66 | + edgeEndNotAvailable = !edgeEndAvailable(beginningVertex, endVertex); |
| 67 | + }; |
| 68 | + |
| 69 | + value = randomValue(mt); |
| 70 | + |
| 71 | + adjacencyList[beginningVertex].push_front({endVertex, value}); |
| 72 | + incidenceMatrix[i][beginningVertex] = value; |
| 73 | + incidenceMatrix[i][endVertex] = -1 * value; |
| 74 | + } |
| 75 | + |
| 76 | +} |
| 77 | + |
| 78 | +string DirectedGraph::runAlgorithm(char index, char arg1, int arg2, int arg3) { |
| 79 | + string output; |
| 80 | + |
| 81 | + if (index == 1) { |
| 82 | + if (arg1 == 0) { |
| 83 | + output = dijkstrasAlgorithmOnMatrix(arg2, arg3, true); |
| 84 | + } else if (arg1 == 1) { |
| 85 | + output = dijkstrasAlgorithmOnList(arg2, arg3, true); |
| 86 | + } else { |
| 87 | + throw "Nieznany blad!"; // should never be thrown |
| 88 | + } |
| 89 | + } else { |
| 90 | + throw "Algorytm nie istnieje!"; |
| 91 | + } |
| 92 | + |
| 93 | + return output; |
| 94 | +} |
| 95 | + |
| 96 | +void DirectedGraph::test() { |
| 97 | + |
| 98 | +} |
| 99 | + |
| 100 | +// protected |
| 101 | + |
| 102 | +void DirectedGraph::loadRawDataToMatrix(std::vector<int> rawData) { |
| 103 | + incidenceMatrix.clear(); |
| 104 | + int i = 0; |
| 105 | + incidenceMatrix.resize(rawData[i++]); // clear vector and resize to first item of raw data |
| 106 | + |
| 107 | + for (auto& row : incidenceMatrix) { |
| 108 | + row.assign(rawData[i], 0); |
| 109 | + } |
| 110 | + i++; |
| 111 | + |
| 112 | + for (int j = 0; j < incidenceMatrix.size(); j++) { |
| 113 | + int edgeBeginning = rawData[i++]; |
| 114 | + int edgeEnd = rawData[i++]; |
| 115 | + int edgeValue = rawData[i++]; |
| 116 | + |
| 117 | + if (edgeBeginning == edgeEnd) { |
| 118 | + incidenceMatrix.clear(); |
| 119 | + throw "Petle sa nieakceptowane!"; |
| 120 | + } |
| 121 | + |
| 122 | + incidenceMatrix[j][edgeEnd] = edgeValue * -1; |
| 123 | + incidenceMatrix[j][edgeBeginning] = edgeValue; |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +void DirectedGraph::loadRawDataToList(std::vector<int> rawData) { |
| 128 | + adjacencyList.clear(); |
| 129 | + int i = 0; |
| 130 | + |
| 131 | + int numberOfEdges = rawData[i++]; |
| 132 | + |
| 133 | + adjacencyList.resize(rawData[i++]); |
| 134 | + |
| 135 | + for (int j = 0; j < numberOfEdges; j++) { |
| 136 | + int edgeBeginning = rawData[i++]; |
| 137 | + int edgeEnd = rawData[i++]; |
| 138 | + int edgeValue = rawData[i++]; |
| 139 | + |
| 140 | + if (edgeBeginning == edgeEnd) { |
| 141 | + incidenceMatrix.clear(); |
| 142 | + throw "Petle sa nieakceptowane!"; |
| 143 | + } |
| 144 | + |
| 145 | + adjacencyList[edgeBeginning].push_front({edgeEnd, edgeValue}); |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +// private |
| 150 | + |
| 151 | +std::string DirectedGraph::dijkstrasAlgorithmOnMatrix(int beginVertex, int endVertex, bool print) { |
| 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 | + 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; |
| 220 | + |
| 221 | + output += to_string(currentVertex); |
| 222 | + |
| 223 | + while (currentVertex != beginVertex) { |
| 224 | + currentVertex = previousVertex[currentVertex]; |
| 225 | + output += " <- " + to_string(currentVertex); |
| 226 | + } |
| 227 | + |
| 228 | + return output; |
| 229 | + } |
| 230 | + |
| 231 | + return ""; |
| 232 | +} |
| 233 | + |
| 234 | +std::string DirectedGraph::dijkstrasAlgorithmOnList(int beginVertex, int endVertex, bool print) { |
| 235 | + if (adjacencyList.size() == 0) |
| 236 | + throw "Graf pusty!"; |
| 237 | + |
| 238 | + int numberOfVertices = adjacencyList.size(); |
| 239 | + |
| 240 | + |
| 241 | + if (beginVertex >= numberOfVertices || endVertex >= numberOfVertices) |
| 242 | + throw "Poczatkowy lub koncowy wierzcholek nie istnieje!"; |
| 243 | + |
| 244 | + // create needed arrays and assign default values |
| 245 | + vector<bool> visitedVertices; |
| 246 | + vector<unsigned long> pathLength; |
| 247 | + vector<int> previousVertex; |
| 248 | + visitedVertices.assign(numberOfVertices, false); |
| 249 | + pathLength.assign(numberOfVertices, ULONG_MAX); |
| 250 | + previousVertex.assign(numberOfVertices, -1); |
| 251 | + |
| 252 | + // assaing starting value |
| 253 | + pathLength[beginVertex] = 0; |
| 254 | + int currentVertex = beginVertex; |
| 255 | + unsigned long shortestPath; |
| 256 | + int shortestPathVertex; |
| 257 | + |
| 258 | + // run main loop for numberOfVertices times (every vertex will be visisted) |
| 259 | + for (int i = 0; i < numberOfVertices; i++) { |
| 260 | + unsigned long currentLength = pathLength[currentVertex]; |
| 261 | + |
| 262 | + for (auto& edge : adjacencyList[currentVertex]) { |
| 263 | + if (pathLength[edge.edgeEnd] > currentLength + edge.value){ |
| 264 | + pathLength[edge.edgeEnd] = currentLength + edge.value; |
| 265 | + previousVertex[edge.edgeEnd] = currentVertex; |
| 266 | + } |
| 267 | + } |
| 268 | + |
| 269 | + visitedVertices[currentVertex] = true; |
| 270 | + |
| 271 | + shortestPath = ULONG_MAX; |
| 272 | + shortestPathVertex = -1; |
| 273 | + |
| 274 | + for (int j = 0; j < numberOfVertices; j++) { |
| 275 | + if (!visitedVertices[j]) { |
| 276 | + if (pathLength[j] < shortestPath) { |
| 277 | + shortestPath = pathLength[j]; |
| 278 | + shortestPathVertex = j; |
| 279 | + } |
| 280 | + } |
| 281 | + } |
| 282 | + |
| 283 | + if ((i != numberOfVertices - 1) && (shortestPathVertex == -1)) |
| 284 | + throw "Graf niespojny!"; |
| 285 | + |
| 286 | + currentVertex = shortestPathVertex; |
| 287 | + |
| 288 | + } |
| 289 | + |
| 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; |
| 297 | + |
| 298 | + output += to_string(currentVertex); |
| 299 | + |
| 300 | + while (currentVertex != beginVertex) { |
| 301 | + currentVertex = previousVertex[currentVertex]; |
| 302 | + output += " <- " + to_string(currentVertex); |
| 303 | + } |
| 304 | + |
| 305 | + return output; |
| 306 | + } |
| 307 | + |
| 308 | + return ""; |
| 309 | +} |
0 commit comments