lab_flow
Foreboding Flow
Graph Class Reference

Represents a graph; used by the GraphTools class. More...

#include <graph.h>

Collaboration diagram for Graph:
[legend]

Public Member Functions

 Graph (bool weighted)
 Constructor to create an empty graph. More...
 
 Graph (bool weighted, bool directed)
 Constructor to create an empty graph. More...
 
 Graph (bool weighted, int numVertices, unsigned long seed)
 Constructor to create a random, connected graph. More...
 
vector< VertexgetAdjacent (Vertex source) const
 Gets all adjacent vertices to the parameter vertex. More...
 
Vertex getStartingVertex () const
 Returns one vertex in the graph. More...
 
vector< VertexgetVertices () const
 Gets all vertices in the graph. More...
 
Edge getEdge (Vertex source, Vertex destination) const
 Gets an edge between two vertices. More...
 
vector< EdgegetEdges () const
 Gets all the edges in the graph. More...
 
bool vertexExists (Vertex v) const
 Checks if the given vertex exists. More...
 
bool edgeExists (Vertex source, Vertex destination) const
 Checks if edge exists between two vertices exists. More...
 
Edge setEdgeLabel (Vertex source, Vertex destination, string label)
 Sets the edge label of the edge between vertices u and v. More...
 
string getEdgeLabel (Vertex source, Vertex destination) const
 Gets the edge label of the edge between vertices u and v. More...
 
int getEdgeWeight (Vertex source, Vertex destination) const
 Gets the weight of the edge between two vertices. More...
 
void insertVertex (Vertex v)
 Inserts a new vertex into the graph and initializes its label as "". More...
 
Vertex removeVertex (Vertex v)
 Removes a given vertex from the graph. More...
 
bool insertEdge (Vertex source, Vertex destination)
 Inserts an edge between two vertices. More...
 
Edge removeEdge (Vertex source, Vertex destination)
 Removes the edge between two vertices. More...
 
Edge setEdgeWeight (Vertex source, Vertex destination, int weight)
 Sets the weight of the 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...
 
bool isDirected () const
 
void clear ()
 

Static Public Attributes

static const Vertex InvalidVertex = "_CS225INVALIDVERTEX"
 
static const Edge InvalidEdge = Edge(Graph::InvalidVertex, Graph::InvalidVertex, Graph::InvalidWeight, Graph::InvalidLabel)
 
static const int InvalidWeight = INT_MIN
 
static const string InvalidLabel = "_CS225INVALIDLABEL"
 

Private Member Functions

bool assertVertexExists (Vertex v, string functionName) const
 Returns whether a given vertex exists in the graph. More...
 
bool assertEdgeExists (Vertex source, Vertex destination, string functionName) const
 Returns whether thee edge exists in the graph. More...
 
void error (string message) const
 Prints a graph error and quits the program. More...
 

Private Attributes

unordered_map< Vertex, unordered_map< Vertex, Edge > > adjacency_list
 
bool weighted
 
bool directed
 
Random random
 
int picNum
 
string picName
 

Detailed Description

Represents a graph; used by the GraphTools class.

Constructor & Destructor Documentation

◆ Graph() [1/3]

Graph::Graph ( bool  weighted)

Constructor to create an empty graph.

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

◆ Graph() [2/3]

Graph::Graph ( bool  weighted,
bool  directed 
)

Constructor to create an empty graph.

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

◆ Graph() [3/3]

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

Constructor to create a random, connected graph.

Parameters
weighted- 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

◆ assertEdgeExists()

bool Graph::assertEdgeExists ( Vertex  source,
Vertex  destination,
string  functionName 
) const
private

Returns whether thee edge exists in the graph.

Parameters
source- one vertex
destination- another vertex
functionName- the name of the calling function to return in the event of an error

◆ assertVertexExists()

bool Graph::assertVertexExists ( Vertex  v,
string  functionName 
) const
private

Returns whether a given vertex exists in the graph.

Parameters
v- the vertex to check
functionName- the name of the calling function to return in the event of an error

