0% found this document useful (0 votes)
12 views16 pages

03 Network Algorithms

This document discusses network algorithms, specifically focusing on minimum spanning trees (MST) and their applications in real-world problems such as connecting towns with minimal cable. It details two greedy algorithms, Kruskal's and Prim's, for constructing MSTs, including step-by-step procedures and examples. Additionally, it introduces Dijkstra's algorithm for finding the shortest paths in networks, emphasizing its efficiency for computer implementation.

Uploaded by

Fatima Muhaimin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views16 pages

03 Network Algorithms

This document discusses network algorithms, specifically focusing on minimum spanning trees (MST) and their applications in real-world problems such as connecting towns with minimal cable. It details two greedy algorithms, Kruskal's and Prim's, for constructing MSTs, including step-by-step procedures and examples. Additionally, it introduces Dijkstra's algorithm for finding the shortest paths in networks, emphasizing its efficiency for computer implementation.

Uploaded by

Fatima Muhaimin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Modelling

With
Algorithms
3. Network Algorithms
This chapter continues the work done on graphs in Chapter 2. For many real-world problems we need to
know more than just whether nodes are connected or not. For example, problems involving the distance
between towns or the costs of various links require a numerical value to be given to each edge. Such a
numerical value is called a weight. A graph whose edges have weights is called a network.
Above is an example of the artwork developed by Robert Bosch from 2001, using a solution to the the
travelling salesperson problem. Each vertex in the network is travelled to exactly once and each vertex is
joined to the closest vertex that hasn’t previously been visited.

Minimum Connector Problems

We need to consider effective ways of connecting a network. A cable TV company, in Plymouth, wishes to
make connections in the most economical way to all the towns in the south west shown on the map.
They must link all towns using as little cable as possible.

What is the minimum length of cable needed to connect the towns?

You will remember that a set of edges with no loops or cycles is called a tree. The set of edges which
solves the minimum connector problem is called the minimum spanning tree (MST) for the network. It
is a subgraph (part of the whole graph) of minimum total weight.

We will consider two algorithms that can be used to solve the cable TV problem. Both are examples of
“greedy algorithms”: ones which give an optimal solution based on only one factor and doesn’t consider
other relevant factors. The algorithms developed by Robert Prim and Joseph Kruskal may be applied to a
network or a matrix of information. Kruskal’s and Prim’s algorithms do produce optimal solutions if
applied correctly.
1
Kruskal’s Algorithm

Kruskal’s algorithm builds a minimum spanning tree by adding one edge at a time (and associated
vertices) to a subgraph.
The subgraph need not be connected at intermediate stages, though the final subgraph is connected.

• First list the edges in ascending order of weight.


• Select the least weight edge of the network .
• Work through the network selecting, from those edges that have not yet been selected, the least
wieght edge that does not link vertices between which a route has already been created.
• If at any stage there is a choice of least weight edges, choose arbitrarily between them.
• Calculate the weight of the minimum spanning tree.

This will gradually build up a tree. There must be no cycles in the network or it will not be a tree and you
will have made some unnecessary connections. Kruskal’s algorithm is not particularly well suited to
computer implementation as it requires the edges to be arranged in order of length. This would mean
using one of the sorting algorithms. It also needs a way of recognising when cycles might be created,
which is particularly tricky on a computer.
For a network with ‘n’ vertices, minimum spanning tree will have ‘n – 1’ edges.

List all the edges in the network in ascending order:

List the order edges are selected:

Minimum Spanning Tree Weight =

Watch – FMSP video 2.3 ‘Kruskals Algorithm’ on our site. 2


Practice Questions 1 – Kruskal’s Algorithm

1. The owner of a caravan site has caravans positioned as shown in the diagram, with distances in
metres between, and wants to lay on a water supply to each of them. Use Kruskal’s algorithm to
determine how the caravans should be connected so that the total length of pipe required is a
minimum.

2. Use Kruskal’s algorithm to find a minimum


connector for this network, shown in Figure 3.6.
List the arcs that are used in the order that they
are included and give the total weight of the
minimum spanning tree.

3. Use Kruskal’s algorithm to find a


minimum connector for this network.
List the arcs that are used in the order that
they are included and give the total weight
of the minimum spanning tree.

3
4. Use Kruskal’s algorithm to find a minimum connector for this network. Show all appropriate detail and
the weight of the minimum spanning tree.

Applying Kruskal’s Algorithm Matrix/Tabluar Form


If a network does not have any directed edges, the matrix representing it will be symmetrical. Ignoring
the leading diagonal, the upper half of the matrix represents the edges contained in the network (the
lower half are the same edges).

• Again, firstly we can list the edges in ascending order of weight.


