# Count single node isolated sub-graphs in a disconnected graph

A disconnected Graph with N vertices and K edges is given. The task is to find the count of singleton sub-graphs. A singleton graph is one with only single vertex.

**Examples:**

Input :Vertices : 6 Edges : 1 2 1 3 5 6Output :1Explanation :The Graph has 3 components : {1-2-3}, {5-6}, {4} Out of these, the only component forming singleton graph is {4}.

The idea is simple for graph given as adjacency list representation. We traverse the list and find the indices(representing a node) with no elements in list, i.e. no connected components.

Below is the representation :

## C++

`// CPP code to count the singleton sub-graphs ` `// in a disconnected graph ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to compute the count ` `int` `compute(vector<` `int` `> graph[], ` `int` `N) ` `{ ` ` ` `// Storing intermediate result ` ` ` `int` `count = 0; ` ` ` ` ` `// Traversing the Nodes ` ` ` `for` `(` `int` `i = 1; i <= N; i++) ` ` ` ` ` `// Singleton component ` ` ` `if` `(graph[i].size() == 0) ` ` ` `count++; ` ` ` ` ` `// Returning the result ` ` ` `return` `count; ` `} ` ` ` `// Driver ` `int` `main() ` `{ ` ` ` `// Number of nodes ` ` ` `int` `N = 6; ` ` ` ` ` `// Adjacency list for edges 1..6 ` ` ` `vector<` `int` `> graph[7]; ` ` ` ` ` `// Representing edges ` ` ` `graph[1].push_back(2); ` ` ` `graph[2].push_back(1); ` ` ` ` ` `graph[2].push_back(3); ` ` ` `graph[3].push_back(2); ` ` ` ` ` `graph[5].push_back(6); ` ` ` `graph[6].push_back(5); ` ` ` ` ` `cout << compute(graph, N); ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java code to count the singleton sub-graphs ` `// in a disconnected graph ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to compute the count ` `static` `int` `compute(` `int` `[]graph, ` `int` `N) ` `{ ` ` ` `// Storing intermediate result ` ` ` `int` `count = ` `0` `; ` ` ` ` ` `// Traversing the Nodes ` ` ` `for` `(` `int` `i = ` `1` `; i < ` `7` `; i++) ` ` ` `{ ` ` ` `// Singleton component ` ` ` `if` `(graph[i] == ` `0` `) ` ` ` `count++; ` ` ` `} ` ` ` ` ` `// Returning the result ` ` ` `return` `count; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `// Number of nodes ` ` ` `int` `N = ` `6` `; ` ` ` ` ` `// Adjacency list for edges 1..6 ` ` ` `int` `[]graph = ` `new` `int` `[` `7` `]; ` ` ` `// Representing edges ` ` ` `graph[` `1` `] = ` `2` `; ` ` ` `graph[` `2` `] = ` `1` `; ` ` ` `graph[` `2` `] = ` `3` `; ` ` ` `graph[` `3` `] = ` `2` `; ` ` ` `graph[` `5` `] = ` `6` `; ` ` ` `graph[` `6` `] = ` `5` `; ` ` ` ` ` `System.out.println(compute(graph, N)); ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

## Python3

`# Python code to count the singleton sub-graphs ` `# in a disconnected graph ` ` ` `# Function to compute the count ` `def` `compute(graph, N): ` ` ` `# Storing intermediate result ` ` ` `count ` `=` `0` ` ` ` ` `# Traversing the Nodes ` ` ` `for` `i ` `in` `range` `(` `1` `, N` `+` `1` `): ` ` ` ` ` `# Singleton component ` ` ` `if` `(` `len` `(graph[i]) ` `=` `=` `0` `): ` ` ` `count ` `+` `=` `1` ` ` ` ` `# Returning the result ` ` ` `return` `count ` ` ` `# Driver ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `# Number of nodes ` ` ` `N ` `=` `6` ` ` ` ` `# Adjacency list for edges 1..6 ` ` ` `graph ` `=` `[[] ` `for` `i ` `in` `range` `(` `7` `)] ` ` ` ` ` `# Representing edges ` ` ` `graph[` `1` `].append(` `2` `) ` ` ` `graph[` `2` `].append(` `1` `) ` ` ` ` ` `graph[` `2` `].append(` `3` `) ` ` ` `graph[` `3` `].append(` `2` `) ` ` ` ` ` `graph[` `5` `].append(` `6` `) ` ` ` `graph[` `6` `].append(` `5` `) ` ` ` ` ` `print` `(compute(graph, N)) ` |

*chevron_right*

*filter_none*

## C#

`// C# code to count the singleton sub-graphs ` `// in a disconnected graph ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to compute the count ` `static` `int` `compute(` `int` `[]graph, ` `int` `N) ` `{ ` ` ` `// Storing intermediate result ` ` ` `int` `count = 0; ` ` ` ` ` `// Traversing the Nodes ` ` ` `for` `(` `int` `i = 1; i < 7; i++) ` ` ` `{ ` ` ` `// Singleton component ` ` ` `if` `(graph[i] == 0) ` ` ` `count++; ` ` ` `} ` ` ` ` ` `// Returning the result ` ` ` `return` `count; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `// Number of nodes ` ` ` `int` `N = 6; ` ` ` ` ` `// Adjacency list for edges 1..6 ` ` ` `int` `[]graph = ` `new` `int` `[7]; ` ` ` ` ` `// Representing edges ` ` ` `graph[1] = 2; ` ` ` `graph[2] = 1; ` ` ` `graph[2] = 3; ` ` ` `graph[3] = 2; ` ` ` `graph[5] = 6; ` ` ` `graph[6] = 5; ` ` ` ` ` `Console.WriteLine(compute(graph, N)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

1

This article is contributed by **Rohit Thapliyal**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- BFS for Disconnected Graph
- Maximum and minimum isolated vertices in a graph
- Minimum edges to be added in a directed graph so that any node can be reachable from a given node
- Kth largest node among all directly connected nodes to the given node in an undirected graph
- Number of single cycle components in an undirected graph
- Minimum value of distance of farthest node in a Graph
- k'th heaviest adjacent node in a graph where each vertex has weight
- Maximum number of nodes which can be reached from each node in a graph.
- Minimum labelled node to be removed from undirected Graph such that there is no cycle
- Count of different groups using Graph
- Count of all cycles without any inner cycle in a given Graph
- Count number of edges in an undirected graph
- Count the number of Prime Cliques in an undirected graph
- Program to count Number of connected components in an undirected graph
- Count ways to change direction of edges such that graph becomes acyclic
- Count of unique lengths of connected components for an undirected graph using STL
- Minimum nodes to be colored in a Graph such that every node has a colored neighbour
- Count total ways to reach destination from source in an undirected Graph
- Convert the undirected graph into directed graph such that there is no path of length greater than 1
- Maximum number of edges that N-vertex graph can have such that graph is Triangle free | Mantel's Theorem