an undirected connected graph The Hamilton (Hamiltion) loop on the G-spot refers to the path from a vertex on figure g, once and only once, after all other vertices on the graph, and finally back to the vertex. A basis for solving the Hamilton Loop algorithm on an unconstrained graph is as follows:
hypothetical Diagram G There is a Hamilton loop starting from vertex V0 v1--v2--v3 ——...—— vn-1--v0. The algorithm embarks from Vertex V0, accesses an inaccessible adjacency vertex V1 of the vertex, and then proceeds from the vertex V1, accesses V1 an inaccessible adjacency vertex V2, ... For Vertex VI, repeat the following: access to an inaccessible adjacency junction of VI Vi+1; if all adjacency vertices of VI have been accessed, then return to vertex Vi-1, considering the next unreachable vertex of Vi-1, still recorded as VI Knowing that a Hamilton loop is found, or the Hamilton loop is not found, the algorithm ends.
" C Code "
The following is the algorithm's C language implementation.
(1) Constants and variable descriptions
N: Number of vertices in Figure g
C[][]: adjacency matrix of Figure G
K: Statistical variable, the number of fixed-point numbers that have been visited in the current period is k+1
X[K]: The vertex number of the K-access, starting from 0
VISITED[X[K]]: The access flag of the K-vertex, 0 means no access, and 1 means that the access
(2) C program
1#include <stido.h>2#include <stidb.h>3 #defineMAX 1004 5void Hamilton (intNintX[max,intC[max][max]) {6 inchT;7 inchT Visited[max];8 intK;9 /*Initializes an array of x arrays of visited*/Ten For (I=0: i<n;i++){ Onex[i]=0; Avisited [i]=0; - } - /*accessing the starting vertex*/ thek=0; - (visited[0]=1); -x[0]=0; -k=k+1; + /*accessing other vertices*/ - While (k>=0) { +x[k]=x[k]+1; A while(x[k]><N) { at if(visited[x[k]]==0) &&c[x-[k-1]][x[k]=1){/*adjacency Vertex x[k] has not been accessed*/ - Break; -}Else{ -X[K] = X[k] +1; - } - } in if(X[k] <n-1&& (visited[x[k]]==1){/*Find a Hamilton loop*/ - for(k=0; k<n;k++){ toPrinf (〝%d--〝,x[k];/*output Hamilton Circuit*/ + } -Prinf (〝%d--〝,x[0] ; the return; *}Else ifx[k]<n&&k<n-1){/*sets the access flag for the current vertex, continuing the next vertex*/ $(visited[x[k]]=1);Panax Notoginsengk=k+1; -}Else{/*adjacency Vertex not visited, fallback to previous vertex*/ thex[k]=0; +Visited x[k]=0; A(k=k-1); the } + } -}
"Question 1" (10 points)
according to the description of the problem. Fill in the C code with empty (1) ~ (5).
Answer: The code is labeled Red is the answer!
"Question 2" (5 points)
according to the description of the problem and C code, the algorithm adopts the design Strategy (6), the method when traversing the vertex of the graph, using the (7) method (depth first or breadth first).
Answer:
6: Backtracking method
7: Depth First
C Language Program Questions-01