lab_graphs
Gangnam-Style Graphs
Graph Class Reference

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< EdgegetEdges () 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...
 

Detailed Description

Represents a graph of vertices and edges and allows basic operations to be performed on it.

Constructor & Destructor Documentation

◆ Graph() [1/2]

Graph::Graph ( bool  isWeighted)

Constructor to create an empty graph.

Parameters
isWeighted- specifies whether the graph is a weighted graph or not

◆ Graph() [2/2]

Graph::Graph ( bool  isWeighted,
int  numVertices,
unsigned long  seed 
)

Constructor to create a random, connected graph.

Parameters
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

Member Function Documentation

◆ getAdjacent()

vector< Vertex > Graph::getAdjacent ( Vertex  v) const

Gets all adjacent vertices to the parameter vertex.

Parameters
v- the vertex to get neighbors from
Returns
a vector of vertices

◆ getStartingVertex()

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.

Returns
a vertex from the graph

◆ getVertices()

vector< Vertex > Graph::getVertices ( ) const

Gets all vertices in the graph.

Returns
a vector of all vertex labels in the graph

◆ getEdge()

Edge Graph::getEdge ( Vertex  u,
Vertex  v 
) const

Gets an edge between two vertices.

Parameters
u- one vertex the edge is connected to
v- the other vertex the edge is connected to
Returns
the edge between u and v

◆ getEdges()

vector< Edge > Graph::getEdges ( ) const

Gets all the edges in the graph.

Returns
a vector of all the edges in the graph

◆ setVertexName()

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.

Parameters
v- the vertex to label
label- label of the vertex to change

◆ getVertexName()

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.

Parameters
v- vertex to get the name from
Returns
the vertex name

◆ setVertexLabel()

void Graph::setVertexLabel ( Vertex  v,
string  label 
)

Labels a vertex with a string.

This is a method.

Parameters
v- the vertex to label
label- label of the vertex to change

◆ getVertexLabel()

string Graph::getVertexLabel ( Vertex  v) const

Gets the label of a vertex.

Parameters
v- vertex to get label from
Returns
the vertex label

◆ setEdgeLabel()

void Graph::setEdgeLabel ( Vertex  u,
Vertex  v,
string  label 
)

Sets the edge label of an edge between vertices u and v.

Parameters
u- one vertex the edge is connected to
v- the other vertex the edge is connected to

◆ getEdgeLabel()

string Graph::getEdgeLabel ( Vertex  u,
Vertex  v 
) const

Gets the edge label of an edge between vertices u and v.

Parameters
u- one vertex the edge is connected to
v- the other vertex the edge is connected to
Returns
the edge label between u and v

◆ getEdgeWeight()

int Graph::getEdgeWeight ( Vertex  u,
Vertex  v 
) const

Gets the weight of an edge between two vertices.

Parameters
u- one vertex the edge is connected to
v- the other vertex the edge is connected to
Returns
the weight of the edge

◆ insertVertex()

Vertex Graph::insertVertex ( string  label = "")

Inserts a new vertex into the graph and labels it.

Parameters
labelthe label for the vertex
Returns
a copy of the vertex that was inserted

◆ removeVertex()

void Graph::removeVertex ( Vertex  v)

Removes a given vertex from the graph.

Parameters
v- the vertex to remove

◆ insertEdge()

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.

Parameters
u- one vertex the edge is connected to
v- the other vertex the edge is connected to
Returns
whether inserting the edge was successful

◆ removeEdge()

void Graph::removeEdge ( Vertex  u,
Vertex  v 
)

Removes an edge between two vertices.

Parameters
u- one vertex the edge is connected to
v- the other vertex the edge is connected to

◆ setEdgeWeight()

void Graph::setEdgeWeight ( Vertex  u,
Vertex  v,
int  weight 
)

Sets the weight of an edge between two vertices.

Parameters
u- one vertex the edge is connected to
v- the other vertex the edge is connected to
weight- the weight to set the edge

◆ initSnapshot()

void Graph::initSnapshot ( string  title)

Creates a name for snapshots of the graph.

Parameters
title- the name to save the snapshots as

◆ snapshot()

void Graph::snapshot ( )

Saves a snapshot of the graph to file.

initSnapshot() must be run first.

◆ print()

void Graph::print ( ) const

Prints the graph to stdout.

◆ savePNG()

void Graph::savePNG ( string  title) const

Saves the graph as a PNG image.

Parameters
title- the filename of the PNG image

The documentation for this class was generated from the following files: