📜  最小两党团体

📅  最后修改于: 2021-05-04 22:48:46             🧑  作者: Mango

给定从1到NN个顶点的图的邻接表表示,任务是计算给定图的最小二分群。

例子:

方法:
这个想法是在给定的N个节点图中找到所有连接组件的最大高度,以找到最小的二部组。步骤如下:

  1. 对于给定图中所有未访问的顶点,从当前顶点开始查找当前连接组件的高度。
  2. 启动“ DFS遍历”以查找所有已连接组件的高度。
  3. 计算出的所有连接组件的最大高度给出了所需的最小二部分组。

下面是上述方法的实现:

C++
#include 
using namespace std;
 
// Function to find the height sizeof
// the current component with vertex s
int height(int s, vector adj[],
           int* visited)
{
    // Visit the current Node
    visited[s] = 1;
    int h = 0;
 
    // Call DFS recursively to find the
    // maximum height of current CC
    for (auto& child : adj[s]) {
 
        // If the node is not visited
        // then the height recursively
        // for next element
        if (visited[child] == 0) {
            h = max(h, 1 + height(child, adj,
                                  visited));
        }
    }
    return h;
}
 
// Function to find the minimum Groups
int minimumGroups(vector adj[], int N)
{
    // Intialise with visited array
    int visited[N + 1] = { 0 };
 
    // To find the minimum groups
    int groups = INT_MIN;
 
    // Traverse all the non visited Node
    // and calculate the height of the
    // tree with current node as a head
    for (int i = 1; i <= N; i++) {
 
        // If the current is not visited
        // therefore, we get another CC
        if (visited[i] == 0) {
            int comHeight;
            comHeight = height(i, adj, visited);
            groups = max(groups, comHeight);
        }
    }
 
    // Return the minimum bipartite matching
    return groups;
}
 
// Function that adds the current edges
// in the given graph
void addEdge(vector adj[], int u, int v)
{
    adj[u].push_back(v);
    adj[v].push_back(u);
}
 
// Drivers Code
int main()
{
    int N = 5;
 
    // Adjacency List
    vector adj[N + 1];
 
    // Adding edges to List
    addEdge(adj, 1, 2);
    addEdge(adj, 3, 2);
    addEdge(adj, 4, 3);
 
    cout << minimumGroups(adj, N);
}


Java
import java.util.*;
 
class GFG{
  
// Function to find the height sizeof
// the current component with vertex s
static int height(int s, Vector adj[],
           int []visited)
{
    // Visit the current Node
    visited[s] = 1;
    int h = 0;
  
    // Call DFS recursively to find the
    // maximum height of current CC
    for (int  child : adj[s]) {
  
        // If the node is not visited
        // then the height recursively
        // for next element
        if (visited[child] == 0) {
            h = Math.max(h, 1 + height(child, adj,
                                  visited));
        }
    }
    return h;
}
  
// Function to find the minimum Groups
static int minimumGroups(Vector adj[], int N)
{
    // Intialise with visited array
    int []visited= new int[N + 1];
  
    // To find the minimum groups
    int groups = Integer.MIN_VALUE;
  
    // Traverse all the non visited Node
    // and calculate the height of the
    // tree with current node as a head
    for (int i = 1; i <= N; i++) {
  
        // If the current is not visited
        // therefore, we get another CC
        if (visited[i] == 0) {
            int comHeight;
            comHeight = height(i, adj, visited);
            groups = Math.max(groups, comHeight);
        }
    }
  
    // Return the minimum bipartite matching
    return groups;
}
  
// Function that adds the current edges
// in the given graph
static void addEdge(Vector adj[], int u, int v)
{
    adj[u].add(v);
    adj[v].add(u);
}
  
// Drivers Code
public static void main(String[] args)
{
    int N = 5;
  
    // Adjacency List
    Vector []adj = new Vector[N + 1];
    for (int i = 0 ; i < N + 1; i++)
        adj[i] = new Vector();
     
    // Adding edges to List
    addEdge(adj, 1, 2);
    addEdge(adj, 3, 2);
    addEdge(adj, 4, 3);
  
    System.out.print(minimumGroups(adj, N));
}
}
 
