📜  图的优势集

📅  最后修改于: 2022-05-13 01:57:54.096000             🧑  作者: Mango

图的优势集

在图论中,图 G = (V, E) 的支配集是 V 的子集 D,使得不在 D 中的每个顶点都与 D 的至少一个成员相邻。支配数是图的顶点数G 的最小支配集。

例子:

输入:具有 4 个顶点和 4 个边的图输出:主导集 S= { a, b } or { a, d } or { a, c } 等等。输入:一个有 6 个顶点和 7 个边的图输出:主导集 S= { a, d, f } 或 { e, c } 等等。

人们认为,可能没有有效的算法可以为所有图找到最小的支配集,但是有有效的逼近算法。
算法 :

  • 首先我们必须将一个集合'S'初始化为空
  • 取连接顶点的图的任何边“e”(比如 A 和 B )
  • 在 A 和 B 之间添加一个顶点(比如说 A )到我们的集合 S
  • 删除图中连接到 A 的所有边
  • 回到第 2 步并重复,如果图中仍有一些边
  • 最后的集合 S 是图的支配集
C++
// C++ program to find the Dominant Set of a graph
#include 
using namespace std;
  
vector > g;
bool box[100000];
  
vector Dominant(int ver, int edge)
{
    vector S; // set S
    for (int i = 0; i < ver; i++) {
        if (!box[i]) {
            S.push_back(i);
            box[i] = true;
            for (int j = 0; j < (int)g[i].size(); j++) {
                if (!box[g[i][j]]) {
                    box[g[i][j]] = true;
                    break;
                }
            }
        }
    }
    return S;
}
  
// Driver function
int main()
{
    int ver, edge, x, y;
  
    ver = 5; // Enter number of vertices
    edge = 6; // Enter number of Edges
    g.resize(ver);
  
    // Setting all index value of an array as 0
    memset(box, 0, sizeof(box)); 
  
    // Enter all the end-points of all the Edges
    // g[x--].push_back[y--]      g[y--].push_back[x--]
    g[0].push_back(1);
    g[1].push_back(0); // x = 1, y = 2 ;
    g[1].push_back(2);
    g[2].push_back(1); // x = 2, y = 3 ;
    g[2].push_back(3);
    g[3].push_back(2); // x = 3, y = 4 ;
    g[0].push_back(3);
    g[3].push_back(0); // x = 1, y = 4 ;
    g[3].push_back(4);
    g[4].push_back(3); // x = 4, y = 5 ;
    g[2].push_back(4);
    g[4].push_back(2); // x = 3, y = 5 ;
  
    vector S = Dominant(ver, edge);
    cout << "The Dominant Set is : { ";
    for (int i = 0; i < (int)S.size(); i++)
        cout << S[i] + 1 << " ";
    cout << "}";
    return 0;
}


Java
// Java program to find the Dominant Set of a graph
import java.util.*;
  
class GFG
{
  
static Vector []g;
static boolean []box = new boolean[100000];
  
static Vector Dominant(int ver, int edge)
{
    Vector S = new Vector(); // set S
    for (int i = 0; i < ver; i++) 
    {
        if (!box[i]) 
        {
            S.add(i);
            box[i] = true;
            for (int j = 0; j < (int)g[i].size(); j++) 
            {
                if (!box[g[i].get(j)])
                {
                    box[g[i].get(j)] = true;
                    break;
                }
            }
        }
    }
    return S;
}
  
// Driver code
public static void main(String[] args)
{
    int ver, edge, x, y;
  
    ver = 5; // Enter number of vertices
    edge = 6; // Enter number of Edges
    g = new Vector[ver];
    for (int i = 0; i < ver; i++)
        g[i] = new Vector();
  
  
    // Enter all the end-points of all the Edges
    // g[x--].push_back[y--]     g[y--].push_back[x--]
    g[0].add(1);
    g[1].add(0); // x = 1, y = 2 ;
    g[1].add(2);
    g[2].add(1); // x = 2, y = 3 ;
    g[2].add(3);
    g[3].add(2); // x = 3, y = 4 ;
    g[0].add(3);
    g[3].add(0); // x = 1, y = 4 ;
    g[3].add(4);
    g[4].add(3); // x = 4, y = 5 ;
    g[2].add(4);
    g[4].add(2); // x = 3, y = 5 ;
  
    Vector S = Dominant(ver, edge);
    System.out.print("The Dominant Set is : { ");
    for (int i = 0; i < (int)S.size(); i++)
        System.out.print(S.get(i) + 1 + " ");
    System.out.print("}");
}
}
  
// This code is contributed by Rajput-Ji


C#
// C# program to find the Dominant Set of a graph
using System;
using System.Collections.Generic;
  
class GFG
{
  
static List []g;
static bool []box = new bool[100000];
  
static List Dominant(int ver, int edge)
{
    List S = new List(); // set S
    for (int i = 0; i < ver; i++) 
    {
        if (!box[i]) 
        {
            S.Add(i);
            box[i] = true;
            for (int j = 0; j < (int)g[i].Count; j++) 
            {
                if (!box[g[i][j]])
                {
                    box[g[i][j]] = true;
                    break;
                }
            }
        }
    }
    return S;
}
  
// Driver code
public static void Main(String[] args)
{
    int ver, edge;
  
    ver = 5; // Enter number of vertices
    edge = 6; // Enter number of Edges
    g = new List[ver];
    for (int i = 0; i < ver; i++)
        g[i] = new List();
  
    // Enter all the end-points of all the Edges
    // g[x--].push_back[y--]     g[y--].push_back[x--]
    g[0].Add(1);
    g[1].Add(0); // x = 1, y = 2 ;
    g[1].Add(2);
    g[2].Add(1); // x = 2, y = 3 ;
    g[2].Add(3);
    g[3].Add(2); // x = 3, y = 4 ;
    g[0].Add(3);
    g[3].Add(0); // x = 1, y = 4 ;
    g[3].Add(4);
    g[4].Add(3); // x = 4, y = 5 ;
    g[2].Add(4);
    g[4].Add(2); // x = 3, y = 5 ;
  
    List S = Dominant(ver, edge);
    Console.Write("The Dominant Set is : { ");
    for (int i = 0; i < (int)S.Count; i++)
        Console.Write(S[i] + 1 + " ");
    Console.Write("}");
}
}
  
// This code is contributed by PrinciRaj1992


输出:
The Dominant Set is : { 1 3 5 }

参考:维基