15.spanning tree

Upload: vikrant-sahu

Post on 24-Feb-2018

228 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/25/2019 15.Spanning Tree

    1/50

    Spanning Tree

  • 7/25/2019 15.Spanning Tree

    2/50

    What is A Spanning Tree?

    u

    v

    b

    a

    c

    d

    e

    f

    A spanningtree for anundirected graph G=(V,E)is a subgraphof G that isa treeand contains all theverticesof G

    an a graph have !orethan one spanning tree?

    an an unconnected graphhave a spanning tree?

  • 7/25/2019 15.Spanning Tree

    3/50

    "ini!al Spanning Tree#

    4 4

    3

    2

    9

    15

    8

    1014

    3

    u

    v

    b

    a

    c

    d

    e

    f

    Mst T: w( T)= (u,v) Tw(u,v) is minimized

    The $eightof a subgraphis the su! of the $eightsof it edges#

    A !ini!u! spanning treefor a $eighted graph is aspanning tree $ith!ini!u! $eight#

    an a graph have !orethen one !ini!u!spanning tree?

  • 7/25/2019 15.Spanning Tree

    4/50

    E%a!ple of a &roble! that Translates into a"ST

    The Problem Several pins of an electronic circuit !ust be

    connected using the least a!ount of $ire#

    Modeling the Problem The graph is a co!plete, undirected graph

    G= ( V, E ,W), $here Vis the set of pins, Eis

    the set of all possible interconnections bet$eenthe pairs of pins and $(e) is the length of the$ire needed to connect the pair of vertices#

    'ind a !ini!u! spanning tree#

  • 7/25/2019 15.Spanning Tree

    5/50

    Greed hoice

    We $ill sho$ t$o $as to build a !ini!u!spanning tree#

    A "ST can be gro$n fro! the currentspanning tree b adding the nearest verte%and the edge connecting the nearest verte% tothe "ST# (&ri!s algorith!)

    A "ST can be gro$n fro! a forest of spanningtrees b adding the s!allest edge connectingt$o spanning trees# (*rus+als algorith!)

  • 7/25/2019 15.Spanning Tree

    6/50

    Notation

    Treevertices- in the tree constructed so far .ontree vertices- rest of vertices

    Prims Selection rule

    Select the minimum weight edge between a tree-

    nde and a nn-tree nde and add t the tree

  • 7/25/2019 15.Spanning Tree

    7/50

    The Prim algorithm Main Idea

    Select a vertex to be a tree-node

    while(there are non-tree vertices) {ifthere is no edge connecting a tree node

    with a non-tree node returnno spanning tree

    select an edge o! minim"m weight between atree node and a non-tree node

    add the selected edge and its new vertex tothe tree#

    ret"rn tree

  • 7/25/2019 15.Spanning Tree

    8/50

    &ri!s Algorith!

    'E

    A /

    0

    1

    23

    4

    3

    5

    6 5

    4

    5

  • 7/25/2019 15.Spanning Tree

    9/50

    &ri!s Algorith!

    'E

    A /

    0

    A

  • 7/25/2019 15.Spanning Tree

    10/50&ri!s Algorith!

    'E

    A /

    05

  • 7/25/2019 15.Spanning Tree

    11/50&ri!s Algorith!

    'E

    A /

    05

    64

    5

    5

    3

    1

    2

    3

  • 7/25/2019 15.Spanning Tree

    12/50

    &ri!s Algorith!

    'E

    A /

    0

    4

    5

    6 5

    4

    5

  • 7/25/2019 15.Spanning Tree

    13/50

    &ri!s Algorith!

    'E

    A /

    0

    4

    5

    6 5

    5

    4

  • 7/25/2019 15.Spanning Tree

    14/50

    &ri!s Algorith!

    'E

    A /

    0

    4

    5

    6 5

    5

  • 7/25/2019 15.Spanning Tree

    15/50

    &ri!s Algorith!

    'E

    A /

    0

    4

    5

    6 5

    5

  • 7/25/2019 15.Spanning Tree

    16/50

    &ri!s Algorith!

    'E

    A /

    0

    4

    5

    6 5

    5

    !ini!u! spanning tree

  • 7/25/2019 15.Spanning Tree

    17/50

    *rus+als Algorith!

    $r"s%al&s 'lgorithm

    6# Each verte% is in its o$n cluster

    5# Ta+e the edge e$ith the s!allest $eight if econnects t$o vertices in different clusters, then eis added to the "ST and the t$o clusters, $hich are connected b e, are !erged into a

    single cluster if econnects t$o vertices, $hich are alread

    in the sa!e cluster, ignore it

    4# ontinue until n6edges $ere selected

  • 7/25/2019 15.Spanning Tree

    18/50

    *rus+als Algorith!

    'E

    A /

    0

    1

    23

    4

    3

    5

    6 5

    4

    5

  • 7/25/2019 15.Spanning Tree

    19/50

    *rus+als Algorith!

    'E

    A /

    0

    1

    23

    4

    3

    5

    6 5

    4

    5

  • 7/25/2019 15.Spanning Tree

    20/50

    *rus+als Algorith!

    'E

    A /

    0

    1

    23

    4

    3

    5

    6 5

    4

    5

  • 7/25/2019 15.Spanning Tree

    21/50

    *rus+als Algorith!

    'E

    A /

    0

    1

    23

    4

    3

    5

    6 5

    4

    5

  • 7/25/2019 15.Spanning Tree

    22/50

    *rus+als Algorith!

    'E

    A /

    0

    1

    23

    4

    3

    5

    6 5

    4

    5

  • 7/25/2019 15.Spanning Tree

    23/50

    *rus+als Algorith!

    'E

    A /

    0

    1

    23

    4

    3

    5

    6 5

    4

    5

    ccle77

  • 7/25/2019 15.Spanning Tree

    24/50

    *rus+als Algorith!

    'E

    A /

    0

    1

    23

    4

    3

    5

    6 5

    4

    5

  • 7/25/2019 15.Spanning Tree

    25/50

    *rus+als Algorith!

    'E

    A /

    0

    1

    23

    4

    3

    5

    6 5

    4

    5

  • 7/25/2019 15.Spanning Tree

    26/50

    *rus+als Algorith!

    'E

    A /

    0

    4

    5

    6 5

    5

    !ini!u! spanning tree

  • 7/25/2019 15.Spanning Tree

    27/50

    Graph Traversal

    Traversing a graph !eans visiting all thevertices in the graph e%actl once#

    /readth 'irst Search (/'S)

    0epth 'irst Search (0'S)

  • 7/25/2019 15.Spanning Tree

    28/50

    0'SSi!ilar to inorder traversal of a binar

    search tree

    Starting fro! a given node, this traversalvisits all the nodes up to the deepestlevel and so on#

  • 7/25/2019 15.Spanning Tree

    29/50

    v6

    v8v5 v4

    v3

    v9

    v2v1

    v6

    v8v5 v4

    v3

    v9

    v2v1

    0'S

    S * +, - + - +. - +/0 +1 - +20 +30 +4

  • 7/25/2019 15.Spanning Tree

    30/50

    v6

    v8v5 v4

    v3

    v9

    v2v1

    v6

    v8v5 v4

    v3

    v9

    v2v1

    0'S

    S * +, - + - +. - +/0 +1 - +20 +40 +3

  • 7/25/2019 15.Spanning Tree

    31/50

    0'S TraversalVisit the verte% v

    Visit all the vertices along the path $hich begins at v

    Visit the verte% v5 then the verte% i!!ediate

    ad:acent to v, let it be vx# ;f vxhas an i!!ediatead:acent v6then visit it and so on till there is adead end#

    ead end- A verte% $hich does not have ani!!ediate ad:acent or its i!!ediate ad:acent hasbeen visited#

  • 7/25/2019 15.Spanning Tree

    32/50

    After co!ing to an dead end $ebac+trac+ to v to see if it has ananother ad:acent verte% other than vxand then continue the sa!e fro! it else

    fro! the ad:acent of the ad:acent($hich is not visited earlier) and so on#

  • 7/25/2019 15.Spanning Tree

    33/50

    &ush the starting verte% into the STA*

    While ST'7$ not empt6 do

    & all ad:acent verte% of +onto STA*

    End of ;'

    End of WhileST

  • 7/25/2019 15.Spanning Tree

    34/50

    *

    A

    /

    0

    E

    '

    G

    Ad:acenc @ist

    A- ',,//- G,

    - '0- E- 0,,'- 0G- ,E- 0,**- E,G

  • 7/25/2019 15.Spanning Tree

    35/50

    0'S of G starting at

    6B ;nitiall push onto STA*STA* -

    V;S;T- C

    5B & onto the STA* allneighbor of

    STA*- 0, *V;S;T-

  • 7/25/2019 15.Spanning Tree

    36/50

    4B & all neighbor of * ontoSTA*

    STA*- 0,E,G

    V;S;T- , *

    3B & all neighbor of G ontoSTA*

    STA*- 0,E, E, ,

    V;S;T- , *, G

  • 7/25/2019 15.Spanning Tree

    37/50

    1B & all neighbor of ontoSTA*

    STA*- 0,E,E, '

    V;S;T- , *, G,

    2B &

  • 7/25/2019 15.Spanning Tree

    38/50

    1B & all neighbor of 0 ontoSTA*

    STA*- 0,E,E,

    V;S;T- , *, G, , ',0

    2B &

  • 7/25/2019 15.Spanning Tree

    39/50

    1B &

  • 7/25/2019 15.Spanning Tree

    40/50

    *

    A

    /

    0

    E

    '

    G

    Ad:acenc @ist

    A- ',,//- G,

    - '0- E- 0,,'- 0G- ,E- 0,**- E,G

    , *, G, , ', 0, E

  • 7/25/2019 15.Spanning Tree

    41/50

    /'S Traversal

    An verte% in label i $ill be visited onl

    after the visiting of all the vertices inits preceding level that is at level iD 6

  • 7/25/2019 15.Spanning Tree

    42/50

    /'S Traversal6B Enter the starting verte% vin a ueue

    85B While 8is not e!pt do

    0elete an ite! fro! 8, sa "

    ;f "is not in +ISITstore "in+ISIT

    Enter all ad:acent vertices of "

    into 84B Stop

  • 7/25/2019 15.Spanning Tree

    43/50

    v6

    v8v5 v4

    v3

    v9

    v2v1

  • 7/25/2019 15.Spanning Tree

    44/50

    6B ;nsert the starting verte% V6in F

    F = V6

    V;S;T = C

    5B 0elete an ite! fro! F, let it be u = V6

    u is not in V;S;T# Store u in V;S;T

    and its ad:acent ele!ent in F F = V5 , V4

    V;S;T = V6

  • 7/25/2019 15.Spanning Tree

    45/50

    4B 0elete an ite! fro! F, let it be u = V5u is not in V;S;T# Store u in V;S;T and

    its ad:acent ele!ent in F

    F = + , V4 , +15 +.

    V;S;T = V6 , +

    3B 0elete an ite! fro! F, let it be u = V4u is not in V;S;T# Store u in V;S;T and

    its ad:acent ele!ent in F

    F = +4 , V3, V15 +15 +3

    V;S;T = V6 , V55 +4

  • 7/25/2019 15.Spanning Tree

    46/50

    1B 0elete an ite! fro! F, let it be u = V3u is not in V;S;T# Store u in V;S;T and

    its ad:acent ele!ent in F F = +1, V1, V3, V2, +2

    V;S;T = V6 , V5, V4, +1

    2B 0elete an ite! fro! F, let it be u =V1u is not in V;S;T# Store u in V;S;T and

    its ad:acent ele!ent in F

    F = +., V3, V2, V8 , V9V;S;T = V6 , V5, V4, V3, +.

  • 7/25/2019 15.Spanning Tree

    47/50

    9B 0elete an ite! fro! F, let it be u =V3

    u is in V;S;T#

    F = +1, V2, V8 , V9

    V;S;T = V6 , V5, V4, V3, V18B 0elete an ite! fro! F, let it be u =V2

    u is not in V;S;T# Store u in V;S;Tand its ad:acent ele!ent in F

    F = +3, V8 , V9

    =

  • 7/25/2019 15.Spanning Tree

    48/50

    B 0elete an ite! fro! F, let it be u =V8u is not in V;S;T# Store u in V;S;T and

    its ad:acent ele!ent in F F = +2 , V9, V6

    V;S;T = V6 , V5, V4, V3, V1, V2, +2

    6HB 0elete an ite! fro! F, let it be u =V9u is not in V;S;T# Store u in V;S;T and

    its ad:acent ele!ent in F

    F = +/, V6V;S;T = V6 , V5, V4, V3, V1, V2, V8,

    +/

  • 7/25/2019 15.Spanning Tree

    49/50

    66B 0elete an ite! fro! F, let it be u =V6u is in V;S;T#

    F = +,

    V;S;T = V6 , V5, V4, V3, V1, V2,V8, V9

    65B F is e!pt, Stop

    F =

    V;S;T = V6 , V5, V4, V3, V1, V2,V8, V9

  • 7/25/2019 15.Spanning Tree

    50/50

    v6

    v8v5 v4

    v3

    v9

    v2v1

    v6

    v8v5 v4

    v3

    v9

    v2v1

    /'S