+ All Categories
Home > Documents > 27 2 11 Spanning Trees

27 2 11 Spanning Trees

Date post: 05-Apr-2018
Category:
Upload: mdhuq1
View: 216 times
Download: 0 times
Share this document with a friend

of 23

Transcript
  • 7/31/2019 27 2 11 Spanning Trees

    1/23

    1

    Minimum Spanning Trees(some material adapted from slides by Peter Lee)

  • 7/31/2019 27 2 11 Spanning Trees

    2/23

    2

    Problem: Laying Telephone Wire

    Central office

  • 7/31/2019 27 2 11 Spanning Trees

    3/23

    3

    Wiring: Nave Approach

    Central office

    Expensive!

  • 7/31/2019 27 2 11 Spanning Trees

    4/23

    4

    Wiring: Better Approach

    Central office

    Minimize the total length of wire connecting the customers

  • 7/31/2019 27 2 11 Spanning Trees

    5/23

    5

    Minimum Spanning Tree (MST)(see Weiss, Section 24.2.2)

    it is a tree (i.e., it is acyclic)

    it covers all the vertices V

    contains |V| - 1 edges

    the total cost associated with treeedges is the minimum among allpossible spanning trees

    not necessarily unique

    A minimum spanning tree is a subgraph of anundirected weighted graph G, such that

  • 7/31/2019 27 2 11 Spanning Trees

    6/23

    6

    Applications of MST

    Any time you want to visit all vertices in agraph at minimum cost (e.g., wire routingon printed circuit boards, sewer pipelayout, road planning)

    Internet content distribution$$$, also a hot research topic

    Idea: publisher produces web pages, contentdistribution network replicates web pages to

    many locations so consumers can access athigher speed

    MST may not be good enough!

    content distribution on minimum cost tree may take

    a long time!

  • 7/31/2019 27 2 11 Spanning Trees

    7/23

    7

    How Can We Generate a MST?

    a

    ce

    d

    b2

    45

    9

    6

    4

    5

    5

    a

    ce

    d

    b2

    45

    9

    6

    4

    5

    5

  • 7/31/2019 27 2 11 Spanning Trees

    8/23

    8

    Prims Algorithm

    Let V ={1,2,..,n} and U be the set ofvertices that makes the MST and T be theMST

    Initially : U = {1} and T =

    while (U V)

    let (u,v) be the lowest cost edge such that

    u U and v V-UT = T {(u,v)}

    U = U {v}

  • 7/31/2019 27 2 11 Spanning Trees

    9/23

    9

    Prims Algorithm implementation

    Initializationa. Pick a vertexr to be the root

    b. SetD(r) = 0, parent(r) = null

    c. For all vertices v V, v r, setD(v) =

    d. Insert all vertices into priority queueP,using distances as the keys

    a

    ce

    d

    b2

    45

    9

    6

    4

    5

    5

    e a b c d

    0

    Vertex Parent

    e -

  • 7/31/2019 27 2 11 Spanning Trees

    10/23

    10

    Prims Algorithm

    WhileP

    is not empty:

    1. Select the next vertex u to add to the tree

    u = P.deleteMin()

    2. Update the weight of each vertex w adjacent tou which is not in the tree (i.e., w P)

    Ifweight(u,w)< D(w),

    a.parent(w) = u

    b.D(w) = weight(u,w)c. Update the priority queue to reflect

    new distance forw

  • 7/31/2019 27 2 11 Spanning Trees

    11/23

    11

    Prims algorithm

    a

    ce

    d

    b2

    45

    9

    6

    4

    5

    5

    d b c a

    4 5 5

    Vertex Parent

    e -

    b e

    c e

    d e

    The MST initially consists of the vertex e, and we update

    the distances and parent for its adjacent vertices

  • 7/31/2019 27 2 11 Spanning Trees

    12/23

    12

    Prims algorithm

    a

    ce

    d

    b2

    45

    9

    6

    4

    5

    5

    a c b

    2 4 5

    Vertex Parent

    e -

    b e

    c d

    d e

    a d

  • 7/31/2019 27 2 11 Spanning Trees

    13/23

    13

    Prims algorithm

    a

    ce

    d

    b2

    45

    9

    6

    4

    5

    5

    c b

    4 5

    Vertex Parent

    e -

    b e

    c d

    d e

    a d

  • 7/31/2019 27 2 11 Spanning Trees

    14/23

    14

    Prims algorithm

    a

    ce

    d

    b2

    45

    9

    6

    4

    5

    5

    b

    5

    Vertex Parent

    e -

    b e

    c d

    d e

    a d

  • 7/31/2019 27 2 11 Spanning Trees

    15/23

    15

    Prims algorithm

    Vertex Parent

    e -

    b e

    c d

    d e

    a d

    a

    ce

    d

    b2

    45

    9

    6

    4

    5

    5

    The final minimum spanning tree

  • 7/31/2019 27 2 11 Spanning Trees

    16/23

    16

    Prims Algorithm Invariant

    At each step, we add the edge (u,v)s.t. the weight of(u,v) is minimumamong all edges where u is in thetree and v is not in the tree

    Each step maintains a minimumspanning tree of the vertices that

    have been included thus far

    When all vertices have beenincluded, we have a MST for thegraph!

  • 7/31/2019 27 2 11 Spanning Trees

    17/23

    17

    Running time of Prims algorithm

    Initialization of priority queue (array): O(|V|)

    Update loop: |V| calls Choosing vertex with minimum cost edge: O(|V|)

    Updating distance values of unconnectedvertices: each edge is considered only onceduring entire execution, for a total of O(|E|)

    updates

    Overall cost: O(|E| + |V| 2)

  • 7/31/2019 27 2 11 Spanning Trees

    18/23

    18

    Another Approach Kruskals

    a

    ce

    d

    b2

    45

    9

    6

    4

    5

    5

    Create a forest of trees from the vertices Repeatedly merge trees by adding safe edges

    until only one tree remains

    A safe edge is an edge of minimum weight which

    does not create a cycle

    forest: {a}, {b}, {c}, {d}, {e}

  • 7/31/2019 27 2 11 Spanning Trees

    19/23

    19

    Kruskals algorithm

    Initializationa. Create a set for each vertex v V

    b. Initialize the set of safe edgesA

    comprising the MST to the empty set

    c. Sort edges by increasing weight

    a

    ce

    d

    b2

    45

    9

    6

    4

    5

    5

    {a}, {b}, {c}, {d}, {e}

    A =

    E = {(a,d), (c,d), (d,e), (a,c),

    (b,e), (c,e), (b,d), (a,b)}

  • 7/31/2019 27 2 11 Spanning Trees

    20/23

    20

    Kruskals algorithm

    For each edge (u,v) E in increasing orderwhile more than one set remains:

    Ifu and v, belong to different sets

    a.A = A{(u,v)}

    b. merge the sets containing u and v

    ReturnA

    Use Union-Find algorithm to efficientlydetermine ifuand v belong to differentsets

  • 7/31/2019 27 2 11 Spanning Trees

    21/23

    21

    Kruskals algorithm

    E = {(a,d), (c,d), (d,e), (a,c),

    (b,e), (c,e), (b,d), (a,b)}

    Forest

    {a}, {b}, {c}, {d}, {e}

    {a,d}, {b}, {c}, {e}

    {a,d,c}, {b}, {e}{a,d,c,e}, {b}

    {a,d,c,e,b}

    A

    {(a,d)}

    {(a,d), (c,d)}{(a,d), (c,d), (d,e)}

    {(a,d), (c,d), (d,e), (b,e)}

    a

    ce

    d

    b2

    45

    96

    4

    5

    5

  • 7/31/2019 27 2 11 Spanning Trees

    22/23

    22

    After each iteration, every tree in theforest is a MST of the vertices it connects

    Algorithm terminates when all vertices areconnected into one tree

    Kruskals Algorithm Invariant

  • 7/31/2019 27 2 11 Spanning Trees

    23/23

    23

    Greedy Approach

    Like Dijkstras algorithm, both Prims andKruskals algorithms are greedyalgorithms

    The greedy approach works for the MSTproblem; however, it does not work formany other problems!


Recommended