Algorithms_in_C++  1.0.0
Set of algorithms implemented in C++.
graph Class Reference
Collaboration diagram for graph:
[legend]

Public Member Functions

 graph (int v)
 
void addedge (int src, int dest)
 
void printgraph ()
 
void bfs (int s)
 
 graph (int n)
 Constructor that intiliazes the graph on creation and set the connected components to 0.
 
void addEdge (int, int)
 Function that add edge between two nodes or vertices of graph. More...
 
int getConnectedComponents ()
 Function the calculates the connected compoents in the graph by performing the depth first search on graph. More...
 

Private Member Functions

void depth_first_search ()
 Function that perfoms depth first search algorithm on graph.
 
void explore (int, vector< bool > &)
 Utility function for depth first seach algorithm this function explores the vertex which is passed into. More...
 

Private Attributes

int v
 
list< int > * adj
 
vector< vector< int > > adj
 adj stores adjacency list representation of graph
 
int connected_components
 keep track of connected components
 

Detailed Description

Class for representing graph as a adjacency list.

Member Function Documentation

◆ addEdge()

void graph::addEdge ( int  u,
int  v 
)

Function that add edge between two nodes or vertices of graph.

Parameters
uany node or vertex of graph
vany node or vertex of graph
75  {
76  adj[u - 1].push_back(v - 1);
77  adj[v - 1].push_back(u - 1);
78 }

◆ explore()

void graph::explore ( int  u,
vector< bool > &  visited 
)
private

Utility function for depth first seach algorithm this function explores the vertex which is passed into.

Parameters
uvertex or node to be explored
visitedalready visited vertex
101  {
102  visited[u] = true;
103  for (auto v : adj[u]) {
104  if (!visited[v]) {
105  explore(v, visited);
106  }
107  }
108 }

◆ getConnectedComponents()

int graph::getConnectedComponents ( )
inline

Function the calculates the connected compoents in the graph by performing the depth first search on graph.

Returns
connected_components total connected components in graph
63  {
65  return connected_components;
66  }
Here is the call graph for this function:

The documentation for this class was generated from the following files:
graph::explore
void explore(int, vector< bool > &)
Utility function for depth first seach algorithm this function explores the vertex which is passed in...
Definition: connected_components.cpp:101
graph::depth_first_search
void depth_first_search()
Function that perfoms depth first search algorithm on graph.
Definition: connected_components.cpp:83
graph::connected_components
int connected_components
keep track of connected components
Definition: connected_components.cpp:43