Number of Provinces (Two Recursive STACK BFS Approach in Java) 
IntuitionThe problem requires us to find the number of provinces (connected components) in a given adjacency matrix.
Each city is a node, and a connection betwe
# Intuition
The problem requires us to find the number of provinces (connected components) in a given adjacency matrix.

- Each city is a node, and a connection between cities represents an edge.
- If a city is connected to another directly or indirectly, they belong to the same province.
- The problem can be visualized as a graph traversal problem where we count the number of connected components.


# Approach
1. **Use BFS to traverse the graph recursively:**

    - We maintain a visited array to track visited cities.
    Iterate through each city:
        - If it's not visited, it means we found a new province.
        - Start a BFS traversal using a recursive approach with     two functions (bfs1 and bfs2).
        - The BFS will visit all connected cities, marking them as visited.
After BFS completes, we increment the province count.

2. **Recursive BFS Implementation:**

    - bfs1: Picks a city from the queue, marks its unvisited neighbors, and passes them to bfs2.
    - bfs2: Moves the nextQueue into queue and calls bfs1 again.

# Complexity
- **Time complexity:**

    - We traverse each city once O(n).
    - For each city, we check all its neighbors in O(n).
    - Total complexity: O(n²) (since we traverse the adjacency matrix).
- **Space Complexity:**
    - O(n) for the visited array.
    - O(n) for the recursion call stack in the worst case.
Total: O(n).


# Code
```java []
class Solution {
    public static int findCircleNum(int[][] isConnected) {
        int n = isConnected.length;
        boolean[] visited = new boolean[n];
        int provinceCount = 0;

        for (int i = 0; i < n; i++) {
            if (!visited[i]) {
                List<Integer> queue = new ArrayList<>();
                queue.add(i);
                visited[i] = true;
                bfs1(isConnected, queue, visited);
                provinceCount++;
            }
        }

        return provinceCount;
    }


    private static void bfs1(int[][] isConnected, List<Integer> queue, boolean[] visited) {
        if (queue.isEmpty()) return;

        int city = queue.remove(0);

        List<Integer> nextQueue = new ArrayList<>();
        for (int neighbor = 0; neighbor < isConnected.length; neighbor++) {
            if (isConnected[city][neighbor] == 1 && !visited[neighbor]) {
                visited[neighbor] = true;
                nextQueue.add(neighbor);
            }
        }

        bfs2(isConnected, queue, nextQueue, visited);
    }
    private static void bfs2(int[][] isConnected, List<Integer> queue, List<Integer> nextQueue, boolean[] visited) {
        if (!nextQueue.isEmpty()) {
            queue.addAll(nextQueue);
        }
        bfs1(isConnected, queue, visited);
    }

}

```
```cpp []
class Solution {
public:
    int findCircleNum(vector<vector<int>>& isConnected) {
        int n = isConnected.size();
        vector<bool> visited(n, false);
        int provinceCount = 0;

        for (int i = 0; i < n; i++) {
            if (!visited[i]) {
                vector<int> queue;
                queue.push_back(i);
                visited[i] = true;
                bfs1(isConnected, queue, visited);
                provinceCount++;
            }
        }

        return provinceCount;
    }

private:
    void bfs1(vector<vector<int>>& isConnected, vector<int>& queue, vector<bool>& visited) {
        if (queue.empty()) return;

        int city = queue.front();
        queue.erase(queue.begin());

        vector<int> nextQueue;
        for (int neighbor = 0; neighbor < isConnected.size(); neighbor++) {
            if (isConnected[city][neighbor] == 1 && !visited[neighbor]) {
                visited[neighbor] = true;
                nextQueue.push_back(neighbor);
            }
        }

        bfs2(isConnected, queue, nextQueue, visited);
    }


    void bfs2(vector<vector<int>>& isConnected, vector<int>& queue, vector<int>& nextQueue, vector<bool>& visited) {
        if (!nextQueue.empty()) {
            queue.insert(queue.end(), nextQueue.begin(), nextQueue.end());
        }
        bfs1(isConnected, queue, visited);
    }
};

```
```python []
class Solution:
    def findCircleNum(self, isConnected: List[List[int]]) -> int:
        n = len(isConnected)
        visited = [False] * n
        province_count = 0

        for i in range(n):
            if not visited[i]:
                queue = [i]
                visited[i] = True
                self.bfs1(isConnected, queue, visited)
                province_count += 1

        return province_count


    def bfs1(self, isConnected: List[List[int]], queue: List[int], visited: List[bool]):
        if not queue:
            return

        city = queue.pop(0)
        next_queue = []

        for neighbor in range(len(isConnected)):
            if isConnected[city][neighbor] == 1 and not visited[neighbor]:
                visited[neighbor] = True
                next_queue.append(neighbor)

        self.bfs2(isConnected, queue, next_queue, visited)


    def bfs2(self, isConnected: List[List[int]], queue: List[int], next_queue: List[int], visited: List[bool]):
        if next_queue:
            queue.extend(next_queue)
        self.bfs1(isConnected, queue, visited)

```

['Breadth-First Search', 'C++', 'Java', 'Python3']