-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph.cpp
executable file
·184 lines (143 loc) · 3.96 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
#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include "Graph.h"
std::set<Vertex> getVerticesFromFile(std::ifstream* fs);
std::set<Edge> getEdgesFromFile(std::ifstream* fs);
Graph* Graph::constructFromFile(const char* fileName)
{
std::cout << "Construct the graph from the file..." << std::endl;
std::ifstream fs;
fs.open(fileName);
if (fs.fail())
{
std::cout << "Failed to open file: " << fileName << "!" << std::endl;
fs.close();
return NULL;
}
std::set<Vertex> vertices;
std::set<Edge> edges;
std::string row;
std::stringstream converter;
while(!fs.eof())
{
std::getline(fs, row);
if (row.length() == 0 || row[0] != '#') continue; // skip row
const std::size_t startTag = row.find(TAG_START);
const std::size_t verticesTag = row.find(TAG_VERTICES);
const std::size_t edgesTag = row.find(TAG_EDGES);
if (startTag == std::string::npos
|| (verticesTag == std::string::npos
&& edgesTag == std::string::npos)
|| (verticesTag != std::string::npos
&& edgesTag != std::string::npos))
{
std::cout << "File format is wrong!" << std::endl;
if (DEBUG) std::cout << "at line: " << row << std::endl;
fs.close();
return NULL;
}
if (verticesTag != std::string::npos)
{
vertices = getVerticesFromFile(&fs);
}
else if (edgesTag != std::string::npos)
{
edges = getEdgesFromFile(&fs);
}
}
if (vertices.size() > 0 && edges.size() > 0) {
return new Graph(vertices, edges);
} else {
return NULL;
}
}
std::set<Vertex> getVerticesFromFile(std::ifstream* fs)
{
if (DEBUG) std::cout << "Reading vertices..." << std::endl;
std::set<Vertex> vertices;
std::string row;
std::stringstream converter;
while(!fs -> eof())
{
converter.clear(); // reset converter
Vertex v;
std::getline(*fs, row);
if (row[0] == '#'
&& row.find(TAG_END) != std::string::npos)
break; // reach #END
converter.str(row);
converter >> v;
if (converter.fail())
{
std::cout << "Fail to read vertex: " << row << std::endl;
continue; // skip wrong row
}
vertices.insert(v);
}
return vertices;
}
std::set<Edge> getEdgesFromFile(std::ifstream* fs)
{
if (DEBUG) std::cout << "Reading edges..." << std::endl;
std::set<Edge> edges;
std::string row;
char ch;
std::stringstream converter;
while(!fs -> eof())
{
converter.clear(); // reset converter
int start, end, cost;
std::getline(*fs, row);
if (row[0] == '#'
&& row.find(TAG_END) != std::string::npos)
break; // reach #END
converter.str(row);
converter >> start >> end >> cost;
if (converter.fail())
{
std::cout << "Fail to read edge: " << row << std::endl;
continue; // skip wrong row
}
Edge e;
e.start = start;
e.end = end;
e.cost = cost;
edges.insert(e);
}
return edges;
}
Graph::Graph(const std::set<Vertex> vertices, const std::set<Edge> edges)
: vertices(vertices), edges(edges)
{
if (!DEBUG) return;
std::cout << "Vertices: " << std::endl;
for (Vertex v : vertices)
{
std::cout << v << std:: endl;
}
std::cout << "Edges: " << std::endl;
for (Edge e : edges)
{
std::cout << e.start << " " << e.end << " " << e.cost << " " << std:: endl;
}
}
const bool Graph::isValid() const
{
for (Edge e : edges)
{
if (vertices.find(e.start) == vertices.end()
|| vertices.find(e.end) == vertices.end())
{
if (DEBUG) std::cout << "\'" << e.start << "\'" << "or" << "\'" << e.end << "\'" <<
" is not a declared vertex." << std::endl;
return false;
}
}
return true;
}
const int Graph::size() const
{
return vertices.size();
}