• Select the least weight edge of the network.
• Work through the network selecting, from those edges that have not yet been selected, the least
weight edge that does not link vertices between which a route has already been created.
• If at any stage there is a choice of least weight edges, choose arbitrarily between them.
• Calculate the weight of the minimum spanning tree.

The edges in ascending order:


A B C D E F 9,10,12,13,14,15,17,18,20,21,22,24,25,29,31
A
The order edges are selected:
B 1st – DE (9)
2nd – AF (10)
C 3rd – AB (12) - Note BF (13) rejected
D 4th – CF (14) as it forms a cycle
5th – AD (15)
E
F Minimum Spanning Tree Weight = 60

4
Prim’s Algorithm

Prim’s algorithm builds a minimum spanning tree by adding one vertex at a time (and an associated
edge) to a connected subgraph. It has two forms, graphical and matrix.

Method by graph
• Choose a starting vertex.
• Connect it to the “nearest” vertex. That is, out of all the edges joined to the start vertex, choose the
one with the minimum weight. (There might be a choice here.)
• Connect to the tree of connected vertices that vertex which is nearest to the connected set.
(There may be a choice, chose at random) (N.B. Not necessarily to the last vertex connected)
• Repeat until all vertices are connected.
• Calculate the length of the minimum spanning tree.

List the order vertices are selected:

Minimum Spanning Tree Weight =

Watch – FMSP video 2.1 ‘Prims Algorithm’ on our site. 5


Practice Questions 2 – Prim’s Algorithm
1. Use Prim’s algorithm to find a minimum spanning tree for the
network shown to the right. List clearly the order your vertices
join the minimum spanning tree.

2. Use Prim’s algorithm, starting at Stratford-


upon-Avon, to find a minimum connector for this
network given in Figure 3.7.
List the arcs that are used in the order that they
are included and give the total weight of the
minimum spanning tree.

3. The map, right, shows six cities A,B,C,D,E and F together


with the costs involved (£ million) in building pipelines
to connect them together. Use Prim’s algorithm to find
the least cost pipe network which connects all six cities together.

4. Use Prim’s algorithm, starting from Carlisle,


to find a minimum connector for the network
shown in Figure 3.8.

6
Applying Prim’s Algorithm Matrix/Tabluar Form
If a network does not have any directed edges, the matrix representing it will be symmetrical and we can
apply Prim’s algorithm without much difficulty. (Problems arise in networks with directed edges).

Step 1 Start with the matrix representing the network and choose a starting vertex (Usually the first one
in the matrix unless told otherwise). Delete the row corresponding to that vertex.
Step 2 Label with a 1 the column corresponding to the start vertex and ring the smallest entry in
that column. (There may be a choice of entry to ring.)
Step 3 Delete the row corresponding to the entry you have just ringed.
Step 4 Label with the next number the column corresponding to the vertex you have just ringed.
Step 5 Ring the lowest undeleted entry from all the columns numbered so far. (There may be a choice.)
Step 6 Repeat instructions 3, 4 and 5 until all the rows are deleted. The ringed entries represent the
minimum connector and may be added to calculate the minimum spanning tree.

The table method works because you start with a vertex and add connections to it. The columns that you
number are connected once they are numbered and further connections may be made to those vertices
only. Also deleting the row corresponding to that vertex stops us from trying to connect it again and thus
making a cycle. It is equivalent to the graphical method of Prim’s algorithm.

1 3 4 5 6 2
A B C D E F
This is what our final matrix should look like after running
A the method, with annotations. The weight of the minimum
spanning tree is the sum of the numbers circled.
B With ‘n’ vertices we have ‘n-1’edges used in the solution.
C But how did we get here, lets run through step by step.
D
E
F

Use Prim’s algorithm on the matrix, starting from A,


A B C D E F to find a minimum connector.
A
B
C
D
E
F

7
Practice Questions 3 – Prim’s & Kruskal’s Algorithm

1. The table gives the distance (in miles) between six places in Ireland.
Use the tabular form of Prim’s algorithm to find a minimum spanning tree connecting these places.

Athlone Dublin Galway Limerick Sligo Wexford


Athlone - 78 56 73 71 114
Dublin 78 - 132 121 135 96
Galway 56 132 - 64 80 154
Limerick 73 121 64 - 144 116
Sligo 71 135 80 144 - 185
Wexford 114 96 154 116 185 -

2. An international language publisher employs authors to produce books in their native languages.
Translations are then made so that the books can be sold in other countries, although not all books are
suitable for all countries. The costs (£00s) of making translations between 10 principal languages, where
translators are available, are as follows

From Language

A B C D E F G H I J
A - 3 3 10 3 4 - 2 1 2
B 3 - 2 4 - 1 1 4 2 1
To Language

