Given an undirected graph and a set of vertices, we have to count the number of non reachable nodes from the given head node using depth first search.

Consider below undirected graph with two disconnected components:

In this graph if we consider 0 as head node, then the node 0, 1 and 2 are reachable. We mark all the reachable nodes as visited. All those nodes which are not mark as visited i.e, node 3 and 4 are non reachable nodes. Hence their count is 2.

Examples:

Input : 5 0 1 0 2 1 2 3 4 Output : 2

We can either use BFS or DFS for this purpose. In below implementation DFS is used. We do DFS from given source. Since the given graph is undirected, all the vertices that belong to the disconnected component are non-reachable nodes. We use the visit array for this purpose, the array which is used to keep track of non-visited vertices in DFS. In DFS, if we start from head node it will mark all the nodes connected to the head node as visited. Then after traversing the graph we count the number of nodes that are not mark as visited from the head node.

`// C++ program to count non-reachable nodes ` `// from a given source using DFS. ` `#include <iostream> ` `#include <list> ` `using` `namespace` `std; ` ` ` `// Graph class represents a directed graph ` `// using adjacency list representation ` `class` `Graph { ` ` ` `int` `V; ` `// No. of vertices ` ` ` ` ` `// Pointer to an array containing ` ` ` `// adjacency lists ` ` ` `list<` `int` `>* adj; ` ` ` ` ` `// A recursive function used by DFS ` ` ` `void` `DFSUtil(` `int` `v, ` `bool` `visited[]); ` ` ` `public` `: ` ` ` `Graph(` `int` `V); ` `// Constructor ` ` ` ` ` `// function to add an edge to graph ` ` ` `void` `addEdge(` `int` `v, ` `int` `w); ` ` ` ` ` `// DFS traversal of the vertices ` ` ` `// reachable from v ` ` ` `int` `countNotReach(` `int` `v); ` `}; ` ` ` `Graph::Graph(` `int` `V) ` `{ ` ` ` `this` `->V = V; ` ` ` `adj = ` `new` `list<` `int` `>[V]; ` `} ` ` ` `void` `Graph::addEdge(` `int` `v, ` `int` `w) ` `{ ` ` ` `adj[v].push_back(w); ` `// Add w to v’s list. ` ` ` `adj[w].push_back(v); ` `// Add v to w's list. ` `} ` ` ` `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 ` ` ` `list<` `int` `>::iterator i; ` ` ` `for` `(i = adj[v].begin(); i != adj[v].end(); ++i) ` ` ` `if` `(!visited[*i]) ` ` ` `DFSUtil(*i, visited); ` `} ` ` ` `// Returns count of not reachable nodes from ` `// vertex v. ` `// It uses recursive DFSUtil() ` `int` `Graph::countNotReach(` `int` `v) ` `{ ` ` ` `// Mark all the vertices as not visited ` ` ` `bool` `* visited = ` `new` `bool` `[V]; ` ` ` `for` `(` `int` `i = 0; i < V; i++) ` ` ` `visited[i] = ` `false` `; ` ` ` ` ` `// Call the recursive helper function ` ` ` `// to print DFS traversal ` ` ` `DFSUtil(v, visited); ` ` ` ` ` `// Return count of not visited nodes ` ` ` `int` `count = 0; ` ` ` `for` `(` `int` `i = 0; i < V; i++) { ` ` ` `if` `(visited[i] == ` `false` `) ` ` ` `count++; ` ` ` `} ` ` ` `return` `count; ` `} ` ` ` `int` `main() ` `{ ` ` ` `// Create a graph given in the above diagram ` ` ` `Graph g(8); ` ` ` `g.addEdge(0, 1); ` ` ` `g.addEdge(0, 2); ` ` ` `g.addEdge(1, 2); ` ` ` `g.addEdge(3, 4); ` ` ` `g.addEdge(4, 5); ` ` ` `g.addEdge(6, 7); ` ` ` ` ` `cout << g.countNotReach(2); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

5

## Recommended Posts:

- Count the number of nodes at given level in a tree using BFS.
- Count nodes within K-distance from all nodes in a set
- Print levels with odd number of nodes and even number of nodes
- Number of Unicolored Paths between two nodes
- Calculate number of nodes in all subtrees | Using DFS
- Number of sink nodes in a graph
- Level with maximum number of nodes using DFS in a N-ary tree
- Minimize the number of weakly connected nodes
- Maximize number of nodes which are not part of any edge in a Graph
- Calculate number of nodes between two vertices in an acyclic Graph by Disjoint Union method
- Sum of all odd nodes in the path connecting two given nodes
- Count number of trees in a forest
- Count number of edges in an undirected graph
- Subtree of all nodes in a tree using DFS
- Convert a tree to forest of even nodes

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.