0% found this document useful (0 votes)
60 views54 pages

Tours in Graph

The document discusses graph algorithms for finding special paths and cycles in graphs, including Euler paths/cycles and Hamilton paths/cycles. It provides definitions and properties of these concepts, as well as algorithms for finding Euler paths and cycles in both undirected and directed graphs. The algorithms run in O(m) time where m is the number of edges in the graph. It also discusses De Bruijn graphs and sequences.

Uploaded by

Raqibul Islam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views54 pages

Tours in Graph

The document discusses graph algorithms for finding special paths and cycles in graphs, including Euler paths/cycles and Hamilton paths/cycles. It provides definitions and properties of these concepts, as well as algorithms for finding Euler paths and cycles in both undirected and directed graphs. The algorithms run in O(m) time where m is the number of edges in the graph. It also discusses De Bruijn graphs and sequences.

Uploaded by

Raqibul Islam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 54

Graph Algorithms

Tours in Graphs
Graph Algorithms
Special Paths and Cycles in Graphs
Euler Path: A path that traverses all the edges of the graph
exactly once.
Euler Cycle: A cycle that traverses all the edges of the graph
exactly once.
Hamilton Path: A simple path that visits all the vertices of
the graph exactly once.
Hamilton Cycle: A simple cycle that visits all the vertices of
the graph exactly once
Graph Algorithms 1
An Euler Path
Graph Algorithms 2
An Euler Cycle
Graph Algorithms 3
A Hamilton Path
Graph Algorithms 4
A Hamilton Cycle
Graph Algorithms 5
Complexity
An Euler Path (or an Euler Cycle), if exists, can be found
in any graph with m edges in O(m)-time.
Finding a Hamilton Path or a Hamilton Cycle:
A hard to solve problem. It is strongly believed that no
polynomial time algorithm exists to solve these problems.
A simple special case of the Traveller Salesperson
Problem (TSP).
Graph Algorithms 6
Euler Paths and Cycles in Undirected Graphs
Edge representation path: P = (e
0
, e
1
, . . . , e
m1
).
P is an Euler Path in a graph with m edges if
e
i
= e
j
for all 0 i = j < m.
e
i
= (x, y) and e
i+1
= (y, z)
for 0 i < m1 and vertices x, y, z.
An Euler Cycle C is an Euler Path P for which
e
m1
= (x, y) and e
0
= (y, z) for vertices x, y, z.
Graph Algorithms 7
Euler Paths and Cycles in Directed Graphs
Edge representation path: P = (e
0
, e
1
, . . . , e
m1
)
P is an Euler Path in a directed graph with m edges if
e
i
= e
j
for all 0 i = j < m.
e
i
= (x y) and e
i+1
= (y z)
for 0 i < m1 and vertices x, y, z.
An Euler Cycle C is an Euler Path P for which
e
m1
= (x y) and e
0
= (y z) for vertices x, y, z.
Graph Algorithms 8
The bridges of Konigsberg
B
A
D
C
A
B
C D
No Euler Cycle or Euler Path exist!!!
Graph Algorithms 9
A Toy Example
The left graph has no Euler Path.
The middle graph has an Euler Path but not an Euler Cycle.
The right graph has an Euler Cycle.
Graph Algorithms 10
Graphs with Euler Cycles
Theorem: An undirected and connected graph has an Euler
Cycle i all the vertices have an even degree.
Remark: A self-loop adds 2 to the degree of the vertex.
The only-if direction:
Let C = (e
0
, e
1
, . . . , e
m1
) be an Euler Cycle.
Let y be a vertex.
If e
i
= (x, y) then e
i+1
= (y, z) ( (m1) + 1 = 0 ).
Therefore the degree of y must be even.
Graph Algorithms 11
Proof: The If Direction
Assume all the degrees are even.
Construct an O(m)-time algorithm producing an Euler Cycle
represented by vertices.
Each edge is examined constant number of times with
an appropriate data structure.
Main idea: Explore unused edges as long as they exist.
Graph Algorithms 12
Data Structure and Variables
Edges are marked either used or unused.
Initially all the edges are marked unused.
At the end all the edges are marked used.
An arbitrary starting vertex x.
A main cycle C.
Initially C is empty.
At the end C contains all the edges.
An exploring path P = (y, . . .).
Initially P = (x).
At the end P is empty.
A secondary cycle C