C 2 1 - 2 - 4 1 1 - 3
D 10 4 3 - 5 - 3 6 2 1
E 3 - - 5 - 6 4 5 3 5
F 4 1 6 - 6 - 2 3 1 1
G - 1 2 3 4 4 - 2 4 5
H 2 4 2 6 5 5 2 - 3 1
I 1 2 - 2 3 2 4 3 - 2
J 2 1 4 1 5 1 5 1 2 -

(a) A book has been written in language F, and the publisher wishes to have it translated into all other
languages (a worldwide translation). Find the minimum cost method of achieving this.

(b) The cost is not symmetric. What effect does this have? Would the cost of making a worldwide
translation be the same for a book originally written in language C?

(c) Why would translating from language D to language A never be used in making a worldwide
translation? Is it worth retaining the services of the A to D translator?

8
3. Using Prim’s algorithm in tabular form and starting from Malvern, find a minimum connector for this
network. List the arcs that are used in the order that they are included and give the total weight of the
minimum spanning tree.

4. Use Prim’s algorithm on the matrix, starting from Dorchester, to find a minimum connector for this
network.

Watch – FMSP video 2.2 ‘Prims Matrix Method’ on our site.


– Complete ‘MEI MWA Network Algorithms – Minimum Spanning Trees Exercise 1’.
– Complete ‘MEI MWA Network Algorithms – Minimum Spanning Trees Exercise 2’ .
9
Shortest Path Problems
Dijkstra is pronounced “Dike – stra”
Dijkstra’s Algorithm

Dijkstra provided an algorithm for the shortest route through a network.


Dijkstra’s algorithm is another greedy algorithm, it will give an optimal solution based only on the weight
used in the network. It is also an example of a labelling algorithm. As you pass through the network
each fresh vertex is assigned a label. This label indicates the path through the network and gives the
shortest “distance” from the start or source vertex. The labels also give opportunity for improving working
values as you go.
Remember that the algorithm is designed for computer use on large networks.

The standard notation is that the Start vertex is labelled S and the Terminus vertex is labelled T
Boxes should be drawn for each vertex like this:

vertex permanent
number label

temporary labels

Temporary labels show the total weight so far from S to this vertex via the permanent labels.
Temporary labels may be crossed out and replaced by lower values.
The vertex number is the order that the vertices were assigned a permanent label.

Step 1 Assign the permanent label 1 to the starting vertex, 0 may also be used as the first label.

Step 2 Assign temporary labels to all the vertices that are connected directly to the most recent
permanently labelled vertex.

Step 3 Choose the vertex with the smallest temporary label and assign a permanent label to that
vertex. Be careful – you may need to choose one from earlier that you have forgotten about!
There may be a choice of several equal temporary labels, in which case randomly choose.

Step 4 Repeat steps 2 and 3 until all vertices have permanent labels.

Step 5 Find the shortest path by tracing backwards through the network to the start vertex. It is
tempting to try to avoid tracing back by keeping a record of the route whilst labelling, but don’t,
as this is much less efficient.

10
11
Dijkstra’s Algorithm From A Matrix

It is not necessary to draw the network and


the boxes to apply Dijkstra’s algorithm to find
a shortest path between two vertices. We will
apply the algorithm to the table, to find
the shortest path from Malvern to Evesham.

Malvern Ross

1 0
19

Worcester Tewksbury
First, we should fill in the box for Malvern, as this is our
2 8 starting vertex. The vertex number will be 1 and it will
13
have a permanent label of 0.
8

M M Next we can add temporary labels from Malvern to


other towns that are connected via edges shown in the
Hereford Gloucester table. Looking down the Malvern column, we can see 5
towns connected and so should add 5 temporary labels
in. These will be for Worcester, Hereford, Ross,
Tewksbury and Gloucester.
19 20
The least weight of these temporary labels is 8, so
M M Worcester can be made permanent.

Evesham Cheltenham Next, we can look down the Worcester column and add
more temporary labels from Worcester. There are three
more temporary labels to add, these will add onto the
permanent value of 8 that is already permanent for
Worcester.

Continue the algorithm until all the boxes are complete.


What is the shortest path from Malvern to Evesham and what is the route taken?

Practice Questions 4 - Dijkstra’s Algorithm

1. (a) Use Dijkstra’s algorithm to find the shortest path from A to F. Show all necessary working.

(b) A directed edge from C to D is now


added, with weight -4. What is the
shortest path from A to F now?

How can an edge have a negative weight?

Explain why Dijkstra’s algorithm cannot


be used to find this path.

12
2. The diagram shows a number of satellite towns A,B,C,D,E and F surrounding a city K.
The number on each edge is the travelling time in minutes.
Use Dijkstra’s algorithm to find the shortest time to travel from A to E.

3. Little Red Riding Hood wants to travel through the woods from her house (A) to Grandma’s house (G).
The network shows the possible paths that she may use and the time in minutes to travel each path.

