Find the size of all Connected Non-Empty Cells of a Matrix in C++12 May 2025 | 4 min read Problem Statement:We are given a binary matrix, which indicates that there will be only two elements present in the matrix either zero (0) or one (1), where a non-empty cell is represented by one (1) and an empty cell by zero (0). Finding every possible connected non-empty cell component is our task. Two cells must be adjacent in either a horizontal or vertical direction for them to be referred to as linked. For Example: Input:
Output:
Input:
Output:
Algorithm:
Program 1:Let us take a C++ program to find the size of all Connected Non-Empty Cells of a Matrix using DFS traversal. Output: ![]() Time Complexity:The time complexity to find the sizes of all connected non-empty cells of a matrix using DFS traversal is O(rs * cs). Space Complexity:The space complexity to find the sizes of all connected non-empty cells of a matrix using DFS traversal is O(rs * cs). Program 2:Let us take another C++ program to find the size of all Connected Non-Empty Cells of a Matrix using BFS traversal: Output: ![]() Time Complexity:The time complexity to find the sizes of all connected non-empty cells of a matrix using BFS traversal is O(rs * cs). Space Complexity:The space complexity to find the sizes of all connected non-empty cells of a matrix using BFS traversal is O(rs * cs). Conclusion:In summary, we can conclude that both Breadth First Search (BFS) and Depth First Search (DFS) provide effective and efficient solutions with the time and space complexities of O(rs*cs). Here, rs represents the number of rows, and cs represents the number of columns in the matrix for determining the sizes of all connected non-empty cells of a matrix. The decision for choosing between DFS and BFS depends on various factors, such as memory limitations, traversal orders, and problem specifications. After considering all the factors, both approaches are efficient for solving the given problem. Next TopicShift 2D Grid in C++ |
Leonardo Numbers in C++
In this article, we will discuss the with its significance and different approaches. Introduction to Leonardo numbers form an interesting sequence in mathematics, closely related to the Fibonacci sequence but with a slight variation in their recurrence relation. These numbers are named after the Italian...
16 min read
Trie of all Suffixes in C++
In this article, you will learn about the trie of all suffixes in C++ with its history, implementation, applications, advantages, and disadvantages. What is the Trie in C++? A trie is also called as a prefix tree. It is a tree-like data structure that is used for...
10 min read
Find Eventual Safe States in C++
Introduction Within the branch of computer science also related to graph theory, there are quite a number of instances where we encounter the need to find certain states/nodes that can be defined as 'safe' states/nodes. A state is said to be safe if the system, commencing from...
10 min read
Paxos Algorithm in C++
Introduction: The Paxos Algorithm is a fundamental consensus protocol designed to allow multiple systems, or nodes, to agree on a single value, even in situations where some nodes might fail, or messages between them could be delayed or lost. It is particularly useful in distributed computing,...
9 min read
Difference between C++ and Ada
In this article, we discuss the differences between C++ and Ada. Before understanding the differences, let us understand about each. What is the C++? C++ was developed by Bjarne Stroustrup in 1985 as an enhancement of the C programming language to provide developers with high-level abstraction in...
4 min read
How to resolve build errors due to Circular Dependency amongst classes in C++
In this article, we will discuss circular dependency, a condition when two or more entities (modules/classes/components) directly or indirectly depend upon each other. In other words, circular dependency arises when the execution or compilation of a module or component requires, as a prerequisite, another module or...
4 min read
std::reference_wrapper in C++
An assignable object or reference to a function of type T can be wrapped in a copy constructible by using the class template std::reference_wrapper. It is possible to copy or store instances of std::reference_wrapper in containers, but they are implicitly convertible to "T&" so that they...
4 min read
Word Squares in C++
In this article, we will discuss the Word Squares method in C++ with its syntax, parameter, and example. What are Word Squares? Word squares refer to a language that is made up of words that are fitted into grids within a square. These words are read the same...
14 min read
Bertrand's Postulate in C++
In this article, we are going to look at Bertrand's postulate with its examples in C++. What is the Bertrand's Postulate? Joseph Bertrand, a French mathematician, Baptized Bertrand's Postulate an important mathematical theory that resulted in the name of Bertrand. Bertrand first stated the theorem- the British mathematician...
6 min read
Command Design Pattern in C++
The Command Design pattern is a behavioral pattern that provides the ability to decouple a requester from the receiver by encoding a request as an object, thus enabling the customization of clients with different requests, request order, and the ability to support operations that can be...
4 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India

