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

Commit ca1a5e8

Browse files
committed
Merge branch 'prims-algorithm' into development
2 parents e8fa276 + 172f9c1 commit ca1a5e8

11 files changed

Lines changed: 302 additions & 83 deletions

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ if (CMAKE_BUILD_TYPE MATCHES RELEASE)
77
set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++")
88
endif (CMAKE_BUILD_TYPE MATCHES RELEASE)
99

10-
set(SOURCE_FILES main.cpp Program.cpp Graph.cpp DirectedGraph.cpp UndirectedGraph.cpp)
10+
set(SOURCE_FILES main.cpp Program.cpp Graph.cpp DirectedGraph.cpp UndirectedGraph.cpp MinHeapElement.cpp)
1111
add_executable(GraphRepresentationsAndAlgorithmsComparison ${SOURCE_FILES})

DirectedGraph.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ void DirectedGraph::generate(int numberOfVertices, int density) {
7474

7575
}
7676

77-
void DirectedGraph::runAlgorithm(char index, char arg1, int arg2, int arg3) {
77+
string DirectedGraph::runAlgorithm(char index, char arg1, int arg2, int arg3) {
7878
if (index == 1) {
7979
if (arg1 == 0) {
8080
dijkstrasAlgorithmOnMatrix();

DirectedGraph.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class DirectedGraph : public Graph {
1616

1717
void generate(int numberOfVertices, int density) override;
1818

19-
void runAlgorithm(char index, char arg1, int arg2, int arg3) override;
19+
std::string runAlgorithm(char index, char arg1, int arg2, int arg3) override;
2020

2121
void test() override;
2222

Graph.cpp

Lines changed: 83 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,87 @@ int Graph::getNumberOfAvailableAlgorithms() {
2424

2525
string Graph::printIncidenceMatrix() {
2626
string output = getName();
27+
output += "\n";
28+
29+
output += printMatrix(incidenceMatrix);
30+
31+
return output;
32+
}
33+
34+
string Graph::printAdjacencyList() {
35+
string output = getName();
36+
output += "\n";
37+
38+
output += printList(adjacencyList);
39+
40+
return output;
41+
}
42+
43+
void Graph::loadDataFrom(std::string fileName) {
44+
vector<int> rawData = loadRawDataFrom(fileName);
45+
loadRawDataToMatrix(rawData);
46+
loadRawDataToList(rawData);
47+
}
48+
49+
// protected
50+
51+
vector<int> Graph::loadRawDataFrom(string path) {
52+
vector<int> returnIntVector = vector<int>();
53+
vector<string> values = vector<string>();
54+
55+
fstream file(path, ios::in);
56+
57+
if (!file.is_open())
58+
throw "Plik nie istnieje, badz zablokowany dostep!";
59+
60+
string temp = "";
61+
while (file >> temp) {
62+
try {
63+
returnIntVector.push_back(stoi(temp));
64+
} catch (const exception& e) {
65+
returnIntVector.clear();
66+
throw "Bledna zawartosc pliku! Upewnij sie ze podales odpowiedni format!";
67+
}
68+
}
69+
70+
return returnIntVector;
71+
}
72+
73+
bool Graph::edgeBeginningAvailable(int vertex) {
74+
int i = 0;
75+
for (auto& v : adjacencyList[vertex]) {
76+
i++;
77+
}
78+
79+
if (i < (adjacencyList.size() - 1)) return true;
80+
return false;
81+
}
82+
83+
bool Graph::edgeEndAvailable(int beginning, int end) {
84+
if (beginning == end) {
85+
return false;
86+
}
87+
88+
for (auto& v : adjacencyList[beginning]) {
89+
if (v.edgeEnd == end)
90+
return false;
91+
}
92+
return true;
93+
}
94+
95+
std::string Graph::printMatrix(vector<vector<int>> v) {
96+
string output = "";
2797
string temp = "";
2898

29-
if (incidenceMatrix.size() == 0) {
99+
if (v.size() == 0) {
30100
output = "Graf pusty!";
31101
return output;
32102
}
33103

34-
output += "\n K\\W ||"; //lewy gorny rog ma ' K\W ||', podwojny | dla wyroznienia komorki
104+
output += " K\\W ||"; //lewy gorny rog ma ' K\W ||', podwojny | dla wyroznienia komorki
35105

36106
// wypisz pierwsza linijke
37-
for (int i = 0; i < incidenceMatrix[0].size(); i++) {
107+
for (int i = 0; i < v[0].size(); i++) {
38108
temp = to_string(i);
39109

40110
output += " ";
@@ -47,14 +117,14 @@ string Graph::printIncidenceMatrix() {
47117
output += "\n";
48118

49119
// pozioma linia dla odzielenia wierszy
50-
int firstLineSize = output.size() - getName().size() - 2; //-2 bo dwa razy \n
120+
int firstLineSize = output.size() - 1; //-1 bo \n
51121
for (int i = 0; i < firstLineSize; i++) {
52122
output += "-";
53123
}
54124
output += "\n";
55125

56126
//wypisz kolejne linijki
57-
for (int i = 0; i < incidenceMatrix.size(); i++) {
127+
for (int i = 0; i < v.size(); i++) {
58128
// pierwsza pozycja kolejnej linijki
59129
temp = to_string(i);
60130

@@ -66,8 +136,8 @@ string Graph::printIncidenceMatrix() {
66136
output += temp + " ||";
67137

68138
// kolejne pozycje kolejnych linijek
69-
for (int j = 0; j < incidenceMatrix[0].size(); j++) {
70-
temp = to_string(incidenceMatrix[i][j]);
139+
for (int j = 0; j < v[0].size(); j++) {
140+
temp = to_string(v[i][j]);
71141

72142
output += " ";
73143
for (int k = 0; k < 5 - temp.size(); k++) {
@@ -83,24 +153,24 @@ string Graph::printIncidenceMatrix() {
83153
return output;
84154
}
85155

86-
string Graph::printAdjacencyList() {
87-
string output = getName();
156+
std::string Graph::printList(vector<forward_list<Graph::EdgeListElement>> v) {
157+
string output = "";
88158
string temp = "";
89159

90-
if (adjacencyList.size() == 0) {
160+
if (v.size() == 0) {
91161
output = "Graf pusty!";
92162
return output;
93163
}
94164

95-
output += "\n W || wierzcholek konca krawedzi, waga | kolejna... | ...\n"; // pierwsza linijka
165+
output += " W || wierzcholek konca krawedzi, waga | kolejna... | ...\n"; // pierwsza linijka
96166

97167
// druga linijka rozdzielajaca
98168
for (int i = 0; i < 80; i++) {
99169
output += "-";
100170
}
101171
output += "\n";
102172

103-
for (int i = 0; i < adjacencyList.size(); i++) {
173+
for (int i = 0; i < v.size(); i++) {
104174
// pierwsza pozycja kolejnej linijki
105175
temp = to_string(i);
106176

@@ -112,7 +182,7 @@ string Graph::printAdjacencyList() {
112182
output += temp + " ||";
113183

114184
// kolejne pozycje
115-
for (auto& element : adjacencyList[i]) {
185+
for (auto& element : v[i]) {
116186
output += " " + to_string(element.edgeEnd) + ", " + to_string(element.value) + " |";
117187
}
118188

@@ -121,55 +191,3 @@ string Graph::printAdjacencyList() {
121191

122192
return output;
123193
}
124-
125-
void Graph::loadDataFrom(std::string fileName) {
126-
vector<int> rawData = loadRawDataFrom(fileName);
127-
loadRawDataToMatrix(rawData);
128-
loadRawDataToList(rawData);
129-
}
130-
131-
// protected
132-
133-
vector<int> Graph::loadRawDataFrom(string path) {
134-
vector<int> returnIntVector = vector<int>();
135-
vector<string> values = vector<string>();
136-
137-
fstream file(path, ios::in);
138-
139-
if (!file.is_open())
140-
throw "Plik nie istnieje, badz zablokowany dostep!";
141-
142-
string temp = "";
143-
while (file >> temp) {
144-
try {
145-
returnIntVector.push_back(stoi(temp));
146-
} catch (const exception& e) {
147-
returnIntVector.clear();
148-
throw "Bledna zawartosc pliku! Upewnij sie ze podales odpowiedni format!";
149-
}
150-
}
151-
152-
return returnIntVector;
153-
}
154-
155-
bool Graph::edgeBeginningAvailable(int vertex) {
156-
int i = 0;
157-
for (auto& v : adjacencyList[vertex]) {
158-
i++;
159-
}
160-
161-
if (i < (adjacencyList.size() - 1)) return true;
162-
return false;
163-
}
164-
165-
bool Graph::edgeEndAvailable(int beginning, int end) {
166-
if (beginning == end) {
167-
return false;
168-
}
169-
170-
for (auto& v : adjacencyList[beginning]) {
171-
if (v.edgeEnd == end)
172-
return false;
173-
}
174-
return true;
175-
}

Graph.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class Graph {
4545

4646
virtual void generate(int numberOfVertices, int density)= 0;
4747

48-
virtual void runAlgorithm(char index, char arg1, int arg2, int arg3)= 0;
48+
virtual std::string runAlgorithm(char index, char arg1, int arg2, int arg3)= 0;
4949

5050
virtual void test()= 0;
5151

@@ -59,6 +59,9 @@ class Graph {
5959
bool edgeBeginningAvailable(int vertex);
6060
bool edgeEndAvailable(int beginning, int end);
6161

62+
std::string printMatrix(std::vector<std::vector<int>> v);
63+
std::string printList(std::vector<std::forward_list<EdgeListElement>> v);
64+
6265
};
6366

6467

MinHeapElement.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// Created by barto on 20.05.18.
3+
//
4+
5+
#include "MinHeapElement.h"
6+
7+
// MinHeapElement
8+
// public
9+
10+
MinHeapElement::MinHeapElement(int edgeBeginning, int edgeEnd, int edgeValue) {
11+
this->edgeBeginning = edgeBeginning;
12+
this->edgeEnd = edgeEnd;
13+
this->edgeValue = edgeValue;
14+
}
15+
16+
int MinHeapElement::getEdgeBeginning() const {
17+
return edgeBeginning;
18+
}
19+
20+
int MinHeapElement::getEdgeEnd() const {
21+
return edgeEnd;
22+
}
23+
24+
int MinHeapElement::getEdgeValue() const {
25+
return edgeValue;
26+
}
27+
28+
// MinHeapElementComparator
29+
// public
30+
31+
int MinHeapElementComparator::operator()(const MinHeapElement &element1, const MinHeapElement &element2) {
32+
return element1.getEdgeValue() > element2.getEdgeValue();
33+
}

MinHeapElement.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// Created by barto on 20.05.18.
3+
//
4+
5+
#ifndef GRAPHREPRESENTATIONSANDALGORITHMSCOMPARISON_MINHEAPELEMENT_H
6+
#define GRAPHREPRESENTATIONSANDALGORITHMSCOMPARISON_MINHEAPELEMENT_H
7+
8+
9+
class MinHeapElement {
10+
public:
11+
MinHeapElement(int edgeBeginning, int edgeEnd, int edgeValue);
12+
13+
private:
14+
int edgeBeginning;
15+
int edgeEnd;
16+
int edgeValue;
17+
18+
public:
19+
int getEdgeBeginning() const;
20+
int getEdgeEnd() const;
21+
int getEdgeValue() const;
22+
23+
};
24+
25+
class MinHeapElementComparator {
26+
public:
27+
int operator() (const MinHeapElement& element1, const MinHeapElement& element2);
28+
29+
};
30+
31+
32+
#endif //GRAPHREPRESENTATIONSANDALGORITHMSCOMPARISON_MINHEAPELEMENT_H

Program.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ void Program::start() {
2121
string matrix;
2222
string list;
2323
char index;
24+
string result;
2425

2526
do {
2627
printGraphTypeSelect();
@@ -111,7 +112,8 @@ void Program::start() {
111112
case '9':
112113
index = option2 - 53;
113114
try {
114-
runAlgorithm(index);
115+
result = runAlgorithm(index);
116+
cout << result << endl;
115117
} catch (const char* e) {
116118
cerr << e << endl;
117119
}
@@ -163,12 +165,14 @@ void Program::printGraphMenu() {
163165
cout << "Podaj opcje: ";
164166
}
165167

166-
void Program::runAlgorithm(char index) {
168+
string Program::runAlgorithm(char index) {
167169
string arg1s;
168170
char arg1;
169171
int arg2;
170172
int arg3;
171173

174+
string output;
175+
172176
if (graph->getNumberOfAvailableAlgorithms() > index - 1) {
173177
cout << "Podaj argument 1: ";
174178
cin >> arg1s;
@@ -198,8 +202,10 @@ void Program::runAlgorithm(char index) {
198202
cerr << "Bledna wartosc! Podaj argument 3: ";
199203
}
200204

201-
graph->runAlgorithm(index, arg1, arg2, arg3);
205+
output = graph->runAlgorithm(index, arg1, arg2, arg3);
202206
} else {
203207
cerr << "Nie ma takiej opcji, wybierz jeszcze raz." << endl;
204208
}
209+
210+
return output;
205211
}

Program.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Program {
2121

2222
void printGraphMenu();
2323

24-
void runAlgorithm(char index);
24+
std::string runAlgorithm(char index);
2525
};
2626

2727

0 commit comments

Comments
 (0)