prim algo

Embed Size (px)

Citation preview

  • 7/31/2019 prim algo

    1/20

  • 7/31/2019 prim algo

    2/20

    Wiltshire

    Prim`s Algorithm

    n In Lesson 1 we learnt about Kruskal`s algorithm,which was used to solve minimum connectorproblems.

    n Another method that can be used is Prim`salgorithm.

    n Step 1 Select any node

    n Step 2 Connect it to the nearest node

    n

    Step 3 Connect one node already selected tothe nearest unconnected node.

    n Step 4 Repeat 3 until all nodes are connected.

  • 7/31/2019 prim algo

    3/20

    Wiltshire

    Prim`s Algorithm

    n Consider the example we looked at last lesson.

    n Select any node you like.

    n Lets select F.

  • 7/31/2019 prim algo

    4/20

    Wiltshire

    Prim`s Algorithm

    n Connect it to the nearest node.

    n C and D are both 3 away so we can choose either.

    n Lets select C.

  • 7/31/2019 prim algo

    5/20

    Wiltshire

    Prim`s Algorithm

    n The nearest node to either of F or C is D, which is only3 away from F.

    n So connect D to F.

  • 7/31/2019 prim algo

    6/20

    Wiltshire

    Prim`s Algorithm

    n The nearest to D, F or C is E which is 2 from D.

    n So connect E to D.

  • 7/31/2019 prim algo

    7/20

    Wiltshire

    Prim`s Algorithm

    n The nearest to any of these four nodes is A which is 5away from F.

    n Connect A to F.

  • 7/31/2019 prim algo

    8/20

    Wiltshire

    Prim`s Algorithm

    n We now need to connect the last node, B.

    n The shortest arc is AB, which is 2.

    n Connect B to A.

  • 7/31/2019 prim algo

    9/20

    Wiltshire

    Prim`s Algorithm

    n All the nodes are now connected so this is theminimum connector or minimal spanning tree.

  • 7/31/2019 prim algo

    10/20

  • 7/31/2019 prim algo

    11/20

    Wiltshire

    Prim`s on a Distance Table

    n We are going to apply Prim`s algorithm tothe distance table.

    n This demonstrates how a computer could

    apply the algorithm.n Prim`s is more suitable than Kruskal`s as

    computers have a problem recognisingloops.

    nAs you go through the algorithm, see if youcan relate the procedure to the last example.

  • 7/31/2019 prim algo

    12/20

    Wiltshire

    Prim`s on a Distance Table

    n Step 1 Select any arbitrary node.

    n Step 2 Delete the row and loop the columnthat correspond to the node selected.

    n Step 3 Choose the smallest number in the loop.

    n Step 4 Delete the row that this smallest number isin.

    n Step 5 Loop the column that corresponds to therow just deleted.

    n Step 6 Choose the smallest number in any loop.

    n Step 7 Repeat steps 4, 5 and 6 until all rows havebeen deleted and columns looped.

  • 7/31/2019 prim algo

    13/20

    Wiltshire

    Prim`s on a Distance Table

    n Here I have chosen F.

    n Delete the row.

    n Loop the column.

    n Select the smallest number in the loop.

  • 7/31/2019 prim algo

    14/20

    Wiltshire

    Prim`s on a Distance Table

    n Delete row C.

    n Loop column C.

    n Select the smallest number in any loop that is notcrossed out.

  • 7/31/2019 prim algo

    15/20

    Wiltshire

    Prim`s on a Distance Table

    n Delete row D.

    n Loop column D.

    n Select the smallest number in any loop that is notcrossed out.

  • 7/31/2019 prim algo

    16/20

    Wiltshire

    Prim`s on a Distance Table

    n Delete row E.

    n Loop column E.

    n Select the smallest number in any loop that is notcrossed out.

  • 7/31/2019 prim algo

    17/20

    Wiltshire

    Prim`s on a Distance Table

    n Delete row A.

    n Loop column A.

    n Select the smallest number in any loop that is notcrossed out.

  • 7/31/2019 prim algo

    18/20

    Wiltshire

    Prim`s on a Distance Table

    n Delete row B.

    n Loop column B.

  • 7/31/2019 prim algo

    19/20

    Wiltshire

    Prim`s on a Distance Table

    n The algorithm is complete when all the columns havebeen looped and the rows crossed out.

    n The circles show the edges in the minimum connector.

  • 7/31/2019 prim algo

    20/20

    Wiltshire

    Prim`s on a Distance Table

    n In this case they are AB, DE, AF, CF, DF

    n Can you explain why this procedure is exactly the sameas applying Prim`s algorithm?