Open In App

Count number of trees in a forest

Last Updated : 14 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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. 

Implementation:

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;
}


Java




// Java program to count number of trees in a forest.
import java.io.*;
import java.util.*;
 
// This class represents a directed graph using adjacency list
// representation
class Graph
{
    private int V; // No. of vertices
 
    // Array of lists for Adjacency List Representation
    private LinkedList<Integer> adj[];
 
    // Constructor
    Graph(int v)
    {
        V = v;
        adj = new LinkedList[v];
        for (int i = 0; i <  v; ++i)
            adj[i] = new LinkedList();
    }
 
    //Function to add an edge into the graph
    void addEdge(int v, int w)
    {
        adj[v].add(w); // Add w to v's list.
    }
 
    // A function used by DFS
    void DFSUtil(int v,boolean visited[])
    {
        // Mark the current node as visited and print it
        visited[v] = true;
         
        // Recur for all the vertices adjacent to this vertex
        Iterator<Integer> i = adj[v].listIterator();
        while (i.hasNext())
        {
            int n = i.next();
            if (!visited[n])
            {
                DFSUtil(n,visited);
            }
        }
    }
 
    // The function to do DFS traversal. It uses recursive DFSUtil()
    int countTrees()
    {
        // Mark all the vertices as not visited(set as
        // false by default in java)
        boolean visited[] = new boolean[V];
        int res = 0;
         
        // Call the recursive helper function to print DFS traversal
        // starting from all vertices one by one
        for (int i = 0; i < V; ++i)
        {
            if (visited[i] == false)
            {
                DFSUtil(i, visited);
                res ++;
            }
        }
        return res;
    }
 
    // Driver code
    public static void main(String args[])
    {
        Graph g = new Graph(5);
 
        g.addEdge(0, 1);
        g.addEdge(0, 2);
        g.addEdge(3, 4);
 
        System.out.println(g.countTrees());
    }
}
 
// This code is contributed by mayankbansal2


Python3




# Python3 program to count number 
# of trees in a forest.
 
# A utility function to add an
# edge in an undirected graph.
def addEdge(adj, u, v):
    adj[u].append(v)
    adj[v].append(u)
 
# A utility function to do DFS of graph
# recursively from a given vertex u.
def DFSUtil(u, adj, visited):
    visited[u] = True
    for i in range(len(adj[u])):
        if (visited[adj[u][i]] == False):
            DFSUtil(adj[u][i], adj, visited)
 
# Returns count of tree is the
# forest given as adjacency list.
def countTrees(adj, V):
    visited = [False] * V
    res = 0
    for u in range(V):
        if (visited[u] == False):
            DFSUtil(u, adj, visited)
            res += 1
    return res
 
# Driver code
if __name__ == '__main__':
 
    V = 5
    adj = [[] for i in range(V)]
    addEdge(adj, 0, 1)
    addEdge(adj, 0, 2)
    addEdge(adj, 3, 4)
    print(countTrees(adj, V))
 
# This code is contributed by PranchalK


C#




// C# program to count number of trees in a forest.
using System;
using System.Collections.Generic;
 
// This class represents a directed graph
// using adjacency list representation
class Graph
{
    private int V; // No. of vertices
 
    // Array of lists for
    // Adjacency List Representation
    private List<int> []adj;
 
    // Constructor
    Graph(int v)
    {
        V = v;
        adj = new List<int>[v];
        for (int i = 0; i < v; ++i)
            adj[i] = new List<int>();
    }
 
    // Function to add an edge into the graph
    void addEdge(int v, int w)
    {
        adj[v].Add(w); // Add w to v's list.
    }
 
    // A function used by DFS
    void 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
        foreach(int i in adj[v])
        {
            int n = i;
            if (!visited[n])
            {
                DFSUtil(n, visited);
            }
        }
    }
 
    // The function to do DFS traversal.
    // It uses recursive DFSUtil()
    int countTrees()
    {
        // Mark all the vertices as not visited
        // (set as false by default in java)
        bool []visited = new bool[V];
        int res = 0;
         
        // Call the recursive helper function
        // to print DFS traversal starting from
        // all vertices one by one
        for (int i = 0; i < V; ++i)
        {
            if (visited[i] == false)
            {
                DFSUtil(i, visited);
                res ++;
            }
        }
        return res;
    }
 
    // Driver code
    public static void Main(String []args)
    {
        Graph g = new Graph(5);
 
        g.addEdge(0, 1);
        g.addEdge(0, 2);
        g.addEdge(3, 4);
 
        Console.WriteLine(g.countTrees());
    }
}
 
