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

Commit 2f26220

Browse files
committed
Add Dijkstra's algorithm on matrix
1 parent e1b6d03 commit 2f26220

1 file changed

Lines changed: 75 additions & 1 deletion

File tree

DirectedGraph.cpp

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,81 @@ void DirectedGraph::loadRawDataToList(std::vector<int> rawData) {
149149
// private
150150

151151
std::string DirectedGraph::dijkstrasAlgorithmOnMatrix(int beginVertex, int endVertex) {
152-
throw "Algorytm jeszcze nie zaimplementowany!";
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[beginVertex] > 0) {
181+
for (int j = 0; j < numberOfVertices; j++) {
182+
if (row[j] < 0) {
183+
if (pathLength[j] > currentLength + row[beginVertex]) {
184+
pathLength[j] = currentLength + row[beginVertex];
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;
153227
}
154228

155229
std::string DirectedGraph::dijkstrasAlgorithmOnList(int beginVertex, int endVertex) {

0 commit comments

Comments
 (0)