Given an **undirected acyclic graph** having **N** nodes and **M** edges, the task is to find the size of the largest tree in the forest formed by the graph.

A forest is a collection of disjoint trees. In other words, we can also say that forest is a collection of an acyclic graph which is not connected.

**Examples:**

Input:N = 5, edges[][] = {{0, 1}, {0, 2}, {3, 4}}

Output:3

Explanation:

There are 2 trees, each having size 3 and 2 respectively.0 / \ 1 2and

3 \ 4Hence the size of the largest tree is 3.

Input:N = 5, edges[][] = {{0, 1}, {0, 2}, {3, 4}, {0, 4}, {3, 5}}

Output:6

**Approach:** The idea is to first count the number of reachable nodes from every forest. Therefore:

- Apply DFS on every node and obtain the size of the tree formed by this node and check if every connected node is visited from one source.
- If the size of the current tree is greater than the answer then update the answer to current tree’s size.
- Again perform DFS traversal if some set of nodes are not yet visited.
- Finally, the max of all the answers when all the nodes are visited is the final answer.

Below is the implementation of the above approach:

`// C++ program to find the size ` `// of the largest tree in the 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 perform DFS of a ` `// graph recursively from a given vertex u ` `// and returns the size of the tree formed by u ` `int` `DFSUtil(` `int` `u, vector<` `int` `> adj[], ` ` ` `vector<` `bool` `>& visited) ` `{ ` ` ` `visited[u] = ` `true` `; ` ` ` `int` `sz = 1; ` ` ` ` ` `// Iterating through all the nodes ` ` ` `for` `(` `int` `i = 0; i < adj[u].size(); i++) ` ` ` `if` `(visited[adj[u][i]] == ` `false` `) ` ` ` ` ` `// Perform DFS if the node is ` ` ` `// not yet visited ` ` ` `sz += DFSUtil( ` ` ` `adj[u][i], adj, visited); ` ` ` `return` `sz; ` `} ` ` ` `// Function to return the size of the ` `// largest tree in the forest given as ` `// the adjacency list ` `int` `largestTree(vector<` `int` `> adj[], ` `int` `V) ` `{ ` ` ` `vector<` `bool` `> visited(V, ` `false` `); ` ` ` `int` `answer = 0; ` ` ` ` ` `// Iterating through all the vertices ` ` ` `for` `(` `int` `u = 0; u < V; u++) { ` ` ` `if` `(visited[u] == ` `false` `) { ` ` ` ` ` `// Find the answer ` ` ` `answer ` ` ` `= max(answer, ` ` ` `DFSUtil(u, adj, visited)); ` ` ` `} ` ` ` `} ` ` ` `return` `answer; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `V = 5; ` ` ` `vector<` `int` `> adj[V]; ` ` ` `addEdge(adj, 0, 1); ` ` ` `addEdge(adj, 0, 2); ` ` ` `addEdge(adj, 3, 4); ` ` ` `cout << largestTree(adj, V); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

3

**Time Complexity:** *O(V + E)*, where V is the number of vertices and E is the number of edges.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Largest component size in a graph formed by connecting non-co-prime nodes
- Count number of trees in a forest
- Number of groups formed in a graph of friends
- Total number of Spanning Trees in a Graph
- Number of spanning trees of a weighted complete Graph
- Total number of Spanning trees in a Cycle Graph
- Find if an undirected graph contains an independent set of a given size
- Find all cliques of size K in an undirected graph
- Largest subarray sum of all connected components in undirected graph
- Largest subset of Graph vertices with edges of 2 or more colors
- Find the maximum component size after addition of each edge to the graph
- Finding minimum vertex cover size of a graph using binary search
- Kth largest node among all directly connected nodes to the given node in an undirected graph
- Convert a tree to forest of even nodes
- Maximum edge removal from tree to make even forest
- 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
- Graph implementation using STL for competitive programming | Set 2 (Weighted graph)
- Detect cycle in the graph using degrees of nodes of graph
- Convert undirected connected graph to strongly connected directed graph

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.