.
Initially and at the end C

is empty.
Graph Algorithms 13
Finding a Secondary Cycle
Let P = (y, . . . , z) be the exploring path.
While z (the last vertex in P) has unused edges:
Let (z, w) be an unused edge.
Mark (z, w) as used.
Append w at the end of P: P = (y, . . . , z, w).
Let the secondary cycle C

= P = (y, . . . , y).
Need to prove: this process terminates only at y.
Graph Algorithms 14
Combining the Main and the Secondary Cycles
Let C = (x, . . . , a, y, b, . . . , x) be the main cycle.
Let C

= (y, c, . . . , d, y) be the secondary cycle.


Then C = (x, . . . , a, y, c, . . . , d, y, b, . . . , x).
x
y
b
c a
y
d
a
d
x y
b
c
Graph Algorithms 15
The Algorithm
Find the rst secondary cycle C

:
Start the exploring path with x.
The rst main cycle C is C

.
While there exists an unused edge:
Find y in C with an unused edges.
Find a secondary cycle C

starting with y.
Combine the cycles C and C

into C.
Return the cycle C.
Graph Algorithms 16
Correctness: Key Observations
Since all the edges have an even degree it follows that the
nding a secondary cycle procedure can be stuck only at y
which is the rst vertex of the exploring path.
If the main cycle C does not contain all the edges in the
graph, then it must contain a vertex with an unused edge
due to connectivity.
Graph Algorithms 17
Complexity
Each edge is explored only once when it is unused and then
becomes used forever.
Can be done in O(m)-time with adjacency lists.
Each edge is traversed only once while looking for a vertex
with an unused edge in the main cycle.
Can be done if the main cycle is a linked list and if
the algorithm remembers the last starting vertex for the
exploring path.
Graph Algorithms 18
Complexity
There are at most n 1 cycle combinations since a new
exploring path never reaches again the connecting vertex.
A combination can be done in O(1)-time if the cycles are
maintained as double linked lists.
Hence, O(m) complexity in connected graphs (n m).
Graph Algorithms 19
Directed Graphs
In a strongly connected graph there exists a directed path
between any two vertices.
The in-degree of a vertex x d
in
(x) is the number of
edges terminating at x.
The out-degree of a vertex x d
out
(x) is the number of
edges originating at x.
Theorem: A directed and strongly connected graph has an
Euler Cycle i d
in
(x) = d
out
(x) for each vertex x.
Graph Algorithms 20
Euler Paths
Theorem: An undirected and connected graph has an Euler
Path if at most 2 vertices have an odd degree.
Theorem: A strongly connected directed graph has a directed
Euler Path starting with x and ending at y, x = y, if:
d
in
(z) = d
out
(z) for any vertex z / {x, y}.
d
in
(x) = d
out
(x) 1.
d
in
(y) = d
out
(y) + 1.
Graph Algorithms 21
Covering Paths
Lemma: In an undirected graph the number of vertices with
odd degree is even.
Denition: k disjoint paths cover a graph G if each edge of
G belongs to one of the k paths.
Theorem: A connected directed graph with 2k vertices with
an odd degree can be covered with k disjoint paths.
Graph Algorithms 22
Proof
Match the odd-degree 2k vertices with k new edges.
All the vertices in the new graph have an even degree.
Find an Euler Cycle in the new graph.
The new edges are not adjacent in the Euler Cycle since
each vertex belongs to at most one new edge.
Omit the k new edges from the Euler Cycle.
The cycle split to k paths that cover all the edges.
Graph Algorithms 23
De-Bruijn Sequences
= {0, 1, . . . , 1} an alpha-bet of letters.
There exists

distinct words of length over .


= 2 and = 3:
000, 001, 010, 011, 100, 101, 110, 111.
= 3 and = 2:
00, 01, 02, 10, 11, 12, 20, 21, 22.
Graph Algorithms 24
De-Bruijn Sequences
A cyclic sequence S
,
= a
0
, a
1
, . . . , a
L1
of length L =

is called a De-Bruijn sequence if for any word w of length


over there exists a unique index 0 i < L such that
w = a
i
, a
i+1
, . . . , a
i+1
(the addition is done mod L).
= 2 and = 3 = 00011101.
= 3 and = 2 = 001122021.
Graph Algorithms 25
Directed De-Bruijn graphs
G
,
= (V
,
, E
,
) is a De-Bruijn graph:
Vertices: all the n =
1
words of length 1.
V
2,4
= {000, 001, . . . , 111}.
V
3,3
= {00, 01, . . . , 22}.
Edges: all the m =

