-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.cpp
More file actions
416 lines (373 loc) · 10.3 KB
/
Copy pathGraph.cpp
File metadata and controls
416 lines (373 loc) · 10.3 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#include "Graph.h"
#include <string>
#include <iostream>
#include <unordered_map>
#include <vector>
#include <fstream>
#include <queue>
#include <algorithm>
#include <cctype>
#include <climits>
using namespace std;
/* This holds the connected name and the corresponding weight
struct EdgeInfo{
std::string name;
int weight;
};
// This will hold the data members of the vertices, including an adjacency list
struct VertexInfo{
int value;
std::vector<EdgeInfo> edges;
};
// A map is used so that the name is used as the index
std::unordered_map<std::string, VertexInfo> vertices;
*/
void clear_spaces(string& str){
if(str.size() > 0){
str.erase(0, 1);
}
}
#include <sstream>
void Graph::readFromFile(string file){
vertices.clear();
fstream fin(file);
string temp;
getline(fin, temp);
stringstream ss(temp);
int N;
ss >> N;
for(int i = 0; i < N; i++){
getline(fin, temp);
stringstream vss(temp);
string name, val;
getline(vss, name, ',');
getline(vss, val);
// take care of comma at the end, using a self-made pop_back
float value = ::atof(val.c_str());
addVertex(name, value);
}
while(getline(fin, temp)){
//if name1 exits, the line also has name2 and weight
stringstream ess(temp);
string name1, name2, _weight;
getline(ess, name1, ',');
getline(ess, name2, ',');
getline(ess, _weight);
//erase whitespaces in strings
clear_spaces(name2);
int weight = ::atoi(_weight.c_str());
addEdge(name1, name2, weight);
}
fin.close();
}
void Graph::writeToFile(string file){
unordered_map<string, unordered_map<string, bool> > visited;
cout << vertices.size() << endl;
for(auto it = vertices.begin(); it != vertices.end(); it++){
cout << it->first << ", " << (it->second).value << endl;
}
for(auto it = vertices.begin(); it != vertices.end(); it++){
// mark this vertex as visited, but still go through it to see if any of it's edges aren't marked
size_t size = ((it->second).edges).size();
for(int i = 0; i < size; i++){
string name2 = ((it->second).edges)[i].name;
if(!(visited[it->first][name2])){
// don't print duplicates
visited[it->first][name2] = 1;
visited[name2][it->first] = 1;
cout << it->first << ", " << name2 << ", " << ((it->second).edges)[i].weight << endl;
}
}
}
}
bool Graph::empty(){
return vertices.empty();
}
void Graph::addEdge(string v1, string v2, int weight){
if(vertices.find(v1) == vertices.end() || vertices.find(v2) == vertices.end()){
cout << "One or more of the vertices do not exist, can't create edge" << endl;
return;
}
for(int i = 0; i < vertices[v1].edges.size(); i++){
if(vertices[v1].edges[i].name == v2){
// update weight
vertices[v1].edges[i].weight = weight;
//do same for v2
for(int j = 0; j < vertices[v2].edges.size(); j++){
if(vertices[v2].edges[j].name == v1){
vertices[v2].edges[j].weight = weight;
return;
}
}
}
}
EdgeInfo e1(v2, weight);
EdgeInfo e2(v1, weight);
(vertices[v1].edges).push_back(e1);
(vertices[v2].edges).push_back(e2);
}
void Graph::addVertex(string name, float val){
if(val < 0){
cerr << name << " has a negative value. IGNORED." << endl;
return;
}
if(vertices.find(name) != vertices.end()){
vertices[name].value = val;
return;
}
VertexInfo vertex(val);
vertices[name] = vertex;
}
int Graph::connectedNodes(string source, unordered_map<string, bool>& visited){
queue<string> nodes;
nodes.push(source);
visited[source] = 1;
int ret = 0;
while(!nodes.empty()){
source = nodes.front();
nodes.pop();
ret++;
for(int i = 0; i < vertices[source].edges.size(); i++){
string edge = vertices[source].edges[i].name;
if(!visited[edge]++){
nodes.push(edge);
}
}
}
return ret;
}
int Graph::numConnectedComponents(){
unordered_map<string, bool> visited;
int total = 0;
int components = 0;
while(total < vertices.size()){
components++;
string node;
for(auto it = vertices.begin(); it != vertices.end(); it++){
if(!visited[it->first]){
node = it->first;
break;
}
}
total += connectedNodes(node, visited);
}
return components;
}
bool Graph::tree_aux(string node, string parent, unordered_map<string, bool>& visited){
for(int i = 0; i < vertices[node].edges.size(); i++){
string adj = (vertices[node].edges)[i].name;
if(adj != parent && visited[adj]){
return false;
}
visited[adj]++;
if(adj != parent && !tree_aux(adj, node, visited))
return false;
}
return true;
}
bool Graph::tree(){
if(numConnectedComponents() > 1) return false;
unordered_map<string, bool> visited;
string node = (vertices.begin())->first;
visited[node] = 1;
for(int i = 0; i < vertices[node].edges.size(); i++){
string adj = (vertices[node].edges)[i].name;
if(!tree_aux(adj, node, visited))
return false;
}
return true;
}
void Graph::minWeightComponent(string src){
unordered_map<string, bool> totalhelp;
int total = connectedNodes(src, totalhelp);
int completed = 1;
vector<string> minTree;
vector<string> edges;
minTree.push_back(src);
unordered_map<string, unordered_map<string, bool> > visited;
while(completed < total){
//this will change to find smallest edge
int min = INT_MAX;
string smin1, smin2;
for(int i = 0; i < minTree.size(); i++){
string name1 = minTree[i];
for(int j = 0; j < vertices[name1].edges.size(); j++){
string name2 = vertices[name1].edges[j].name;
if(!visited[name1][name2] && vertices[name1].edges[j].weight < min){
min = vertices[name1].edges[j].weight;
smin1 = name1;
smin2 = name2;
}
}
}
minTree.push_back(smin2);
edges.push_back(smin1);
edges.push_back(smin2);
visited[smin1][smin2] = 1;
visited[smin2][smin1] = 1;
completed++;
}
cout << "{{";
for(int i = 0; i < minTree.size();i++){
cout << minTree[i];
if(i < minTree.size()-1)
cout << ", ";
}
cout << "}, {";
for(int i = 0; i < edges.size(); i++){
cout << "(" << edges[i] << ", ";
i++;
cout << edges[i] << ")";
if(i < edges.size()-1)
cout << ",";
}
cout << "}}" << endl << endl;
}
bool Graph::DFS_aux(string node, string val, unordered_map<string, bool>& visited){
if(node == val)
return true;
visited[node] = 1;
bool ret = false;
for(int i = 0; i < vertices[node].edges.size(); i++){
string adj = (vertices[node].edges)[i].name;
if(!visited[adj]){
if(DFS_aux(adj, val, visited)){
ret = true;
break;
}
}
}
return ret;
}
bool Graph::DFS(string source, string val){
// Makse sure that source is in the vertices
if(vertices.find(source) == vertices.end())
return false;
unordered_map<string, bool> visited;
return DFS_aux(source, val, visited);
}
bool Graph::BFS(string source, string val){
if(vertices.find(source) == vertices.end())
return false;
unordered_map<string, bool> visited;
queue<string> nodes;
nodes.push(source);
visited[source] = 1;
while(!nodes.empty()){
source = nodes.front();
nodes.pop();
if(source == val)
return true;
for(int i = 0; i < vertices[source].edges.size(); i++){
string edge = vertices[source].edges[i].name;
if(!visited[edge]++){
nodes.push(edge);
}
}
}
return false;
}
int Graph::closeness(string v1, string v2){ }
bool Graph::partitionable_aux(string node, int side, unordered_map<string, int>& visited){
for(int i = 0; i < vertices[node].edges.size(); i++){
string name = vertices[node].edges[i].name;
if(visited[name] == 0){
visited[name] = -side;
if(!partitionable_aux(name, -side, visited)){
return false;
}
} else if(visited[name] == side){
return false;
} // else it has already been visited
}
return true;
}
bool Graph::partitionable(){
if(numConnectedComponents() > 1) return false;
// visited will hold the class of each element: 1 and -1
unordered_map<string, int> visited;
string node = (vertices.begin())->first;
visited[node] = 1;
return partitionable_aux(node, 1, visited);
}
bool Graph::isSubGraph(const Graph& g){
// iterator through all vertices in g
for(auto it = g.vertices.begin(); it != g.vertices.end(); it++){
string v = it->first;
//make sure v exists
if(vertices.find(v) == vertices.end()){
return false;
}
for(int i = 0; i < it->second.edges.size(); i++){
string adj = it->second.edges[i].name;
bool found = false;
for(int j = 0; j < vertices[v].edges.size(); j++){
if(adj == vertices[v].edges[j].name){
found = true;
break;
}
}
if(!found)
return false;
}
}
return true;
}
#include <cfloat>
float Fabs(float v){
if(v < 0)
return -v;
else
return v;
}
bool leftequals(float left, float right, float value){
float l = Fabs(left - value);
float r = Fabs(right - value);
return l == r;
}
bool leftcloser(float left, float right, float value){
float l = Fabs(left - value);
float r = Fabs(right - value);
return l < r;
}
/*struct nodepath{
float total;
vector<string> path;
nodepath() : total(0.0), bool(0) {}
};*/
void Graph::CloseVal_aux(string name, nodepath npath, float value, nodepath& final){
npath.path.push_back(name);
npath.total += vertices[name].value;
if(leftequals(npath.total, final.total, value)){
final.path.push_back("\n---\n");
final.path.insert(final.path.end(), npath.path.begin(), npath.path.end());
} else if(leftcloser(npath.total, final.total, value)){
final = npath;
}
for(int i = 0; i< vertices[name].edges.size(); i++){
string nextstr = vertices[name].edges[i].name;
bool visited = false;
for(int j = 0; j < npath.path.size(); j++){
if(npath.path[j] == nextstr){
visited = true;
break;
}
}
if(!visited && leftcloser(npath.total + vertices[nextstr].value, npath.total, value)){
CloseVal_aux(nextstr, npath, value, final);
}
}
}
void Graph::printPathCloseVal(float value){
nodepath final;
final.total = FLT_MAX;
for(auto it = vertices.begin(); it != vertices.end(); it++){
nodepath path;
CloseVal_aux(it->first, path, value, final);
}
for(int i = 0; i < final.path.size(); i++){
cout << final.path[i] << " ";
if(i < final.path.size()-1)
cout << "-> ";
}
}