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

Commit bca27d5

Browse files
committed
Merge branch 'dijkstras-algorithm' into development
2 parents d26d2bd + 93d4be6 commit bca27d5

File tree

2 files changed

+155
-8
lines changed

2 files changed

+155
-8
lines changed

DirectedGraph.cpp

Lines changed: 153 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <cmath>
66
#include <random>
7+
#include <climits>
78
#include "DirectedGraph.h"
89

910
using namespace std;
@@ -75,17 +76,21 @@ void DirectedGraph::generate(int numberOfVertices, int density, int range) {
7576
}
7677

7778
string DirectedGraph::runAlgorithm(char index, char arg1, int arg2, int arg3) {
79+
string output;
80+
7881
if (index == 1) {
7982
if (arg1 == 0) {
80-
dijkstrasAlgorithmOnMatrix();
83+
output = dijkstrasAlgorithmOnMatrix(arg2, arg3);
8184
} else if (arg1 == 1) {
82-
dijkstrasAlgorithmOnList();
85+
output = dijkstrasAlgorithmOnList(arg2, arg3);
8386
} else {
8487
throw "Nieznany blad!"; // should never be thrown
8588
}
8689
} else {
8790
throw "Algorytm nie istnieje!";
8891
}
92+
93+
return output;
8994
}
9095

9196
void DirectedGraph::test() {
@@ -143,10 +148,152 @@ void DirectedGraph::loadRawDataToList(std::vector<int> rawData) {
143148

144149
// private
145150

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;
148227
}
149228

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;
152299
}

DirectedGraph.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ class DirectedGraph : public Graph {
2525
void loadRawDataToList(std::vector<int> rawData) override ;
2626

2727
private:
28-
void dijkstrasAlgorithmOnMatrix();
29-
void dijkstrasAlgorithmOnList();
28+
std::string dijkstrasAlgorithmOnMatrix(int beginVertex, int endVertex);
29+
std::string dijkstrasAlgorithmOnList(int beginVertex, int endVertex);
3030

3131
};
3232

0 commit comments

Comments
 (0)