Area coloring Problem Coding

Source: Internet
Author: User

Graph coloring problem (graph coloring problem, GCP), also known as coloring problem , is one of the most famous np-complete problems. This is what I want to analyze with you today.

Easy to understand, that is: there is a picture is divided into n areas, give you k color, let you coloring for each area, the final requirement to meet the color of the adjacent area is not the same.

First of all, how should we express this graph with n regions in the programming implementation? (Borrowing one of my previously written assignments, assuming there are 6 regions), we can use a two-dimensional array to store the adjacency between each region 22, as follows: to explain that, by means of an array of a[6][6] to represent this 6*6 's two-dimensional array, then a[3][4] represents the relationship between region 3 and Region 4, If a[3][4]==1, it proves that the two areas are adjacent, otherwise it is not adjacent.

Next, we use code to implement the coloring of these 6 areas. (assuming that 4 different colors are given, you can also use a one-dimensional array such as b[4] to represent ha ~ ~)

1#include <stdio.h>2#include <stdlib.h>3 #defineStackSize 1004 #defineAddsize 105 #defineCOLUMN 66 7typedefstruct8 {9     int*top;Ten     int*Base; One     intstacksize; A }stack; -  - intR[column][column]; the  - voidReadint*p); - voidInitstack (Stack *p);//build an empty stack - BOOLJudgecolor (Stack *p,intMintj);//Color method to determine whether the,j<=3; can be used in the first J colors + voidPush (Stack *p,Charj);//type J color into the stack - voidPop (Stack *p,int*W);//out of the stack + voidDraw (Stack P);//Drawing A  at voidMain () - {     -Read (r[0]); - Stack P; -Initstack (&P); -     intm =0, J; in      while(M <=5) -     { to          for(j =0; J <=3; J + +)//section J Color attempt +         { -             if(Judgecolor (&p,m,j)) the             { *Push (&P, j); $                  Break;Panax Notoginseng             } -         } the          while(j==4)//failed to paint successfully +         { A--m;//return to previous area the             if(M <0)//before the fallback to the No. 0 area, there is no area, that is, you cannot successfully color these 6 areas +             { -printf"NO solution!"); $                 return; $             } -  -             intW//Previous Area color thePop (&p, &w);//Back Stack -              for(j = w +1; J <=3; J + +)Wuyi             { the                 if(Judgecolor (&AMP;P,M,J))//can be on this color -Push (&P, j); Wu             } -         } Aboutm++;//to paint the next area $     } -printf"ok.\n"); -      -      for(intt =0; T <6; t++) A     { +printf"area%d is shaded as%d\n", T, P.Base[t]);//The pointer is removed to indicate the array the     } -      $     return; the } the  the voidReadint*p) the { -FILE * fp = fopen (".. /relation_table.txt","R"); in     if(!fp) exit; the      for(intI=0;i<6; i++) the     { About          for(intj =0; J <6; J + +) the         { theFSCANF (FP,"%d", p++); the         } +     } - fclose (FP); the }Bayi  the voidInitstack (Stack *p)//build an empty stack the { -P->Base= (int*)malloc(sizeof(int)*StackSize); -     if(!p->Base) exit; theP->top = p->Base; theP->stacksize =StackSize; the     return; the } -  the voidPush (Stack *p,CharJ//type J color into the stack the { the     if(p->top-p->Base= = p->stacksize)//Stack Full94     { theP->Base= (int*)realloc(p->Base, (p->stacksize + addsize) *sizeof(int)); the         if(!p->Base) exit; theP->top = p->Base+ p->stacksize;98P->stacksize + =addsize; About     } -* (p->top++) =J;101     return;102 }103 104 voidPop (Stack *p,int*W)//out of the stack the {106     if(p->Base= = P->top) exit;107*w = * (--p->top);108     return;109 } the 111 BOOLJudgecolor (Stack * P,intMintJ//Determine if the section m can be used in the color of section J the {    113      for(intI=0; I<m&&m>0; i++) the     { the         if(R[i][m] = =1)//section I area adjacent to M area the         {117             if(* (p->Base+ i) = = j)//The J color was used.118                 return false;119         } -     }121     return true;122}


Let's take a look at the final result, the time to witness the miracle!

Of course, you can also modify the above parameters, to solve more areas more color coloring problem!

Area coloring Problem Coding

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.