Given an array arr[] of N integers of value from 0 to N, the task is to count the number of components in Index Array.
Index array means if we are at ith index then it leads to arr[i].
The component of an index array is counted when it forms a cycle. If no cycle persists or the array contains a single element then also we consider it as a component.
For Example:
Let array arr[] = {1, 2, 0, 3}
{1, 2, 0} will form one component as starting from index 0 we reach the index 0 again as:
1 -> 2(arr[1]) -> 0(arr[2]) -> 1(arr[0])
Examples:
Input: arr[] = {1, 2, 3, 5, 0, 4, 6}
Output: 2
Explanation:
Below is the traversal of the 2 components:
Component 1: Start traversal from 0, then the path of traversal is given by:
1 -> 2(arr[1]) -> 3(arr[2]) -> 5(arr[3]) -> 4(arr[5]) -> 0(arr[4]) -> 1(arr[0]).
Component 2: Only 6 is unvisited it creates one more component.
So, the total components = 2.Input: arr[] = {1, 2, 0, 3}
Output: 2
Explanation:
Below is the traversal of the 2 components:
Component 1: Start traversal from 0, then the path of traversal is given by:
1 -> 2(arr[1]) -> 0(arr[2]) -> 1(arr[0])
Component 2: Only 3 is unvisited it creates one more component.
So, the total components = 2.
Approach: The idea is to use the concept of DFS traversal. Below are the steps:
- Start from the first unvisited index which will be index with integer 0 in it.
- During DFS Traversal mark the visited elements in the array until the elements form a cycle.
- If a cycle is formed then it means that we have got one component and hence increase the component count.
- Repeat all the above steps for all the unvisited index in the array and count the total components in the given index array.
- If all the index of the array are visited, then print the total count of connected components.
Below is the implementation of the above approach:
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function for DFS traversal void dfs(int i, int a[], map<int, int>& m) { // Check if not visited then // recurr for the next index if (m[i] == 0) { m[i] = 1; // DFS Traversal for next index dfs(a[i], a, m); } return; } // Function for checking which // indexes are remaining int allvisited(int a[], int n, map<int, int>& m) { for (int i = 0; i < n; i++) { // Marks that the ith // index is not visited if (m[i] == 0) return i; } return -1; } // Function for counting components int count(int a[], int n) { int c = 0; // To mark the visited index // during DFS Traversal map<int, int> m; // Function call int x = allvisited(a, n, m); while (x != -1) { // Count number of components c++; // DFS call dfs(x, a, m); x = allvisited(a, n, m); } // Print the total count of components cout << c << endl; } // Driver Code int main() { // Given array arr[] int arr[] = { 1, 4, 3, 5, 0, 2, 6 }; int N = sizeof(arr) / sizeof(arr[0]); // Function Call count(arr, N); return 0; } |
3
Time Complexity: O(N)
Auxiliary Space: O(N)
Recommended Posts:
- Minimum index i such that all the elements from index i to given index are equal
- Queries for number of distinct elements from a given index till last index in an array
- Rearrange array such that even index elements are smaller and odd index elements are greater
- Sort Array such that smallest is at 0th index and next smallest it at last index and so on
- Number of permutations such that sum of elements at odd index and even index are equal
- Number of connected components in a doubly linked list
- Number of single cycle components in an undirected graph
- Maximum number of edges among all connected components of an undirected graph
- Number of connected components in a 2-D matrix of strings
- Program to count Number of connected components in an undirected graph
- Check if the length of all connected components is a Fibonacci number
- Maximum number of edges to be removed to contain exactly K connected components in the Graph
- Even numbers at even index and odd numbers at odd index
- Check if every index i has an index j such that sum of elements in both directions are equal
- Count of contiguous subarrays possible for every index by including the element at that index
- Check if it is possible to reach to the index with value K when start index is given
- Strongly Connected Components
- Tarjan's Algorithm to find Strongly Connected Components
- Biconnected Components
- Connected Components in an undirected graph
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.


