📜  索引数组中的组件总数

📅  最后修改于: 2021-06-26 17:00:19             🧑  作者: Mango

给定从0到NN个整数的数组arr [] ,任务是计算索引数组中的组件数。

例子:

方法:想法是使用DFS遍历的概念。步骤如下:

  1. 从第一个未访问的索引开始,该索引将是其中具有整数0的索引。
  2. DFS遍历期间,在数组中标记已访问的元素,直到元素形成一个循环为止。
  3. 如果形成一个循环,则意味着我们只有一个组件,因此增加了组件数量。
  4. 对数组中所有未访问的索引重复上述所有步骤,并计算给定索引数组中的总数。
  5. 如果访问了数组的所有索引,则打印已连接组件的总数。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function for DFS traversal
void dfs(int i, int a[],
         map& 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& 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 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;
}


Java
// Java program for the above approach
import java.util.*;
class Main
{
    // Function for DFS traversal
    static void dfs(int i, int[] a,
                    HashMap m)
    {
          
        // Check if not visited then
        // recurr for the next index
        if (!m.containsKey(i))
        {
            m.put(i, 1);
              
            // DFS Traversal for next index
            dfs(a[i], a, m);
        }
        return;
    }
       
    // Function for checking which
    // indexes are remaining
    static int allvisited(int[] a, int n,
                          HashMap m)
    {
        for(int i = 0; i < n; i++)
        {
              
            // Marks that the ith
            // index is not visited
            if (!m.containsKey(i))
                return i;
        }
        return -1;
    }
       
    // Function for counting components
    static void count(int[] a, int n)
    {
        int c = 0;
          
        // To mark the visited index
        // during DFS Traversal
        HashMap m = new HashMap<>();
                                             
        // 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
        System.out.print(c);
    }
 
    public static void main(String[] args)
    {
       
        // Given array arr[]
        int[] arr = { 1, 4, 3, 5, 0, 2, 6 };
       
        int N = arr.length;
       
        // Function Call
        count(arr, N);
    }
}
 
// This code is contributed by divyesh072019


Python3
# Python3 program for the above approach
  
# Function for DFS traversal
def dfs(i, a, m):
 
    # Check if not visited then
    # recurr for the next index
    if i in m:
        if m[i] == 0:
            m[i] = 1
             
            # DFS Traversal for next index
            dfs(a[i], a, m)
    else:
        m[i] = 1
         
        # DFS Traversal for next index
        dfs(a[i], a, m)
  
    return
 
# Function for checking which
# indexes are remaining
def allvisited(a, n, m):
     
    for i in range(n):
  
        # Marks that the ith
        # index is not visited
        if i in m:
            if m[i] == 0:
                return i
         
        else:
            return i
 
    return -1
 
# Function for counting components
def count(a, n):
 
    c = 0
  
    # To mark the visited index
    # during DFS Traversal
    m = dict()
  
    # Function call
    x = allvisited(a, n, m)
  
    while (x != -1):
  
        # Count number of components
        c += 1
  
        # DFS call
        dfs(x, a, m)
  
        x = allvisited(a, n, m)
     
    # Print the total count of components
    print(c)
     
# Driver Code
if __name__=='__main__':
 
    # Given array arr[]
    arr = [ 1, 4, 3, 5, 0, 2, 6 ]
     
    N = len(arr)
  
    # Function Call
    count(arr, N)
  
# This code is contributed by rutvik_56


C#
// C# program for the above approach
using System;
using System.Collections;
using System.Collections.Generic;
 
class GFG{
  
// Function for DFS traversal
static void dfs(int i, int []a,
     Dictionary m)
{
     
    // Check if not visited then
    // recurr for the next index
    if (!m.ContainsKey(i))
    {
        m[i] = 1;
         
        // DFS Traversal for next index
        dfs(a[i], a, m);
    }
    return;
}
  
// Function for checking which
// indexes are remaining
static int allvisited(int []a, int n,
               Dictionary m)
{
    for(int i = 0; i < n; i++)
    {
         
        // Marks that the ith
        // index is not visited
        if (!m.ContainsKey(i))
            return i;
    }
    return -1;
}
  
// Function for counting components
static void count(int []a, int n)
{
    int c = 0;
     
    // To mark the visited index
    // during DFS Traversal
    Dictionary m = new Dictionary();
                                        
    // 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
    Console.Write(c);
}
  
// Driver Code
public static void Main()
{
     
    // Given array arr[]
    int []arr = { 1, 4, 3, 5, 0, 2, 6 };
  
    int N = arr.Length;
  
    // Function Call
    count(arr, N);
}
}
 
// This code is contributed by pratham76


输出:
3

时间复杂度: O(N)
辅助空间: O(N)

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。