// This code is contributed by 29AjayKumar


Python3
import sys
 
# Function to find the height sizeof
# the current component with vertex s
def height(s, adj, visited):
 
    # Visit the current Node
    visited[s] = 1
    h = 0
  
    # Call DFS recursively to find the
    # maximum height of current CC
    for child in adj[s]:
  
        # If the node is not visited
        # then the height recursively
        # for next element
        if (visited[child] == 0):
            h = max(h, 1 + height(child, adj,
                                  visited))
     
    return h
 
# Function to find the minimum Groups
def minimumGroups(adj, N):
 
    # Intialise with visited array
    visited = [0 for i in range(N + 1)]
  
    # To find the minimum groups
    groups = -sys.maxsize
  
    # Traverse all the non visited Node
    # and calculate the height of the
    # tree with current node as a head
    for i in range(1, N + 1):
  
        # If the current is not visited
        # therefore, we get another CC
        if (visited[i] == 0):
            comHeight = height(i, adj, visited)
            groups = max(groups, comHeight)
         
    # Return the minimum bipartite matching
    return groups
 
# Function that adds the current edges
# in the given graph
def addEdge(adj, u, v):
     
    adj[u].append(v)
    adj[v].append(u)
 
# Driver code   
if __name__=="__main__":
     
    N = 5
  
    # Adjacency List
    adj = [[] for i in range(N + 1)]
  
    # Adding edges to List
    addEdge(adj, 1, 2)
    addEdge(adj, 3, 2)
    addEdge(adj, 4, 3)
  
    print(minimumGroups(adj, N))
 
# This code is contributed by rutvik_56


C#
using System;
using System.Collections.Generic;
 
class GFG{
   
// Function to find the height sizeof
// the current component with vertex s
static int height(int s, List []adj,
           int []visited)
{
    // Visit the current Node
    visited[s] = 1;
    int h = 0;
   
    // Call DFS recursively to find the
    // maximum height of current CC
    foreach (int  child in adj[s]) {
   
        // If the node is not visited
        // then the height recursively
        // for next element
        if (visited[child] == 0) {
            h = Math.Max(h, 1 + height(child, adj,
                                  visited));
        }
    }
    return h;
}
   
// Function to find the minimum Groups
static int minimumGroups(List []adj, int N)
{
    // Intialise with visited array
    int []visited= new int[N + 1];
   
    // To find the minimum groups
    int groups = int.MinValue;
   
    // Traverse all the non visited Node
    // and calculate the height of the
    // tree with current node as a head
    for (int i = 1; i <= N; i++) {
   
        // If the current is not visited
        // therefore, we get another CC
        if (visited[i] == 0) {
            int comHeight;
            comHeight = height(i, adj, visited);
            groups = Math.Max(groups, comHeight);
        }
    }
   
    // Return the minimum bipartite matching
    return groups;
}
   
// Function that adds the current edges
// in the given graph
static void addEdge(List []adj, int u, int v)
{
    adj[u].Add(v);
    adj[v].Add(u);
}
   
// Drivers Code
public static void Main(String[] args)
{
    int N = 5;
   
    // Adjacency List
    List []adj = new List[N + 1];
    for (int i = 0 ; i < N + 1; i++)
        adj[i] = new List();
      
    // Adding edges to List
    addEdge(adj, 1, 2);
    addEdge(adj, 3, 2);
    addEdge(adj, 4, 3);
   
    Console.Write(minimumGroups(adj, N));
}
}
  
// This code is contributed by Rajput-Ji


输出:
3






时间复杂度: O(V + E),其中V是顶点数,E是边集。