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

Commit 966bf17

Browse files
committed
Merge branch 'import-graphs' into development
2 parents 4829273 + 0b2edf4 commit 966bf17

7 files changed

Lines changed: 154 additions & 27 deletions

File tree

DirectedGraph.cpp

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include "DirectedGraph.h"
66

7+
using namespace std;
8+
79
// public
810

911
DirectedGraph::DirectedGraph() : Graph("Graf skierowany", 1) {}
@@ -16,10 +18,6 @@ std::string DirectedGraph::getAvailableAlgorithms() {
1618
return output;
1719
}
1820

19-
void DirectedGraph::loadDataFrom(std::string fileName) {
20-
21-
}
22-
2321
void DirectedGraph::generate(int numberOfVertices, int density) {
2422

2523
}
@@ -31,3 +29,42 @@ void DirectedGraph::runAlgorithm(int index, int arg1, int arg2) {
3129
void DirectedGraph::test() {
3230

3331
}
32+
33+
// private
34+
35+
void DirectedGraph::loadRawDataToMatrix(std::vector<int> rawData) {
36+
incidenceMatrix.clear();
37+
int i = 0;
38+
incidenceMatrix.resize(rawData[i++]); // clear vector and resize to first item of raw data
39+
40+
for (auto& row : incidenceMatrix) {
41+
row.assign(rawData[i], 0);
42+
}
43+
i++;
44+
45+
for (int j = 0; j < incidenceMatrix.size(); j++) {
46+
int edgeBeginning = rawData[i++];
47+
int edgeEnd = rawData[i++];
48+
int edgeValue = rawData[i++];
49+
50+
incidenceMatrix[j][edgeEnd] = edgeValue * -1;
51+
incidenceMatrix[j][edgeBeginning] = edgeValue;
52+
}
53+
}
54+
55+
void DirectedGraph::loadRawDataToList(std::vector<int> rawData) {
56+
adjacencyList.clear();
57+
int i = 0;
58+
59+
int numberOfEdges = rawData[i++];
60+
61+
adjacencyList.resize(rawData[i++]);
62+
63+
for (int j = 0; j < numberOfEdges; j++) {
64+
int edgeBeginning = rawData[i++];
65+
int edgeEnd = rawData[i++];
66+
int edgeValue = rawData[i++];
67+
68+
adjacencyList[edgeBeginning].push_front({edgeEnd, edgeValue});
69+
}
70+
}

DirectedGraph.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@ class DirectedGraph : public Graph {
1414

1515
std::string getAvailableAlgorithms() override;
1616

17-
void loadDataFrom(std::string fileName) override;
18-
1917
void generate(int numberOfVertices, int density) override;
2018

2119
void runAlgorithm(int index, int arg1, int arg2) override;
2220

2321
void test() override;
2422

23+
protected:
24+
void loadRawDataToMatrix(std::vector<int> rawData) override ;
25+
void loadRawDataToList(std::vector<int> rawData) override ;
26+
2527
};
2628

2729

Graph.cpp

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,29 @@
22
// Created by barto on 19.05.18.
33
//
44

5+
#include <fstream>
56
#include "Graph.h"
67

8+
using namespace std;
9+
710
// protected
811

9-
Graph::Graph(const std::string name, int numberOfAvailableAlgorithms)
12+
Graph::Graph(const string name, int numberOfAvailableAlgorithms)
1013
: name(name), numberOfAvailableAlgorithms(numberOfAvailableAlgorithms) {}
1114

1215
// public
1316

14-
std::string Graph::getName() {
17+
string Graph::getName() {
1518
return name;
1619
}
1720

1821
int Graph::getNumberOfAvailableAlgorithms() {
1922
return numberOfAvailableAlgorithms;
2023
}
2124

22-
std::string Graph::printIncidenceMatrix() {
23-
std::string output = getName();
24-
std::string temp = "";
25+
string Graph::printIncidenceMatrix() {
26+
string output = getName();
27+
string temp = "";
2528

2629
if (incidenceMatrix.size() == 0) {
2730
output = "Graf pusty!";
@@ -32,7 +35,7 @@ std::string Graph::printIncidenceMatrix() {
3235

3336
// wypisz pierwsza linijke
3437
for (int i = 0; i < incidenceMatrix[0].size(); i++) {
35-
temp = std::to_string(i);
38+
temp = to_string(i);
3639

3740
output += " ";
3841
for (int j = 0; j < 5 - temp.size(); j++) { //miejsce na 6 znakow dla kazdej komorki
@@ -53,7 +56,7 @@ std::string Graph::printIncidenceMatrix() {
5356
//wypisz kolejne linijki
5457
for (int i = 0; i < incidenceMatrix.size(); i++) {
5558
// pierwsza pozycja kolejnej linijki
56-
temp = std::to_string(i);
59+
temp = to_string(i);
5760

5861
output += " ";
5962
for (int j = 0; j < 5 - temp.size(); j++) {
@@ -64,7 +67,7 @@ std::string Graph::printIncidenceMatrix() {
6467

6568
// kolejne pozycje kolejnych linijek
6669
for (int j = 0; j < incidenceMatrix[0].size(); j++) {
67-
temp = std::to_string(incidenceMatrix[i][j]);
70+
temp = to_string(incidenceMatrix[i][j]);
6871

6972
output += " ";
7073
for (int k = 0; k < 5 - temp.size(); k++) {
@@ -80,9 +83,9 @@ std::string Graph::printIncidenceMatrix() {
8083
return output;
8184
}
8285

83-
std::string Graph::printAdjacencyList() {
84-
std::string output = getName();
85-
std::string temp = "";
86+
string Graph::printAdjacencyList() {
87+
string output = getName();
88+
string temp = "";
8689

8790
if (adjacencyList.size() == 0) {
8891
output = "Graf pusty!";
@@ -99,7 +102,7 @@ std::string Graph::printAdjacencyList() {
99102

100103
for (int i = 0; i < adjacencyList.size(); i++) {
101104
// pierwsza pozycja kolejnej linijki
102-
temp = std::to_string(i);
105+
temp = to_string(i);
103106

104107
output += " ";
105108
for (int j = 0; j < 5 - temp.size(); j++) {
@@ -110,11 +113,41 @@ std::string Graph::printAdjacencyList() {
110113

111114
// kolejne pozycje
112115
for (auto& element : adjacencyList[i]) {
113-
output += " " + std::to_string(element.edgeEnd) + ", " + std::to_string(element.value) + " |";
116+
output += " " + to_string(element.edgeEnd) + ", " + to_string(element.value) + " |";
114117
}
115118

116119
output += "\n";
117120
}
118121

119122
return output;
120123
}
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+
return returnIntVector; // should throw an error
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+
}

Graph.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,21 @@ class Graph {
4141

4242
std::string printAdjacencyList();
4343

44-
virtual void loadDataFrom(std::string fileName)= 0;
44+
void loadDataFrom(std::string fileName);
4545

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

4848
virtual void runAlgorithm(int index, int arg1, int arg2)= 0;
4949

5050
virtual void test()= 0;
5151

52+
protected:
53+
54+
std::vector<int> loadRawDataFrom(std::string path);
55+
56+
virtual void loadRawDataToMatrix(std::vector<int> rawData)= 0;
57+
virtual void loadRawDataToList(std::vector<int> rawData)= 0;
58+
5259
};
5360

5461

Program.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,11 @@ void Program::start() {
6161
cout << "Podaj nazwe pliku: ";
6262
cin >> file;
6363

64-
graph->loadDataFrom(file);
64+
try {
65+
graph->loadDataFrom(file);
66+
} catch (const char* e) {
67+
cout<<e<<endl;
68+
}
6569
break;
6670

6771
case '2': // generuj losowo

UndirectedGraph.cpp

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include "UndirectedGraph.h"
66

7+
using namespace std;
8+
79
// public
810

911
UndirectedGraph::UndirectedGraph() : Graph("Graf nieskierowany", 1) {}
@@ -16,10 +18,6 @@ std::string UndirectedGraph::getAvailableAlgorithms() {
1618
return output;
1719
}
1820

19-
void UndirectedGraph::loadDataFrom(std::string fileName) {
20-
21-
}
22-
2321
void UndirectedGraph::generate(int numberOfVertices, int density) {
2422

2523
}
@@ -31,3 +29,47 @@ void UndirectedGraph::runAlgorithm(int index, int arg1, int arg2) {
3129
void UndirectedGraph::test() {
3230

3331
}
32+
33+
// private
34+
35+
void UndirectedGraph::loadRawDataToMatrix(vector<int> rawData) {
36+
incidenceMatrix.clear();
37+
int i = 0;
38+
incidenceMatrix.resize(rawData[i++]); // clear vector and resize to first item of raw data
39+
40+
for (auto& row : incidenceMatrix) {
41+
row.assign(rawData[i], 0);
42+
}
43+
i++;
44+
45+
for (int j = 0; j < incidenceMatrix.size(); j++) {
46+
int edgeBeginning = rawData[i++];
47+
int edgeEnd = rawData[i++];
48+
int edgeValue = rawData[i++];
49+
50+
incidenceMatrix[j][edgeEnd] = edgeValue;
51+
incidenceMatrix[j][edgeBeginning] = edgeValue;
52+
}
53+
}
54+
55+
void UndirectedGraph::loadRawDataToList(std::vector<int> rawData) {
56+
adjacencyList.clear();
57+
int i = 0;
58+
59+
int numberOfEdges = rawData[i++];
60+
61+
adjacencyList.resize(rawData[i++]);
62+
63+
for (int j = 0; j < numberOfEdges; j++) {
64+
int edgeBeginning = rawData[i++];
65+
int edgeEnd = rawData[i++];
66+
int edgeValue = rawData[i++];
67+
68+
adjacencyList[edgeBeginning].push_front({edgeEnd, edgeValue});
69+
70+
if (edgeBeginning == edgeEnd)
71+
continue;
72+
73+
adjacencyList[edgeEnd].push_front({edgeBeginning, edgeValue});
74+
}
75+
}

UndirectedGraph.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@ class UndirectedGraph : public Graph {
1414

1515
std::string getAvailableAlgorithms() override;
1616

17-
void loadDataFrom(std::string fileName) override;
18-
1917
void generate(int numberOfVertices, int density) override;
2018

2119
void runAlgorithm(int index, int arg1, int arg2) override;
2220

2321
void test() override;
2422

23+
protected:
24+
void loadRawDataToMatrix(std::vector<int> rawData) override ;
25+
void loadRawDataToList(std::vector<int> rawData) override ;
26+
2527
};
2628

2729

0 commit comments

Comments
 (0)