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

Commit

Permalink
Merge branch 'refactoring'
Browse files Browse the repository at this point in the history
  • Loading branch information
baatochan committed May 24, 2018
2 parents ed0e5fe + 7599af6 commit f15fa2c
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 29 deletions.
24 changes: 12 additions & 12 deletions DirectedGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include <cmath>
#include <random>
#include <climits>
#include <iostream>
#include <fstream>
#include <ctime>
Expand All @@ -26,7 +25,7 @@ std::string DirectedGraph::getAvailableAlgorithms() {
}

void DirectedGraph::generate(int numberOfVertices, int density, int range) {
double dens = (double)density / 100;
double dens = (double) density / 100;
dens *= numberOfVertices * (numberOfVertices - 1);
int numberOfEdges = round(dens);

Expand All @@ -37,7 +36,7 @@ void DirectedGraph::generate(int numberOfVertices, int density, int range) {
incidenceMatrix.resize(numberOfEdges);
adjacencyList.resize(numberOfVertices);

for (auto& row : incidenceMatrix) {
for (auto &row : incidenceMatrix) {
row.assign(numberOfVertices, 0);
}

Expand All @@ -59,13 +58,13 @@ void DirectedGraph::generate(int numberOfVertices, int density, int range) {
for (int i = 0; i < numberOfEdges; i++) {
// random beggining
edgeBeginningNotAvailable = true;
while (edgeBeginningNotAvailable){
while (edgeBeginningNotAvailable) {
beginningVertex = randomVertex(mt);
edgeBeginningNotAvailable = !edgeBeginningAvailable(beginningVertex);
};
// random end
edgeEndNotAvailable = true;
while (edgeEndNotAvailable){
while (edgeEndNotAvailable) {
endVertex = randomVertex(mt);
edgeEndNotAvailable = !edgeEndAvailable(beginningVertex, endVertex);
};
Expand Down Expand Up @@ -109,7 +108,7 @@ void DirectedGraph::test() {
double result = 0;
int beginVertex;
int endVertex;

cout.setf(ios::fixed);

for (int i = 0; i < 5; i++) {
Expand All @@ -118,9 +117,10 @@ void DirectedGraph::test() {
path = "..\\wyniki\\";
path += to_string(time(0));
path += "-gSkierowany-algorytmDijkstry-n" + to_string(numberOfElements[i]) + "-g" +
to_string(density[j]) + "-r" + representationType[k] + ".txt";
to_string(density[j]) + "-r" + representationType[k] + ".txt";

cout << "Test - Graf: Skierowany - Algorytm: Dijkstry - Ilosc elem: " << numberOfElements[i] << " - Gestosc: " << density[j] << " - Reprezentacja: " << representationType[k] << endl;
cout << "Test - Graf: Skierowany - Algorytm: Dijkstry - Ilosc elem: " << numberOfElements[i]
<< " - Gestosc: " << density[j] << " - Reprezentacja: " << representationType[k] << endl;

fstream file(path, fstream::out);

Expand Down Expand Up @@ -189,7 +189,7 @@ void DirectedGraph::loadRawDataToMatrix(std::vector<int> rawData) {
int i = 0;
incidenceMatrix.resize(rawData[i++]); // clear vector and resize to first item of raw data

for (auto& row : incidenceMatrix) {
for (auto &row : incidenceMatrix) {
row.assign(rawData[i], 0);
}
i++;
Expand Down Expand Up @@ -261,7 +261,7 @@ std::string DirectedGraph::dijkstrasAlgorithmOnMatrix(int beginVertex, int endVe
for (int i = 0; i < numberOfVertices; i++) {
unsigned long currentLength = pathLength[currentVertex];

for (auto& row : incidenceMatrix) {
for (auto &row : incidenceMatrix) {
if (row[currentVertex] > 0) {
for (int j = 0; j < numberOfVertices; j++) {
if (row[j] < 0) {
Expand Down Expand Up @@ -370,8 +370,8 @@ std::string DirectedGraph::dijkstrasAlgorithmOnList(int beginVertex, int endVert
for (int i = 0; i < numberOfVertices; i++) {
unsigned long currentLength = pathLength[currentVertex];

for (auto& edge : adjacencyList[currentVertex]) {
if (pathLength[edge.edgeEnd] > currentLength + edge.value){
for (auto &edge : adjacencyList[currentVertex]) {
if (pathLength[edge.edgeEnd] > currentLength + edge.value) {
pathLength[edge.edgeEnd] = currentLength + edge.value;
previousVertex[edge.edgeEnd] = currentVertex;
}
Expand Down
6 changes: 4 additions & 2 deletions DirectedGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ class DirectedGraph : public Graph {
void test() override;

protected:
void loadRawDataToMatrix(std::vector<int> rawData) override ;
void loadRawDataToList(std::vector<int> rawData) override ;
void loadRawDataToMatrix(std::vector<int> rawData) override;

void loadRawDataToList(std::vector<int> rawData) override;

private:
std::string dijkstrasAlgorithmOnMatrix(int beginVertex, int endVertex, bool print);

std::string dijkstrasAlgorithmOnList(int beginVertex, int endVertex, bool print);

};
Expand Down
8 changes: 4 additions & 4 deletions Graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ vector<int> Graph::loadRawDataFrom(string path) {
while (file >> temp) {
try {
returnIntVector.push_back(stoi(temp));
} catch (const exception& e) {
} catch (const exception &e) {
returnIntVector.clear();
throw "Bledna zawartosc pliku! Upewnij sie ze podales odpowiedni format!";
}
Expand All @@ -72,7 +72,7 @@ vector<int> Graph::loadRawDataFrom(string path) {

bool Graph::edgeBeginningAvailable(int vertex) {
int i = 0;
for (auto& v : adjacencyList[vertex]) {
for (auto &v : adjacencyList[vertex]) {
i++;
}

Expand All @@ -85,7 +85,7 @@ bool Graph::edgeEndAvailable(int beginning, int end) {
return false;
}

for (auto& v : adjacencyList[beginning]) {
for (auto &v : adjacencyList[beginning]) {
if (v.edgeEnd == end)
return false;
}
Expand Down Expand Up @@ -182,7 +182,7 @@ std::string Graph::printList(vector<forward_list<Graph::EdgeListElement>> v) {
output += temp + " ||";

// kolejne pozycje
for (auto& element : v[i]) {
for (auto &element : v[i]) {
output += " " + to_string(element.edgeEnd) + ", " + to_string(element.value) + " |";
}

Expand Down
3 changes: 3 additions & 0 deletions Graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,15 @@ class Graph {
std::vector<int> loadRawDataFrom(std::string path);

virtual void loadRawDataToMatrix(std::vector<int> rawData)= 0;

virtual void loadRawDataToList(std::vector<int> rawData)= 0;

bool edgeBeginningAvailable(int vertex);

bool edgeEndAvailable(int beginning, int end);

std::string printMatrix(std::vector<std::vector<int>> v);

std::string printList(std::vector<std::forward_list<EdgeListElement>> v);

};
Expand Down
4 changes: 3 additions & 1 deletion MinHeapElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ class MinHeapElement {

public:
int getEdgeBeginning() const;

int getEdgeEnd() const;

int getEdgeValue() const;

};

class MinHeapElementComparator {
public:
int operator() (const MinHeapElement& element1, const MinHeapElement& element2);
int operator()(const MinHeapElement &element1, const MinHeapElement &element2);

};

Expand Down
1 change: 0 additions & 1 deletion Program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
//

#include <iostream>
#include <limits>
#include "Program.h"

using namespace std;
Expand Down
14 changes: 7 additions & 7 deletions UndirectedGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include <random>
#include <queue>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <ctime>
Expand All @@ -27,7 +26,7 @@ std::string UndirectedGraph::getAvailableAlgorithms() {
}

void UndirectedGraph::generate(int numberOfVertices, int density, int range) {
double dens = (double)density / 100;
double dens = (double) density / 100;
dens *= numberOfVertices * (numberOfVertices - 1);
dens /= 2;
int numberOfEdges = round(dens);
Expand All @@ -39,7 +38,7 @@ void UndirectedGraph::generate(int numberOfVertices, int density, int range) {
incidenceMatrix.resize(numberOfEdges);
adjacencyList.resize(numberOfVertices);

for (auto& row : incidenceMatrix) {
for (auto &row : incidenceMatrix) {
row.assign(numberOfVertices, 0);
}

Expand Down Expand Up @@ -96,7 +95,7 @@ string UndirectedGraph::runAlgorithm(char index, char arg1, int arg2, int arg3)
throw "Algorytm nie istnieje!";
}

return output;
return output;
}

void UndirectedGraph::test() {
Expand All @@ -120,7 +119,8 @@ void UndirectedGraph::test() {
path += "-gNieskierowany-algorytmPrima-n" + to_string(numberOfElements[i]) + "-g" +
to_string(density[j]) + "-r" + representationType[k] + ".txt";

cout << "Test - Graf: Nieskierowany - Algorytm: Prima - Ilosc elem: " << numberOfElements[i] << " - Gestosc: " << density[j] << " - Reprezentacja: " << representationType[k] << endl;
cout << "Test - Graf: Nieskierowany - Algorytm: Prima - Ilosc elem: " << numberOfElements[i]
<< " - Gestosc: " << density[j] << " - Reprezentacja: " << representationType[k] << endl;

fstream file(path, fstream::out);

Expand Down Expand Up @@ -181,7 +181,7 @@ void UndirectedGraph::loadRawDataToMatrix(vector<int> rawData) {
int i = 0;
incidenceMatrix.resize(rawData[i++]); // clear vector and resize to first item of raw data

for (auto& row : incidenceMatrix) {
for (auto &row : incidenceMatrix) {
row.assign(rawData[i], 0);
}
i++;
Expand Down Expand Up @@ -328,7 +328,7 @@ std::string UndirectedGraph::primsAlgorithmOnList(bool print) {

do {
// look for edges from first vertices
for (auto& element : adjacencyList[vertexID]) {
for (auto &element : adjacencyList[vertexID]) {
queue.push(MinHeapElement(vertexID, element.edgeEnd, element.value));
}

Expand Down
6 changes: 4 additions & 2 deletions UndirectedGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ class UndirectedGraph : public Graph {
void test() override;

protected:
void loadRawDataToMatrix(std::vector<int> rawData) override ;
void loadRawDataToList(std::vector<int> rawData) override ;
void loadRawDataToMatrix(std::vector<int> rawData) override;

void loadRawDataToList(std::vector<int> rawData) override;

private:
std::string primsAlgorithmOnMatrix(bool print);

std::string primsAlgorithmOnList(bool print);

};
Expand Down

0 comments on commit f15fa2c

Please sign in to comment.