PDST Information Workshops – Autumn 2018

Prim’s Algorithm

Prim’s algorithm was developed in 1930 by Czech mathematician Vojtěch Jarník and later rediscovered and republished by computer scientists Robert C. Prim in 1957 and Edsger W. Dijkstra in 1959. Prim's algorithm is a greedy algorithm that finds a minimum spanning tree (MST) for a weighted undirected graph. This means it finds a subset of the edges that forms a tree that includes every vertex, where the total weight of all the edges in the tree is minimized. The algorithm operates by building this tree one vertex at a time, from an arbitrary starting vertex, at each step adding the cheapest possible connection from the tree to another vertex. Criteria for creating MST using Prims Algorithm

 You can choose any starting vertex  Continue by selecting the next lowest vertex attached to the tree.  You can only select a vertex once.  You cannot select a vertex that would produce a cycle (closed loop)  The number of edges in the MST is calculated by: #edges = (#vertices, -1).

Vojtěch Jarník, Robert C Prim & Edsger W Dijkstra

Page | 1

PDST Information Workshops – Autumn 2018

Prim’s Algorithm (Practical Example 1)

A telecommunications company is contracted to connect six towns in and South Tipperary with high speed fibre optic cabling. Determine the lowest price to complete the project if the length of protective piping and fibre optic cable is estimated to cost €10,000 per kilometre. Details of the network framework is shown in Figure 1

Cahir

30 40

Mallow 45

Macroom 35 30

40 25 Cork 30

30 55

Bandon

Figure 1

https://commons.wikimedia.org/wiki/File: 1

Page | 2

PDST Information Workshops – Autumn 2018

Instructions

1. Select any random town (vertex) as the starting point (e.g. Mallow). Figure 2.

Cahir

30 40

Mallow Fermoy 45

Macroom 35 30

40 25

Cork 30 Midleton

30 55

Bandon Figure 2

2. Select the shortest distance (Edge) from this point to the next unvisited town. You can only visit a town (Vertex) once. Figure 3.

Cahir

30 40

Mallow Fermoy 45

Macroom 35 30

40 25 Cork Midleton 30

30 55

Bandon

Figure 3

Page | 3

PDST Information Workshops – Autumn 2018

3. Continue to select the shortest distances to the next unvisited town without completing a cycle (acyclic graph). Figure 4.

Cahir

30 40

Mallow Fermoy

45

Macroom 35 30

40 25 Cork 30 Midleton

30 55

Bandon Figure 4

4. Continue to select shortest distances to the next unvisited town (vertex & edge) without completing a cycle. Figure 5.

Cahir

30 40

Mallow Fermoy

45

Macroom 35 30

40 25 Cork 30 Midleton

30 55

Bandon Figure 5

Page | 4

PDST Information Workshops – Autumn 2018

5. Continue to select unvisited vertex, but graph must remain acyclic. Figure 6.

Cahir

30 40

Mallow Fermoy 45

Macroom 35 30

40 25 Cork 30 Midleton

30 55

Bandon Figure 6

6. In figure 7, Cork to Mallow cannot be used as it would form a cycle as would selecting Bandon to Midleton. Therefore Bandon to Macroom (30 Km’s) is chosen as the shortest remaining distance.

Cahir

30 40

Mallow Fermoy 45

Macroom 35 30

40 25

Cork 30 Midleton

30 55

Bandon Figure 7

Page | 5

PDST Information Workshops – Autumn 2018

In figure 8 the only remaining option is Fermoy to Cahir as any other route selected would form a cycle. This completed the graph.

Cahir

30 40

Mallow Fermoy

45

Macroom 35 30

40 25 Cork 30 Midleton

30 55

Bandon Figure 8

Formula for the total number of edges is: #edges = (#vertex, -1)

#edges = (7 -1) #edges = 6

Cahir

30 40

Mallow Fermoy

Macroom 30

25

Cork 30 Midleton

30

Bandon Figure 9

Page | 6

PDST Information Workshops – Autumn 2018

The shortest length of fibre optic cabling & therefore the lowest cost to connect all towns in the project is €1,850,000.

From To Distance (km) Cost Macroom Bandon 30 €300,000

Bandon Cork 30 €300,000 Cork Midleton 25 €250,000 Midleton Fermoy 30 €300,000 Fermoy Mallow 30 €300,000 Fermoy Cahir 40 €400,000

Total 185 €1,850,000

https://commons.wikimedia.org/wiki

Page | 7