Color of the image (that is, the minimum color problem. The time complexity is N ^ 2)

Source: Internet
Author: User

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 ");

}

 


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.