words of length .
E
2,4
= {0000, 0001, . . . , 1111}.
E
3,3
= {000, 001, . . . , 222}.
The edge (b
1
, . . . , b

) connects the vertices:


(b
1
, b
2
. . . , b
1
) (b
2
, . . . , b
1
, b

)
Graph Algorithms 26
G
2,3
00
01
10
11 000
001 011
100
101
110
111 010
Graph Algorithms 27
G
3,2
00
01
02 10
11
12
20
21
22
0
1 2
Graph Algorithms 28
De-Bruign Sequences Always Exist
Lemma: For all positive integers and there exists a
directed Euler Cycle in G
,
.
Proof: G
,
is strongly connected and for any vertex
in-degree = out-degree = .
Lemma: An Euler Cycle in G
,
implies a De-Bruijn sequence
S
,
.
Proof: Follow the Euler Cycle. Initially the sequence is the
rst vertex on the path. Append only the last letter of the
next vertex to the current sequence.
Theorem: For all positive integers and there exists a
De-Bruijn sequence S
,
.
Graph Algorithms 29
G
2,3
and S
2,3
00
01
10
11 000
001 011
100
101
110
111 010
Euler Cycle: 00 00 01 11 11 10 01 10 00
De-Bruijn sequence: 00011101
Graph Algorithms 30
G
3,2
and S
3,2
00
01
02 10
11
12
20
21
22
0
1 2
Euler Cycle: 0 0 1 1 2 2 0 2 1 0
De-Bruijn sequence: 001122021
Graph Algorithms 31
Undirected Hamilton Paths and Cycles
A path of vertices: P = (v
0
, v
1
, . . . , v
n1
).
P is a Hamilton Path in a graph with n vertices if
v
i
= v
j
for all 0 i = j < n.
(v
i
, v
i+1
) is an edge for 0 i < n 1.
A Hamilton Cycle C is a Hamilton Path P for which
(v
n1
, v
0
) is also an edge.
Graph Algorithms 32
Directed Hamilton Paths and Cycles
A directed path of vertices: P = (v
0
v
1
. . . v
n1
)
P is a directed Hamilton Path in a graph with n vertices if
v
i
= v
j
for all 0 i = j < n.
(v
i
v
i+1
) is a directed edge for 0 i < n 1.
A directed Hamilton Cycle C is a directed Hamilton Path
P for which (v
n1
v
0
) is also an edge.
Graph Algorithms 33
The Petersen Graph
A B
C
D
E
F
G
H
I
J
There is no Hamilton Cycle.
The following is a Hamilton Path:
P = (A, B, C, D, E, J, H, F, I, G)
Graph Algorithms 34
The Knight-Chess Graph
Denition: The Knight-Chess graph has 64 vertices one for
each square on the 88 chess board. 2 vertices are adjacent
i a knight can move from one to another in one step.
*
Graph Algorithms 35
The Knight-Chess Problem
Problem: Is it possible to cover all the squares of the chess
board with knight moves?
An equivalent formulation: Does the Knight-Chess graph
has a Hamilton path?
1
2
3
4
5
6
7
8
9
Graph Algorithms 36
Tournaments
Denition: A tournament is a simple directed graph such
that for each pair of vertices u and v, either the directed
edge u v exists or the directed edge v u exists but
not both and not none.
Observation: There are exactly

n
2

