lab_graphs
Gangnam-Style Graphs
|
Represents a graph of vertices and edges and allows basic operations to be performed on it. More...
#include "graph.h"
Public Member Functions | |
Graph (bool isWeighted) | |
Constructor to create an empty graph. More... | |
Graph (bool isWeighted, int numVertices, unsigned long seed) | |
Constructor to create a random, connected graph. More... | |
vector< Vertex > | getAdjacent (Vertex v) const |
Gets all adjacent vertices to the parameter vertex. More... | |
Vertex | getStartingVertex () const |
Returns one vertex in the graph. More... | |
vector< Vertex > | getVertices () const |
Gets all vertices in the graph. More... | |
Edge | getEdge (Vertex u, Vertex v) const |
Gets an edge between two vertices. More... | |
vector< Edge > | getEdges () const |
Gets all the edges in the graph. More... | |
void | setVertexName (Vertex v, string name) |
Creates a name for a vertex. More... | |
string | getVertexName (Vertex v) const |
Gets the name of a vertex. More... | |
void | setVertexLabel (Vertex v, string label) |
Labels a vertex with a string. More... | |
string | getVertexLabel (Vertex v) const |
Gets the label of a vertex. More... | |
void | setEdgeLabel (Vertex u, Vertex v, string label) |
Sets the edge label of an edge between vertices u and v. More... | |
string | getEdgeLabel (Vertex u, Vertex v) const |
Gets the edge label of an edge between vertices u and v. More... | |
int | getEdgeWeight (Vertex u, Vertex v) const |
Gets the weight of an edge between two vertices. More... | |
Vertex | insertVertex (string label="") |
Inserts a new vertex into the graph and labels it. More... | |
void | removeVertex (Vertex v) |
Removes a given vertex from the graph. More... | |
bool | insertEdge (Vertex u, Vertex v) |
Inserts an edge between two vertices. More... | |
void | removeEdge (Vertex u, Vertex v) |
Removes an edge between two vertices. More... | |
void | setEdgeWeight (Vertex u, Vertex v, int weight) |
Sets the weight of an edge between two vertices. More... | |
void | initSnapshot (string title) |
Creates a name for snapshots of the graph. More... | |
void | snapshot () |
Saves a snapshot of the graph to file. More... | |
void | print () const |
Prints the graph to stdout. More... | |
void | savePNG (string title) const |
Saves the graph as a PNG image. More... | |
Represents a graph of vertices and edges and allows basic operations to be performed on it.
Graph::Graph | ( | bool | isWeighted | ) |
Constructor to create an empty graph.
isWeighted | - specifies whether the graph is a weighted graph or not |
Graph::Graph | ( | bool | isWeighted, |
int | numVertices, | ||
unsigned long | seed | ||
) |
Constructor to create a random, connected graph.
isWeighted | - specifies whether the graph is a weighted graph or not |
numVertices | - the number of vertices the graph will have |
seed | - a random seed to create the graph with |
vector< Vertex > Graph::getAdjacent | ( | Vertex | v | ) | const |
Gets all adjacent vertices to the parameter vertex.
v | - the vertex to get neighbors from |
Vertex Graph::getStartingVertex | ( | ) | const |
Returns one vertex in the graph.
This function can be used to find a random vertex with which to start a traversal.
vector< Vertex > Graph::getVertices | ( | ) | const |
Gets all vertices in the graph.
Edge Graph::getEdge | ( | Vertex | u, |
Vertex | v | ||
) | const |
Gets an edge between two vertices.
u | - one vertex the edge is connected to |
v | - the other vertex the edge is connected to |
Gets all the edges in the graph.
void Graph::setVertexName | ( | Vertex | v, |
string | name | ||
) |
Creates a name for a vertex.
This is NOT for marking as unvisited, cross, etc. Use setVertexLabel instead for that. This is only really used for the premade graphs where particular nodes have fun names, such as cities.
v | - the vertex to label |
label | - label of the vertex to change |
string Graph::getVertexName | ( | Vertex | v | ) | const |
Gets the name of a vertex.
This is only really used for the premade graphs where particular nodes have fun names, such as cities.
v | - vertex to get the name from |
void Graph::setVertexLabel | ( | Vertex | v, |
string | label | ||
) |
Labels a vertex with a string.
This is a method.
v | - the vertex to label |
label | - label of the vertex to change |
string Graph::getVertexLabel | ( | Vertex | v | ) | const |
Gets the label of a vertex.
v | - vertex to get label from |
void Graph::setEdgeLabel | ( | Vertex | u, |
Vertex | v, | ||
string | label | ||
) |
Sets the edge label of an edge between vertices u and v.
u | - one vertex the edge is connected to |
v | - the other vertex the edge is connected to |
string Graph::getEdgeLabel | ( | Vertex | u, |
Vertex | v | ||
) | const |
Gets the edge label of an edge between vertices u and v.
u | - one vertex the edge is connected to |
v | - the other vertex the edge is connected to |
int Graph::getEdgeWeight | ( | Vertex | u, |
Vertex | v | ||
) | const |
Gets the weight of an edge between two vertices.
u | - one vertex the edge is connected to |
v | - the other vertex the edge is connected to |
Vertex Graph::insertVertex | ( | string | label = "" | ) |
Inserts a new vertex into the graph and labels it.
label | the label for the vertex |
void Graph::removeVertex | ( | Vertex | v | ) |
Removes a given vertex from the graph.
v | - the vertex to remove |
bool Graph::insertEdge | ( | Vertex | u, |
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.
u | - one vertex the edge is connected to |
v | - the other vertex the edge is connected to |
void Graph::removeEdge | ( | Vertex | u, |
Vertex | v | ||
) |
Removes an edge between two vertices.
u | - one vertex the edge is connected to |
v | - the other vertex the edge is connected to |
void Graph::setEdgeWeight | ( | Vertex | u, |
Vertex | v, | ||
int | weight | ||
) |
Sets the weight of an edge between two vertices.
u | - one vertex the edge is connected to |
v | - the other vertex the edge is connected to |
weight | - the weight to set the edge |
void Graph::initSnapshot | ( | string | title | ) |
Creates a name for snapshots of the graph.
title | - the name to save the snapshots as |
void Graph::snapshot | ( | ) |
Saves a snapshot of the graph to file.
initSnapshot() must be run first.
void Graph::print | ( | ) | const |
Prints the graph to stdout.
void Graph::savePNG | ( | string | title | ) | const |
Saves the graph as a PNG image.
title | - the filename of the PNG image |