// This code is contributed by PrinciRaj1992


Javascript




<script>
// Javascript program to count number of trees in a forest.
 
// This class represents a directed graph
// using adjacency list representation
var V; // No. of vertices
 
// Array of lists for
// Adjacency List Representation
var adj;
// Constructor
function Graph( v)
{
    V = v;
    adj = Array.from(Array(v), ()=>Array());
}
// Function to add an edge into the graph
function addEdge(v, w)
{
    adj[v].push(w); // Add w to v's list.
}
// A function used by DFS
function DFSUtil(v, visited)
{
    // Mark the current node as
    // visited and print it
    visited[v] = true;
     
    // Recur for all the vertices
    // adjacent to this vertex
    for(var i of adj[v])
    {
        var n = i;
        if (!visited[n])
        {
            DFSUtil(n, visited);
        }
    }
}
// The function to do DFS traversal.
// It uses recursive DFSUtil()
function countTrees()
{
    // Mark all the vertices as not visited
    // (set as false by default in java)
    var visited = Array(V).fill(false);
    var res = 0;
     
    // Call the recursive helper function
    // to print DFS traversal starting from
    // all vertices one by one
    for(var i = 0; i < V; ++i)
    {
        if (visited[i] == false)
        {
            DFSUtil(i, visited);
            res ++;
        }
    }
    return res;
}
 
// Driver code
Graph(5);
addEdge(0, 1);
addEdge(0, 2);
addEdge(3, 4);
document.write(countTrees());
 
// This code is contributed by rutvik_56.
</script>


Output

2

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

Space Complexity: O(V). We use an array of size V to store the visited nodes.

Approach:- Here’s an implementation of counting the number of trees in a forest using BFS in C++

  •    Define a bfs function that takes the forest, a start node, and a visited array as inputs. The function performs BFS starting from the start node and marks all visited nodes in the visited array.
  •    Inside the bfs function, create a queue q to store the nodes that are to be visited in the BFS. Initially, push the start node onto the queue and mark it as visited in the visited array.

C++




#include <iostream>
#include <queue>
#include <vector>
using namespace std;
 
// define a pair to represent a node in the forest
typedef pair<int, int> Node;
 
// function to perform BFS from a given node and mark all visited nodes
void bfs(vector<vector<int>>& forest, Node start, vector<vector<bool>>& visited) {
    // create a queue for BFS
    queue<Node> q;
    q.push(start);
    visited[start.first][start.second] = true;
 
    // BFS loop
    while (!q.empty()) {
        Node curr = q.front();
        q.pop();
 
        // add unvisited neighboring nodes to the queue
        int dx[] = {-1, 0, 1, 0};
        int dy[] = {0, 1, 0, -1};
        for (int i = 0; i < 4; i++) {
            int nx = curr.first + dx[i];
            int ny = curr.second + dy[i];
            if (nx >= 0 && nx < forest.size() && ny >= 0 && ny < forest[0].size() && forest[nx][ny] == 1 && !visited[nx][ny]) {
                q.push(make_pair(nx, ny));
                visited[nx][ny] = true;
            }
        }
    }
}
 
// function to count the number of trees in a forest using BFS
int count_trees_in_forest(vector<vector<int>>& forest) {
    int count = 0;
    int n = forest.size();
    int m = forest[0].size();
 
    // create a 2D boolean array to keep track of visited nodes
    vector<vector<bool>> visited(n, vector<bool>(m, false));
 
    // iterate over all nodes in the forest and perform BFS from each unvisited tree
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (forest[i][j] == 1 && !visited[i][j]) {
                bfs(forest, make_pair(i, j), visited);
                count++;
            }
        }
    }
 
    return count;
}
 
