📜  具有 2 种或更多颜色边的 Graph 顶点的最大子集

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

具有 2 种或更多颜色边的 Graph 顶点的最大子集

给定一个具有N个节点或顶点的无向完全图。图的边缘是彩色的,找到具有 2 种或更多颜色边缘的最大顶点子集。我们将图作为邻接矩阵 C[][] 给出,其中 C[i][j] 是从顶点 i 到顶点 j 的边的颜色。由于图是无向的,因此 C[j][i] 的值 C[i][j] 相同。

我们将 C[i][i] 定义为零,尽管不存在这样的边。即图表不包含自环。

例子:

由于图是完整的,每条边可以是 n*(n-1)/2 +1 种不同颜色中的一种。这些颜色标记为从 0 到 n*(n-1)/2,包括端点。但并非所有这些 n*(n-1)/2 +1 颜色都需要使用。即,两个不同的边缘可能具有相同的颜色。

如果它的所有邻居都具有相同的颜色,我们就称它为“坏”顶点。显然,我们的子集中不能有任何这样的坏顶点,所以从图中删除这样的坏顶点。这可能会引入更多坏顶点,但我们可以不断重复这个过程,直到找到一个没有坏顶点的子集。所以,最后,我们应该通过一个没有任何坏顶点的图来保持,这意味着我们子集的每个顶点与其他相邻顶点至少有两个不同颜色的边。

例子:
输入 :
让 C[6][6]:
{{0, 9, 2, 4, 7, 8},
{9, 0, 9, 9, 7, 9},
{2, 9, 0, 3, 7, 6},
{4, 9, 3, 0, 7, 1},
{7, 7, 7, 7, 0, 7},
{8, 9, 6, 1, 7, 0}};

图1

步骤 I:首先,我们可以看到第 5 行(节点 'e')仅包含 7 意味着节点 'e' 通过颜色代码为 7 的边连接,因此它没有多个颜色边,因此我们必须删除5 来自子集。现在,我们的图将只包含 5 个顶点,如下所示:
C[5][5]:
{{0, 9, 2, 4, 8},
{9, 0, 9, 9, 9},
{2, 9, 0, 3, 6},
{4, 9, 3, 0, 1},
{8, 9, 6, 1, 0}};

图2

第二步:进一步,我们可以看到第 2 行(节点 'b')也包含不超过 1 个颜色边缘,所以我们也应该删除第 2 行和第 2 列。这导致我们的新图表为:
C[4][4]:
{{0, 2, 4, 8},
{2, 0, 3, 6},
{4, 3, 0, 1},
{8, 6, 1, 0}};

图3

第三步:现在,我们可以看到每个顶点有超过 1 个不同颜色的边。因此,子集中的顶点总数为 4。

C++
// C++ program to find size of subset of graph vertex
// such that each vertex has more than 1 color edges
#include 
using namespace std;
  
// Number of vertices
const int N = 6;
  
// function to calculate max subset size
int subsetGraph(int C[][N])
{
    // set for number of vertices
    set vertices;
    for (int i = 0; i < N; ++i)
        vertices.insert(i);
  
    // loop for deletion of vertex from set
    while (!vertices.empty())
    {
        // if subset has only 1 vertex return 0
        if (vertices.size() == 1)
            return 1;
  
        // for each vertex iterate and keep removing
        // a vertex while we find a vertex with all
        // edges of same color.
        bool someone_removed = false;
        for (int x : vertices)
        {
            // note down different color values
            // for each vertex
            set values;
            for (int y : vertices)
                if (y != x)
                    values.insert(C[x][y]);
  
            // if only one color is found
            // erase that vertex (bad vertex)
            if (values.size() == 1)
            {
                vertices.erase(x);
                someone_removed = true;
                break;
            }
        }
  
        // If no vertex was removed in the
        // above loop.
        if (!someone_removed)
            break;
    }
  
    return (vertices.size());
}
  
// Driver program
int main()
{
    int C[][N] = {{0, 9, 2, 4, 7, 8},
        {9, 0, 9, 9, 7, 9},
        {2, 9, 0, 3, 7, 6},
        {4, 9, 3, 0, 7, 1},
        {7, 7, 7, 7, 0, 7},
        {8, 9, 6, 1, 7, 0}
    };
    cout << subsetGraph(C);
    return 0;
}


Java
// Java program to find size of 
// subset of graph vertex such that 
// each vertex has more than 1 color edges 
import java.util.*;
  
class GFG 
{
  
    // Number of vertices 
    static int N = 6;
  
