d1,l5 1 kruskal's and prim's algorithms

Upload: mizmell

Post on 03-Apr-2018

230 views

Category:

Documents


1 download

TRANSCRIPT

  • 7/29/2019 D1,L5 1 Kruskal's and Prim's Algorithms

    1/18

    Minimum spanning trees

  • 7/29/2019 D1,L5 1 Kruskal's and Prim's Algorithms

    2/18

    Minimum Connector Algorithms

    Kruskals algorithm

    1. Select the shortest edge in a

    network

    2. Select the next shortest edge

    which does not create a cycle

    3. Repeat step 2 until all vertices

    have been connected

    Prims algorithm

    1. Select any vertex

    2. Select the shortest edgeconnected to that vertex

    3. Select the shortest edge

    connected to any vertex

    already connected

    4. Repeat step 3 until all

    vertices have been

    connected

  • 7/29/2019 D1,L5 1 Kruskal's and Prim's Algorithms

    3/18

    A cable company want to connect five villages to their network

    which currently extends to the market town of Avonford. What is

    the minimum length of cable needed?

    Avonford Fingley

    Brinleigh Cornwell

    Donster

    Edan

    2

    7

    4

    5

    8 64

    5

    3

    8

    Example

  • 7/29/2019 D1,L5 1 Kruskal's and Prim's Algorithms

    4/18

    We model the situation as a network, then theproblem is to find the minimum connector for the

    network

    A F

    B C

    D

    E

    2

    7

    4

    5

    8 64

    5

    3

    8

  • 7/29/2019 D1,L5 1 Kruskal's and Prim's Algorithms

    5/18

    AF

    BC

    D

    E

    2

    7

    45

    86

    4

    5

    3

    8

    List the edges in

    order of size:

    ED 2

    AB 3

    AE 4

    CD 4BC 5

    EF 5

    CF 6

    AF 7

    BF 8DF 8

    Kruskals Algorithm

  • 7/29/2019 D1,L5 1 Kruskal's and Prim's Algorithms

    6/18

    Select the shortest

    edge in the network

    ED 2

    Kruskals Algorithm

    AF

    BC

    D

    E

    2

    7

    45

    86

    4

    5

    3

    8

  • 7/29/2019 D1,L5 1 Kruskal's and Prim's Algorithms

    7/18

    Select the next shortest

    edge which does notcreate a cycle

    ED 2

    AB 3

    Kruskals Algorithm

    AF

    BC

    D

    E

    2

    7

    45

    86

    4

    5

    3

    8

  • 7/29/2019 D1,L5 1 Kruskal's and Prim's Algorithms

    8/18

    Select the next shortest

    edge which does notcreate a cycle

    ED 2

    AB 3

    CD 4 (or AE 4)

    Kruskals Algorithm

    AF

    BC

    D

    E

    2

    7

    45

    86

    4

    5

    3

    8

  • 7/29/2019 D1,L5 1 Kruskal's and Prim's Algorithms

    9/18

    Select the next shortest

    edge which does notcreate a cycle

    ED 2

    AB 3

    CD 4AE 4

    Kruskals Algorithm

    AF

    BC

    D

    E

    2

    7

    45

    86

    4

    5

    3

    8

  • 7/29/2019 D1,L5 1 Kruskal's and Prim's Algorithms

    10/18

    Select the next shortest

    edge which does notcreate a cycle

    ED 2

    AB 3

    CD 4AE 4

    BC 5 forms a cycle

    EF 5

    Kruskals Algorithm

    AF

    BC

    D

    E

    2

    7

    45

    86

    4

    5

    3

    8

  • 7/29/2019 D1,L5 1 Kruskal's and Prim's Algorithms

    11/18

    All vertices have been

    connected.

    The solution is

    ED 2

    AB 3CD 4

    AE 4

    EF 5

    Total weight of tree: 18

    Kruskals Algorithm

    AF

    BC

    D

    E

    2

    7

    45

    86

    4

    5

    3

    8

  • 7/29/2019 D1,L5 1 Kruskal's and Prim's Algorithms

    12/18

    AF

    BC

    D

    E

    2

    7

    45

    86

    4

    5

    3

    8

    Select any vertex

    A

    Select the shortest

    edge connected to

    that vertex

    AB 3

    Prims Algorithm

  • 7/29/2019 D1,L5 1 Kruskal's and Prim's Algorithms

    13/18

    AF

    BC

    D

    E

    2

    7

    45

    86

    4

    5

    3

    8

    Select the shortest

    edge connected toany vertex already

    connected.

    AE 4

    Prims Algorithm

  • 7/29/2019 D1,L5 1 Kruskal's and Prim's Algorithms

    14/18

    Select the shortest

    edge connected toany vertex already

    connected.

    ED 2

    Prims Algorithm

    AF

    BC

    D

    E

    2

    7

    45

    86

    4

    5

    3

    8

  • 7/29/2019 D1,L5 1 Kruskal's and Prim's Algorithms

    15/18

    Select the shortest

    edge connected toany vertex already

    connected.

    DC 4

    Prims Algorithm

    AF

    BC

    D

    E

    2

    7

    45

    86

    4

    5

    3

    8

  • 7/29/2019 D1,L5 1 Kruskal's and Prim's Algorithms

    16/18

    Select the shortest

    edge connected toany vertex already

    connected.

    CB 5 forms a cycle

    EF 5

    Prims Algorithm

    AF

    BC

    D

    E

    2

    7

    45

    86

    4

    5

    3

    8

  • 7/29/2019 D1,L5 1 Kruskal's and Prim's Algorithms

    17/18

    Prims Algorithm

    AF

    BC

    D

    E

    2

    7

    45

    86

    4

    5

    3

    8

    All vertices have been

    connected.

    The solution is

    ED 2

    AB 3CD 4

    AE 4

    EF 5

    Total weight of tree: 18

  • 7/29/2019 D1,L5 1 Kruskal's and Prim's Algorithms

    18/18

    Both algorithms will always give solutions with thesame length.

    They will usually select edges in a different order

    you must show this in your workings.

    Occasionally they will use different edges thismay happen when you have to choose betweenedges with the same length. In this case there is

    more than one minimum connector for thenetwork.

    Some points to note