The problem arises from the timing of a presentation. There are several departments that want to hold the presentation. Several students are interested in multiple departments and hope to provide a time scheme, all students are required to attend all the presentations they are interested in, and end the presentation in the shortest time.
Each presentation serves as a vertex. The presentation sessions that each student is interested in are connected to each other and become the least coloring problem of a graph.
The problem of graph coloring can be completed within the polynomial time (why is there no polynomial algorithm in some books? It is strange, it can be done clearly), the color number of the image, that is, the presentation time number of the presentation, the maximum color number is the longest time required.
Algorithm idea: perform in-depth traversal and set the color of the vertex during the traversal process. The rule is to find all adjacent vertices and select the color with the smallest number that has not been used, use this color as the color of the current node.
Algorithm correctness: The Algorithm Execution process ensures that each vertex is different from other adjacent vertex colors and that the minimum number of colors is used, which is obviously correct.
# Include <iostream>
# Define Max 15
Using namespace std;
Int vertexCount = 0;
Int color [Max] = {0 };
Int arc [Max] [Max] = {0 };
Int visited [Max] = {0 };
Void init ()
{
Cout <"Enter the number of specified points: \ n ";
Cin> vertexCount;
Int t;
Cout <"Enter the number of edges: \ n ";
Cin> t;
For (int I = 0; I! = T; ++ I)
{
Cout <"Enter the" <I + 1 <"Edge: \ n ";
Int a, B;
Cin> a> B;
The arc [A-1] [b-1] = 1;
The arc [b-1] [A-1] = 1;
}
}
Void DFSTraverse (int s)
{
If (visited [s]) return;
Int t = 1;
Bool flag;
Do {
Flag = false;
For (int I = 0; I! = VertexCount; ++ I)
{
If (arc [s] [I] & color [I] = t)
{
Flag = true;
T ++;
Break;
}
}
} While (flag );
Color [s] = t;
Visited [s] = 1;
For (int I = 0; I! = VertexCount; ++ I)
{
If (arc [s] [I] & visited [I] = 0)
DFSTraverse (I );
}
}
Void show ()
{
For (int I = 0; I! = VertexCount; ++ I)
Cout <"vertex" <I + 1 <"color" <color [I] <endl;
}
Void main ()
{
Init ();
For (int I = 0; I! = VertexCount; ++ I)
DFSTraverse (I );
Show ();
: System ("pause ");
}