    // function to calculate max subset size 
    static int subsetGraph(int C[][])
    {
        // set for number of vertices 
        HashSet vertices = new HashSet<>();
        for (int i = 0; i < N; ++i) 
        {
            vertices.add(i);
        }
  
        // loop for deletion of vertex from set 
        while (!vertices.isEmpty())
        {
              
            // if subset has only 1 vertex return 0 
            if (vertices.size() == 1)
            {
                return 1;
            }
  
            // for each vertex iterate and keep removing 
            // a vertex while we find a vertex with all 
            // edges of same color. 
            boolean someone_removed = false;
            for (int x : vertices) 
            {
                  
                // note down different color values 
                // for each vertex 
                HashSet values = new HashSet<>();
                for (int y : vertices) 
                {
                    if (y != x) 
                    {
                        values.add(C[x][y]);
                    }
                }
  
                // if only one color is found 
                // erase that vertex (bad vertex) 
                if (values.size() == 1) 
                {
                    vertices.remove(x);
                    someone_removed = true;
                    break;
                }
            }
  
            // If no vertex was removed in the 
            // above loop. 
            if (!someone_removed) 
            {
                break;
            }
        }
  
        return (vertices.size());
    }
  
    // Driver code 
    public static void main(String[] args)
    {
        int C[][] = {{0, 9, 2, 4, 7, 8},
        {9, 0, 9, 9, 7, 9},
        {2, 9, 0, 3, 7, 6},
        {4, 9, 3, 0, 7, 1},
        {7, 7, 7, 7, 0, 7},
        {8, 9, 6, 1, 7, 0}
        };
        System.out.println(subsetGraph(C));
    }
} 
  
// This code has been contributed by 29AjayKumar


Python3
# Python3 program to find size of subset 
# of graph vertex such that each vertex
# has more than 1 color edges
  
# function to calculate max subset size 
def subsetGraph(C):
    global N
      
    # set for number of vertices 
    vertices = set()
    for i in range(N):
        vertices.add(i) 
  
    # loop for deletion of vertex from set 
    while (len(vertices) != 0):
          
        # if subset has only 1 vertex return 0 
        if (len(vertices) == 1): 
            return 1
  
        # for each vertex iterate and keep removing 
        # a vertex while we find a vertex with all 
        # edges of same color. 
        someone_removed = False
        for x in vertices:
              
            # note down different color values 
            # for each vertex 
            values = set()
            for y in vertices:
                if (y != x): 
                    values.add(C[x][y]) 
  
            # if only one color is found 
            # erase that vertex (bad vertex) 
            if (len(values) == 1):
                vertices.remove(x) 
                someone_removed = True
                break
  
        # If no vertex was removed in the 
        # above loop. 
        if (not someone_removed): 
            break
  
    return len(vertices)
  
# Driver Code
  
# Number of vertices 
N = 6
C = [[0, 9, 2, 4, 7, 8], 
     [9, 0, 9, 9, 7, 9],
     [2, 9, 0, 3, 7, 6],
     [4, 9, 3, 0, 7, 1],
     [7, 7, 7, 7, 0, 7],
     [8, 9, 6, 1, 7, 0]]
print(subsetGraph(C))
  
# This code is contributed by PranchalK


C#
// C# program to find size of 
// subset of graph vertex such that 
// each vertex has more than 1 color edges 
using System;
using System.Collections.Generic; 
  
class GFG 
{
  
    // Number of vertices 
    static int N = 6;
  
    // function to calculate max subset size 
    static int subsetGraph(int [,]C)
    {
        // set for number of vertices 
        HashSet vertices = new HashSet();
        for (int i = 0; i < N; ++i) 
        {
            vertices.Add(i);
        }
  
        // loop for deletion of vertex from set 
        while (vertices.Count != 0)
        {
              
            // if subset has only 1 vertex return 0 
            if (vertices.Count == 1)
            {
                return 1;
            }
  
            // for each vertex iterate and keep removing 
            // a vertex while we find a vertex with all 
            // edges of same color. 
            Boolean someone_removed = false;
            foreach (int x in vertices) 
            {
                  
                // note down different color values 
                // for each vertex 
                HashSet values = new HashSet();
                foreach (int y in vertices) 
                {
                    if (y != x) 
                    {
                        values.Add(C[x, y]);
                    }
                }
  
                // if only one color is found 
                // erase that vertex (bad vertex) 
                if (values.Count == 1) 
                {
                    vertices.Remove(x);
                    someone_removed = true;
                    break;
                }
            }
  
            // If no vertex was removed in the 
            // above loop. 
            if (!someone_removed) 
            {
                break;
            }
        }
  
        return (vertices.Count);
    }
  
    // Driver code 
    public static void Main(String[] args)
    {
        int [,]C = {{0, 9, 2, 4, 7, 8},
                    {9, 0, 9, 9, 7, 9},
                    {2, 9, 0, 3, 7, 6},
                    {4, 9, 3, 0, 7, 1},
                    {7, 7, 7, 7, 0, 7},
                    {8, 9, 6, 1, 7, 0}};
        Console.WriteLine(subsetGraph(C));
    }
}
  
// This code is contributed by Rajput-Ji


Javascript


输出:

4