03 Network Algorithms
03 Network Algorithms
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.
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.
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.
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.
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.
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.
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.
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
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.
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.
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
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
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.
1. (a) Use Dijkstra’s algorithm to find the shortest path from A to F. Show all necessary working.
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.
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.
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.
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.
15
3. (i) The five points on the graph below may
be connected in pairs by straight lines.
(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