📜  图的边缘着色

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

在图论中,图的边缘着色是对图的边缘的“颜色”分配,因此,没有两个相邻的边具有相同的颜色和最佳的颜色数。如果两个边连接到相同的顶点,则将它们称为相邻边。尚无已知的多项式时间算法可对具有最佳颜色数的每个图形进行边缘着色。但是,已经开发出许多算法来放松这些条件中的一个或多个条件,它们仅适用于图形的子集,或者它们并不总是使用最佳数量的颜色,或者它们并不总是在多项式时间内运行。

例子

Input : u1 = 1, v1 = 4 
        u2 = 1, v2 = 2
        u3 = 2, v3 = 3
        u4 = 3, v4 = 4
Output : Edge 1 is of color 1
         Edge 2 is of color 2
         Edge 3 is of color 1
         Edge 4 is of color 2

The above input shows the pair of vertices(ui, vi)
who have an edge between them. The output shows the color 
assigned to the respective edges.

边缘着色是几种不同类型的图形着色问题之一。图表的上图显示了绿色和黑色为图表的边缘着色,其中没有相邻的边缘具有相同的颜色。

以下是一种解决可能无法使用最佳颜色数量的边缘着色问题的算法:
算法:

  1. 使用BFS遍历开始遍历图形。
  2. 拾取任意一个顶点,并为其连接的所有边缘赋予不同的颜色,并将这些边缘标记为有色。
  3. 遍历其边缘之一。
  4. 重复步骤,获得一个新的顶点,直到所有边缘都被着色为止。

下面是上述方法的实现:

// C++ program to illustrate Edge Coloring
#include 
using namespace std;
  
// function to determine the edge colors
void colorEdges(int ptr, vector > >& gra,
                vector& edgeColors, bool isVisited[])
{
    queue q;
    int c = 0;
  
    set colored;
  
    // return if isVisited[ptr] is true
    if (isVisited[ptr])
        return;
  
    // Mark the current node visited
    isVisited[ptr] = 1;
  
    // Traverse all edges of current vertex
    for (int i = 0; i < gra[ptr].size(); i++) {
        // if already colored, insert it into the set
        if (edgeColors[gra[ptr][i].second] != -1)
            colored.insert(edgeColors[gra[ptr][i].second]);
    }
  
    for (int i = 0; i < gra[ptr].size(); i++) {
        // if not visited, inset into the queue
        if (!isVisited[gra[ptr][i].first])
            q.push(gra[ptr][i].first);
  
        if (edgeColors[gra[ptr][i].second] == -1) {
            // if col vector -> negative
            while (colored.find(c) != colored.end())
  
                // increment the color
                c++;
  
            // copy it in the vector
            edgeColors[gra[ptr][i].second] = c;
  
            // then add it to the set
            colored.insert(c);
            c++;
        }
    }
  
    // while queue's not empty
    while (!q.empty()) {
        int temp = q.front();
        q.pop();
  
        colorEdges(temp, gra, edgeColors, isVisited);
    }
  
    return;
}
  
// Driver Function
int main()
{
    set empty;
  
    // declaring vector of vector of pairs, to define Graph
    vector > > gra;
  
    vector edgeColors;
  
    bool isVisited[100000] = { 0 };
  
    // Enter the Number of Vertices
    // and the number of edges
    int ver = 4;
    int edge = 4;
  
    gra.resize(ver);
    edgeColors.resize(edge, -1);
  
    // Enter edge & vertices of edge
    // x--; y--;
    // Since graph is undirected, push both pairs
    // (x, y) and (y, x)
    // graph[x].push_back(make_pair(y, i));
    // graph[y].push_back(make_pair(x, i));
    gra[0].push_back(make_pair(1, 0));
    gra[1].push_back(make_pair(0, 0));
  
    gra[1].push_back(make_pair(2, 1));
    gra[2].push_back(make_pair(1, 1));
  
    gra[2].push_back(make_pair(3, 2));
    gra[3].push_back(make_pair(2, 2));
  
    gra[0].push_back(make_pair(3, 3));
    gra[3].push_back(make_pair(0, 3));
  
    colorEdges(0, gra, edgeColors, isVisited);
  
    // printing all the edge colors
    for (int i = 0; i < edge; i++)
        cout << "Edge " << i + 1 << " is of color "
             << edgeColors[i] + 1 << "\n";
  
    return 0;
}
输出:
Edge 1 is of color 1
Edge 2 is of color 2
Edge 3 is of color 1
Edge 4 is of color 2

参考: https://en.wikipedia.org/wiki/Edge_coloring