📜  为图形着色所需的最少颜色数

📅  最后修改于: 2021-10-25 04:47:40             🧑  作者: Mango

给定一个有N个顶点和E 个边的图。边由U[]V[] 给出,使得对于每个索引 i, U[i]连接到V[i] 。任务是找到为给定图形着色所需的最少颜色数。
例子

方法:
我们将保留两个数组count[]colors[] 。数组count[]将存储每个节点的边数, colors[]将存储每个节点的颜色。将每个顶点的计数初始化为 0,将每个顶点的颜色初始化为 1。
脚步:

  1. 为给定的边集创建一个邻接表,并保留每个节点的边数
  2. 遍历所有节点并将该节点插入到没有边的队列Q中。
  3. Q不为空时,请执行以下操作:
    • 从队列中弹出元素。
    • 对于连接到弹出节点的所有节点:
      1. 减少弹出节点的当前边数。
      2. 如果当前的颜色小于或等于其父节点的颜色(弹出的节点),则更新当前节点的颜色 = 1 + 弹出节点的颜色
      3. 如果 count 为零,则将此节点插入队列Q 中
  4. color []数组中的最大元素将给出为给定图形着色所需的最小颜色数。

下面是上述方法的实现:

C++
// C++ program to find the minimum
// number of colors needed to color
// the graph
#include 
using namespace std;
 
// Function to count the minimum
// number of color required
void minimumColors(int N, int E,
                   int U[], int V[])
{
 
    // Create array of vectors
    // to make adjacency list
    vector adj[N];
 
    // Initialise colors array to 1
    // and count array to 0
    vector count(N, 0);
    vector colors(N, 1);
 
    // Create adjacency list of
    // a graph
    for (int i = 0; i < N; i++) {
        adj[V[i] - 1].push_back(U[i] - 1);
        count[U[i] - 1]++;
    }
 
    // Declare queue Q
    queue Q;
 
    // Traverse count[] and insert
    // in Q if count[i] = 0;
    for (int i = 0; i < N; i++) {
        if (count[i] == 0) {
            Q.push(i);
        }
    }
 
    // Traverse queue and update
    // the count of colors
    // adjacent node
    while (!Q.empty()) {
        int u = Q.front();
        Q.pop();
 
        // Traverse node u
        for (auto x : adj[u]) {
            count[x]--;
 
            // If count[x] = 0
            // insert in Q
            if (count[x] == 0) {
                Q.push(x);
            }
 
            // If colors of child
            // node is less than
            // parent node, update
            // the count by 1
            if (colors[x] <= colors[u]) {
                colors[x] = 1 + colors[u];
            }
        }
    }
 
    // Stores the minimumColors
    // requires to color the graph.
    int minColor = -1;
 
    // Find the maximum of colors[]
    for (int i = 0; i < N; i++) {
        minColor = max(minColor, colors[i]);
    }
 
    // Print the minimum no. of
    // colors required.
    cout << minColor << endl;
}
 
// Driver function
int main()
{
    int N = 5, E = 6;
    int U[] = { 1, 2, 3, 1, 2, 3 };
    int V[] = { 3, 3, 4, 4, 5, 5 };
 
    minimumColors(N, E, U, V);
    return 0;
}


Java
// Java program to find the minimum
// number of colors needed to color
// the graph
import java.util.*;
 
