-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.h
270 lines (229 loc) · 8.04 KB
/
graph.h
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
// we took this from the lab_ml directory
#pragma once
#include <list>
#include <unordered_map>
#include <utility>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <climits>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <set>
#include <sstream>
#include <vector>
#include "edge.h"
#include "random.h"
using std::cerr;
using std::cout;
using std::endl;
using std::vector;
using std::set;
using std::string;
using std::to_string;
using std::vector;
using std::pair;
using std::make_pair;
using std::unordered_map;
using namespace std;
/**
* Represents a graph; used by the GraphTools class.
*
*/
class Graph
{
public:
/**
* Constructor to create an empty graph.
* @param weighted - specifies whether the graph is a weighted graph or
* not
*/
Graph(bool weighted);
/**
* Constructor to create an empty graph.
* @param weighted - specifies whether the graph is a weighted graph or
* not
* @param directed - specifies whether the graph is directed
*/
Graph(bool weighted, bool directed);
//constructor to create a weighted graph from a text file
Graph(string & filename);
//split helper copied over from mp_schedules
int SplitString(const std::string & str1, char sep, std::vector<std::string> &fields) {
std::string str = str1;
std::string::size_type pos;
while((pos=str.find(sep)) != std::string::npos) {
fields.push_back(str.substr(0,pos));
str.erase(0,pos+1);
}
fields.push_back(str);
return fields.size();
}
/**
* Constructor to create a random, connected graph.
* @param weighted - specifies whether the graph is a weighted graph or
* not
* @param numVertices - the number of vertices the graph will have
* @param seed - a random seed to create the graph with
*/
Graph(bool weighted, int numVertices, unsigned long seed);
/**
* Gets all adjacent vertices to the parameter vertex.
* @param source - vertex to get neighbors from
* @return a vector of vertices
*/
vector<Vertex> getAdjacent(Vertex source) const;
/**
* Returns one vertex in the graph. This function can be used
* to find a random vertex with which to start a traversal.
* @return a vertex from the graph
*/
Vertex getStartingVertex() const;
/**
* Gets all vertices in the graph.
* @return a vector of all vertices in the graph
*/
vector<Vertex> getVertices() const;
/**
* Gets an edge between two vertices.
* @param source - one vertex the edge is connected to
* @param destination - the other vertex the edge is connected to
* @return - if exist, return the corresponding edge
* - if edge doesn't exist, return Edge()
*/
Edge getEdge(Vertex source, Vertex destination) const;
/**
* Gets all the edges in the graph.
* @return a vector of all the edges in the graph
*/
vector<Edge> getEdges() const;
/**
* Checks if the given vertex exists.
* @return - if Vertex exists, true
* - if Vertex doesn't exist, return false
*/
bool vertexExists (Vertex v) const;
/**
* Checks if edge exists between two vertices exists.
* @return - if Edge exists, true
* - if Edge doesn't exist, return false
*/
bool edgeExists(Vertex source, Vertex destination) const;
/**
* Sets the edge label of the edge between vertices u and v.
* @param source - one vertex the edge is connected to
* @param destination - the other vertex the edge is connected to
* @return - if edge exists, set the label to the corresponding edge(if not directed, set the reverse one too), return edge with new label
* - if edge doesn't exist, return InvalidEdge
*/
Edge setEdgeLabel(Vertex source, Vertex destination, string label);
/**
* Gets the edge label of the edge between vertices u and v.
* @param source - one vertex the edge is connected to
* @param destination - the other vertex the edge is connected to
* @return - if edge exists, return edge label
* - if edge doesn't exist, return InvalidLabel
*/
string getEdgeLabel(Vertex source, Vertex destination) const;
/**
* Gets the weight of the edge between two vertices.
* @param source - one vertex the edge is connected to
* @param destination - the other vertex the edge is connected to
* @return - if edge exists, return edge wright
* - if doesn't, return InvalidWeight
*/
int getEdgeWeight(Vertex source, Vertex destination) const;
/**
* Inserts a new vertex into the graph and initializes its label as "".
* @param v - the name for the vertex
*/
void insertVertex(Vertex v);
/**
* Removes a given vertex from the graph.
* @param v - the vertex to remove
* @return - if v exists, return v
* - if not, return InvalidVertex;
*/
Vertex removeVertex(Vertex v);
/**
* Inserts an edge between two vertices.
* A boolean is returned for use with the random graph generation.
* Hence, an error is not thrown when it fails to insert an edge.
* @param source - one vertex the edge is connected to
* @param destination - the other vertex the edge is connected to
* @return whether inserting the edge was successful
*/
bool insertEdge(Vertex source, Vertex destination);
/**
* Removes the edge between two vertices.
* @param source - one vertex the edge is connected to
* @param destination - the other vertex the edge is connected to
* @return - if edge exists, remove it and return removed edge
* - if not, return InvalidEdge
*/
Edge removeEdge(Vertex source, Vertex destination);
/**
* Sets the weight of the edge between two vertices.
* @param source - one vertex the edge is connected to
* @param destination - the other vertex the edge is connected to
* @param weight - the weight to set to the edge
* @return - if edge exists, set edge weight and return edge with new weight
* - if not, return InvalidEdge
*/
Edge setEdgeWeight(Vertex source, Vertex destination, int weight);
void updateEdgeWeight(Vertex source, Vertex destination, int weightIncrement);
/**
* Creates a name for snapshots of the graph.
* @param title - the name to save the snapshots as
*/
void initSnapshot(string title);
/**
* Saves a snapshot of the graph to file.
* initSnapshot() must be run first.
*/
void snapshot();
/**
* Prints the graph to stdout.
*/
void print() const;
/**
* Saves the graph as a PNG image.
* @param title - the filename of the PNG image
*/
void savePNG(string title) const;
bool isDirected() const;
void clear();
const static Vertex InvalidVertex;
const static Edge InvalidEdge;
const static int InvalidWeight;
const static string InvalidLabel;
private:
mutable unordered_map<Vertex, unordered_map<Vertex, Edge>> adjacency_list;
bool weighted;
bool directed;
Random random;
int picNum;
string picName;
/**
* Returns whether a given vertex exists in the graph.
* @param v - the vertex to check
* @param functionName - the name of the calling function to return
* in the event of an error
*/
bool assertVertexExists(Vertex v, string functionName) const;
/**
* Returns whether thee edge exists in the graph.
* @param source - one vertex
* @param destination - another vertex
* @param functionName - the name of the calling function to return
* in the event of an error
*/
bool assertEdgeExists(Vertex source, Vertex destination, string functionName) const;
/**
* Prints a graph error and quits the program.
* The program is exited with a segfault to provide a stack trace.
* @param message - the error message that is printed
*/
void error(string message) const;
};