directed edges in a
tournament with n vertices.
Observation: The underlying graph of a tournament with n
vertices is the complete graph K
n
.
Theorem: A tournament always has a Hamilton path.
Graph Algorithms 37
A Tournament with 6 Vertices
Graph Algorithms 38
A Hamilton Path in the Tournament
Graph Algorithms 39
A Hamilton Cycle in the Tournament
Graph Algorithms 40
Algorithm to nd Hamilton Path in a Tournament
(1) Start with the path P
1
= (v
1
) for an arbitrary vertex v
1
.
(2) Let the current path be P
i
= (v
1
v
2
v
i
)
for 1 i n.
(3) If i = n, terminate with the Hamilton Path P
n
.
(4) Let v be a vertex not in the path.
(5) Insert v into P
i
to get the path P
i+1
.
(6) Goto Step (2).
Graph Algorithms 41
Path Augmentation
If (v v
1
) is an edge, then P
i+1
= (v v
1
v
i
).
If (v
i
v) is an edge, then P
i+1
= (v
1
v
i
v).
Otherwise
1j<i
s.t. (v
j
v) and (v v
j+1
) are edges,
then P
i+1
= (v
1
v
j
v v
j+1
v
i
).
Graph Algorithms 42
The Algorithm is Correct
The path augmentation is always successful.
Therefore, eventually P
n
exists which is a Hamilton Path.
Graph Algorithms 43
The Algorithm is Ecient
Inserting a vertex to a path can be done in O(n) time using
the adjacency matrix.
There are n iterations.
The total complexity is O(n
2
).
With a binary search for the insertion point, the algorithm
probs the adjacency matrix O(nlog n) times. But the
overall complexity is still O(n
2
).
Graph Algorithms 44
A Hamilton Cycle Greedy Algorithm: Outline
As long as possible, construct a path by adding vertices to
both end-vertices of the path.
Close this path into a cycle by either connecting both
end-vertices or by nding a switch vertex.
Connect a new vertex to the cycle and break it to be a new
longer path.
Repeat the above process until either a Hamilton Cycle is
found or an operation is impossible.
Graph Algorithms 45
Converting a Path to a Cycle
Graph Algorithms 46
Converting a Cycle to a Path
Graph Algorithms 47
A Hamilton Cycle Greedy Algorithm: Description
(1) Initially, let P = (x) be a path with an arbitrary vertex x.
(2) Expand the path P from both ends until impossible. Let
P = (x
0
x
1
x
h
)
where there are no edges from x
0
and x
h
outside P.
(3) If (x
0
, x
h
) is an edge then construct the cycle
C = (x
0
x
1
x
h
x
0
).
Goto step 6.
(4) If for some 0 < i < h the edges (x
0
, x
i+1
) and (x
i
, x
h
)
exist, then construct the cycle
C = (x
0
x
1
x
i
x
h
x
h1
x
i+1
x
0
).
Goto step 6.
Graph Algorithms 48
Greedy Algorithm to nd an Undirected Hamilton Cycle
(5) Terminate Unsuccessfully with the path P.
(6) If h = n1 then Terminate Successfully with the Hamilton
Cycle C.
(7) If there is no edge from C outside of C, then Terminate
Unsuccessfully with the cycle C.
(8) Let (x
i
, x) be an arbitrary edge from C to outside of C,
then construct the path
P = (x x
i
x
i+1
x
h
x
0
x
i1
).
(9) Goto step 2 with a longer path.
Graph Algorithms 49
Sometimes Hamilton Cycles Exist
Theorem: Let G be a connected graph with n vertices. If
d(u) + d(v) n for any two vertices u = v in G, then G
has a Hamilton Cycle.
Corollary: Let G be a connected graph with n vertices. If
d(u) n/2 for any vertex u in G, then G has a Hamilton
Cycle.
Graph Algorithms 50
Proof of the Theorem
Step 4, whenever executed, is always successful.
Therefore, the algorithm never reaches step 5.
The algorithm never terminates in step 7 since the graph is
connected.
The algorithm terminates sucessfuly with a Hamilton Cycle
in step 6 since the path is longer in each iteration.
Graph Algorithms 51
Why Step 4 is Always Successful?
Assume that step 4 fails for h n 1 with the path
P = (x
0
x
1
x
h1
x
h
).
Let x
i
1
, x
i
2
, . . . , x
i
k
be the neighbors of x
0
in P.
x
i
1
1
, x
i
2
1
, . . . , x
i
k
1
cannot be neighbors of x
h
.
d(x
h
) h k n 1 k.
d(x
0
) +d(x
h
) < n.
A contradiction.
Graph Algorithms 52
Complexity
Represent the graph with an adjacency matrix.
Augmenting a path by one vertex at its end-point can
be done in O(n) time for a total of O(n
2
) for all the
augmentations.
Converting a path into a cycle can be done in O(n) time
for a total of O(n
2
) for all such conversions.
All the conversions of cycles into paths can be done in
O(n
2
) by scanning the adjacency matrix only once.
The algorithm time complexity is O(n
2
).
Graph Algorithms 53

You might also like