# Using depth first search to test graph connectivity in C++

Problem description

Given a directed or undirected graph, determine whether it is connected or not. Specifically is it possible for any pair of nodes to communicate with each other?

This brief post reproduces this web page whereby the problem was to determine whether a graph is strongly connected or not. I use a slight modification of `Graph` so that the user can define whether the graph consists of directed or undirected edges. When setting the directed parameter to false, the `Graph` class assumes that the edges are undirected, and so adds an additional link in the opposite direction to maintain bi-connectivity between edges (links).

The algorithm uses a depth-first search algorithm to test whether all the graph nodes get visited during the recursive search. For the purpose of brevity I have also removed a number of code comments, given that the code was fairly self-documenting to start with:

Example Graphs

1. A disconnected un-directed graph, whereby nodes `[3,4]` are disconnected from nodes `[0,1,2]`:

2. A disconnected directed graph. For example, node `[1]` can communicate with nodes `[0,2,3]` but not node `[4]`:

3. A connected un-directed graph. All nodes can communicate with any other node:

4. A disconnected directed graph. For example, node `[0]` can communicate with nodes `[1,2,3]` but node `[3]` cannot communicate with any other node:

5. A connected un-directed graph. All nodes can communicate with any other node:

6. A connected directed graph. All nodes can communicate with any other node. For example, although there is no direct link between nodes `[0,3]`, a direct path between the two nodes still exists, via nodes `[0,1,2,3]`.

Code listing

The following code listing shows how to implement each of the six examples listed above:

```// Program to check if a given directed graph is strongly connected or not
#include <iostream>
#include <list>
#include <stack>

// Storage for visited nodes
bool visited[ 100000 ];

class Graph
{
int V;    // No. of vertices
bool directed;

// A recursive function to print DFS starting from v
void DFSUtil(int v, bool visited[]);
public:
// Constructor and Destructor
Graph(int V, bool directed)
{
this->V = V;
this->directed = directed;
}
~Graph() { delete [] adj; }

// Method to add an edge

// The main function that returns true if the graph is strongly
// connected, otherwise false
bool isSC();

// Function that returns reverse (or transpose) of this graph
Graph* getTranspose();
};

// A recursive function to print DFS starting from v
void Graph::DFSUtil(int v, bool visited[])
{
// Mark the current node as visited and print it
visited[v] = true;

// Recur for all the vertices adjacent to this vertex

std::list<int>::iterator i;
if (!visited[*i])
DFSUtil(*i, visited);
}

// Function that returns reverse (or transpose) of this graph
Graph* Graph::getTranspose()
{
Graph* g = new Graph(V, directed);
for (int v = 0; v < V; v++)
{
// Recur for all the vertices adjacent to this vertex
std::list<int>::iterator i;
{
}
}
return g;
}

{

}

// The main function that returns true if graph is strongly connected
bool Graph::isSC()
{
// Step 1: Mark all the vertices as not visited (For first DFS)

for (int i = 0; i < V; i++)
visited[i] = false;

// Step 2: Do DFS traversal starting from first vertex.
DFSUtil(0, visited);

// If DFS traversal doesn’t visit all vertices, then return false.
for (int i = 0; i < V; i++)
if (visited[i] == false)
return false;

// Step 3: Create a reversed graph
Graph* gr = getTranspose();

// Step 4: Mark all the vertices as not visited (For second DFS)
for(int i = 0; i < V; i++)
visited[i] = false;

// Step 5: Do DFS for reversed graph starting from first vertex.
// Starting Vertex must be same starting point of first DFS
gr->DFSUtil(0, visited);

// If all vertices are not visited in second DFS, then
// return false
for (int i = 0; i < V; i++)
if (visited[i] == false)
return false;

return true;
}

// Driver program to test above functions
int main()
{
// Example 1: disconnected un-directed graph:
//
//     0      3
//    / \    /
//   1   2  4
// nodes [3,4] disconnected from nodes [0,1,2]
Graph g1( 5, false );
g1.isSC() ? std::cout << "Yes\n" :
std::cout << "No\n";

// Example 2: disconnected (directed) graph:
//
// 3 <- 2 <- 1 -> 0 <- 4
// eg 1 can communicate with [2,3,0] but not 4
Graph g2( 5, true );
g2.isSC() ? std::cout << "Yes\n" :
std::cout << "No\n";

// Example 3: connected (un-directed) graph:
//
// 3 <-> 2 <-> 1 <-> 0 <-> 4
// All nodes can communicate with any other node
Graph g3( 5, false );
g3.isSC() ? std::cout << "Yes\n" :
std::cout << "No\n";

// Example 4: disconnected (directed) graph
//
// 0 -> 1 -> 2 -> 3
// 0 can communicate with [1,2,3] but 3 cannot communicate
// with any other node
Graph g4( 4, true );
g4.isSC() ? std::cout << "Yes\n" :
std::cout << "No\n";

// Example 5: connected (un-directed) graph
//
// 0 <-> 1 <-> 2 <-> 3
// All nodes can communicate with any other node
Graph g5( 4, false );
g5.isSC() ? std::cout << "Yes\n" :
std::cout << "No\n";

// Example 6: connected directed graph
//
//      0 -> 1 -> 2 -> 3 -> 0
//
//           4 -> 2 -> 4
// All nodes can communicate with any other node
Graph g6( 5, true );
``` No No Yes No Yes Yes```