int main() {
    // example usage
    vector<vector<int>> forest = {
        {0, 1, 1, 0, 0},
        {0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0},
        {0, 0, 0, 0, 1},
        {0, 0, 0, 0, 0}
    };
    int num_trees = count_trees_in_forest(forest);
    cout << "The forest has " << num_trees << " trees." << endl;
    return 0;
}


Java




import java.util.*;
 
public class Forest {
    // define a pair to represent a node in the forest
    static class Node {
        int x;
        int y;
        Node(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }
 
    // function to perform BFS from a given node and mark all visited nodes
    static void bfs(int[][] forest, Node start, boolean[][] visited) {
        // create a queue for BFS
        Queue<Node> q = new LinkedList<>();
        q.add(start);
        visited[start.x][start.y] = true;
 
        // BFS loop
        while (!q.isEmpty()) {
            Node curr = q.poll();
 
            // add unvisited neighboring nodes to the queue
            int[] dx = {-1, 0, 1, 0};
            int[] dy = {0, 1, 0, -1};
            for (int i = 0; i < 4; i++) {
                int nx = curr.x + dx[i];
                int ny = curr.y + dy[i];
                if (nx >= 0 && nx < forest.length && ny >= 0 && ny < forest[0].length && forest[nx][ny] == 1 && !visited[nx][ny]) {
                    q.add(new Node(nx, ny));
                    visited[nx][ny] = true;
                }
            }
        }
    }
 
    // function to count the number of trees in a forest using BFS
    static int count_trees_in_forest(int[][] forest) {
        int count = 0;
        int n = forest.length;
        int m = forest[0].length;
 
        // create a 2D boolean array to keep track of visited nodes
        boolean[][] visited = new boolean[n][m];
 
        // iterate over all nodes in the forest and perform BFS from each unvisited tree
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (forest[i][j] == 1 && !visited[i][j]) {
                    bfs(forest, new Node(i, j), visited);
                    count++;
                }
            }
        }
 
        return count;
    }
 
    public static void main(String[] args) {
        // example usage
        int[][] forest = {
            {0, 1, 1, 0, 0},
            {0, 0, 0, 0, 0},
            {0, 0, 0, 0, 0},
            {0, 0, 0, 0, 1},
            {0, 0, 0, 0, 0}
        };
        int num_trees = count_trees_in_forest(forest);
        System.out.println("The forest has " + num_trees + " trees.");
    }
}


Python3




from typing import List, Tuple
from queue import Queue
 
# define a tuple to represent a node in the forest
Node = Tuple[int, int]
 
# function to perform BFS from a given node and mark all visited nodes
 
 
def bfs(forest: List[List[int]], start: Node, visited: List[List[bool]]) -> None:
    # create a queue for BFS
    q = Queue()
    q.put(start)
    visited[start[0]][start[1]] = True
 
    # BFS loop
    while not q.empty():
        curr = q.get()
 
        # add unvisited neighboring nodes to the queue
        dx = [-1, 0, 1, 0]
        dy = [0, 1, 0, -1]
        for i in range(4):
            nx = curr[0] + dx[i]
            ny = curr[1] + dy[i]
            if 0 <= nx < len(forest) and 0 <= ny < len(forest[0]) and forest[nx][ny] == 1 and not visited[nx][ny]:
                q.put((nx, ny))
                visited[nx][ny] = True
 
# function to count the number of trees in a forest using BFS
 
 
def count_trees_in_forest(forest: List[List[int]]) -> int:
    count = 0
    n, m = len(forest), len(forest[0])
 
    # create a 2D boolean array to keep track of visited nodes
    visited = [[False for _ in range(m)] for _ in range(n)]
 
    # iterate over all nodes in the forest and perform BFS from each unvisited tree
    for i in range(n):
        for j in range(m):
            if forest[i][j] == 1 and not visited[i][j]:
                bfs(forest, (i, j), visited)
                count += 1
 
    return count
 
 
# example usage
forest = [
    [0, 1, 1, 0, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 1],
    [0, 0, 0, 0, 0]
]
num_trees = count_trees_in_forest(forest)
print(f"The forest has {num_trees} trees.")


