Four-color problem, also known as four-color conjecture, four-color theorem, is one of the world's three major mathematical conjecture. The four-color theorem is a well-known mathematical theorem, and the popular saying is that each plane map can be dyed with only four colors, and no two contiguous areas have the same color. 1976 with the help of the computer to prove the four-color problem, the problem has finally become a theorem, this is the first computer-proof theorem. (This text from Baidu Encyclopedia)
This article mainly introduces the use of C language to implement map four-color chart. Mainly includes: The design data structure, the algorithm realization and so on.
1 data structure of the graph
There is no need to represent a specific map polygon, only the Voronoi diagram of the polygon can be represented, and the simple point is visible:
For the map polygon on the left, you only need to represent the right side of the graph in the algorithm. One of the nodes (V1, v2 ... Represents a map polygon, and the line of the node represents a neighboring relationship between two polygons.
Therefore, in the data structure only need to represent the form of a diagram. Here, the data structure of the adjacency table is as follows:
#include <stdio.h> #include <malloc.h> #include <stdlib.h> #define Max_vertex_num 20typedef struct arcnode {int Adjvex;//The position of the vertex to which the edge is pointing struct arcnode *nextarc;//Pointer to the next edge} Arcnode, *arcnlist; typedef struct {float x;float y;} Vertex; Point typedef struct VNODE {VERTEX data;//vertex information arcnlist firstarc;//Pointer to the edge of the first attachment to the vertex int color;//color of the point} Vnode, Adjlist[max_ve Rtex_num];typedef struct{adjlist vertics; int vexnum, arcnum;////The current number of vertices and sides of the graph} udgraph;
2 Algorithm of the graph
The algorithm of coloring uses backtracking method. The basic idea is: first to color a polygon, after shading to determine whether to meet the requirements, if the requirements continue to color other polygons, if not meet the requirements of the current coloring and backtracking, with other shading scheme. Recursion is done until the map is all shaded.
The C language code of the map four-color dye diagram is implemented as follows. It first uses a maximum of 4 colors for a given map, judging whether the map can be shaded with only 4 colors, if it returns true, and vice versa.
BOOL Iscolorok (udgraph G, int node) {//To determine the scheme line coloring for node nodes vnode V = g.vertics[node-1]; Arcnlist T = v.firstarc;while (t) {if (V.color = = G.vertics[t->adjvex].color) return false; T = T->nextarc;} return true;} BOOL Getcolorpowset (udgraph &g, int color_num, int step, int a, int b) {//** Backtracking on graph coloring if (step > G.vexnum) {if (G.verti Cs[a-1].color = = b) {//b-color return true on the a area graph;}} else {int i;for (i=1; i<=color_num; i++) {g.vertics[step-1].color = I;if (Iscolorok (g, step)) {if (Getcolorpowset (g, col Or_num, step + 1, A, b)) return true; G.vertics[step-1].color = 0;}} return false;}
3 Test Run
The code of the test process includes: the process of building the diagram and outputting the result of the coloring. The code for these two procedures is as follows:
int createudgraph (udgraph &g) {int A, b, I; Arcnlist p;printf ("Please enter the number of nodes and the number of sides:"), scanf ("%d,%d", & (G.vexnum), & (G.arcnum)); GetChar (); Elimination of carriage return for (i=0; i<g.vexnum; i++) {//Initialize each node G.vertics[i].firstarc =null; G.vertics[i].color = 0;} printf ("Enter the topological relationship between these%d points: \ n", g.vexnum);//The next step is to create this g.arcnum*2 edge for (i=1; i<=g.arcnum; i++) {scanf ("%d,%d", &a, &B); Indicates the adjacency between point A and point B, connecting the line between point A and point B getchar ();//eliminate carriage return P = (arcnlist) malloc (sizeof (Arcnode)); if (!p) exit ( -2);p->adjvex = B-1;p->nextarc = G.vertics[a-1].firstarc; G.vertics[a-1].firstarc = p;//above is to add a node to point A, and then add a node P = (arcnlist) malloc (sizeof (Arcnode)) on point B; if (!p) exit ( -2);p->adjvex = A-1;p->nextarc = G.vertics[b-1].firstarc; G.vertics[b-1].firstarc = P;} return 1;} void Outputgraph (Udgraph G) {int i;for (i=0; i<g.vexnum; i++) printf ("Node%d coloring scheme:%d\n", i+1, G.vertics[i].color);} int main () {int A, b;//a area graph B-color udgraph g;createudgraph (G);p rintf ("Enter a shading scheme (i.e., which color is in which area):"); scanf ("%d,%d", &a, &B);//b-Color GetChar () on a area map;//Remove carriage return if (Getcolorpowset (g, 4, 1, A, b)) {//If the last color of the image succeeds printf ("The graph color succeeds: \ n"); Outputgraph (g);} else printf ("The graph color fails, scheme is incorrect \ n" );//If the last color of the image is successfully return 1;}
Run the above code and test as follows:
Its graphical meanings are as follows:
Complete the full text. Reprint please indicate the source.
C-language implementation of map four-color chart