Networks

A network is a graph in which one or more edges is assigned a number, called its weight.

Perhaps the easiest way to think of this is to imagine a graph showing the main roads between towns. The number on the edges would normally represent the distance that you have to travel along that road. Figure 1 shows such a network.

Figure 1. Network diagram of road connections between major towns in southern England.
Figure 1. Network diagram of road connections between major towns in southern England.

This graph of roads connecting towns in southern England has had the distances in miles added to the edges that represent the routes. This makes the graph a network.

The numbers that correspond to each edge are called weights.

The discrete maths that is included at A Level contains two main applications of networks. These are:

  1. Minimum connector. The tree that connects all of the vertices in the set with the minimum weight when all included edges are summed.
  2. Shortest path The shortest path that connects any two vertices in the set.

Minimum Connector

The minimum connector problem gives a way to join every vertex in a network so that the total weight of the edges used is minimised.

Figure 1: Roads connecting towns in southern England

The towns in southern England are to be connected with a new fibre-optic cable system. The hub of the system is to be in London. What is the minimum length of cable needed to connect the towns?

There are two algorithms that we can use to solve this type of problem:

  1. Kruskal’s algorithm
  2. Prim’s algorithm

We will look at each of these individually and see the benefits of Prim’s algorithm for computational purposes.

Kruskal’s Algorithm

Kruskal’s algorithm finds the minimum spanning tree for a network.

Kruskal’s algorithm has the following steps:

  1. Select the edge with the lowest weight that does not create a cycle. If there are two or more edges with the same weight choose one arbitrarily.
  2. Repeat step 1 until the graph is connected and a tree has been formed.

Example

Finding the minimum spanning tree that follows the road network in southern England

Using Kruskal’s algorithm the minimum spanning tree is generated as follows (each selected edge is coloured red). Click or tap an image for a larger view.

Figure 1: Roads connecting towns in southern England
Figure 1: Roads connecting towns in southern England
Figure 2: Kruskal solution step 1
Figure 3: Kruskal solution step 2
Figure 4: Kruskal solution step 3
Figure 5: Kruskal solution step 4
Figure 6: Kruskal solution step 5

The solution for the minimum spanning tree contains the edges {(Bristol, Swindon), (Swindon, Oxford), (Oxford, Reading), (Reading, London), (Reading, Southampton)}. The total weight (distance) for the minimum solution is 27 + 30 + 40 + 40 + 42 = 179 \text{(miles)}.

Prim’s Algorithm

Prim’s algorithm generates a minimum spanning tree for a network. Prim’s algorithm has the following steps:

  1. Select any vertex. Connect the nearest vertex.
  2. Find the vertex that is nearest to the current tree but not already connected and connect that.
  3. Repeat step 2 until all vertices are connected.

Often, assuming that you are doing this for the purposes of an exam, you will be told the starting vertex for step 1. If you find that there is more than one vertex equally close in step 2, choose one arbitrarily.

Example

Finding the minimum spanning tree that follows the road network in southern England.

Using Prim’s algorithm the minimum spanning tree is generated as follows (each selected edge and node is coloured red). Click or tap an image for a larger view.

Figure 1: simplified road network of southern England, Swindon selected as a start point.
Figure 2: Prim’s algorithm step 1, connecting Swindon to Oxford, the nearest adjacent node.
Figure 3: Prim’s algorithm step 2, connecting Oxford to Reading, the nearest node not already in the tree.
Figure 4: Prim’s algorithm step 2, connecting Swindon to Bristol, the nearest node not already in the tree. Bristol was chosen arbitrarily at this point, but London is the same distance and could have equally have been chosen.

Figure 5: Prim’s algorithm step 2, connecting Reading to London, the nearest node not already in the tree.

Figure 6: Prim’s algorithm step 2, connecting Reading to Southampton, the nearest node not already in the tree.

The solution contains the following edges {(Bristol, Swindon), (Swindon, Oxford), (Oxford, Reading), (Reading, London), (Reading, Southampton)} and the total weight (distance) for the minimum solution is 27 + 30 + 40 + 40 + 42 = 179 \text{(miles)}.

Other points to note

Prim’s Algorithm also has a table-based form that can easily be applied to matrices or distance tables. This means that it is particularly suited for applying to a computerised solution.

Due to the number of comparisons of weight needed at each iteration, Prim’s Algorithm is \text{O}(n^3).

Prim’s Algorithm – table form

Prim’s algorithm is also suitable for use on distance tables or matrices, or the equivalent for the problem. This is useful for large problems where drawing the network diagram would be hard or time-consuming.

That tables can be used makes the algorithm more suitable for automation than Kruskal’s algorithm. The reason for this is that the data used would have to be sorted to be used with Kruskal’s algorithm. With Prim’s algorithm, however, it is only the minimum value that is of interest, so no sorting is normally necessary.

We will look again at our question that requires a minimum spanning tree for the network of towns in the south of England using main road connections. The network diagram is as shown in figure 1.

Figure 1: Network of road connections in southern England.

The network shown in Figure 1 can be represented by the adjacency matrix shown in Table 1.

Bristol
London
Oxford
Reading
Southampton
Swindon
Bristol×××7540
London×6040×70
Oxford×6027×30
Reading×40274245
Southampton75××4270
Swindon4070304570
Table 1: tabular version of road network. × means no direct link.

The tabular form of Prim’s algorithms has the following steps:

  1. Select any vertex (town). Cross out its row. Select the shortest distance (lowest value) from the column(s) for the crossed out row(s). Highlight that value.
  2. Cross out the row with the newly highlighted value in. Repeat step 1. Continue until all rows are crossed out.
  3. Once all rows are crossed out, read off the connections. The column and the row of each highlighted value are the vertices that are linked and should be included.

Example

First we will choose a town at random – Swindon – and cross out that row. Then we highlight the smallest value in the column for the crossed out row.

Bristol
London
Oxford
Reading
Southampton
Swindon
Bristol×××7540
London×6040×70
Oxford×6027×30
Reading×40274245
Southampton75××4270
Swindon4070304570
Table 2: Prim’s algorithm first iteration.

Next we need to cross out the row with the newly-highlighted value in (the Oxford row). Then we look for, and highlight, the smallest value in the columns for the two crossed out rows (Swindon and Oxford).

Bristol
London
Oxford
Reading
Southampton
Swindon
Bristol×××7540
London×6040×70
Oxford×6027×30
Reading×40274245
Southampton75××4270
Swindon4070304570
Table 3: Prim’s algorithm second iteration.

Next we need to cross out the row with the newly-highlighted value in (the Reading row). Then we look for, and highlight, the smallest value in the columns for the three crossed out rows (Swindon, Oxford, and Reading).

Bristol
London
Oxford
Reading
Southampton
Swindon
Bristol×××7540
London×6040×70
Oxford×6027×30
Reading×40274245
Southampton75××4270
Swindon4070304570
Table 4: Prim’s algorithm third iteration.

Next we need to cross out the row with the newly-highlighted value in (the Bristol row). Then we look for, and highlight, the smallest value in the columns for the four crossed out rows (Swindon, Oxford, Reading, and Bristol).

Bristol
London
Oxford
Reading
Southampton
Swindon
Bristol×××7540
London×6040×70
Oxford×6027×30
Reading×40274245
Southampton75××4270
Swindon4070304570
Table 5: Prim’s algorithm fourth iteration.

Next we need to cross out the row with the newly-highlighted value in (the London row). Then we look for, and highlight, the smallest value in the columns for the crossed out rows (Swindon, Oxford, Reading, Bristol, and Southampton).

Bristol
London
Oxford
Reading
Southampton
Swindon
Bristol×××7540
London×6040×70
Oxford×6027×30
Reading×40274245
Southampton75××4270
Swindon4070304570
Table 6: Prim’s algorithm fifth and final iteration.

We’ve now selected a value from the last undeleted row. This means we’ve selected all the edges that we need to create the minimum spanning tree for the network. All we have left to do is write out the connections between the vertices.

The connections in the network are found by taking the row and column headings for each selected value in the table. The edges are: {(Bristol, Swindon), (London, Reading), (Oxford, Swindon), (Reading, Oxford), (Southampton, Reading)}. This is the set of edges as in the minimum spanning tree generated by the diagrammatic version of the algorithm.

Shortest Route

The second problem that we will consider for networks is that of finding the shortest route between any two vertices in the network. As the name suggests the shortest route problem is most commonly applied to transport networks. Other applications include, for example, cost minimisation. Although there are a number of algorithms that are designed to find minimum cost routes between vertices in a network the only algorithm with which you need to be familiar at A Level is Dijkstra’s algorithm.

Dijkstra’s Algorithm

Dijkstra’s algorithm generates the shortest path tree from a given node to any (or every) other node in the network.

Although the problem that we will use as an example is fairly trivial and can be solved by inspection, the technique that we will use can be applied to much larger problems.

Dijkstra’s algorithm requires that each node in the network be assigned values (labels). There is a working label and a permanent label, as well as an ordering label. Whilst going through the steps of the algorithm you will assign a working label to each vertex. The smallest working label at each iteration will become permanent.

The steps of Dijkstra’s algorithm are:

  1. Give the start point the permanent label of 0, and the ordering label 1.
  2. Any vertex directly connected to the last vertex given a permanent label is assigned a working label equal to the weight of the connecting edge added to the permanent label you are coming from. If it already has a working label replace it only if the new working label is lower.
  3. Select the minimum current working value in the network and make it the permanent label for that node.
  4. If the destination node has a permanent label go to step 5, otherwise go to step 2.
  5. Connect the destination to the start, working backwards. Select any edge for which the difference between the permanent labels at each end is equal to the weight of the edge.

It is a good idea to use a system for keeping track of the current working labels and the ordering and permanent labels of each of the nodes in the network. A standard system found on A Level exams is that a small grid is drawn near each of the nodes, working labels are written in the lower box, ordering labels in the upper left box, and permanent labels in the upper right box, as shown in figure 1.

Figure 1: Labelling system for Dijkstra’s Algorithm.

Example

In this example we will consider the network in figure 2. We would like to find the shortest path from node A to node H.

Network with 8 nodes.
Figure 2: A simple network.
The first four steps of the algorithm are completed.

As you can see at the end of the video, the destination node (node H) has a permanent label. This means that a shortest route from A to H has been found.

The shortest route is found by tracing back from the destination node (node H) to the start (node A), selecting each edge for which the difference between permanent values at its terminating nodes is equal to the weight of the edge. The shortest routes for this network are shown in figure 3 below. The edges shown in green (AB and BC) are an alternative to the edge AC shown in red. In this case there are two equivalent shortest routes from A to H.

Solution to shortest path on a network with 8 nodes.
Figure 3: The shortest routes from A to H in this network.

The solution to the shortest route problem, from A to H, in this network is therefore A-C-G-H, or the equivalent distance A-B-C-G-H. Both have a total distance of 11 units, given by the permanent label of the terminal node.

Critical Path Analysis – Introduction

Critical path analysis allows you to determine the best way of arranging activities. The typical question to answer is “what is the minimum time required for a process?”

Performing activities one after the other as you come to them might not be the best way of organising a project. If more than one activity can be worked on at the same time you will need to decide when to start each one. Below, in table 1, are the activities needed to build a house with durations given in days. We are going to perform a critical path analysis on the process of house construction.

TaskDescriptionDuration
AExcavate foundations2
BPour concrete1
CBuild walls3
DConstruct roof sections7
EPut up roof sections1
FTile roof3
GInstall plumbing4
HInstall electrics3
IInstall windows and doors2
JPaint ceilings2
KPaint walls3
LLay carpets2
Table 1: Activities involved in building a house

The first thing that we need to do is to decide which of the activities depend on other activities:

  • B requires that A be complete.
  • C requires that B be complete.
  • E requires that C and D be complete.
  • F requires that E be complete.
  • G, H, and I all require that F be complete.
  • J and K require that G, H, and I be complete.
  • L requires that J and K be complete.

With this information we can draw an activity network. In an activity network the edges represent activities, such as those listed above. The nodes represent events. An event is the start and/or finish of one or more activities. The activity network for the house building example is shown in figure 1.

Figure 1: The activity network for house building.
Figure 1: The activity network for house building.

The activity network shows that there are 12 events involved in the building of this house, and that 12 activities (plus 3 dummy activities) are required before the house is complete.

As you can see, there are a few activities that can occur concurrently. Later we will use this example to find the critical path through the operation (finding a critical path). It is the longest path and determines the minimum length of time that is necessary to complete the house.

Activity Networks

As we saw in the introduction to critical path analysis, an activity network is a graphical representation of activities and events that are needed for a process. There are several rules that have to be obeyed when creating accurate networks.

These rules are:

  1. An edge is used to represent an activity.
  2. A vertex represents an event, which is the start or end of one or more activities.
  3. Events are numbered so that the activity ends at a higher numbered event than it started.
  4. Events are numbered so that no activity starts and ends at the same event. An activity can therefore be uniquely identified by its start and end events.
  5. In order to avoid ambiguity between activities you must use ‘dummy’ activities. These are activities that have zero length and are inserted to ensure that precedence requirements are met. They allow two activities that ought to have the same start and end events to be distinguished.
  6. There should only be one start event and one finish event.
  7. The activity detail (or code) and duration are written along the edges.
  8. The edge length has no meaning.

Using these rules you should be able to create activity networks for any process that involves more than one activity.

Earliest and Latest Times

Once you have drawn out an activity network the next step is to work out the earliest and latest possible times for each event. This will let you find the minimum time necessary for the tasks to be completed taking activity precedence into account.

In order to make the identification of earliest and latest times easier, they are often put into a pair of boxes. The left box will be the earliest time and the right box will be the latest time. When working out the earliest times for each event you must start at the first event and assign an earliest time of 0. Work through each event in turn. Add the duration of the activity to the earliest time of the start event to get the earliest time of the end event. If there is more than one way of arriving at the end event then take the largest value for the earliest time.

Figure 1: Earliest times for the house building activity network.
Figure 1: Earliest times for the house building activity network.

Once you have gone through the entire network assigning earliest times you can begin to work out the latest times. This is done by starting at the last event and working backwards though the network. To start the process let the latest time of the last event be equal to the earliest time of that event. By subtracting the duration of an activity from the event that it ends at you will be able to find the latest time for the start event for that activity. If there is more than one way to arrive at a start event then take the smallest value for the latest time.

Figure 2: Earliest and latest times for the house building activity network.
Figure 2: Earliest and latest times for the house building activity network.

By looking at the earliest / latest times for the last event you will know the minimum duration for the overall process.