C#




// C# code for above mentioned approach
using System;
using System.Collections.Generic;
 
class Program {
    // define a pair to represent a node in the forest
    class Node {
        public int x;
        public int y;
        public Node(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
    }
 
    // function to perform BFS from a given node and mark
    // all visited nodes
    static void bfs(int[][] forest, Node start,
                    bool[][] visited)
    {
        // create a queue for BFS
        Queue<Node> q = new Queue<Node>();
        q.Enqueue(start);
        visited[start.x][start.y] = true;
 
        // BFS loop
        while (q.Count != 0) {
            Node curr = q.Dequeue();
 
            // add unvisited neighboring nodes to the queue
            int[] dx = { -1, 0, 1, 0 };
            int[] dy = { 0, 1, 0, -1 };
            for (int i = 0; i < 4; i++) {
                int nx = curr.x + dx[i];
                int ny = curr.y + dy[i];
                if (nx >= 0 && nx < forest.Length && ny >= 0
                    && ny < forest[0].Length
                    && forest[nx][ny] == 1
                    && !visited[nx][ny]) {
                    q.Enqueue(new Node(nx, ny));
                    visited[nx][ny] = true;
                }
            }
        }
    }
 
    // function to count the number of trees in a forest
    // using BFS
    static int count_trees_in_forest(int[][] forest)
    {
        int count = 0;
        int n = forest.Length;
        int m = forest[0].Length;
 
        // create a 2D boolean array to keep track of
        // visited nodes
        bool[][] visited = new bool[n][];
        for (int i = 0; i < n; i++) {
            visited[i] = new bool[m];
        }
 
        // iterate over all nodes in the forest and perform
        // BFS from each unvisited tree
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (forest[i][j] == 1 && !visited[i][j]) {
                    bfs(forest, new Node(i, j), visited);
                    count++;
                }
            }
        }
 
        return count;
    }
 
    static void Main(string[] args)
    {
        // example usage
        int[][] forest = { new int[] { 0, 1, 1, 0, 0 },
                           new int[] { 0, 0, 0, 0, 0 },
                           new int[] { 0, 0, 0, 0, 0 },
                           new int[] { 0, 0, 0, 0, 1 },
                           new int[] { 0, 0, 0, 0, 0 } };
        int num_trees = count_trees_in_forest(forest);
        Console.WriteLine("The forest has " + num_trees
                          + " trees.");
    }
}
 
// This code is contributed by Tapesh(tapeshdua420)


Javascript




// define a pair to represent a node in the forest
class Node {
    constructor(x, y) {
        this.x = x;
        this.y = y;
    }
}
 
// function to perform BFS from a given node and mark all visited nodes
function bfs(forest, start, visited) {
    // create a queue for BFS
    let q = [];
    q.push(start);
    visited[start.x][start.y] = true;
 
    // BFS loop
    while (q.length > 0) {
        let curr = q.shift();
 
        // add unvisited neighboring nodes to the queue
        let dx = [-1, 0, 1, 0];
        let dy = [0, 1, 0, -1];
        for (let i = 0; i < 4; i++) {
            let nx = curr.x + dx[i];
            let ny = curr.y + dy[i];
            if (nx >= 0 && nx < forest.length && ny >= 0 && ny < forest[0].length && forest[nx][ny] == 1 && !visited[nx][ny]) {
                q.push(new Node(nx, ny));
                visited[nx][ny] = true;
            }
        }
    }
}
 
// function to count the number of trees in a forest using BFS
function count_trees_in_forest(forest) {
    let count = 0;
    let n = forest.length;
    let m = forest[0].length;
 
    // create a 2D boolean array to keep track of visited nodes
    let visited = new Array(n);
    for (let i=0; i<n; i++) {
      visited[i] = new Array(m).fill(false);
    }
 
    // iterate over all nodes in the forest and perform BFS from each unvisited tree
    for (let i=0; i<n; i++) {
      for (let j=0; j<m; j++) {
          if (forest[i][j] == 1 && !visited[i][j]) {
              bfs(forest,new Node(i,j),visited);
              count++;
          }
      }
   }
 
   return count;
}
 
