Given n nodes of a forest (collection of trees), find the number of trees in the forest.

**Examples :**

Input : edges[] = {0, 1}, {0, 2}, {3, 4} Output : 2 Explanation : There are 2 trees 0 3 / \ \ 1 2 4

**Approach :**

1. Apply DFS on every node.

2. Increment count by one if every connected node is visited from one source.

3. Again perform DFS traversal if some nodes yet not visited.

4. Count will give the number of trees in forest.

## C++

`// CPP program to count number of trees in ` `// a forest. ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// A utility function to add an edge in an ` `// undirected graph. ` `void` `addEdge(vector<` `int` `> adj[], ` `int` `u, ` `int` `v) ` `{ ` ` ` `adj[u].push_back(v); ` ` ` `adj[v].push_back(u); ` `} ` ` ` `// A utility function to do DFS of graph ` `// recursively from a given vertex u. ` `void` `DFSUtil(` `int` `u, vector<` `int` `> adj[], ` ` ` `vector<` `bool` `> &visited) ` `{ ` ` ` `visited[u] = ` `true` `; ` ` ` `for` `(` `int` `i=0; i<adj[u].size(); i++) ` ` ` `if` `(visited[adj[u][i]] == ` `false` `) ` ` ` `DFSUtil(adj[u][i], adj, visited); ` `} ` ` ` `// Returns count of tree is the forest ` `// given as adjacency list. ` `int` `countTrees(vector<` `int` `> adj[], ` `int` `V) ` `{ ` ` ` `vector<` `bool` `> visited(V, ` `false` `); ` ` ` `int` `res = 0; ` ` ` `for` `(` `int` `u=0; u<V; u++) ` ` ` `{ ` ` ` `if` `(visited[u] == ` `false` `) ` ` ` `{ ` ` ` `DFSUtil(u, adj, visited); ` ` ` `res++; ` ` ` `} ` ` ` `} ` ` ` `return` `res; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `V = 5; ` ` ` `vector<` `int` `> adj[V]; ` ` ` `addEdge(adj, 0, 1); ` ` ` `addEdge(adj, 0, 2); ` ` ` `addEdge(adj, 3, 4); ` ` ` `cout << countTrees(adj, V); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

2

Time Complexity : O(V + E)

## Recommended Posts:

- Convert a tree to forest of even nodes
- Maximum edge removal from tree to make even forest
- Total number of Spanning Trees in a Graph
- Total number of Spanning trees in a Cycle Graph
- Count the number of non-reachable nodes
- Count the number of nodes at given level in a tree using BFS.
- Count number of edges in an undirected graph
- Disjoint Set Union on trees | Set 1
- Count all possible paths between two vertices
- Count all 0s which are blocked by 1s in binary matrix
- Minimum number of operation required to convert number x into y
- Count all possible walks from a source to a destination with exactly k edges
- Count single node isolated sub-graphs in a disconnected graph
- Print levels with odd number of nodes and even number of nodes
- Count nodes within K-distance from all nodes in a set

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.