GFG App
Open App
Browser
Continue

# Count number of trees in a forest

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:

## Javascript



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 #include #include using namespace std;   // define a pair to represent a node in the forest typedef pair Node;   // function to perform BFS from a given node and mark all visited nodes void bfs(vector>& forest, Node start, vector>& visited) {     // create a queue for BFS     queue 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>& 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> visited(n, vector(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> 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 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 q = new Queue();         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

Output

The forest has 2 trees.

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

My Personal Notes arrow_drop_up