You are on page 1of 18

Hamiltonian Cycle

A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

Page

Hamiltonian Graph
A Hamiltonian cycle is a cycle that passes through all the vertices of the graph. A graph is called a Hamiltonian graph if it includes at least one Hamiltonian cycle.

There are two stages in the Hamiltonian Cycle algorithm


First, the graph is expanded to a complete graph to find the cycle easily. Then

Manipulate this cycle by adding some edges and removing other edges so eventually a Hamiltonian cycle is formed that includes the edges that belong to the original graph
A.R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 2

HamiltonianCycle (graph G = (V, E)) Set label of all edges to 0; K = 1; H = E; // set of edges GH = G; // original graph // Phase 1 While GH contains nonadjacent vertices v with u such that degH(v) + degH(u) >= |V| then H = H U {edges(vu)} GH = (V, H) Label(edge(vu)) = k++; // Phase 2 If there exists a Hamiltonian cycle C k=max {label(edge(pq)) such that edge(pq) is in C} While (k > 0) C = a cycle due to a crossover with edge labeled by a number < k; k=max {label(edge(pq)) such that edge(pq) is in cycle C}

A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

Page

Consider the following graph. We want to find the Hamiltonian cycle in this graph We start with phase 1 where we turn the graph into a complete graph with every vertex is connected to another vertex in the graph
a b c

d f

A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

Page

We start with vertex a. Note that the total number of vertices in the graph is |V| = 6
The nonadjacent vertices to a are c and e

ac can be added because deg(a) + deg(c) = 3 + 3 = 6 which is >= |V|


After adding ac, the deg(a) becomes 4 and ae is also added because deg(a) + deg(e) = 4 + 2 = 6 which is >=6
a b d f c e

a
1 b d f
California State University San Marcos (CSUSM)

1 c
e b 2 d f
Page 5

c e

A.R. Hadaegh Dr. Ahmad R. Hadaegh

The nonadjacent vertices to b are d, e, and f


bd cannot be added because deg(b) + deg(d) = 2 + 3 = 5 which is < 6

be cannot be added because deg(b) + deg(e) = 2 + 3 = 5 which is < 6


bf cannot be added because deg(b) + deg(f) = 2 + 3 = 5 which is < 6
a

1
b 2 d f e c

A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

Page

The nonadjacent vertices to c is d


cd can be added because deg(c) + deg(d) = 4 + 3 = 7 which is > 6

a 1

a 1 c 2 e b d 3 2 e f c

b
d f

A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

Page

The nonadjacent vertices to d is b


db can be added because deg(d) + deg(b) = 4 + 2 = 6 which is >= 6

a
1 b d 3 2 e f

a 1

b 4 d

c 3 2 e f

A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

Page

The nonadjacent vertices to e are b and f


eb can be added because deg(e) + deg(b) = 3 + 3 = 6 which is >= 6

After adding eb, the deg(e) becomes 4 and ef is also added because deg(e) + deg(f) = 4 + 3 = 7 which is > 6

1
b 4 d c 3 2 e f b 5 4 d

1
c b 5 4 d

1 c 3 2 e

2 e f

A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

Page

The nonadjacent vertices to f is b


fb can be added because deg(f) + deg(b) = 4 + 4 = 8 which is > 6

a 1 b 5 4 c 3 6 2 b 5 4

a 1 c 3 7 f 6 2

e
f

A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

Page 10

Now the second phase starts. A cycle is created and K becomes the edge with the maximum value We order the vertices in the cycle such that the vertices attached to edge K are on the extreme ends For the following cycle, the vertices are a, c, e, f, d, b

K = ef
Thus we order the vertices of the cycle as follows: fdbace
a 1 b 5 4 d 7 f
A.R. Hadaegh Dr. Ahmad R. Hadaegh

c
3 6
Page 11

2 e

California State University San Marcos (CSUSM)

Now we find cross over edges and compare their value with the highest value of current cycle.
Two edge cross over each other if they each have a vertex that is the end point of the current cycle The first cross over edges are fb and de: fdbace This is not chosen because the weight of fb is 7 which is > 6
a 1 b 5 4 d 7 f
A.R. Hadaegh Dr. Ahmad R. Hadaegh

c
3 6
Page 12

2
e

California State University San Marcos (CSUSM)

The next cross over edges are fa and be: fdbace


This is selected because be has weight 5 which is less that 6 Thus fa and be are added and ab and ef are removed The edge with highest weight is now be = 5 The new cycle is: bdface

a 1 b 5 4 c b 5 4 d 7 f
A.R. Hadaegh Dr. Ahmad R. Hadaegh

a 1 c 3 6

3
7 6

2
e f

California State University San Marcos (CSUSM)

Page 13

We start to cross over the edges again in the new cycle bdface
The first two edges are bf and de This combination is not chosen because the weight of bf is 7 which is bigger than 5, the maximum weight

a 1 b 5 4 c

3
7 f 6

A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

Page 14

The next two edges are ba and fe: bdface


Again, this combination is not chosen because the weight of fe is 6 which is bigger than 5, the maximum weight

a 1 b 5 4 c

3
7 f 6

A.R. Hadaegh Dr. Ahmad R. Hadaegh

California State University San Marcos (CSUSM)

Page 15

The next two edges are bc and ae: bdface This combination is picked because the weight of ae is 2 which is less than 5, the maximum weight of the cycle Thus bc and ae are added and be and ac are removed Now the maximum weigh of the new cycle is db = 4 The new cycle is bceafd
a 1 b 5 4 d 7 f
A.R. Hadaegh Dr. Ahmad R. Hadaegh

a 1

c
3 6 2 e

b 4

c 5 3 7 f 6
Page 16

2 e

California State University San Marcos (CSUSM)

The next two edges are be and cd: bceafd This combination is not picked because the weight of be is 5 which is not less than 4 The next two edges are ba and ed: bce afd This combination is picked because both edges belong to original graph and their weight is zero Thus ba and ed are added and bd and ea are removed The new cycle is abcedf
a 1 b 4 d 7 c 5 3 6 2 e b 4 d 7 f
California State University San Marcos (CSUSM)

a 1 c 5 3 6
Page 17

2
e

f
A.R. Hadaegh Dr. Ahmad R. Hadaegh

Since the maximum weight of the new cycle is 0, it means all the edges belong to the original graph. Thus we are done.
a 1 b 4 c 5 3 7 f 6

2 e

Thus the Hamiltonian cycle for our original graph is:


a b d f
A.R. Hadaegh Dr. Ahmad R. Hadaegh California State University San Marcos (CSUSM) Page 18

c
e

You might also like