In a "dense" graph, I am trying to construct a Hamiltonian cycle using Palmer's Algorithm. However, I need more explanation for this algorithm because it does not work with me when I implement it. It seems that there is an unclear part in Wikipedia's explanation.
I would be thankful if someone explains it more clearly or give me some links to read.
Here's the algorithm statement:
Palmer (1997) describes the following simple algorithm for constructing a Hamiltonian cycle in a graph meeting Ore's condition. Arrange the vertices arbitrarily into a cycle, ignoring adjacencies in the graph. While the cycle contains two consecutive vertices
vi
andvi + 1
that are not adjacent in the graph, perform the following two steps:
Search for an index
j
such that the four verticesvi
,vi + 1
,vj
, andvj + 1
are all distinct and such that the graph contains edges fromvi
tovj + 1
and fromvj
tovi + 1
Reverse the part of the cycle between
vi + 1
andvj
(inclusive).
To be more specific, I do not get the part where they say: "Arrange the vertices arbitrarily into a cycle" in this case, is this right to do: 0,1,2,3,4,0
and what do they mean by: "Reverse the part of the cycle"?
Indeed, wikipedia's description of the algorithm
is¹was wrong. Palmer's own description isYou need a pair of crossing chords, i.e. you need edges
That way, by reversing the part from
v_{i+1}
tov_j
(inclusive), you move the vertexv_j
- adjacent tov_i
in the graph - next tov_i
in your cycle, and the vertexv_{i+1}
- adjacent tov_{j+1}
in the graph - is moved next tov_{j+1}
in the cycle. Thus we obtain two new pairs of neighbours in the cycle that are adjacent in the graph,(v_i, v_j)
and(v_{i+1}, v_{j+1})
, and possibly destroy one pair of cycle-neighbours that are adjacent in the graph,(v_j, v_{j+1})
. The number of pairs of cycle-neighbours that are adjacent in the graph increases by 1 or by two each step, so the algorithm terminates.With the wrong indexing of wikipedia, moving
v_j
next tov_i
andv_{i+1}
next tov_{j+1}
need not generate a new pair of cycle-neighbours that are adjacent in the graph, thus the algorithm need not terminate.So let's play it through for your example
arranging it as
1426351
initially (no adjacent neighbours).The first pair of cycle-neighbours not adjacent in the graph is
(1,4) = (v_1,v_2)
. Scan for an indexj > 2
such thatv_j
is adjacent tov_1
andv_{j+1}
tov_2
, the first such occurrence isj = 3
. Now reverse the part4...2
in the cycle (in this case, there's no vertex between 4 and 2), giving the next cyclewith two pairs of adjacent neighours (
(1,2)
and(4,6)
). The first indexi
withv_i
not adjacent tov_{i+1}
is 2. Scan for the firstj > 3
such thatv_j
is adjacent tov_2 = 2
andv_{j+1}
adjacent tov_3 = 4
. That givesj = 5
. Now the part betweenv_3
andv_5
(inclusive), giving the next cycleOnce more,
v_3 = 3
is not adjacent tov_4 = 6
, soi = 3
,j = 5
, reversing yieldsNow the only bad pair is
(v_6,v_1) = (5,1)
. The smallestj > 1
such thatv_j
is adjacent tov_6 = 5
andv_{j+1}
tov_1 = 1
isj = 2
. Now reverse the part fromv_1
tov_2
yieldingwhich is a Hamiltonian cycle.
¹ I'll fix it in a moment.
Yes. You might get a faster solution by starting from a more carefully chosen initial cycle however the algorithm will succeed starting from any valid initial cycle provided that the graph meets Ore's condition.
It means take the path from vi + 1 to vj and reverse it so that if you started with:
you end up with:
so that in your example if we choose i = 0 and j = 3, the end result will be:
Here is a link to Palmer's paper (see the reference section in Wikipedia).