From d5cb71c477bbed51d1e0ad1619a8f9f6c8a18ba5 Mon Sep 17 00:00:00 2001 From: Samuel Oberhofer Date: Fri, 24 Jun 2022 16:49:39 +0200 Subject: [PATCH] use uint in for loops --- Uebung 5/Uebung5_1/graph.cpp | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/Uebung 5/Uebung5_1/graph.cpp b/Uebung 5/Uebung5_1/graph.cpp index 8ad45ed..37be78a 100644 --- a/Uebung 5/Uebung5_1/graph.cpp +++ b/Uebung 5/Uebung5_1/graph.cpp @@ -19,7 +19,7 @@ Graph::Graph(const std::vector &vertices, std::vector((int)this->vertices.size(), INT_MAX)); // creation of an NxN Matrix, based on the // size of vertices - for (int i = 0; i < edges.size(); i++) { + for (unsigned int i = 0; i < edges.size(); i++) { insertEdge(edges[i]); // edges are added one by one, utilizing the // insertEdge()-Function } @@ -45,7 +45,7 @@ void Graph::insertVertex(const std::string &vertex) { this->vertices.size(), std::vector(this->vertices.size())); // resizes the adjacencyMatrix // to the new size - for (int i = 0; i < this->vertices.size(); i++) { /* -- */ + for (unsigned int i = 0; i < this->vertices.size(); i++) { /* -- */ adjacencyMatrix[i].resize( this->vertices.size()); /* resizes every "sub" vector of the matrix to the new size */ @@ -74,7 +74,7 @@ void Graph::deleteVertex(const std::string &vertex) { this->adjacencyMatrix.begin() + index); // erases the entries from the adjacencyMatrix at "column" // position "index" - for (int i = 0; i < this->adjacencyMatrix[0].size(); i++) { /* -- */ + for (unsigned int i = 0; i < this->adjacencyMatrix[0].size(); i++) { /* -- */ this->adjacencyMatrix[i].erase(this->adjacencyMatrix[i].begin() + index); /* erases the entries from the adjacencyMatrix in every "row" */ @@ -170,7 +170,7 @@ std::vector Graph::neighbours(const std::string &vertex) { vertex); // resolves the vertex to the index within the adjacencyMatrix std::vector resVector = {}; // initializes an empty std::vetor of std::string - for (int i = 0; i < this->adjacencyMatrix[indexVertex].size(); + for (unsigned int i = 0; i < this->adjacencyMatrix[indexVertex].size(); i++) { /* Loops through all entries of the subvector of adjacencyMatrix[indexVertex] */ if (this->adjacencyMatrix[indexVertex][i] != 0 && @@ -194,13 +194,13 @@ Function that prints the current Graph's adjacencyMatrix. void Graph::printGraph() { std::cout << "--------------------------------------------\n"; std::cout << "\t\t"; - for (int i = 0; i < this->vertices.size(); i++) { + for (unsigned int i = 0; i < this->vertices.size(); i++) { std::cout << this->vertices[i] << "\t"; } std::cout << "\n"; - for (int i = 0; i < this->vertices.size(); i++) { + for (unsigned int i = 0; i < this->vertices.size(); i++) { std::cout << this->vertices[i] << "\t-->\t"; - for (int j = 0; j < this->vertices.size(); j++) { + for (unsigned int j = 0; j < this->vertices.size(); j++) { std::cout << this->adjacencyMatrix[i][j] << "\t"; } std::cout << "\n"; @@ -217,7 +217,7 @@ corresponding index (in the adjacencyMatrix) of type int. - function returns -1 in case the resolution of the name is unsuccessful / -- */ int Graph::resolveVertex(const std::string &vertexName) { - for (int i = 0; i < this->vertices.size(); i++) { + for (unsigned int i = 0; i < this->vertices.size(); i++) { if (this->vertices[i] == vertexName) { return i; } @@ -493,7 +493,7 @@ std::vector> Graph::MST(const std::string &vertex) { int leastweight = INT_MAX; std::string leastVertex; std::string lastVisited; - for (int i = 0; i < this->vertices.size(); i++) { + for (unsigned int i = 0; i < this->vertices.size(); i++) { if (visited[i]) { std::vector theNeighbours = this->neighbours(this->vertices[i]); @@ -525,19 +525,19 @@ Graph::performDijkstraPath(std::string sourceVertex, std::string targetVertex, int dist[this->vertices.size()]; bool sptSet[vertices.size()]; int parent[vertices.size()]; - for (int i = 0; i < vertices.size(); i++) { + for (unsigned int i = 0; i < vertices.size(); i++) { parent[i] = -1; } - for (int i = 0; i < this->vertices.size(); i++) { + for (unsigned int i = 0; i < this->vertices.size(); i++) { dist[i] = INT_MAX; sptSet[i] = false; } dist[resolveVertex(sourceVertex)] = 0; - for (int count = 0; count < this->vertices.size() - 1; count++) { + for (unsigned int count = 0; count < this->vertices.size() - 1; count++) { int m = minDistance(dist, sptSet); sptSet[m] = true; - for (int v = 0; v < vertices.size(); v++) { + for (unsigned int v = 0; v < vertices.size(); v++) { if (!sptSet[v] && mst[m][v] && dist[m] != INT_MAX && dist[m] + mst[m][v] < dist[v]) { dist[v] = dist[m] + mst[m][v]; @@ -570,7 +570,7 @@ Graph::performDijkstraPath(std::string sourceVertex, std::string targetVertex, int Graph::minDistance(int dist[], bool sptSet[]) { int min = INT_MAX, min_index; - for (int v = 0; v < this->vertices.size(); v++) + for (unsigned int v = 0; v < this->vertices.size(); v++) if (sptSet[v] == false && dist[v] <= min) min = dist[v], min_index = v;