lab_graphs
Gangnam-Style Graphs
Graph Class Reference

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

#include "graph.h"

+ Collaboration diagram for Graph:

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...
 
Vertex setVertexLabel (Vertex v, string label)
 Set label to Vertex v. More...
 
string getVertexLabel (Vertex v) const
 Set label to Vertex v. 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...
 

Detailed Description

Represents a graph; used by the GraphTools class.

Constructor & Destructor Documentation

Graph::Graph ( bool  weighted)

Constructor to create an empty graph.

Parameters
weighted- specifies whether the graph is a weighted graph or not
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::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

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
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
vector< Vertex > Graph::getVertices ( ) const

Gets all vertices in the graph.

Returns
a vector of all vertices in the graph
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()
vector< Edge > Graph::getEdges ( ) const

Gets all the edges in the graph.

Returns
a vector of all the edges in the graph
Vertex Graph::setVertexLabel ( Vertex  v,
string  label 
)

Set label to Vertex v.

Returns
- if Vertex exists, set new label to the Vertex and return v
  • if Vertex doesn't exist, return InvalidVertex
string Graph::getVertexLabel ( Vertex  v) const

Set label to Vertex v.

Returns
- if Vertex exists, return the label of v
  • if Vertex doesn't exist, return InvalidLabel
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
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
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
void Graph::insertVertex ( Vertex  v)

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

Parameters
v- the name for the vertex
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;
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
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
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
void Graph::initSnapshot ( string  title)

Creates a name for snapshots of the graph.

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

Parameters
title- the filename of the PNG image

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