This repository has been archived by the owner on May 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph.cpp
193 lines (148 loc) · 3.79 KB
/
Graph.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//
// Created by barto on 19.05.18.
//
#include <fstream>
#include "Graph.h"
using namespace std;
// protected
Graph::Graph(const string name, int numberOfAvailableAlgorithms)
: name(name), numberOfAvailableAlgorithms(numberOfAvailableAlgorithms) {}
// public
string Graph::getName() {
return name;
}
int Graph::getNumberOfAvailableAlgorithms() {
return numberOfAvailableAlgorithms;
}
string Graph::printIncidenceMatrix() {
string output = getName();
output += "\n";
output += printMatrix(incidenceMatrix);
return output;
}
string Graph::printAdjacencyList() {
string output = getName();
output += "\n";
output += printList(adjacencyList);
return output;
}
void Graph::loadDataFrom(std::string fileName) {
vector<int> rawData = loadRawDataFrom(fileName);
loadRawDataToMatrix(rawData);
loadRawDataToList(rawData);
}
// protected
vector<int> Graph::loadRawDataFrom(string path) {
vector<int> returnIntVector = vector<int>();
vector<string> values = vector<string>();
fstream file(path, ios::in);
if (!file.is_open())
throw "Plik nie istnieje, badz zablokowany dostep!";
string temp = "";
while (file >> temp) {
try {
returnIntVector.push_back(stoi(temp));
} catch (const exception &e) {
returnIntVector.clear();
throw "Bledna zawartosc pliku! Upewnij sie ze podales odpowiedni format!";
}
}
return returnIntVector;
}
bool Graph::edgeBeginningAvailable(int vertex) {
int i = 0;
for (auto &v : adjacencyList[vertex]) {
i++;
}
if (i < (adjacencyList.size() - 1)) return true;
return false;
}
bool Graph::edgeEndAvailable(int beginning, int end) {
if (beginning == end) {
return false;
}
for (auto &v : adjacencyList[beginning]) {
if (v.edgeEnd == end)
return false;
}
return true;
}
std::string Graph::printMatrix(vector<vector<int>> v) {
string output = "";
string temp = "";
if (v.size() == 0) {
output = "Graf pusty!";
return output;
}
output += " K\\W ||"; //lewy gorny rog ma ' K\W ||', podwojny | dla wyroznienia komorki
// wypisz pierwsza linijke
for (int i = 0; i < v[0].size(); i++) {
temp = to_string(i);
output += " ";
for (int j = 0; j < 5 - temp.size(); j++) { //miejsce na 6 znakow dla kazdej komorki
if (temp.size() > 5) break;
output += " ";
}
output += temp + " |";
}
output += "\n";
// pozioma linia dla odzielenia wierszy
int firstLineSize = output.size() - 1; //-1 bo \n
for (int i = 0; i < firstLineSize; i++) {
output += "-";
}
output += "\n";
//wypisz kolejne linijki
for (int i = 0; i < v.size(); i++) {
// pierwsza pozycja kolejnej linijki
temp = to_string(i);
output += " ";
for (int j = 0; j < 5 - temp.size(); j++) {
if (temp.size() > 5) break;
output += " ";
}
output += temp + " ||";
// kolejne pozycje kolejnych linijek
for (int j = 0; j < v[0].size(); j++) {
temp = to_string(v[i][j]);
output += " ";
for (int k = 0; k < 5 - temp.size(); k++) {
if (temp.size() > 5) break;
output += " ";
}
output += temp + " |";
}
output += "\n";
}
return output;
}
std::string Graph::printList(vector<forward_list<Graph::EdgeListElement>> v) {
string output = "";
string temp = "";
if (v.size() == 0) {
output = "Graf pusty!";
return output;
}
output += " W || wierzcholek konca krawedzi, waga | kolejna... | ...\n"; // pierwsza linijka
// druga linijka rozdzielajaca
for (int i = 0; i < 80; i++) {
output += "-";
}
output += "\n";
for (int i = 0; i < v.size(); i++) {
// pierwsza pozycja kolejnej linijki
temp = to_string(i);
output += " ";
for (int j = 0; j < 5 - temp.size(); j++) {
if (temp.size() > 5) break;
output += " ";
}
output += temp + " ||";
// kolejne pozycje
for (auto &element : v[i]) {
output += " " + to_string(element.edgeEnd) + ", " + to_string(element.value) + " |";
}
output += "\n";
}
return output;
}