class GFG
{
 
// Function to count the minimum
// number of color required
static void minimumColors(int N, int E,
                int U[], int V[])
{
 
    // Create array of vectors
    // to make adjacency list
    Vector []adj = new Vector[N];
 
    // Initialise colors array to 1
    // and count array to 0
    int []count = new int[N];
    int []colors = new int[N];
    for (int i = 0; i < N; i++) {
        adj[i] = new Vector();
        colors[i] = 1;
    }
     
    // Create adjacency list of
    // a graph
    for (int i = 0; i < N; i++) {
        adj[V[i] - 1].add(U[i] - 1);
        count[U[i] - 1]++;
    }
 
    // Declare queue Q
    Queue Q = new LinkedList<>();
 
    // Traverse count[] and insert
    // in Q if count[i] = 0;
    for (int i = 0; i < N; i++) {
        if (count[i] == 0) {
            Q.add(i);
        }
    }
 
    // Traverse queue and update
    // the count of colors
    // adjacent node
    while (!Q.isEmpty()) {
        int u = Q.peek();
        Q.remove();
 
        // Traverse node u
        for (int x : adj[u]) {
            count[x]--;
 
            // If count[x] = 0
            // insert in Q
            if (count[x] == 0) {
                Q.add(x);
            }
 
            // If colors of child
            // node is less than
            // parent node, update
            // the count by 1
            if (colors[x] <= colors[u]) {
                colors[x] = 1 + colors[u];
            }
        }
    }
 
    // Stores the minimumColors
    // requires to color the graph.
    int minColor = -1;
 
    // Find the maximum of colors[]
    for (int i = 0; i < N; i++) {
        minColor = Math.max(minColor, colors[i]);
    }
 
    // Print the minimum no. of
    // colors required.
    System.out.print(minColor +"\n");
}
 
// Driver function
public static void main(String[] args)
{
    int N = 5, E = 6;
    int U[] = { 1, 2, 3, 1, 2, 3 };
    int V[] = { 3, 3, 4, 4, 5, 5 };
 
    minimumColors(N, E, U, V);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program to find the minimum
# number of colors needed to color
# the graph
from collections import deque
 
# Function to count the minimum
# number of color required
def minimumColors(N, E, U, V):
 
    # Create array of vectors
    # to make adjacency list
    adj = [[] for i in range(N)]
 
    # Initialise colors array to 1
    # and count array to 0
    count = [0]*N
    colors = [1]*(N)
 
    # Create adjacency list of
    # a graph
    for i in range(N):
        adj[V[i] - 1].append(U[i] - 1)
        count[U[i] - 1] += 1
 
    # Declare queue Q
    Q = deque()
 
    # Traverse count[] and insert
    # in Q if count[i] = 0
    for i in range(N):
        if (count[i] == 0):
            Q.append(i)
 
    # Traverse queue and update
    # the count of colors
    # adjacent node
    while len(Q) > 0:
        u = Q.popleft()
 
        # Traverse node u
        for x in adj[u]:
            count[x] -= 1
 
            # If count[x] = 0
            # insert in Q
            if (count[x] == 0):
                Q.append(x)
 
            # If colors of child
            # node is less than
            # parent node, update
            # the count by 1
            if (colors[x] <= colors[u]):
                colors[x] = 1 + colors[u]
 
    # Stores the minimumColors
    # requires to color the graph.
    minColor = -1
 
    # Find the maximum of colors[]
    for i in range(N):
        minColor = max(minColor, colors[i])
 
    # Print the minimum no. of
    # colors required.
    print(minColor)
 
# Driver function
N = 5
E = 6
U = [1, 2, 3, 1, 2, 3]
V = [3, 3, 4, 4, 5, 5]
 
minimumColors(N, E, U, V)
 
# This code is contributed by mohit kumar 29


C#
// C# program to find the minimum
// number of colors needed to color
// the graph
using System;
using System.Collections.Generic;
 
class GFG
{
  
// Function to count the minimum
// number of color required
static void minimumColors(int N, int E,
                int []U, int []V)
{
  
    // Create array of vectors
    // to make adjacency list
    List []adj = new List[N];
  
    // Initialise colors array to 1
    // and count array to 0
    int []count = new int[N];
    int []colors = new int[N];
    for (int i = 0; i < N; i++) {
        adj[i] = new List();
        colors[i] = 1;
    }
      
    // Create adjacency list of
    // a graph
    for (int i = 0; i < N; i++) {
        adj[V[i] - 1].Add(U[i] - 1);
        count[U[i] - 1]++;
    }
  
    // Declare queue Q
    List Q = new List();
  
    // Traverse []count and insert
    // in Q if count[i] = 0;
    for (int i = 0; i < N; i++) {
        if (count[i] == 0) {
            Q.Add(i);
        }
    }
  
    // Traverse queue and update
    // the count of colors
    // adjacent node
    while (Q.Count!=0) {
        int u = Q[0];
        Q.RemoveAt(0);
  
        // Traverse node u
        foreach (int x in adj[u]) {
            count[x]--;
  
            // If count[x] = 0
            // insert in Q
            if (count[x] == 0) {
                Q.Add(x);
            }
  
            // If colors of child
            // node is less than
            // parent node, update
            // the count by 1
            if (colors[x] <= colors[u]) {
                colors[x] = 1 + colors[u];
            }
        }
    }
  
    // Stores the minimumColors
    // requires to color the graph.
    int minColor = -1;
  
    // Find the maximum of colors[]
    for (int i = 0; i < N; i++) {
        minColor = Math.Max(minColor, colors[i]);
    }
  
    // Print the minimum no. of
    // colors required.
    Console.Write(minColor +"\n");
}
  
// Driver function
public static void Main(String[] args)
{
    int N = 5, E = 6;
    int []U = { 1, 2, 3, 1, 2, 3 };
    int []V = { 3, 3, 4, 4, 5, 5 };
  
    minimumColors(N, E, U, V);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
3

时间复杂度: O(N+E),其中 N = 顶点数,E = 边数

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程