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

Commit 9f06dce

Browse files
committed
Fix bugs in the algorithm
1 parent 87512fc commit 9f06dce

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

UndirectedGraph.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ void UndirectedGraph::loadRawDataToList(std::vector<int> rawData) {
154154
string UndirectedGraph::primsAlgorithmOnMatrix() {
155155
// prepare vector for output
156156
vector<vector<int>> minimumSpanningTree;
157+
158+
if (incidenceMatrix.size() == 0)
159+
throw "Graf pusty!";
160+
157161
int numberOfVertices = incidenceMatrix[0].size();
158162
vector<int> foundVertices;
159163
priority_queue<MinHeapElement, vector<MinHeapElement>, MinHeapElementComparator> queue;
@@ -163,7 +167,7 @@ string UndirectedGraph::primsAlgorithmOnMatrix() {
163167
int edgeEnd;
164168
int edgeValue;
165169
int i;
166-
int j;
170+
int j = 0;
167171

168172
foundVertices.push_back(vertexID);
169173

@@ -189,14 +193,17 @@ string UndirectedGraph::primsAlgorithmOnMatrix() {
189193
throw "Nieznany blad!"; // should never be thrown
190194

191195
queue.push(MinHeapElement(vertexID, edgeEnd, edgeValue));
192-
193196
}
194197
}
195198

196199
do {
200+
if (queue.empty()) // jesli kolejka pusta, wywal sie
201+
throw "Graf niespojny!";
197202
MinHeapElement element = queue.top();
203+
vertexID = element.getEdgeBeginning();
198204
edgeEnd = element.getEdgeEnd();
199205
edgeValue = element.getEdgeValue();
206+
queue.pop();
200207
} while (find(foundVertices.begin(), foundVertices.end(), edgeEnd) != foundVertices.end());
201208

202209
foundVertices.push_back(edgeEnd);
@@ -210,10 +217,7 @@ string UndirectedGraph::primsAlgorithmOnMatrix() {
210217
vertexID = edgeEnd;
211218
j++;
212219

213-
} while (foundVertices.size() < numberOfVertices && queue.size() > 0);
214-
215-
if (foundVertices.size() < numberOfVertices)
216-
throw "Graf niespojny!";
220+
} while (foundVertices.size() < numberOfVertices);
217221

218222
string output = "Minimalne drzewo rozpinajace\n";
219223

0 commit comments

Comments
 (0)