📜  图的连接组件数(使用不交集并集)

📅  最后修改于: 2021-04-22 03:01:24             🧑  作者: Mango

给定一个无向图G ,其顶点编号在[0,N]范围内并且数组Edges [] []M条边组成,任务是使用不交集并集算法找到图中连接的组件总数。

例子:

方法:可以使用不交集并集算法解决该问题。请按照以下步骤解决问题:

  • 在DSU算法中,有两个主要函数,即connect()root()函数。
  • connect():连接边缘。
  • root():递归确定给定边的最高父级。
  • 对于每个边沿{a,b},检查a是否连接到b 。如果发现是假的,则通过附加其顶级父母来连接它们。
  • 在为每个边完成以上步骤之后,为每个顶点打印不同的最上层父对象的总数。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Stores the parent of each vertex
int parent[1000000];
 
// Function to find the topmost
// parent of vertex a
int root(int a)
{
    // If current vertex is
    // the topmost vertex
    if (a == parent[a]) {
        return a;
    }
 
    // Otherwsie, set topmost vertex of
    // its parent as its topmost vertex
    return parent[a] = root(parent[a]);
}
 
// Function to connect the component
// having vertex a with the component
// having vertex b
void connect(int a, int b)
{
    // Connect edges
    a = root(a);
    b = root(b);
 
    if (a != b) {
        parent[b] = a;
    }
}
 
// Function to find unique top most parents
void connectedComponents(int n)
{
    set s;
 
    // Traverse all vertices
    for (int i = 0; i < n; i++) {
 
        // Insert all topmost
        // vertices obtained
        s.insert(parent[i]);
    }
 
    // Print count of connected components
    cout << s.size() << '\n';
}
 
// Function to print answer
void printAnswer(int N,
                 vector > edges)
{
 
    // Setting parent to itself
    for (int i = 0; i <= N; i++) {
        parent[i] = i;
    }
 
    // Traverse all edges
    for (int i = 0; i < edges.size(); i++) {
        connect(edges[i][0], edges[i][1]);
    }
 
    // Print answer
    connectedComponents(N);
}
 
// Driver Code
int main()
{
    // Given N
    int N = 8;
 
    // Given edges
    vector > edges = {
        { 1, 0 }, { 0, 2 }, { 5, 3 }, { 3, 4 }, { 6, 7 }
    };
 
    // Function call
    printAnswer(N, edges);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG{
 
// Stores the parent of each vertex
static int []parent = new int[1000000];
 
// Function to find the topmost
// parent of vertex a
static int root(int a)
{
   
    // If current vertex is
    // the topmost vertex
    if (a == parent[a])
    {
        return a;
    }
 
    // Otherwsie, set topmost vertex of
    // its parent as its topmost vertex
    return parent[a] = root(parent[a]);
}
 
// Function to connect the component
// having vertex a with the component
// having vertex b
static void connect(int a, int b)
{
   
    // Connect edges
    a = root(a);
    b = root(b);
 
    if (a != b) {
        parent[b] = a;
    }
}
 
// Function to find unique top most parents
static void connectedComponents(int n)
{
    HashSet s = new HashSet();
 
    // Traverse all vertices
    for (int i = 0; i < n; i++)
    {
 
        // Insert all topmost
        // vertices obtained
        s.add(parent[i]);
    }
 
    // Print count of connected components
    System.out.println(s.size());
}
 
// Function to print answer
static void printAnswer(int N,int [][] edges)
{
 
    // Setting parent to itself
    for (int i = 0; i <= N; i++)
    {
        parent[i] = i;
    }
 
    // Traverse all edges
    for (int i = 0; i < edges.length; i++)
    {
        connect(edges[i][0], edges[i][1]);
    }
 
    // Print answer
    connectedComponents(N);
}
 
// Driver Code
public static void main(String[] args)
{
   
    // Given N
    int N = 8;
 
    // Given edges
   int [][]edges = {{ 1, 0 }, { 0, 2 },
                    { 5, 3 }, { 3, 4 },
                    { 6, 7 }};
 
    // Function call
    printAnswer(N, edges);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program for the above approach
 
# Stores the parent of each vertex
parent = [0]*(1000000)
  
# Function to find the topmost
# parent of vertex a
def root(a) :
    
    # If current vertex is
    # the topmost vertex
    if (a == parent[a]) :       
        return a
         
    # Otherwsie, set topmost vertex of
    # its parent as its topmost vertex
    parent[a] = root(parent[a])
    return parent[a]
     
# Function to connect the component
# having vertex a with the component
# having vertex b
def connect(a, b) :
    
    # Connect edges
    a = root(a)
    b = root(b)
  
    if (a != b) :
        parent[b] = a
         
# Function to find unique top most parents
def connectedComponents(n) :
 
    s = set()
  
    # Traverse all vertices
    for i in range(n) :
  
        # Insert all topmost
        # vertices obtained
        s.add(parent[i])
  
    # Print count of connected components
    print(len(s))
     
# Function to print answer
def printAnswer(N, edges) :
  
    # Setting parent to itself
    for i in range(N + 1) :  
        parent[i] = i
  
    # Traverse all edges
    for i in range(len(edges)) :   
        connect(edges[i][0], edges[i][1])
  
    # Print answer
    connectedComponents(N)
     
# Given N
N = 8
 
# Given edges
edges = [[ 1, 0 ], [ 0, 2 ], [ 5, 3 ], [ 3, 4 ], [ 6, 7 ]]
 
# Function call
printAnswer(N, edges)
 
# This code is contributed by divyesh072019


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG {
     
    // Stores the parent of each vertex
    static int[] parent = new int[1000000];
       
    // Function to find the topmost
    // parent of vertex a
    static int root(int a)
    {
        // If current vertex is
        // the topmost vertex
        if (a == parent[a]) {
            return a;
        }
       
        // Otherwsie, set topmost vertex of
        // its parent as its topmost vertex
        return parent[a] = root(parent[a]);
    }
       
    // Function to connect the component
    // having vertex a with the component
    // having vertex b
    static void connect(int a, int b)
    {
        // Connect edges
        a = root(a);
        b = root(b);
       
        if (a != b) {
            parent[b] = a;
        }
    }
       
    // Function to find unique top most parents
    static void connectedComponents(int n)
    {
        HashSet s = new HashSet();
       
        // Traverse all vertices
        for (int i = 0; i < n; i++) {
       
            // Insert all topmost
            // vertices obtained
            s.Add(parent[i]);
        }
       
        // Print count of connected components
        Console.WriteLine(s.Count);
    }
       
    // Function to print answer
    static void printAnswer(int N, List > edges)
    {
       
        // Setting parent to itself
        for (int i = 0; i <= N; i++) {
            parent[i] = i;
        }
       
        // Traverse all edges
        for (int i = 0; i < edges.Count; i++) {
            connect(edges[i][0], edges[i][1]);
        }
       
        // Print answer
        connectedComponents(N);
    }  
 
  // Driver code
  static void Main() {
       
    // Given N
    int N = 8;
   
    // Given edges
    List> edges = new List>();
    edges.Add(new List { 1, 0 });
    edges.Add(new List { 0, 2 });
    edges.Add(new List { 5, 3 });
    edges.Add(new List { 3, 4 });
    edges.Add(new List { 6, 7 });
   
    // Function call
    printAnswer(N, edges);
  }
}
 
// This code is contributed by divyeshrabadiya07


输出:
3

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