tours in graph

Upload: raqibul-islam

Post on 03-Jun-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/12/2019 Tours in Graph

    1/54

    Graph Algorithms

    Tours in Graphs

    Graph Algorithms

  • 8/12/2019 Tours in Graph

    2/54

    Special Paths and Cycles in Graphs

    Euler Path: A path that traversesallthe edges of the graph

    exactly once.

    Euler Cycle: A cycle that traversesallthe 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 visitsallthe vertices of

    the graph exactly once

    Graph Algorithms 1

  • 8/12/2019 Tours in Graph

    3/54

    An Euler Path

    Graph Algorithms 2

  • 8/12/2019 Tours in Graph

    4/54

    An Euler Cycle

    Graph Algorithms 3

  • 8/12/2019 Tours in Graph

    5/54

    A Hamilton Path

    Graph Algorithms 4

  • 8/12/2019 Tours in Graph

    6/54

    A Hamilton Cycle

    Graph Algorithms 5

  • 8/12/2019 Tours in Graph

    7/54

    Complexity

    An Euler Path (or an Euler Cycle), if exists, can be found

    inanygraph with m edges in O(m)-time.

    Finding a Hamilton Path or a Hamilton Cycle:

    Ahard to solve problem. It is strongly believedthat nopolynomial time algorithm exists to solve these problems.

    A simple special case of the Traveller Salesperson

    Problem (TSP).

    Graph Algorithms 6

  • 8/12/2019 Tours in Graph

    8/54

    Euler Paths and Cycles in Undirected Graphs

    Edge representation path: P = (e0, e1, . . . , em1).

    P is an Euler Path in a graph with m edges if

    ei=ej for all 0i=j < m.

    ei= (x, y) and ei+1= (y, z)

    for 0i < m 1 and vertices x, y, z.

    An Euler Cycle C is an Euler Path P for which em1= (x, y) and e0= (y, z) for vertices x, y, z.

    Graph Algorithms 7

  • 8/12/2019 Tours in Graph

    9/54

    Euler Paths and Cycles in Directed Graphs

    Edge representation path: P = (e0, e1, . . . , em1)

    Pis an Euler Path in a directed graph with m edges if

    ei=ej for all 0i=j < m.

    ei= (xy) and ei+1= (yz)

    for 0i < m 1 and vertices x, y, z.

    An Euler Cycle C is an Euler Path P for which em1= (xy) ande0= (yz) for vertices x, y, z.

    Graph Algorithms 8

  • 8/12/2019 Tours in Graph

    10/54

    The bridges of Konigsberg

    B

    A

    DC

    A

    B

    C D

    NoEuler Cycle or Euler Path exist!!!

    Graph Algorithms 9

  • 8/12/2019 Tours in Graph

    11/54

    A Toy Example

    Theleftgraph has no Euler Path.

    Themiddlegraph has an Euler Path but not an Euler Cycle.

    Therightgraph has an Euler Cycle.

    Graph Algorithms 10

  • 8/12/2019 Tours in Graph

    12/54

    Graphs with Euler Cycles

    Theorem: An undirected andconnectedgraph has an Euler

    Cycleiffall the vertices have aneven degree.

    Remark: Aself-loopadds 2 to the degree of the vertex.

    The only-if direction:

    Let C= (e0, e1, . . . , em1) be an Euler Cycle.

    Let y be a vertex.

    Ifei= (x, y) then ei+1= (y, z) ( (m 1) + 1 = 0 ).

    Therefore the degree ofy must be even.

    Graph Algorithms 11

  • 8/12/2019 Tours in Graph

    13/54

    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 withan appropriate data structure.

    Main idea: Exploreunusededges as long as they exist.

    Graph Algorithms 12

  • 8/12/2019 Tours in Graph

    14/54

    Data Structure and Variables

    Edges are marked eitherusedorunused.

    Initially all the edges are markedunused.

    At the end all the edges are markedused. An arbitrary starting vertex x.

    A main cycle C.

    Initially C is empty. At the end Ccontains 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

  • 8/12/2019 Tours in Graph

    15/54

    Finding a Secondary Cycle

    Let P = (y , . . . , z) be the exploring path.

    While z (the last vertex in P) hasunusededges:

    Let (z, w) be anunusededge.

    Mark (z, w) asused.

    Append w at the end ofP: P = (y , . . . , z , w).

    Let the secondary cycle C

    =P = (y , . . . , y). Need to prove: this process terminates onlyat y.

    Graph Algorithms 14

  • 8/12/2019 Tours in Graph

    16/54

    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

    ca

    y d

    a

    d

    x y

    b

    c

    Graph Algorithms 15

  • 8/12/2019 Tours in Graph

    17/54

    The Algorithm

    Findthe first secondary cycle C:

    Startthe exploring path with x.

    The first main cycle C is C.

    Whilethere exists anunusededge: Find y in Cwith anunusededges.

    Finda secondary cycle C starting with y.

    Combinethe cycles C and C into C.

    Returnthe cycle C.

    Graph Algorithms 16

  • 8/12/2019 Tours in Graph

    18/54

    Correctness: Key Observations

    Since all the edges have an even degree it follows that thefinding a secondary cycle procedure can be stuck only at y

    which is the first 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

  • 8/12/2019 Tours in Graph

    19/54

    Complexity

    Each edge is explored only once when it isunusedand then

    becomesusedforever.

    Can be done in O(m)-time with adjacency lists.

    Each edge is traversed only once while looking for a vertex

    with anunusededge in the main cycle.

    Can be done if the main cycle is a linked list and ifthe algorithm remembers the last starting vertex for the

    exploring path.

    Graph Algorithms 18

  • 8/12/2019 Tours in Graph

    20/54

    Complexity

    There are at most n 1 cycle combinations since a new

    exploring path never reaches again theconnectingvertex.

    A combination can be done in O(1)-time if the cycles are

    maintained asdouble linked lists.

    Hence, O(m) complexity in connected graphs (nm).

    Graph Algorithms 19

  • 8/12/2019 Tours in Graph

    21/54

    Directed Graphs

    In a strongly connected graphthere exists a directed path

    between any two vertices.

    The in-degree of a vertex x din(x) is the number of

    edges terminating at x.

    Theout-degreeof a vertex x dout(x) is the number of

    edges originating at x.

    Theorem: A directed and strongly connected graph has an

    Euler Cycleiffdin(x) =dout(x) for each vertex x.

    Graph Algorithms 20

  • 8/12/2019 Tours in Graph

    22/54

    Euler Paths

    Theorem: An undirected and connected graph has an Euler

    Path if at most 2 vertices have anodddegree.

    Theorem: A strongly connected directed graph has a directed

    Euler Path starting with x and ending at y, x=y, if: din(z) =dout(z) for any vertex z / {x, y}.

    din(x) =dout(x) 1.

    din(y) =dout(y) + 1.

    Graph Algorithms 21

  • 8/12/2019 Tours in Graph

    23/54

    Covering Paths

    Lemma: In an undirected graph the number of vertices with

    odd degree iseven.

    Definition: k disjointpathscovera 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 becoveredwith k disjoint paths.

    Graph Algorithms 22

  • 8/12/2019 Tours in Graph

    24/54

    Proof

    Match the odd-degree 2k vertices with k newedges.

    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 cyclesplitto k paths that cover all the edges.

    Graph Algorithms 23

  • 8/12/2019 Tours in Graph

    25/54

    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

  • 8/12/2019 Tours in Graph

    26/54

    De-Bruijn Sequences

    A cyclic sequence S,=a0, a1, . . . , aL1 of length L=

    is called aDe-Bruijnsequence if for any word w of length over there exists a unique index 0 i < L such that

    w=ai, ai+1, . . . , ai+1 (the addition is donemod L).

    = 2 and = 3 = 00011101.

    = 3 and = 2 = 001122021.

    Graph Algorithms 25

  • 8/12/2019 Tours in Graph

    27/54

  • 8/12/2019 Tours in Graph

    28/54

    G2,3

    00

    01

    10

    11000

    001 011

    100

    101

    110

    111010

    Graph Algorithms 27

  • 8/12/2019 Tours in Graph

    29/54

    G3,2

    00

    01

    0210

    11

    12

    20

    21

    22

    0

    1 2

    Graph Algorithms 28

  • 8/12/2019 Tours in Graph

    30/54

    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 vertexin-degree= out-degree = .

    Lemma: An Euler Cycle inG,impliesa De-Bruijn sequence

    S,.

    Proof: Follow the Euler Cycle. Initially the sequence is the

    first 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

  • 8/12/2019 Tours in Graph

    31/54

    G2,3 and S2,3

    00

    01

    10

    11000

    001 011

    100

    101

    110

    111010

    Euler Cycle: 00 00 01 11 11 10 01 10 00

    De-Bruijn sequence: 00011101

    Graph Algorithms 30

  • 8/12/2019 Tours in Graph

    32/54

    G3,2 and S3,2

    00

    01

    0210

    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

  • 8/12/2019 Tours in Graph

    33/54

    Undirected Hamilton Paths and Cycles

    A path of vertices: P = (v0, v1, . . . , vn1).

    P is a Hamilton Path in a graph with n vertices if

    vi=vj for all 0i=j < n.

    (vi, vi+1) is an edge for 0i < n 1.

    A Hamilton Cycle C is a Hamilton Path P for which

    (vn1, v0) is also an edge.

    Graph Algorithms 32

  • 8/12/2019 Tours in Graph

    34/54

    Directed Hamilton Paths and Cycles

    A directed path of vertices: P = (v0v1. . .vn1)

    P is a directed Hamilton Path in a graph with n vertices if

    vi=vj for all 0i=j < n.

    (vivi+1) is a directed edge for 0i < n 1.

    A directed Hamilton Cycle C is a directed Hamilton Path

    P for which (vn1v0) is also an edge.

    Graph Algorithms 33

  • 8/12/2019 Tours in Graph

    35/54

    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

  • 8/12/2019 Tours in Graph

    36/54

    The Knight-Chess Graph

    Definition: TheKnight-Chess graph has 64 vertices one for

    each square on the88chess board. 2vertices are adjacent

    iff a knight can move from one to another in one step.

    *

    Graph Algorithms 35

  • 8/12/2019 Tours in Graph

    37/54

    The Knight-Chess Problem

    Problem: Is it possible to cover all the squares of the chess

    boardwithknight moves?An equivalent formulation: Does the Knight-Chess graph

    has aHamilton path?

    1

    2

    3

    4

    5

    6

    7

    8

    9

    Graph Algorithms 36

  • 8/12/2019 Tours in Graph

    38/54

    Tournaments

    Definition: A tournament is a simple directed graph such

    that for each pair of vertices u and v, either the directededge u v exists or the directed edge v u exists but

    not both and not none.

    Observation: There are exactlyn2

    directed edges in atournament with n vertices.

    Observation: The underlying graph of a tournament with n

    vertices is the complete graph Kn.

    Theorem: A tournament always has a Hamilton path.

    Graph Algorithms 37

  • 8/12/2019 Tours in Graph

    39/54

    A Tournament with 6 Vertices

    Graph Algorithms 38

  • 8/12/2019 Tours in Graph

    40/54

    A Hamilton Path in the Tournament

    Graph Algorithms 39

  • 8/12/2019 Tours in Graph

    41/54

    A Hamilton Cycle in the Tournament

    Graph Algorithms 40

  • 8/12/2019 Tours in Graph

    42/54

    Algorithm to find Hamilton Path in a Tournament

    (1) Startwith the path P1= (v1) for an arbitrary vertex v1.

    (2) Let the current path be Pi= (v1v2 vi)

    for 1in.

    (3) Ifi=n,terminatewith the Hamilton Path Pn.

    (4) Let v be a vertex not in the path.

    (5) Insert v into Pi to get the path Pi+1.

    (6) GotoStep (2).

    Graph Algorithms 41

  • 8/12/2019 Tours in Graph

    43/54

    Path Augmentation

    If(vv1) is an edge,then Pi+1= (vv1 vi).

    If(viv) is an edge,then Pi+1= (v1 viv).

    Otherwise 1j

  • 8/12/2019 Tours in Graph

    44/54

    The Algorithm is Correct

    The path augmentation is always successful.

    Therefore, eventually Pn exists which is a Hamilton Path.

    Graph Algorithms 43

  • 8/12/2019 Tours in Graph

    45/54

    The Algorithm is Efficient

    Inserting a vertex to a path can be done inO(n)time using

    the adjacency matrix.

    There are n iterations.

    The total complexity is O(n2).

    With abinary searchfor the insertion point, the algorithm

    probs the adjacency matrix O(n log n) times. But theoverall complexity is still O(n2).

    Graph Algorithms 44

  • 8/12/2019 Tours in Graph

    46/54

    A Hamilton Cycle Greedy Algorithm: Outline

    As long as possible, constructa path byaddingvertices to

    both end-vertices of the path.

    Close this path into a cycle by either connecting both

    end-vertices or byfindingaswitch vertex.

    Connecta new vertex to the cycle andbreakit to be a new

    longer path.

    Repeat the above process until either a Hamilton Cycle isfound or an operation is impossible.

    Graph Algorithms 45

  • 8/12/2019 Tours in Graph

    47/54

    Converting a Path to a Cycle

    Graph Algorithms 46

  • 8/12/2019 Tours in Graph

    48/54

    Converting a Cycle to a Path

    Graph Algorithms 47

  • 8/12/2019 Tours in Graph

    49/54

    A Hamilton Cycle Greedy Algorithm: Description

    (1) Initially, let P = (x) be a path with an arbitrary vertex x.

    (2) Expandthe path P from both ends until impossible. Let

    P = (x0 x1 xh)

    where there are no edges from x0 and xh outside P.

    (3) If(x0, xh) is an edge thenconstructthe cycle

    C= (x0 x1 xh x0).Gotostep 6.

    (4) If for some 0 < i < h the edges (x0, xi+1) and (xi, xh)

    exist, thenconstructthe cycleC= (x0 x1 xi xh xh1 xi+1 x0).

    Gotostep 6.

    Graph Algorithms 48

  • 8/12/2019 Tours in Graph

    50/54

    Greedy Algorithm to find an Undirected Hamilton Cycle

    (5) Terminate Unsuccessfullywith the path P.

    (6) Ifh=n 1thenTerminate Successfullywith the HamiltonCycle C.

    (7) If there is no edge from C outside of C, then Terminate

    Unsuccessfullywith the cycle C.

    (8) Let (xi, x) be an arbitrary edge from C to outside of C,

    thenconstructthe path

    P = (x xi xi+1 xh x0 xi1).

    (9) Gotostep 2 with a longer path.

    Graph Algorithms 49

  • 8/12/2019 Tours in Graph

    51/54

    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. Ifd(u)n/2 for any vertex u in G, then G has a Hamilton

    Cycle.

    Graph Algorithms 50

  • 8/12/2019 Tours in Graph

    52/54

    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 isconnected.

    The algorithmterminates sucessfulywith a Hamilton Cycle

    in step 6 since the path is longer in each iteration.

    Graph Algorithms 51

  • 8/12/2019 Tours in Graph

    53/54

    Why Step 4 is Always Successful?

    Assume that step 4failsfor hn 1 with the path

    P = (x0 x1 xh1 xh). Let xi1, xi2, . . . , xik be the neighbors ofx0 in P.

    xi11, xi21, . . . , xik1 cannotbe neighbors ofxh.

    d(xh)h kn 1 k.

    d(x0) + d(xh)< n.

    Acontradiction.

    Graph Algorithms 52

  • 8/12/2019 Tours in Graph

    54/54

    Complexity

    Represent the graph with an adjacency matrix.

    Augmenting a path by one vertex at its end-point canbe done in O(n) time for a total of O(n2) for all the

    augmentations.

    Converting a path into a cycle can be done in O(n) timefor a total ofO(n2) for all such conversions.

    All the conversions of cycles into paths can be done in

    O(n2) by scanning the adjacency matrix only once.

    The algorithm time complexity is O(n2).

    Graph Algorithms 53