# Algorithms_in_C++: graph Class Reference

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

  u | any node or vertex of graph
  v | any node or vertex of graph

{
adj[u - 1].push_back(v - 1);
adj[v - 1].push_back(u - 1);
}

## 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

  u | vertex or node to be explored
  visited | already visited vertex

{
visited[u] = true;
for (auto v : adj[u]) {
if (!visited[v]) {
explore(v, visited);
}
}
}

## 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

{
depth_first_search();
return connected_components;
}
Here is the call graph for this function:

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

- graph/bfs.cpp
- graph/connected_components.cpp

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