# Hamiltonian problem and Hamiltonian ring

Keywords: Algorithm data structure

# Hamiltonian problem and Hamiltonian ring

In 1859, the Irish mathematician Hamilton proposed the following game to travel around the world: London, Paris, Moscow and other world-famous cities were marked on the 20 vertices of the regular dodecahedron, and the edges of the regular dodecahedron represent the routes connecting these cities. Can you take a trip from the top to the top, walk along the edge, pass through each city exactly once, and then return to the starting point.

## 1. Problem overview

The gunnysburg seven bridge problem is to find a simple path traversing all edges in the graph, while Hamilton's traveling around the world problem is to find a basic path traversing all points in the graph. In an undirected graph G = < V, E >, the path that traverses each vertex in g once and only once is called a Hamiltonian path, and the loop that traverses each vertex in g once and only once is called a Hamiltonian cycle. A graph with a Hamiltonian loop is called a Hamiltonian graph (English: Hamiltonian graph, or Traceable graph).

## 2. Algorithm idea

### 2.1 necessary and sufficient conditions

1. Necessary and non sufficient conditions: w (g − s) < = ∣ s ∣ w (G-S) < = | s | w (g − s) < = ∣ s ∣, where ∣ s | s ∣ s ∣ is the number of vertices in S SS, and w (g − s) w (G-S) w (g − s) represents the number of connected subgraphs of the graph obtained by G GG deleting the vertex set S SS.
2. Sufficient and nonessential conditions: if the graph G = < V, E > G = < V, E > G = < V, E > is a simple undirected graph with n ≥ 3 n\geq3n ≥ 3 vertices, and the degree sum of each pair of vertices in the graph G GG is not less than n nn, then there must be a Hamiltonian loop in the graph G GG.

### 2.2 P and NP problems

1.P problem:
In computational theory, we define class P as a language class that can be determined by a certain shape single band Turing machine in polynomial time, that is, Π k t I m e (n k) \ prod_ {k} Time (n ^ k) k Π TIME(nk)P roughly corresponds to the problem class that can be solved on the computer.
2.NP problem:
Correspondingly, there is also N P NPNP class. N P NPNP class is A language class with polynomial time verifier, in which the verifier is defined as follows: the verifier of language A is an algorithm V VV, where A = {w ∣ for A string c,V accepts < W, C >} A = {w | for A character string c,V accepts < W, C >} A = {w ∣ for A string c,V accepts < W, C >}
3. Polynomial time verifiability:
Because the time of the verifier is measured only according to the length of w, the polynomial time verifier runs in polynomial time of the length of w. If language AA has a polynomial time verifier, we call it polynomial time verifiable.
4. Membership and membership certificates:
The verifier uses additional information (i.e., the symbol c cc in the above definition) to verify that the string w ww is a member of AA. This information is called A AA membership certificate. Or certificate. Note that for the polynomial verifier, the certificate has the length of the polynomial (the length of w ww), because this is the length of all information that the verifier can access within its time limit.
5.N P means uncertain polynomial time, which is also a feature of using uncertain polynomial time Turing machine. A very important theorem is that a language is in N P NP if and only if it can be determined by an uncertain polynomial time Turing machine.

### 2.3 judgment method

2.3.1 basic necessary conditions
G = < V, E > G = < V, E > G = < V, E > G = < V, E > G = < V, E > G = < v = < V, E > is a hamiltongraph, then for any noneempty subset S SS of V VVV, V VVV, V, V, V, V, V, V, V, V, V, V, V, V, V, g = < V, V, e, g = < V, E > G > G > G = < V, g = < V, g = < V, G, G, G, G, SG − s, s, G, G, G, G, gggggggggggggggggggggggggggggg is a subgrgraph obtained after deleting the points in S and the edges associated with the points in S and the edges associated with these points, then w (G-S) is a subgrgraph obtained after deleting the points in s, then w (g − − − − − s) deleted the points in S and where w (g − s) w (G-S) w (g − s) is the number of Unicom branches in G − s and g-sg − s.
2.3.2 Dirac theorem (sufficient condition)
Let an undirected graph have n vertices. If the degree of all vertices is greater than or equal to N/2, the Hamiltonian circuit must exist. (N/2 refers to ⌈ N/2 ⌉, rounded up)
2.3.3 race chart (Hamiltonian path)
There is a Hamiltonian path at one point in a race graph of order n (n > = 2)

## 3. Specific steps of the algorithm

void Hamilton(int ans[maxN + 7], bool map[maxN + 7][maxN + 7], int n){
Init();
bool visit[maxN + 7] = {false};
for(i = 1; i <= n; i++) if(map[s][i]) break;
t = i;//Take any point adjacent to s as t
visit[s] = visit[t] = true;
ans[0] = s;
ans[1] = t;
while(true){
while(true){//Expand outward from t
for(i = 1; i <= n; i++){
if(map[t][i] && !visit[i]){
ans[ansi++] = i;
visit[i] = true;
t = i;
break;
}
}
if(i > n) break;
}
//Invert the current sequence, exchange s and T, and continue to expand from t, which is equivalent to expanding outward from s on the original sequence
Hreverse(ansi, ans, i, w)
temp = s;
s = t;
t = temp;
while(true){//Continuing to expand outward from the new t is equivalent to expanding outward from s on the original sequence
for(i = 1; i <= n; i++){
if(map[t][i] && !visit[i]){
ans[ansi++] = i;
visit[i] = true;
t = i;
break;
}
}
if(i > n) break;
}
if(!map[s][t]){//If s and t are not adjacent, adjust
for(i = 1; i < ansi - 2; i++)//Take a point I in the sequence so that ans[i] is connected to t, and ans[i+1] is connected to s
if(map[ans[i]][t] && map[s][ans[i + 1]])break;
w = ansi - 1;
i++;
t = ans[i];
reverse(ans, i, w);//Invert ans [] from ans[i + 1] to part t
}//Now s and t are connected
if(ansi == n) return;//If the current sequence contains n elements, the algorithm ends
for(j = 1; j <= n; j++){//If the number of elements in the current sequence is less than n, find the point ans[i], so that ans[i] is connected to a point other than ans []
if(visit[j]) continue;
for(i = 1; i < ansi - 2; i++)if(map[ans[i]][j])break;
if(map[ans[i]][j]) break;
}
s = ans[i - 1];
t = j;//Assign the newly found point j to t
reverse(ans, 0, i - 1);//Invert the part from s to ans[i-1] in ans []
reverse(ans, i, ansi - 1);//Invert the part from ans[i] to t in ans []
ans[ansi++] = j;//Add point j to ans [] tail
visit[j] = true;
}


## 4. Performance analysis

Time complexity
1.Dirac theorem: if a round is calculated at step 5 every time, since at least one node in each round is added to the path S → T S \rightarrow TS → T, the total number of rounds must not exceed n rounds, so the time complexity is O (N 2) O (n ^ 2) O (N2). Due to the very large number of edges in space, it is more suitable to use adjacency matrix for storage

Posted by jacinthe on Wed, 17 Nov 2021 07:29:56 -0800