let forest=[
 [0 ,1 ,1 ,0 ,0],
 [0 ,0 ,0 ,0 ,0],
 [0, 0, 0, 0, 0],
 [0, 0, 0, 0, 1],
 [0, 0, 0, 0, 0]];
let num_trees = count_trees_in_forest(forest);
console.log("The forest has " + num_trees + " trees.");


Output

The forest has 2 trees.

Time complexity : – O(NM)
Auxiliary Space :- O(MN)



Previous Article
Next Article

Similar Reads

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,
8 min read
Total number of possible Binary Search Trees and Binary Trees with n keys
Total number of possible Binary Search Trees with n different keys (countBST(n)) = Catalan number Cn = (2n)! / ((n + 1)! * n!) For n = 0, 1, 2, 3, … values of Catalan numbers are 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, .... So are numbers of Binary Search Trees. Total number of possible Binary Trees with n different keys (countBT(n)) = countBST(n
12 min read
Introduction to Generic Trees (N-ary Trees)
Generic trees are a collection of nodes where each node is a data structure that consists of records and a list of references to its children(duplicate references are not allowed). Unlike the linked list, each node stores the address of multiple nodes. Every node stores address of its children and the very first node's address will be stored in a s
5 min read
Minimum number of rabbits that must be present in the forest
There are some colored rabbits in a forest. Given an array arr[] of size N, such that arr[i] denotes the number of rabbits having same color as the ith rabbit, the task is to find the minimum number of rabbits that could be in the forest. Examples: Input: arr[] = {2, 2, 0}Output: 4Explanation: Considering the 1st and the 2nd rabbits to be of same c
11 min read
Node whose removal minimizes the maximum size forest from an N-ary Tree
Given an n-ary tree T, the task is to find a node whose removal minimizes the maximum size of all forests(connected components) generated. Examples: Input: 1 / | \ 2 3 4 / \ 5 6Output: 1Explanation:There are six nodes which can be removed to form forests:Remove(1): Largest Forest size is 3Remove(2): Largest Forest size is 3Remove(3): Largest Forest
7 min read
Minimize deletion of edges to convert Tree into a forest of size at most N/2
Given a tree with N nodes, numbered from 0 to N - 1, the task is to find the minimum number of deletion of edges, such that, the tree is converted into a forest where each tree in the forest can have size less than equal to ⌊N/2⌋. Examples: Input: N = 3, edges = [[0, 1], [0, 2]] 0 / \ 1 2Output: 2Explanation: The maximum size of each tree after rem
14 min read
Convert a tree to forest of even nodes
Given a tree of n even nodes. The task is to find the maximum number of edges to be removed from the given tree to obtain forest of trees having even number of nodes. This problem is always solvable as given graph has even nodes. Examples: Input : n = 10 Edge 1: 1 3 Edge 2: 1 6 Edge 3: 1 2 Edge 4: 3 4 Edge 5: 6 8 Edge 6: 2 7 Edge 7: 2 5 Edge 8: 4 9
8 min read
Maximum edge removal from tree to make even forest
Given an undirected tree which has even number of vertices, we need to remove the maximum number of edges from this tree such that each connected component of the resultant forest has an even number of vertices. Examples: In above shown tree, we can remove at max 2 edges 0-2 and 0-4 shown in red such that each connected component will have even num
10 min read
Random Forest Classifier using Scikit-learn
In this article, we will see how to build a Random Forest Classifier using the Scikit-Learn library of Python programming language and to do this, we use the IRIS dataset which is quite a common and famous dataset. Random ForestThe Random forest or Random Decision Forest is a supervised Machine learning algorithm used for classification, regression
8 min read
Minimum count of Full Binary Trees such that the count of leaves is N
Given an integer N and an infinite number of Full Binary Trees of different depths, the task is to choose minimum number of trees such that the sum of the count of leaf nodes in each of the tree is N.Example: Input: N = 7 Output: 3 Trees with depths 2, 1 and 0 can be picked with the number of leaf nodes as 4, 2 and 1 respectively. (4 + 2 + 1) = 7In
3 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg