lab_graphs
Gangnam-Style Graphs
graph.h
Go to the documentation of this file.
1 
14 #ifndef _GRAPH_H_
15 #define _GRAPH_H_
16 
17 #include <cstdlib>
18 #include <fstream>
19 #include <iostream>
20 #include <iomanip>
21 #include <set>
22 #include <sstream>
23 #include <string>
24 #include <unordered_map>
25 #include <utility>
26 #include <vector>
27 
28 #include "lab_graphs_random.h"
29 #include "edge.h"
30 
31 typedef std::unordered_map<Vertex, Edge> EdgeMap;
32 typedef std::unordered_map<Vertex, EdgeMap> VertexMap;
33 typedef std::unordered_map<Vertex, std::string> VertexLabelMap;
34 typedef std::unordered_map<Vertex, std::string> VertexNameMap;
35 
36 using std::cerr;
37 using std::cout;
38 using std::endl;
39 using std::vector;
40 using std::set;
41 using std::string;
42 using std::vector;
43 using std::pair;
44 using std::make_pair;
45 
49 class Graph
50 {
51  public:
56  Graph(bool isWeighted);
57 
64  Graph(bool isWeighted, int numVertices, unsigned long seed);
65 
71  vector<Vertex> getAdjacent(Vertex v) const;
72 
78  Vertex getStartingVertex() const;
79 
84  vector<Vertex> getVertices() const;
85 
92  Edge getEdge(Vertex u, Vertex v) const;
93 
98  vector<Edge> getEdges() const;
99 
108  void setVertexName(Vertex v, string name);
109 
117  string getVertexName(Vertex v) const;
118 
125  void setVertexLabel(Vertex v, string label);
126 
132  string getVertexLabel(Vertex v) const;
133 
139  void setEdgeLabel(Vertex u, Vertex v, string label);
140 
147  string getEdgeLabel(Vertex u, Vertex v) const;
148 
155  int getEdgeWeight(Vertex u, Vertex v) const;
156 
162  Vertex insertVertex(string label = "");
163 
168  void removeVertex(Vertex v);
169 
178  bool insertEdge(Vertex u, Vertex v);
179 
185  void removeEdge(Vertex u, Vertex v);
186 
193  void setEdgeWeight(Vertex u, Vertex v, int weight);
194 
199  void initSnapshot(string title);
200 
205  void snapshot();
206 
210  void print() const;
211 
216  void savePNG(string title) const;
217 
218  private:
219 
221  VertexMap graph;
222 
224  VertexLabelMap vertexLabels;
225 
227  VertexNameMap vertexNames;
228 
230  Random random;
231 
232  bool weighted;
233  int picNum;
234  string picName;
235  size_t vertexCounter;
236 
241  Vertex nextVertex();
242 
249  void assertExists(Vertex v, string functionName) const;
250 
258  void assertConnected(Vertex u, Vertex v, string functionName) const;
259 
265  void error(string message) const;
266 };
267 
268 #endif
bool insertEdge(Vertex u, Vertex v)
Inserts an edge between two vertices.
Definition: graph.cpp:277
Represents a graph of vertices and edges and allows basic operations to be performed on it...
Definition: graph.h:49
int getEdgeWeight(Vertex u, Vertex v) const
Gets the weight of an edge between two vertices.
Definition: graph.cpp:224
vector< Vertex > getVertices() const
Gets all vertices in the graph.
Definition: graph.cpp:158
void setVertexLabel(Vertex v, string label)
Labels a vertex with a string.
Definition: graph.cpp:173
void removeVertex(Vertex v)
Removes a given vertex from the graph.
Definition: graph.cpp:252
Represents an edge in a graph; used by the Graph class.
Definition: edge.h:22
Vertex insertVertex(string label="")
Inserts a new vertex into the graph and labels it.
Definition: graph.cpp:240
Provides random functionality per a given seed.
Definition: lab_graphs_random.h:22
string getVertexName(Vertex v) const
Gets the name of a vertex.
Definition: graph.cpp:514
void snapshot()
Saves a snapshot of the graph to file.
Definition: graph.cpp:398
void setEdgeLabel(Vertex u, Vertex v, string label)
Sets the edge label of an edge between vertices u and v.
Definition: graph.cpp:195
vector< Vertex > getAdjacent(Vertex v) const
Gets all adjacent vertices to the parameter vertex.
Definition: graph.cpp:85
void setEdgeWeight(Vertex u, Vertex v, int weight)
Sets the weight of an edge between two vertices.
Definition: graph.cpp:317
void print() const
Prints the graph to stdout.
Definition: graph.cpp:410
Vertex getStartingVertex() const
Returns one vertex in the graph.
Definition: graph.cpp:103
string getEdgeLabel(Vertex u, Vertex v) const
Gets the edge label of an edge between vertices u and v.
Definition: graph.cpp:210
void removeEdge(Vertex u, Vertex v)
Removes an edge between two vertices.
Definition: graph.cpp:302
void setVertexName(Vertex v, string name)
Creates a name for a vertex.
Definition: graph.cpp:502
void savePNG(string title) const
Saves the graph as a PNG image.
Definition: graph.cpp:440
Graph(bool isWeighted)
Constructor to create an empty graph.
Definition: graph.cpp:15
Edge getEdge(Vertex u, Vertex v) const
Gets an edge between two vertices.
Definition: graph.cpp:146
vector< Edge > getEdges() const
Gets all the edges in the graph.
Definition: graph.cpp:112
string getVertexLabel(Vertex v) const
Gets the label of a vertex.
Definition: graph.cpp:184
Definition and (minimal) implementation of an edge class.
void initSnapshot(string title)
Creates a name for snapshots of the graph.
Definition: graph.cpp:388