lecture-34-cs210-2012.pptx

Upload: moazzam-hussain

Post on 03-Jun-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/11/2019 Lecture-34-CS210-2012.pptx

    1/32

    Data Structures and Algorithms(CS210/ESO207/ESO211)

    Lecture 34

    Greedy strategy: a new algorithm paradigmPart III

    1

  • 8/11/2019 Lecture-34-CS210-2012.pptx

    2/32

    Correctness of a Greedy algorithm

    the optimal solution of original instance of the problem

    the optimal solution of ( smaller ) instance obtained by the greedy step

    2

    A suitable Theorem

  • 8/11/2019 Lecture-34-CS210-2012.pptx

    3/32

    Continuing Problem from last class

    3

    Minimum spanning tree

  • 8/11/2019 Lecture-34-CS210-2012.pptx

    4/32

    Problem Description

    Input: an undirected graph G=(V,E) with : E ,

    Aim: compute a spanning tree (V,E), E E such that ( )E is minimum .

    Lemma 1 (proved in last class):

    If E is the edge of least weight in G, then there is a MST containing .

    Lemma 2:If E is the edge of least weight in G, then must be present in the MST.

    4

    How to use Lemma 2 to designan algorithm for MST ?

    Assume all edge weights are distinct

  • 8/11/2019 Lecture-34-CS210-2012.pptx

    5/32

    How to compute a MST ?

    5

    170

    33

    3531 41 81

    5242

    50

    102

    50 3060

    70

    54

    57

    49

    49

    80

    78

    6354

    53

    44

    29

    6442

    43

  • 8/11/2019 Lecture-34-CS210-2012.pptx

    6/32

    How to compute a MST ?

    6

    170

    33

    3531 41 81

    5242

    50

    102

    50 3060

    70

    54

    57

    49

    49

    80

    78

    6354

    53

    44

    29

    6442

    43

    uv

  • 8/11/2019 Lecture-34-CS210-2012.pptx

    7/32

    How to compute a MST ?

    7

    170

    33

    3531 41 81

    5242

    50

    102

    50 3060

    70

    54

    57

    49

    49

    80

    78

    6354

    5364

    42

    44

    w

    43

    This is graph

  • 8/11/2019 Lecture-34-CS210-2012.pptx

    8/32

    How to compute a MST ?

    Let (u ,v) be the least weight edge in =( , ). Transform into as follows. Remove vertices u and v and add a new vertex w For each edge ( u ,x) , add edge ( w ,x) in . For each edge ( v,x) , add edge ( w ,x) in . In case of multiple edges between w and x, keep only the lighter weight edge.

    Theorem1: W_MST ( ) = W_MST ( ) + w (u ,v)Proof: (by construction)

    1. W_MST ( ) W_MST ( ) + w (u ,v)

    2. W_MST ( ) W_MST ( ) - w (u ,v)

    8

    Use Lemma 2

    straightforward

  • 8/11/2019 Lecture-34-CS210-2012.pptx

    9/32

    How to compute a MST ?

    Using Theorem1 , we get an algorithm for MST with the following drawbacks :

    Complicated to implement.

    Inefficient.

    less insightful (does not convey much about the structure of MST).

    9

  • 8/11/2019 Lecture-34-CS210-2012.pptx

    10/32

    A useful lesson for design of a graph algorithm

    If you have a complicated algorithm for a graph problem,

    search for some graph theoretic property

    to design simpler and more efficient algorithm

    10

  • 8/11/2019 Lecture-34-CS210-2012.pptx

    11/32

    Two graph theoretic properties of MST

    Cut property

    Cycle property

    11

    Every algorithm till date is based onone of these properties !

  • 8/11/2019 Lecture-34-CS210-2012.pptx

    12/32

    Cut Property

    12

  • 8/11/2019 Lecture-34-CS210-2012.pptx

    13/32

    Cut Property

    Definition: For any subset , such that , cut ( , ) is defined as:cut ( , ) = { ( , ) | and or and }

    Cut-property : The least weight edge of a cut ( , ) must be in MST.

    13

    cut ( , )

    23

    13

    37

    48

    67

    u

    v

    Pursuing greedy strategyto minimize weight of MST,what can we say about the

    edges of cut ( , ) ?

  • 8/11/2019 Lecture-34-CS210-2012.pptx

    14/32

    Proof of cut-property

    14

    23

    13

    37

    48

    67

    u

    v

  • 8/11/2019 Lecture-34-CS210-2012.pptx

    15/32

    Proof of cut-property

    Let be the MST, and ( u ,v) .

    Question: What happens if we remove (x,y) from , and add (u ,v) to .

    15

    13

    u

    v

    >13x

    y

  • 8/11/2019 Lecture-34-CS210-2012.pptx

    16/32

    Proof of cut-property

    Let be the MST, and ( u ,v) .

    Question: What happens if we remove (x,y) from , and add (u ,v) to .

    16

    13

    u

    v

    13 xy

    We get a spanning tree with weight < weight ( )

    A contradiction !

  • 8/11/2019 Lecture-34-CS210-2012.pptx

    17/32

    Cycle Property

    17

  • 8/11/2019 Lecture-34-CS210-2012.pptx

    18/32

    Cycle Property

    Let be any cycle in .

    Cycle-property :Maximum weight edge of any cycle can not be present in MST.

    18

    u v

    a

    b

    c

    f

    r

    s

    43

    24

    12

    17 31

    36

    19

    26

    Pursuing greedy strategy

    to minimize weight of MST,what can we say about the

    edges of cycle ?

  • 8/11/2019 Lecture-34-CS210-2012.pptx

    19/32

    Proof of Cycle property

    Let be the MST, and ( u ,v) .

    19

    u v

    a

    b

    c

    f

    r

    s

    43

    24

    12

    17 31

    36

    19

    26

    u v43

  • 8/11/2019 Lecture-34-CS210-2012.pptx

    20/32

    Proof of Cycle property

    Let be the MST, and ( u ,v) .

    20

    43

    24

    12

    17 31

    36

    19

    u v

    u v

    a

    b

    c

    f

    r

    s26

    43

    s

    36

    a24

    b

    12

    c17

    f31

  • 8/11/2019 Lecture-34-CS210-2012.pptx

    21/32

    Proof of Cycle property

    Let be the MST, and ( u ,v) .

    21

    43

    24

    12

    17 31

    36

    19

    u v

    u v

    a

    b

    c

    f

    r

    s26

    s

    43

    36

    Swapping ( u ,v) with ( f ,s), we get a spanning tree with weight < weight ( )

    A contradiction !

    f

  • 8/11/2019 Lecture-34-CS210-2012.pptx

    22/32

    Algorithms based on cut Property

    Spend some time on designing such an algorithmbefore going to the next slide

    22

  • 8/11/2019 Lecture-34-CS210-2012.pptx

    23/32

    How to use cut property to compute a MST ?

    23

    170

    33

    35

    31 41 81

    5242

    50

    102

    50 3060

    70

    54

    57

    49

    49

    80

    78

    6324

    53

    44

    29

    6442

    43

    u

    v70

  • 8/11/2019 Lecture-34-CS210-2012.pptx

    24/32

    How to use cut property to compute a MST ?

    24

    170

    33

    31 41 81

    5242

    50

    102

    50 3060

    70

    54

    57

    49

    49

    80

    78

    6324

    53

    44

    29

    6442

    43

    u

    v70

    35

    w

  • 8/11/2019 Lecture-34-CS210-2012.pptx

    25/32

    How to use cut property to compute a MST ?

    25

    170

    33

    31 41 81

    5242

    50

    102

    50 3060

    70

    54

    57

    49

    49

    80

    78

    6324

    53

    44

    29

    6442

    43

    u

    v70

    35

    w

    x

  • 8/11/2019 Lecture-34-CS210-2012.pptx

    26/32

    How to use cut property to compute a MST ?

    26

    170

    33

    31 41 81

    5242

    50

    102

    50 3060

    70

    54

    57

    49

    49

    80

    78

    6324

    53

    44

    29

    6442

    43

    u

    v70

    35

    w

    x

    y

  • 8/11/2019 Lecture-34-CS210-2012.pptx

    27/32

    How to use cut property to compute a MST ?

    27

    170

    33

    31 41 81

    5242

    50

    102

    50 3060

    70

    54

    57

    49

    49

    80

    78

    6324

    53

    44

    29

    6442

    43

    u

    v70

    35

    w

    x

    y

  • 8/11/2019 Lecture-34-CS210-2012.pptx

    28/32

    How to use cut property to compute a MST ?

    28

    170

    33

    31 41 81

    5242

    50

    102

    50 3060

    70

    54

    57

    49

    49

    80

    78

    6324

    53

    44

    29

    6442

    43

    u

    v70

    35

    w

    x

    y

  • 8/11/2019 Lecture-34-CS210-2012.pptx

    29/32

  • 8/11/2019 Lecture-34-CS210-2012.pptx

    30/32

    Algorithm based on cycle Property

    Spend some time on designing such an algorithmbefore going to the next slide

    30

  • 8/11/2019 Lecture-34-CS210-2012.pptx

    31/32

    An Algorithm based on cycle propertyDescription

    Algorithm (Input: graph =( , ) with weights on edges)While ( ?? ) do

    { Compute any cycle ;Let (u ,v) be the maximum weight edge of the cycle ;

    Remove (u ,v) from ; }

    Return ;

    Number of iterations of the While loop : ??

    Time spent in one iteration of While loop: ??

    Running time of the algorithm: O( )

    31

    has any cycle

    +

    O ( )

  • 8/11/2019 Lecture-34-CS210-2012.pptx

    32/32

    Homework

    Ponder over the following questions before coming for the next class

    Use cycle property and/or cut property to design a new algorithm for MST

    Use some data structure to improve the running time of the algorithms discussed inthis class to O( )

    32