Size of the Largest Trees in a Forest formed by the given Graph

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     2

and

3
 \
  4

Hence 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Output:

3

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

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.