use uint in for loops

This commit is contained in:
Samuel Oberhofer 2022-06-24 16:49:39 +02:00
parent c4f0be8c98
commit d5cb71c477
1 changed files with 14 additions and 14 deletions

View File

@ -19,7 +19,7 @@ Graph::Graph(const std::vector<std::string> &vertices,
std::vector<int>((int)this->vertices.size(), std::vector<int>((int)this->vertices.size(),
INT_MAX)); // creation of an NxN Matrix, based on the INT_MAX)); // creation of an NxN Matrix, based on the
// size of vertices // 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(edges[i]); // edges are added one by one, utilizing the
// insertEdge()-Function // insertEdge()-Function
} }
@ -45,7 +45,7 @@ void Graph::insertVertex(const std::string &vertex) {
this->vertices.size(), this->vertices.size(),
std::vector<int>(this->vertices.size())); // resizes the adjacencyMatrix std::vector<int>(this->vertices.size())); // resizes the adjacencyMatrix
// to the new size // 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( adjacencyMatrix[i].resize(
this->vertices.size()); /* resizes every "sub" vector of the matrix to this->vertices.size()); /* resizes every "sub" vector of the matrix to
the new size */ the new size */
@ -74,7 +74,7 @@ void Graph::deleteVertex(const std::string &vertex) {
this->adjacencyMatrix.begin() + this->adjacencyMatrix.begin() +
index); // erases the entries from the adjacencyMatrix at "column" index); // erases the entries from the adjacencyMatrix at "column"
// position "index" // 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() + this->adjacencyMatrix[i].erase(this->adjacencyMatrix[i].begin() +
index); /* erases the entries from the index); /* erases the entries from the
adjacencyMatrix in every "row" */ adjacencyMatrix in every "row" */
@ -170,7 +170,7 @@ std::vector<std::string> Graph::neighbours(const std::string &vertex) {
vertex); // resolves the vertex to the index within the adjacencyMatrix vertex); // resolves the vertex to the index within the adjacencyMatrix
std::vector<std::string> resVector = std::vector<std::string> resVector =
{}; // initializes an empty std::vetor of std::string {}; // 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 i++) { /* Loops through all entries of the subvector of
adjacencyMatrix[indexVertex] */ adjacencyMatrix[indexVertex] */
if (this->adjacencyMatrix[indexVertex][i] != 0 && if (this->adjacencyMatrix[indexVertex][i] != 0 &&
@ -194,13 +194,13 @@ Function that prints the current Graph's adjacencyMatrix.
void Graph::printGraph() { void Graph::printGraph() {
std::cout << "--------------------------------------------\n"; std::cout << "--------------------------------------------\n";
std::cout << "\t\t"; 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 << this->vertices[i] << "\t";
} }
std::cout << "\n"; 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"; 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 << this->adjacencyMatrix[i][j] << "\t";
} }
std::cout << "\n"; 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 - function returns -1 in case the resolution of the name is unsuccessful
/ -- */ / -- */
int Graph::resolveVertex(const std::string &vertexName) { 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) { if (this->vertices[i] == vertexName) {
return i; return i;
} }
@ -493,7 +493,7 @@ std::vector<std::vector<int>> Graph::MST(const std::string &vertex) {
int leastweight = INT_MAX; int leastweight = INT_MAX;
std::string leastVertex; std::string leastVertex;
std::string lastVisited; 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]) { if (visited[i]) {
std::vector<std::string> theNeighbours = std::vector<std::string> theNeighbours =
this->neighbours(this->vertices[i]); this->neighbours(this->vertices[i]);
@ -525,19 +525,19 @@ Graph::performDijkstraPath(std::string sourceVertex, std::string targetVertex,
int dist[this->vertices.size()]; int dist[this->vertices.size()];
bool sptSet[vertices.size()]; bool sptSet[vertices.size()];
int parent[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; 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; dist[i] = INT_MAX;
sptSet[i] = false; sptSet[i] = false;
} }
dist[resolveVertex(sourceVertex)] = 0; 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); int m = minDistance(dist, sptSet);
sptSet[m] = true; 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 && if (!sptSet[v] && mst[m][v] && dist[m] != INT_MAX &&
dist[m] + mst[m][v] < dist[v]) { dist[m] + mst[m][v] < dist[v]) {
dist[v] = dist[m] + mst[m][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 Graph::minDistance(int dist[], bool sptSet[]) {
int min = INT_MAX, min_index; 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) if (sptSet[v] == false && dist[v] <= min)
min = dist[v], min_index = v; min = dist[v], min_index = v;