(a) Use Dijkstra’s algorithm to find the quickest route from A to G. Show all your working clearly.

(b) The big bad wolf also wants to travel from A to G. He knows which route Little Red Riding Hood has
chosen and he must avoid using any of the edges on her route.
Find the quickest route from A to G for the big bad wolf.

(c) The big bad wolf travels twice as quickly as Little Red Riding Hood. Assuming the big bad wolf leaves A
at the same time and that they use the routes found above, work out how long the big bad wolf has to
wait at Grandma’s house before Little Red Riding Hood arrives.

13
4. Fares (in £) for direct flights between five cities are shown in the table.

A B C D E
A - 90 70 35 30
B 90 - 40 150 55
C 70 40 - 20 50
D 35 150 20 - 100
E 30 55 50 100 -

(a) Without drawing the network, use Dijkstra’s algorithm to find the cheapest routes from A to
every other city.

(b) Suppose each change of flight is estimated to cost an extra £10 of sundry expenses.
Find the cheapest routes now.

5. The network represents a set of 10 locations together with the times in minutes taken to
travel between them.

(a)(i) Use Dijkstra’s algorithm to find the quickest time and route to travel from A to I.
(ii) Use your working in part (i) to write down the quickest routes and times from A to D and from A to E.

(b) The locations are in fact restaurants and the times are those taken by a delivery lorry from a catering
supplies company. The lorry driver wishes to deliver to all of the restaurants, starting from and returning
to A. He applies the following algorithm.

Step 1: Go to the restaurant which can be reached from A in the quickest time.
Step 2: If there are any restaurants which have not been visited, then go to the unvisited restaurant
which can be reached directly (i.e. not via any other restaurant) in the shortest time.
Step 3: If all restaurants have been visited, return to A by the quickest route.

(i) Give the route followed by the driver and the time that it takes him.

(ii) Show that the travelling time can be reduced if the requirement for direct routes is removed, and give
the time taken in this case.

14
Watch – FMSP video 2.4 ‘Dijkstra’s Algorithm’ on our intranet

– Complete “MEI MWA Algorithms – Dijkstra's Algorithm Exercise 1” from our site.
– Complete “MEI MWA Algorithms – Dijkstra's Algorithm Exercise 2” from our site.

Algorithmic Complexity

In real life, as the size of the problems increases, the amount of work involved in creating a solution can
become much greater. You met the concept of complexity in the algorithms chapter. You should be
familiar with O(n), O(n2) and O(n3). The complexity gives an indication of the time it will take to solve a
problem using a particular algorithm, when you consider a larger problem size.

Kruskals and Prims algorithms have quadratic complexity as a function of the number of edges.

Dijkstra’s algorithm has quadratic complexity as a function of the number of vertices.

For example, for algorithms with quadratic complexity, doubling the number of arcs will quadruple the
time taken to perform the algorithm and achieve the solution.

Practice Questions 5 - Complexity

1. A computer takes 0.02 seconds to solve a minimum connector problem, with a set number of edges.
Roughly how long will it take for a problem that is ten times bigger?

2. A student takes 20 seconds to solve a shortest path problem on a network with 12 vertices using
Dijkstra’s algorithm. Approximately how long would you expect the student to take to solve a shortest
path problem on a network with 30 vertices?

3. Calculate the maximum possible number of comparisons and the maximum possible number of
additions used when Dijkstra’s algorithm is applied on a network with n=5 vertices.

Practice Questions 6 - Additional Questions

1. Use an appropriate algorithm to find a minimum connector for the following network.
Give the name of your algorithm and the total weight of the minimum connector.

2. The weights on the network in the diagram represent distances.

(i) Apply Dijkstras algorithm to find the


shortest route from A to F.

(ii) Give the shortest route and its length.

15
3. (i) The five points on the graph below may
be connected in pairs by straight lines.

Use a greedy algorithm to find a connector


of minimum length for the five points.
Show your answer on a copy of the
diagram, explain your method and give the
length of your minimum connector.

(ii) The points on the graph below may be connected


in pairs by straight lines.

a) Find a minimum connector for the four points.

b) Find a fifth point, so that the minimum connector


for all five points is shorter than the minimum
connector for the original four points. Show your
minimum connector and give its length.

4. The diagram represents the roads joining ten villages, labelled A to J.


The numbers give distances in kilometres.

(i) Use Dijkstras algorithm to find a shortest route from A to J. Explain the method carefully and show all
of your working. Give your shortest route and its length.

A driver usually completes this journey at 60 kmph -1. The local radio reports a serious fire at village E and
warns of a delay of 10 minutes.

(ii) Describe how to modify your approach to (i) to find the quickest route, explaining how to take account
of this information. What was the quickest route and how long will it take now?

16

You might also like