What Is Discrete (Decision) Maths

Total Page:16

File Type:pdf, Size:1020Kb

What Is Discrete (Decision) Maths

Prim’s Algorithm

Prim’s algorithm also finds a minimum connector for a graph. The difference between Prim and Kruskal is that Prim’s algorithm builds up a connected graph, at each stage choosing the shortest edge to a new node from the existing graph. Like Kruskal’s, Prim‘s is also greedy.

The Algorithm

Step 1 Select a starting node

Step 2 Connect it to the NEAREST node i.e. the node that is joined to the starting node by the least weight. If there is more than one then it is a free choice.

Step 3 Connect the tree to the nearest vertex to the CONNECTED set. This is not necessarily from the node just connected. Again there may be a free choice.

Step 4 Repeat step 3 until all the nodes are connected.

Example Use Prim’s algorithm to find a minimum spanning tree for the following network and the value of that network.

LANG

£900 £600 £300 6th FORM £300 £200 OFFICE IT £400 £200 £300

LIBRARY

Choose 6th form as the starting point.

Nearest node is IT 6th FORM IT

Nearest is now either Library or Office so we have a free choice.

Choose Library 6th FORM IT

LIBRARY Nearest is now the Office from IT

OFFICE 6th FORM IT

LIBRARY

Nearest is now Languages from Office

LANGUAGES

OFFICE 6th FORM IT

LIBRARY

All the nodes are now connected so we have a minimum spanning tree. The value of this tree is £1000

Matrix Representation of Prim’s Algorithm

Whilst we may use Kruskal’s or Prim’s algorithm in other parts of D1 it is rare that a question will appear on the exam that is just to do with creating a minimum spanning tree. In exams you will be more likely to get a question in which you will be expected to apply Prim’s algorithm to a matrix. Again we follow an algorithm.

The Algorithm

Step 1 Choose a starting node. Delete the ROW corresponding to that node.

Step 2 Label the COLUMN corresponding to the starting vertex with a ‘1’ and ring the SMALLEST UNDELETED ENTRY in the column. If there is more than one entry that is the smallest then we have a free choice.

Step 3 Delete the ROW corresponding to the entry you have just ringed (this prevents you from revisiting any nodes).

Step 4 Label the COLUMN corresponding to the node you have just ringed ‘2’.

Step 5 Ring the SMALLEST UNDELETED ENTRY OUT OF ALL LABELLED COLUMNS (again there may be a choice).

Step 6 Repeat steps 3, 4 and 5 until all rows are deleted. Example Using the previous example but this time in matrix form.

Languages 6 Form IT Library Office Languages 900 600 300 6 Form 900 300 400 IT 600 300 200 200 Library 400 200 300 Office 300 200 300

Start at languages. Label the Languages column 1 and cross out the Languages row.

1 Languages 6 Form IT Library Office Languages 900 600 300 6 Form 900 300 400 IT 600 300 200 200 Library 400 200 300 Office 300 200 300

Smallest uncovered value in the Languages column is 300, which corresponds to the office. Circle this value; label the Office column ‘2’ and then cross out the Office row.

1 2 Languages 6 Form IT Library Office Languages 900 600 300 6 Form 900 300 400 IT 600 300 200 200 Library 400 200 300 Office 300 200 300

Smallest uncovered value in EITHER the Languages column or Office column is 200, which corresponds to IT. Circle this value, label the IT column 3 and cross out the IT row.

1 3 2 Languages 6 Form IT Library Office Languages 900 600 300 6 Form 900 300 400 IT 600 300 200 200 Library 400 200 300 Office 300 200 300

Smallest uncovered value in EITHER the Languages, Office or IT columns is 200, which corresponds to Library. Circle this value, label the Library column 4 and cross out the Library row. 1 3 4 2 Languages 6 Form IT Library Office Languages 900 600 300 6 Form 900 300 400 IT 600 300 200 200 Library 400 200 300 Office 300 200 300

Smallest uncovered value in EITHER the Languages, Office IT or Library columns is 300, which corresponds to 6th Form. Circle this value, label the 6th Form column 5 and cross out the 6th Form row.

1 5 3 4 2 Languages 6 Form IT Library Office Languages 900 600 300 6 Form 900 300 400 IT 600 300 200 200 Library 400 200 300 Office 300 200 300

All rows are now deleted and the circled numbers gives us both the way the minimum spanning tree is connected (i.e. IT to 6th Form and IT to Library etc) as well as the value. From this we can draw the minimum spanning tree.

Languages

Office

6th Form IT

Library

Value = 300 + 300 + 200 + 200 = 1000

Example Write down the matrix that represents the following network. Apply Prim’s algorithm to this matrix to find the minimum spanning tree and its value.

B 8 A 3 E 4 5 1 6 D 7 C Solution starting at A

1 5 4 2 3 A B C D E A 8 5 B 8 4 3 C 4 7 1 D 5 7 6 E 3 1 6

Minimum spanning tree is AD, DE, EB, EC with total weight 5 + 6 + 3 + 1 = 15

Get them to experiment to see what happens when a different node is selected as the starting node.

P35 Ex 3A Questions 2c, 3 & 5 P39 Ex 3B P45 Miscellaneous Ex Question 1, 2, 3, 5, 7 & 8 Worksheet

Recommended publications