◆ edgeExists()

bool Graph::edgeExists ( Vertex  source,
Vertex  destination 
) const

Checks if edge exists between two vertices exists.

Returns
- if Edge exists, true
  • if Edge doesn't exist, return false

◆ error()

void Graph::error ( string  message) const
private

Prints a graph error and quits the program.

The program is exited with a segfault to provide a stack trace.

Parameters
message- the error message that is printed

◆ getAdjacent()

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

Gets all adjacent vertices to the parameter vertex.

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

◆ getEdge()

Edge Graph::getEdge ( Vertex  source,
Vertex  destination 
) const

Gets an edge between two vertices.

Parameters
source- one vertex the edge is connected to
destination- the other vertex the edge is connected to
Returns
- if exist, return the corresponding edge
  • if edge doesn't exist, return Edge()

◆ getEdgeLabel()

string Graph::getEdgeLabel ( Vertex  source,
Vertex  destination 
) const

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

Parameters
source- one vertex the edge is connected to
destination- the other vertex the edge is connected to
Returns
- if edge exists, return edge label
  • if edge doesn't exist, return InvalidLabel

◆ getEdges()

vector< Edge > Graph::getEdges ( ) const

Gets all the edges in the graph.

Returns
a vector of all the edges in the graph

◆ getEdgeWeight()

int Graph::getEdgeWeight ( Vertex  source,
Vertex  destination 
) const

Gets the weight of the edge between two vertices.

Parameters
source- one vertex the edge is connected to
destination- the other vertex the edge is connected to
Returns
- if edge exists, return edge wright
  • if doesn't, return InvalidWeight

◆ 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 vertices in the graph

◆ initSnapshot()

void Graph::initSnapshot ( string  title)

Creates a name for snapshots of the graph.

Parameters
title- the name to save the snapshots as

◆ insertEdge()

bool Graph::insertEdge ( Vertex  source,
Vertex  destination 
)

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
source- one vertex the edge is connected to
destination- the other vertex the edge is connected to
Returns
whether inserting the edge was successful

◆ insertVertex()

void Graph::insertVertex ( Vertex  v)

Inserts a new vertex into the graph and initializes its label as "".

Parameters
v- the name for the vertex

◆ print()

void Graph::print ( ) const

Prints the graph to stdout.

◆ removeEdge()

Edge Graph::removeEdge ( Vertex  source,
Vertex  destination 
)

Removes the edge between two vertices.

Parameters
source- one vertex the edge is connected to
destination- the other vertex the edge is connected to
Returns
- if edge exists, remove it and return removed edge
  • if not, return InvalidEdge

◆ removeVertex()

Vertex Graph::removeVertex ( Vertex  v)

Removes a given vertex from the graph.

Parameters
v- the vertex to remove
Returns
- if v exists, return v
  • if not, return InvalidVertex;

◆ savePNG()

void Graph::savePNG ( string  title) const

Saves the graph as a PNG image.

Parameters
title- the filename of the PNG image

◆ setEdgeLabel()

Edge Graph::setEdgeLabel ( Vertex  source,
Vertex  destination,
string  label 
)

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

Parameters
source- one vertex the edge is connected to
destination- the other vertex the edge is connected to
Returns
- 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

◆ setEdgeWeight()

Edge Graph::setEdgeWeight ( Vertex  source,
Vertex  destination,
int  weight 
)

Sets the weight of the edge between two vertices.

Parameters
source- one vertex the edge is connected to
destination- the other vertex the edge is connected to
weight- the weight to set to the edge
Returns
- if edge exists, set edge weight and return edge with new weight
  • if not, return InvalidEdge

◆ snapshot()

void Graph::snapshot ( )

Saves a snapshot of the graph to file.

initSnapshot() must be run first.

◆ vertexExists()

bool Graph::vertexExists ( Vertex  v) const

Checks if the given vertex exists.

Returns
- if Vertex exists, true
  • if Vertex doesn't exist, return false

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