Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
116 views110 pages

Computing Maths Notes

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)
116 views110 pages

Computing Maths Notes

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/ 110

Computational Mathematics

Computing Mathematics Lecture Notes

Dr Jon Shiach

Spring 2022

Cout
Cin
A

© Department of Computing and Mathematics


Contents

Preliminaries iii
0.1 Learning and Teaching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iii
0.2 Assessment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iv
0.3 Advice to students . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . v

1 Mathematical Fundamentals 1
1.1 Binary numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Hexadecimal numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3 Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.4 Tutorial exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

2 Logic Circuits 11
2.1 Logic gates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
2.2 Circuit diagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.3 Boolean algebra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.4 Simplifying circuits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.5 Universal gates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.6 Canonical normal form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
2.7 Karnaugh maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2.8 Half and full adders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
2.9 Tutorial exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

3 Algorithms 37
3.1 Greatest common divisor algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
3.2 Sorting algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
3.3 Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
3.4 Complexity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
3.5 Tutorial Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

4 Graph Theory 53
4.1 Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
4.2 Depth-first and breadth-first search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
4.3 Shortest path problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
4.4 Dijkstra’s algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
4.5 The Bellman-Ford algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
4.6 The A* algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
4.7 Applications of shortest path problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
4.8 Tutorial exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85

A Exercise solutions 89
A.1 Mathematics Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
A.2 Logic Circuits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
A.3 Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94

i
Contents Back to Table of Contents

A.4 Graph theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97

Index 101

This version compiled on March 9, 2022.

Dr Jon Shiach ii Computing Mathematics Lecture Notes


Preliminaries

0.1 Learning and Teaching


0.1.1 Lecture notes
These are the lecture notes that accompany the computing mathematics part of the unit Computational
Mathematics with the other part covering computer graphics. Students are provided with a printed copy of
the lecture notes so that they can focus on what is being covered in the lecture without having to worrying
about making their own notes. There is also an electronic copy in PDF format which is available on the
moodle area for this unit. These notes are quite comprehensive and are written specifically for this unit
so should serve as your main point of reference. However, it is always advisable to seek out other sources
of information either on the internet or better still textbooks from the library. Mathematical notation can
differ between authors and these notes have been written to use notation that is most commonly found
online.
These notes use a tried and tested format of definition (what is it that we are studying?) → explanation
(why do we use it?) → examples (how is it used?) → exercises (now you try it). The examples in the
printed version of the notes are left empty for students to complete in class. This is done because by
writing out the steps used in a method it helps students to better understand that method. The PDF
version of the notes contains the full solutions to the examples so if you do happen to miss an example
you can complete the example by looking it up in the PDF version.
You should read through the lecture notes prior to attending the lectures that focus on that particular
chapter. Don’t worry about trying to understand everything when you first read through them. Reading
mathematics is not like reading your favourite novel, it often requires repeated reading of a passage before
you fully grasp the concepts that are being explained. In the lectures we will explain the various topics and
provide more insight than what is written in the notes.
The PDF version of the notes contains lots of hyperlinks for easy navigation around the document. Hy-
perlinks show up as blue text and can save lots of time scrolling up and down the pages.

0.1.2 Unit timetable


The timetable for the computing mathematics part of the unit is shown below (this can also be accessed
via MyMMU). The material will be covered in one 2-hour lecture and one 2-hour lab/tutorial.
• Lecture: Thursdays 12:00 - 14:00 in JD E239
• Lab: Thursdays 15:00 - 17:00 in JD C2.04
Students are expected to devote at least 30 hours per week to their studies and should complete all reading
and tutorial exercises during this time.

iii
Chapter 0. Preliminaries Back to Table of Contents

0.1.3 Teaching Schedule


The computing mathematics material will be covered over the 6 weeks of the teaching block as outlined
in table 1 so you should be aware of what is being covered and when. There may be times when we have
to deviate from this slightly and you will be informed of this in the lecture.

Table 1: Computing mathematics teaching schedule

Week Date (w/c) Material


1 14/03/2022
2 21/03/2022
Coursework assignment handed out
3 28/03/2022

4 04/04/2022
Easter Break
5 25/04/2021
6 02/05/2021 Revision.
Coursework assignment deadline – 6th May 2022
7 02/05/2022 Assessment week
Examination – 9th May to 10th May 2022

0.2 Assessment
The assessment for this unit takes the form of two coursework assignments and a take home examination.
• Coursework (20%) – Computing Mathematics
– Released to students on Monday 21st March 2022;
– Deadline is 9pm Friday 6th April 2022;
• Coursework (20%) – Computer Graphics
– Released to students on Monday 21st March 2022;
– Deadline is 9pm Friday 6th May 2022;
• Examination (60%)
– Released to students at 09:00 on Tuesday 10th May 2022;
– Deadline is 12:30 on 9pm Friday 6th May 2022;
– Students will have access to the lecture notes, unit materials on moodle and any other sources
of information available;
– Total of 4 questions, 2 questions on computer graphics and 2 questions on computing mathe-
matics.
You will receive a percentage mark for each assessment component and your overall mark for the unit will
be calculated using the simple equation

unit mark = 0.2 × coursework 1 mark + 0.2 × coursework 2 mark + 0.6 × examination mark.

To successfully pass the unit you will need a unit mark of at least 40%.

Dr Jon Shiach iv Computing Mathematics Lecture Notes


Back to Table of Contents Chapter 0. Preliminaries

0.3 Advice to students


Some general advice to students:
• Attend all of the classes – the key to successfully passing the unit is to attend all of your classes.
Mathematics is not a subject that can be learned easily in isolation just by reading the lecture notes.
You will get much more out of the unit by attending, and more importantly, actively engaging in the
classes.
• Complete all of the exercises – you would not expect an athlete to get faster or stronger without
exercising and the same applies to studying mathematics. The tutorial exercises are designed to give
you practice at using the various techniques and to help you to fully grasp the content. Try to make
sure that you complete all of the exercises before the following week’s lectures. Full worked solutions
are provided in Appendix A at the end of these notes but do try to be disciplined and avoid looking
up the answers before you have attempted the questions.
• Catch up on missed work – for whatever reason there may be a day when you cannot attend your
classes, if this happens make sure you make the effort to catch up on missed work. Read through
the appropriate chapter in the notes (as specified in the teaching schedule), complete the examples
and attempt the tutorial exercises. It is very easy to start falling behind and the longer you leave it
the more difficult it will be to catch up.
• Start the coursework as soon as you are able – the coursework is released to students early in
the teaching block so you are not expected to be able to answer all of the questions right away. As
we progress through the unit you will be told which questions you should now be able to answer. Try
to start these questions as soon as you can and not leave it to the last minute.
• Ask questions! – perhaps the most important piece of advice here, there will be times when you
are not quite sure about a concept, application or question. You can ask for help from the teaching
staff and your fellow students. Mathematics is a hierarchical subject which means it requires full
understanding at a fundamental level before moving onto more advanced topics, so if there are any
gaps in your knowledge don’t be afraid to ask questions.

Dr Jon Shiach v Computing Mathematics Lecture Notes


Chapter 1

Mathematical Fundamentals

This chapter will provide a brief explanation of the fundamental mathematical concepts required for students
to understand the material in the subsequent chapters.

1.1 Binary numbers


A binary number is a number that is represented using base-2 number system where two symbols (usually
0 and 1) are used in combination with their position in a number to represent a value. Consider numbers
expressed in decimal where the nine symbols 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9 are used to represent a value.
The digit on the far right-hand side of a number has the value of 1 and each digit to the left has the value
10 times that of the digit to the right. For example, the number 1234510 1 in decimal has the value

1234510 = (1 × 10000) + (2 × 1000) + (3 × 100) + (4 × 10) + (5 × 1),

or alternatively

1234510 = (1 × 104 ) + (2 × 103 ) + (3 × 102 ) + (4 × 101 ) + (5 × 100 ).

Since the value of each digit is the base 10 raised to the power of n where n is the number of digits from
the digit on the far right-hand side then decimal numbers are said to be expressed using base-10.
Since binary numbers are expressed using base-2 then the binary number 101102 has the value

101102 = (1 × 24 ) + (0 × 23 ) + (1 × 22 ) + (1 × 21 ) + (0 × 20 )
= (1 × 16) + (0 × 8) + (1 × 4) + (1 × 2) + (0 × 1)
= 16 + 0 + 4 + 2 + 0
= 22.

Example 1.1

Convert following binary numbers to decimal:


(a) 112 ; (b) 10112 ; (c) 110002 ; (d) 1010111012 .

Solution:
(a) 112 = 2 + 1 = 3;
(b) 10112 = 4 + 2 + 1 = 7;
(c) 110002 = 8 + 4 = 12;
(d) 1010111012 = 256 + 64 + 16 + 8 + 4 + 1 = 349.

1
To avoid ambiguity the base of the number is expressed as a subscript.

1
Chapter 1. Mathematical Fundamentals Back to Table of Contents

1.1.1 Convert a decimal number to binary

To convert a decimal number to a binary number we use the following steps:

• divide the number by 2 and find the remainder;

• divide the result of the division by 2 and find the remainder;

• repeat the previous step until the result of the division is 0, the digits of the binary number are the
remainders in reverse order.

For example, consider the binary representation of 2210

22 ÷ 2 = 11 remainder 0,
11 ÷ 2 = 5 remainder 1,
5 ÷ 2 = 2 remainder 1,
2 ÷ 2 = 1 remainder 0,
1 ÷ 2 = 0 remainder 1,

therefore 2210 = 101102 .

Example 1.2

Convert the following decimal numbers to binary:


(a) 1010 ; (b) 10010 ; (c) 25610 ;

Solution:
(a) (b)

10 ÷ 2 = 5 remainder 0, 100 ÷ 2 = 50 remainder 0,


5 ÷ 2 = 2 remainder 1, 50 ÷ 2 = 25 remainder 0,
2 ÷ 2 = 1 remainder 0, 25 ÷ 2 = 12 remainder 1,
1 ÷ 2 = 0 remainder 1, 12 ÷ 2 = 6 remainder 0,
∴ 1010 = 10102 . 6 ÷ 2 = 3 remainder 0,
3 ÷ 2 = 1 remainder 1,
1 ÷ 2 = 0 remainder 1,
∴ 10010 = 11001002 .

Dr Jon Shiach 2 Computing Mathematics Lecture Notes


Back to Table of Contents Chapter 1. Mathematical Fundamentals

(c)

256 ÷ 2 = 128 remainder 0,


128 ÷ 2 = 64 remainder 0,
64 ÷ 2 = 32 remainder 0,
32 ÷ 2 = 16 remainder 0,
16 ÷ 2 = 8 remainder 0,
8 ÷ 2 = 4 remainder 0,
4 ÷ 2 = 2 remainder 0,
2 ÷ 2 = 1 remainder 0,
1 ÷ 2 = 0 remainder 1,
∴ 25610 = 1000000002 .

1.1.2 Binary addition

To add two binary numbers we add the last digits together. If the value of the sum is greater than 1
then we use the last digit of the sum and carry a 1 and include it in the sum of the digits to the left.
This continues until all digits and carries have been summed. For example, consider the sum of the binary
numbers 10110 + 1101

1 0 1 1 0
+ 1 1 0 1
carry 1 1 1
sum 1 0 0 0 1 1

Note that this sum is equivalent to 2210 + 1310 = 3510 = 1000112 .

Example 1.3

Use binary addition to calculate the following:


(a) 101 + 11; (b) 101010 + 11011.

Solution:
(a) (b)

1 0 1 1 0 1 0 1 0
+ 1 1 + 1 1 0 1 1
carry 1 1 1 carry 1 1 1 1
sum 1 0 0 0 sum 1 0 0 0 1 0 1

Dr Jon Shiach 3 Computing Mathematics Lecture Notes


Chapter 1. Mathematical Fundamentals Back to Table of Contents

1.2 Hexadecimal numbers

An obvious drawback of using binary numbers is that the number of digits required to represent a value
is more than when representing the value using decimal numbers. In computing, it is common to use
hexadecimal numbers which are expressed using base-16 . Since we require an additional six symbols to
represent a single digit of a hexadecimal numbers we use the first six uppercase letters in the alphabet as
shown in table 1.1.

Table 1.1: Letters used to represent hexadecimal digits.

Decimal Hexadecimal
10 A
11 B
12 C
13 D
14 E
15 F

The techniques for converting between decimal and hexadecimal and addition of hexadecimal is the same
as those for binary numbers. For example, the hexadecimal number 12AB16 has the decimal value

12AB16 = 1 × 163 + 2 × 162 + 10 × 161 + 11 × 160


= 1 × 4096 + 2 × 256 + 10 × 16 + 11 × 1
= 4096 + 512 + 160 + 11
= 4779.

Example 1.4

1. Convert 64087 to hexadecimal;


2. Convert FFF to decimal;
3. Evaluate 43ED + F12B.

Solution:
1.

64087 ÷ 16 = 4005 remainder 7,


4005 ÷ 16 = 250 remainder 5,
250 ÷ 16 = 15 remainder A,
15 ÷ 16 = 0 remainder F,
∴ 6408710 = FA5716 .

Dr Jon Shiach 4 Computing Mathematics Lecture Notes


Back to Table of Contents Chapter 1. Mathematical Fundamentals

2.

FFF = F × 162 + F × 161 + F × 160


= 15 × 256 + 15 × 16 + 15 × 1
= 3840 + 240 + 15
= 4095.

3.

4 3 E D
+ F 1 2 B
carry 1 1 1
sum 1 3 5 1 8

1.3 Logic
Mathematical logic deals with statements that can be assigned a value of true or false but not both. Such
statements are known as propositions propositions and the logic using propositions is often known as
propositional logic. For example the statement “it is raining” is a proposition as it is either true or false
whereas the statement “is it raining” is not a proposition as the answer may not be true or false.

In mathematical notation a true value is denoted using the symbol ‘1’ and a false value is denoted using
the symbol ‘0’2

1.3.1 Logical connectives


Individual propositions can be combined using what are known as logical connectives to form a compound
proposition. For example the compound proposition “it is raining and I have an umbrella” combines the
two propositions “it is raining” and “I have and umbrella” using the logical connective ‘and’. A compound
proposition, by definition, must have a value which is either true or false, in our example the compound
proposition is only true if each of the propositions are both true.

In propositional logic we have three logical connectives: conjunction, disjunction and negation.

Definition 1.1: Logical conjunction

A logical conjunction (often referred to as logical AND) between two propositions p and q is true
only if both p AND q are true. In symbolic notation the conjunction of p and q is written as p ∧ q.

Definition 1.2: Logical disjunction

A logical disjunction (often referred to as logical OR) between two propositions p and q is true if
p is true OR q is true. In symbolic notation the disjunction of p and q is written as p ∨ q.

2
Some authors use T and F to denote true and false respectively.

Dr Jon Shiach 5 Computing Mathematics Lecture Notes


Chapter 1. Mathematical Fundamentals Back to Table of Contents

Definition 1.3: Logical negation

A logical negation (often referred to as logical NOT) of the proposition p is true if p is false and
false if p is true. In symbolic notation negation of p is written as ¬p.

Note that we assume an order of precedence negation −→ conjunction −→ disjunction, e.g., in the com-
pound proposition ¬p ∧ q ∨ r we evaluate ¬p first then ¬p ∧ q before evaluating the disjunction with
r.

1.3.2 Truth tables


A truth table is a table which expresses the values of a logical expression for all possible combination of
values for the individual propositions. The truth table for the three logical connectives AND, OR and NOT
is shown in table 1.2.

p q p∧q p∨q ¬p ¬q
0 0 0 0 1 1
0 1 0 1 1 0
1 0 0 1 0 1
1 1 1 1 0 0

Table 1.2: A truth table for the logical connectives AND, OR and NOT.

Here the columns for p and q give all of the possible combinations of the values of the propositions. Since
we have 2 propositions there will be a total of 22 = 4 combinations. The order of which the combinations
are written does not matter although it is recommended practice to write them in ascending order of their
binary value for consistency.

1.3.3 Logical equivalence


Two logical statements are said to be equivalent if they have the same truth values for all possible
combinations of the individual propositions. This is represented symbolically using p ≡ q. To check
whether two compound propositions are equivalent we can write down the truth table for each one and
check whether they have the same values for all of the combinations of their individual propositions. For
example, consider the two compound propositions ¬p ∧ ¬q and ¬(p ∨ q) which are represent in the truth
table shown in table 1.3.

p q ¬p ¬q ¬p ∧ ¬q p∨q ¬(p ∨ q)
0 0 1 1 1 0 1
0 1 1 0 0 1 0
1 0 0 1 0 1 0
1 1 0 0 0 1 0

Table 1.3: Truth table for the compound propositions ¬p ∧ ¬q and ¬(p ∨ q)

Note that the columns for ¬p ∧ ¬q and ¬(p ∨ q) are the same so we can conclude that ¬p ∧ ¬q ≡ ¬(p ∨ q).

Example 1.5

Use truth tables to confirm the following:


(a) p ∨ (p ∧ q) ≡ p (absorption law); (b) p ∧ (¬p ∨ q) ≡ p ∧ q;

Solution:

Dr Jon Shiach 6 Computing Mathematics Lecture Notes


Back to Table of Contents Chapter 1. Mathematical Fundamentals

(a)
p q p∧q p ∨ (p ∧ q)
0 0 0 0
0 1 0 0
1 0 0 1
1 1 1 1

(b)
p q ¬p ¬p ∨ q p ∧ (¬p ∨ q) p∧q
0 0 1 1 0 0
0 1 1 1 0 0
1 0 0 0 0 0
1 1 0 1 1 1

1.3.4 Laws of logic


The laws of logic are given in theorem 1.1 and can be used to find expressions that are equivalent to a
compound proposition.

Theorem 1.1: Laws of logic

• Commutative law: p ∨ q ≡ p ∨ q and p ∧ q ≡ q ∧ p ;


• Associate law: p ∨ (q ∨ r) ≡ (p ∨ q) ∨ r and p ∧ (q ∧ r) ≡ (p ∧ q) ∧ r;
• Distributive law: p ∧ (q ∨ r) = (p ∧ q) ∨ (p ∧ r) and p ∨ (q ∧ r) ≡ (p ∨ q) ∧ (p ∨ r),
(p ∨ q) ∧ (r ∨ s) ≡ (p ∧ r) ∨ (p ∧ s) ∨ (q ∧ r) ∨ (q ∧ s) and
(p ∧ q) ∨ (r ∧ s) ≡ (p ∨ r) ∧ (p ∨ s) ∧ (q ∨ r) ∧ (q ∨ s) ;
• Identity law: 0 ∨ p ≡ p and 1 ∧ p = p ;
• Idempotence law: p ∨ p ≡ p; and p ∧ p = p ;
• Absorption law: p ∧ (p ∨ q) ≡ p and p ∨ (p ∧ q) ≡ p;
• De Morgan’s law: ¬(p ∧ q) ≡ ¬p ∨ ¬q and ¬(p ∨ q) ≡ ¬p ∧ ¬q;
• Complement law: p ∨ ¬p ≡ 1 and p ∧ ¬p ≡ 0;
• Double negation: ¬¬p ≡ p;

The definition of conjunction and disjunction also give the following results

0 ∧ p ≡ 0,
1 ∨ p ≡ p.

For example, consider the compound proposition (p ∧ q) ∨ (¬p ∧ q) ∨ (p ∧ ¬q)

(p ∧ q) ∨ (¬p ∧ q) ∨ (p ∧ ¬q)
≡ (p ∧ q) ∨ (p ∧ ¬q) ∨ (¬p ∧ q) (commutative law)
≡ (p ∧ (q ∨ ¬q)) ∨ (¬p ∧ q) (distributive law)
≡ (p ∧ 1) ∨ (¬p ∧ q) (complement law)
≡ p ∨ (¬p ∨ q) (identity law)
≡ (p ∨ ¬p) ∧ (p ∨ q) (distributive law)
≡ 1 ∧ (p ∨ q) (complement law)

Dr Jon Shiach 7 Computing Mathematics Lecture Notes


Chapter 1. Mathematical Fundamentals Back to Table of Contents

≡p∨q (identity law)

We can confirm that (p ∧ q) ∨ (¬p ∧ q) ∨ (p ∧ ¬q) ≡ p ∨ q using a truth table.


p q ¬p ¬q p∧q ¬p ∧ q p ∧ ¬q (p ∧ q) ∨ (¬p ∧ q) ∨ (p ∧ ¬q) p∨q
0 0 1 1 0 0 0 0 0
0 1 1 0 0 1 0 1 1
1 0 0 1 0 0 1 1 1
1 1 0 0 1 0 0 1 1

Example 1.6

Use the laws of logic to simplify the following compound propositions starting clearly which law you
are using at each step:
(a) ¬(¬p ∧ ¬q); (b) p ∨ (q ∧ ¬p); (c) ¬((p∨¬q)∨(r∧(p∨¬q))).

Solution:
(a)

¬(¬p ∧ ¬q) ≡ ¬¬p ∨ ¬¬q (De Morgan’s law)


≡p∨q (double negation law)

(b)

p ∨ (q ∧ ¬p) ≡ p ∨ (¬p ∧ q) (commutative law)


≡ (p ∨ ¬p) ∧ (p ∨ q) (distributive law)
≡ 1 ∧ (p ∨ q) (complement law)
≡p∨q (identity law)

(c)

¬((p ∨ ¬q) ∨ (r ∧ (p ∨ ¬q))) ≡ ¬(p ∨ ¬q) ∧ ¬(r ∧ (p ∨ ¬q)) (De Morgan’s law)
≡ ¬(p ∨ ¬q) ∧ (¬r ∨ ¬(p ∨ ¬q)) (De Morgan’s law)
≡ ¬(p ∨ ¬q) ∧ (¬(p ∨ ¬q) ∨ ¬r) (commutative law)
≡ ¬(p ∨ ¬q) (absorption law)
≡ ¬p ∧ ¬¬q (De Morgan’s law)
≡ ¬p ∧ q (double negation law)

Dr Jon Shiach 8 Computing Mathematics Lecture Notes


Back to Table of Contents Chapter 1. Mathematical Fundamentals

1.4 Tutorial exercises


Exercise 1.1. Convert the following binary numbers to decimal:
(a) 1011; (b) 11010; (c) 1101010; (d) 10001001.
Exercise 1.2. Convert the following decimal numbers into binary:
(a) 9; (b) 23; (c) 80; (d) 163;
Exercise 1.3. Convert the following hexadecimal numbers to decimal:
(a) 10; (b) 16; (c) B14; (d) ABCD.
Exercise 1.4. Convert the following decimal numbers to hexadecimal
(a) 24; (b) 128; (c) 485; (d) 1435.
Exercise 1.5. Convert the number 1851 to base-3.
Exercise 1.6. Use binary addition to evaluate 101101 + 100111.
Exercise 1.7. Use hexadecimal addition to evaluate 1CE4F + 10A54.
Exercise 1.8. Write down the truth table for the following compound propositions:
(a) ¬p ∨ (p ∧ q); (b) ¬(p ∨ q) ∧ (p ∨ ¬q); (c) ¬(p ∧ ¬(q ∨ ¬r)).
Exercise 1.9. Use the laws of logic to simplify the compound propositions from exercise 1.8. State which
law you are using at each step.
The solutions to these exercises are given in appendix A.1.

Dr Jon Shiach 9 Computing Mathematics Lecture Notes


Chapter 2

Logic Circuits

Learning outcomes
On successful completion of this chapters students will be able to:

• identify the different logic gates used to construct logic circuits and draw their truth tables;

• draw circuit diagrams of logic circuits and write down the equivalent Boolean expression;

• simplify Boolean expressions using the laws of Boolean algebra;

• draw Karnaugh maps for a Boolean expression and use them to produce a Boolean expression in
canonical disjunctive normal form;

• Construct the half adder and full adder circuits and use them to sum two binary numbers.

2.1 Logic gates


A computer processor is constructed using logic gates which perform an individual logical operation on
one or more binary inputs. By connecting different logic gates in certain configurations we can build logic
circuits that can perform arithmetic operations

2.1.1 NOT gate


A NOT gate, also known as an inverter, is a logic gate which negates the the input. It is represent
mathematically using A and the symbol representing a NOT gate is shown in figure 2.1. The small circle
in the output generally represents negation.

A A
A A 0 1
1 0

Figure 2.1: Symbolic representation of a NOT gate and its truth table.

2.1.2 AND gate


An AND gate implements logical conjunction on two or more inputs. It is represent mathematically using
A · B (often abbreviated to AB) and called the product of A and B. The symbol representing a AND
gate is shown in figure 2.2.

11
Chapter 2. Logic Circuits Back to Table of Contents

A B A·B
0 0 0
A
A·B 0 1 0
B
1 0 0
1 1 1

Figure 2.2: Symbolic representation of an AND gate and its truth table.

2.1.3 OR gate
An OR gate implements logical disjunction on two or more inputs. It is represent mathematically using
A + B and is called the sum of A and B. The symbol representing a OR gate is shown in figure 2.3.

A B A+B
0 0 0
A
A+B 0 1 1
B
1 0 1
1 1 1

Figure 2.3: Symbolic representation of an OR gate and its truth table.

2.1.4 XOR gate


An XOR gate (also known as an exclusive OR gate) implements a logical operation on two inputs that
outputs a true value when there are an odd numbers of true inputs. It is represent mathematically using
A ⊕ B which is equivalent to A · B + A · B and its symbol and truth table are shown in figure 2.4

A B A⊕B
0 0 0
A
A⊕B 0 1 1
B
1 0 1
1 1 0

Figure 2.4: Symbolic representation of an XOR gate and its truth table.

2.1.5 NAND gate


A NAND gate implements a logical operation that is equivalent to the negation of a logical conjunction
between two or more inputs (i.e., the output is false only if all of the inputs are true). It is represent
mathematically using A · B and its symbol and truth table are shown in figure 2.5.

A B A·B
0 0 1
A
A·B 0 1 1
B
1 0 1
1 1 0

Figure 2.5: Symbolic representation of an OR gate and its truth table.

Dr Jon Shiach 12 Computing Mathematics Lecture Notes


Back to Table of Contents Chapter 2. Logic Circuits

2.1.6 NOR gate


A NOR gate implements a logical operation that is equivalent to the negation of a logical disjunction
between two or more inputs (i.e., the output is true only if all of the inputs are false). It is represent
mathematically using A + B and its symbol and truth table are shown in figure 2.6.

A B A+B
0 0 1
A
A+B 0 1 0
B
1 0 0
1 1 0

Figure 2.6: Symbolic representation of an OR gate and its truth table.

2.2 Circuit diagrams


Logic gates are arranged to form logic circuits which are designed to perform logic operations on one or
more inputs and produce one or more outputs. The configuration of logic gates, inputs and outputs are
represented by a circuit diagram. In real circuits, the logic gates are connected using wires which are
represented in circuit diagrams as lines. If a wire carries a charge above a certain current then it is deemed
to have a value of 1, if it below this current then it is deemed to have a value of 0.
There are no rules to how circuit diagrams are drawn although is preferable that they are as simple and
easy to read as possible. For this reason it is standard practice that the inputs are on the left and the
outputs are on the right with the direction of flow going left-to-right. The connecting wires are usually
draw using horizontal or vertical lines. Dots are used to represent the start or end point of a wire or where
there is a split in the wires. Where two wires cross and there is no dot we assume that the two wires do
not intersect.

f (A, B)

Figure 2.7: A simple circuit diagram.

Consider the circuit diagram shown in figure 2.7. Here we have two inputs, A and B, and the circuit
produces a single output f (A, B). The circuit consists of three logic gates: an AND gate, an XOR gate
and an OR gate. The AND and XOR gates both has two inputs A and B, and the outputs from these
gates are then inputted into the OR gate.
Note that the wire from input A is split before the AND gate so A is inputted into both the AND and the
XOR gate (and similar for the input B). In the circuit diagram the A and B wires cross but since there is
no dot we assume that they do not intersect.
The mathematical expression that is equivalent to the circuit in figure 2.7 is

f (A, B) = A · B + A ⊕ B.

Example 2.1

For each circuit diagram below write down a mathematical expression that is equivalent to f (A, B):
1.

Dr Jon Shiach 13 Computing Mathematics Lecture Notes


Chapter 2. Logic Circuits Back to Table of Contents

f (A, B)

B
2.
A
B f (A, B)

Solution:
1.

f (A, B) = AND gate + XOR gate


= (A · OR gate) + OR gate ⊕ B
= A · (A + B) + (A + B) ⊕ B

2.

f (A, B) = AND gate + OR gate


= A · B · C + (B + C + AND gate)
= A · B · C + (B + C + A · B · C)

2.3 Boolean algebra

When working with logic gates we can simplify circuits by utilising Boolean algebra. Boolean algebra
involves variables that can only take the values of 1 and 0 (or true and false) and thus shares similarities
with propositional logic. The laws of Boolean algebra are given in theorem 2.1 (which are essentially the
same as those for propositional logic) and are used to find expressions that have a value that are equivalent
with the purpose of either simplifying the original expression or using particular logic gates.

Not that we assume and order of precedence where conjunction takes precedence over disjunction, e.g., in
A + B · C the conjunction B · C is evaluated before the disjunction of the result with A is evaluated.

Dr Jon Shiach 14 Computing Mathematics Lecture Notes


Back to Table of Contents Chapter 2. Logic Circuits

Theorem 2.1: Laws of Boolean algebra

• Commutative law: A + B ≡ B + A and A · B ≡ B · A ;


• Associative law: A + (B + C) ≡ (A + B) + C and A · (B · C) ≡ (A · B) · C;
• Distributive law: A · (B + C) = A · B + A · C and A + B · C ≡ (A + B) · (A + C) and
• (A + B) · (C + D) ≡ A · C + B · C + A · D + B · D ;
• Identity law: 0 + A ≡ A and 1 · A = A ;
• Idempotence law: A + A ≡ A; and A · A = A ;
• Absorption law: A · (A + B) ≡ A and A + (A · B) ≡ A;
• De Morgan’s law: A · B ≡ A + B and A + B ≡ A · B;
• Complement law: A + A ≡ 1 and A · A ≡ 0;
• Double negation: A ≡ A;

We can also make use of the following which come from the definition of AND and OR

0 · A ≡ 0,
1 + A ≡ 1.

2.4 Simplifying circuits


Boolean algebra can be used to simplify circuits so that fewer logic gates, or logic gates of a preferred type
and to used to produce the same logical output. To do this we express the circuit as a Boolean equation
using the logic gates that are in the circuit and apply the laws of Boolean algebra from theorem 2.1 to
reduce or rewrite the Boolean equation in a form that is more preferable.

f (A, B)

Figure 2.8: A logic circuit for an XOR gate.

For example, consider the logic circuit shown in figure 2.8. This circuit consists of 5 logic gates: 2 NOT
gates, 2 AND gates and an OR gate. Writing this in Boolean algebra we have

f (A, B) = A · B + A · B.

The truth table for this circuit is shown in table 2.1 which shows that this circuit produces the same output
as an XOR gate, i.e.,

A · B + A · B ≡ A ⊕ B.

If we want to build an XOR gate using the fewest logic gates then we can apply the laws of Boolean algebra
to simplify A · B + A · B. At first glance it isn’t immediately obvious how we can simplify this expression,
however if we let C ≡ A · B, D ≡ A and E ≡ B then the Boolean expression is

A·B+A·B ≡C +D·E

and using the distributivity law on the right-hand side then

(C + D) · (C + E) ≡ (A · B + A) · (A · B + B)

Dr Jon Shiach 15 Computing Mathematics Lecture Notes


Chapter 2. Logic Circuits Back to Table of Contents

Table 2.1: Truth table for the circuit A · B + A · B.

A B A B A·B A·B A·B+A·B


0 0 1 1 0 0 0
0 1 1 0 0 1 1
1 0 0 1 1 0 1
1 1 0 0 0 0 0

so

A · B + A · B ≡ (A · B + A) · (A · B + B).

Using the distributivity law again for each of the two bracketed terms

(A · B + A) · (A · B + B) ≡ (A + A) · (A + B) · (A + B) · (B + B)

Here we can use the complement law A + A ≡ 1

(A + A) · (A + B) · (A + B) · (B + B) ≡ 1 · (A + B) · (A + B) · 1

Using the identity law 1 · A ≡ A then

1 · (A + B) · (A + B) · 1 ≡ (A + B) · (A + B).

Finally using De Morgan’s law A + B ≡ A · B

(A + B) · (A + B) ≡ (A + B) · A · B.

The term A · B is equivalent to a NAND gate so we now have a circuit with 3 logic gates: an OR gate,
a NAND gate and an AND gate. The circuit diagram is shown in figure 2.9 and the truth table for the
circuit is shown in table 2.2 showing that the output is equivalent to that of an XOR gate.

A⊕B

Figure 2.9: A simplified logic circuit for and XOR gate using 3 logic gates.

Table 2.2: Truth table for the circuit (A + B) · A · B.

A B A+B A·B A·B (A + B) · A · B


0 0 0 0 1 0
0 1 1 0 1 1
1 0 1 0 1 1
1 1 1 1 0 0

2.4.1 Propagation delay


In a physical circuit the speed current that flows along the wires and through the logic gates is affected
by the length of the wire and the number of logic gates it passes through. The propagation delay is the
delay in the speed of the current through a circuit. When considering the length of the connections in a
computer chip the speed loss along a wire is negligible (approximately 1ns per 15cm of wire) so we use

Dr Jon Shiach 16 Computing Mathematics Lecture Notes


Back to Table of Contents Chapter 2. Logic Circuits

the number of logic gates as a measure of the propagation delay. Since there are multiple routes from the
inputs to the outputs of a circuit we use the route with the most logic gates for the propagation delay.

Consider the circuit diagram shown in figure 2.7 with the routes from the inputs to the outputs traced in
figure 2.10. Two of the routes pass through 2 logic gates and two of the routes pass through 3 logic gates
so this circuit has a propagation delay of 3.

A
2
3
f (A, B)
3
2
B

Figure 2.10: Tracing the propagation delay for the inputs to the outputs.

Example 2.2

For each of the circuits below (Tocci et al. 2007):


1.

C f (A, B, C)

B
2.
A

B f (A, B, C)

(a) write down the Boolean expression for the circuit;


(b) use the laws of Boolean algebra to simplify the Boolean expression;
(c) draw a circuit diagram of the simplified Boolean expression;
(d) construct a truth table for the simplified circuit.

Solution:
1. (a) f (A, B, C) = A · B · A · C + A · B · C
(b)

f (A, B, C) = A · B · A · C + A · B · C
≡ A · B · (A + C) + A · B · C (De Morgan’s law)

Dr Jon Shiach 17 Computing Mathematics Lecture Notes


Chapter 2. Logic Circuits Back to Table of Contents

≡ A · B · (A + C) + A · B · C (double negation)
≡A·A·B+A·B·C +A·B·C (distributivity law)
≡A·B+A·B·C +A·B·C (idempotence law)
≡ A · B + A · C · (B + B) (distributivity law)
≡A·B+A·C ·1 (complement law)
≡A·B+A·C (identity law)
≡ A · (B + C) (distributivity law)

(c)
A
A · (B + C)
B
C
(d)
A B C B B+C A · (B + C)
0 0 0 1 1 0
0 0 1 1 1 0
0 1 0 0 0 0
0 1 1 0 1 0
1 0 0 1 1 1
1 0 1 1 1 1
1 1 0 0 0 0
1 1 1 0 1 1

2. (a) f (A, B, C) = A · B · C + A · B · C + A · B · C
(b)

f (A, B, C) = A · B · C + A · B · C + A · B · C
≡A·B·C +A·B·C +A·B·C +A·B·C (idempotence law)
≡ A · C · (B + B) + A · B · (C + C) (distributivity law)
≡A·C ·1+A·B·1 (complement law)
≡A·C +A·B (identity law)
≡ A · (B + C) (distributivity law)

(c)
A A · (B + C)
B
C
(d)

Dr Jon Shiach 18 Computing Mathematics Lecture Notes


Back to Table of Contents Chapter 2. Logic Circuits

A B C B+C A · (B + C)
0 0 0 0 0
0 0 1 1 0
0 1 0 1 0
0 1 1 1 0
1 0 0 0 0
1 0 1 1 1
1 1 0 1 1
1 1 1 1 1

2.5 Universal gates


NAND gates and NOR gates are known as universal gates because they can be implemented to represent
any other Boolean operation using the same logic gate. They are also more straight forward and cheaper
to build so if we can construct a logic circuit using just NAND gates then we can reduce the costs and
time taken to manufacture a computing chip.
To prove that the NAND gate is a universal gate we need to show that it can be used to represent the
other types of logic gates.
• NOT gate: A ≡ A · A

A A equivalent to A A

Proof.

A·A≡A (idempotence law)

• AND gate: A · B ≡ A · B · A · B
A equivalent to A
A·B A·B
B B

Proof.

A·B·A·B ≡A·B (idempotence law)


≡A·B (double negation)

• OR gate: A + B ≡ A · A · B · B

A
A
A + B equivalent to A+B
B
B

Proof.

A·A·B·B ≡A·B (idempotence law)


≡A+B (De Morgan’s law)

Dr Jon Shiach 19 Computing Mathematics Lecture Notes


Chapter 2. Logic Circuits Back to Table of Contents

≡A+B (double negation)

• XOR gate: A ⊕ B ≡ A · A · B · B · A · B

A
equivalent A
A⊕B A⊕B
to B
B

Proof.

A·A·B·B·A·B ≡A·A·B+B·A·B (De Morgan’s law)


≡A·A·B+B·A·B (double negation)
≡ A · (A + B) + B(A + B) (De Morgan’s law)
≡A·A+A·B+B·A+B·B (distributivity law)
≡0+A·B+A·B+0 (complement law)
≡A·B+A·B (identity law)
≡ A ⊕ B. (definition of A ⊕ B)

• NOR gate: A + B ≡ A · A · B · B · A · A · B · B

A
equivalent A
A+B A+B
to B
B

Proof.

A·A·B·B·A·A·B·B ≡A·B·A·B (idempotence law)


≡A·B (idempotence law)
≡A·B (double negation)
≡A+B De Morgan’s law

A similar approach can be used to prove the NOR gate is also a universal gate.

2.6 Canonical normal form


A Boolean expression with inputs A1 , A2 , . . . , An can be represented in Canonical Disjunctive Normal
Form (CDNF) where a sum of terms where each term is a product of the inputs. So CDNF is also known
as Sum Of Products (SOP) form. For example, the expression

f (A, B, C) = A · B · C + A · B · C + A · B · C + A · B · C, (2.1)

is in SOP form.

Dr Jon Shiach 20 Computing Mathematics Lecture Notes


Back to Table of Contents Chapter 2. Logic Circuits

Alternatively we can represent an expression in Canonical Conjunctive Normal Form (CCNF) where a
product of terms where each term is a sum of the inputs. So CCNF is also known as Product Of Sums
(POS) form. For example, the expression

f (A, B, C) = (A + B + C) · (A + B + C) · (A + B + C) · (A + B + C), (2.2)

is in POS form.

2.6.1 Minterms
A minterm is a product of the inputs with the condition that each input appears once, either in its
uncomplemented or complemented form. A Boolean expression in SOP form is a sum of minterms, i.e.,

f (A1 , A2 , . . . , An ) = m1 + m2 + · · · + mp ,

where mi are the minterms and A1 , A2 , . . . , An are inputs. For example, the Boolean expression in equa-
tion (2.1) has four minterms A · B · C, A · B · C, A · B · C and A · B · C. Minterms can also be thought
of as the product of the inputs where the truth table for f (A1 , A2 , . . . , An ) has a value of 1.
We can use minterms to represent a Boolean expression in a compact form. We determine a minterm
index, i, for the minterm mi = A1 · A2 · . . . · An which is a decimal number by assigning a value of 1 for the
uncomplemented input A or 0 for the complemented input 1. The values in a minterm are concatenated
to give a binary number which is converted to a decimal which is the minterm index. For example, for the
minterm A · B · C we have

A · B · C ≡ m011 ≡ m3 .

A Boolean expression can then be represented by


p
X
f (A1 , A2 , . . . , An ) = mi ,
i=1

where p is the number of minterms and the summation operator represents a sequence of disjunction
P

operations.
For example, for the Boolean expression in equation (2.1)

f (A, B, C) = A · B · C + A · B · C + A · B · C + A · B · C
≡ m111 + m011 + m101 + m010
≡ m7 + m3 + m5 + m2 ,

so f (A, B, C) = mi .
X

i=2,3,5,7

2.6.2 Maxterms
A maxterm is similar to a minterm but for expressions in canonical conjunctive normal form, i.e., in the
expression

f (A1 , A2 , . . . , An ) = M1 · M2 · . . . · Mp ,

Mi are the maxterms. For example, the expression in equation (2.2) has the four maxterms A + B + C,
A + B + C, A + B + C and A + B + C. Maxterms can also be thought of as the sum of the inputs where
the truth table for f (A1 , A2 , . . . , An ) has a value of 0.
Similar to minterms, we can assign an index, i, for the maxterm Mi = A1 + A2 + · · · + An . For example,
for the maxterm A + B + C we have

A + B + C ≡ M101 ≡ M5 .

Dr Jon Shiach 21 Computing Mathematics Lecture Notes


Chapter 2. Logic Circuits Back to Table of Contents

A Boolean expression can then be represented using


p
Y
f (A1 , A2 , . . . , An ) = Mi ,
i=1

where p is the number of maxterms and the product operator represents a sequence of conjunction
Q

operations.
For example, for the Boolean expression in equation (2.2)

f (A, B, C) = (A + B + C) · (A + B + C) · (A + B + C) · (A + B + C)
≡ M100 · M001 · M101 · M110
≡ M 4 · M1 · M5 · M6 ,

so f (A, B, C) ≡ Mi .
Y

i=1,4,5,6

2.6.3 Writing Boolean expressions using minterms


Any Boolean expression can be expressed using minterms by:
1. use the laws of Boolean algebra to remove brackets and negation of disjunctive and conjunctive (OR
and AND) operations;
2. for each term not containing all inputs, append a conjunction (AND) with the disjunction with the
missing input and its complement (e.g., A ≡ A · (B + B));
3. use the distributivity law to expand out the brackets;
4. remove any duplicate minterms.
For example, consider the expression A · B · C + A · B + A · C. Here the input C is missing from the second
term and B is missing from the third term, so

A · B · C + A · B + A + C ≡ A · B · C + A · B · (C + C) + A · C · (B + B)
≡A·B·C +A·B·C +A·B·C +A·B·C +A·B·C
≡A·B·C +A·B·C +A·B·C +A·B·C
≡ m110 + m111 + m011 + m001 ,
X
≡ mi .
i=1,3,6,7

2.6.4 Converting between SOP and POS forms


Consider the truth table for the expression in SOP form: f (A, B, C) = A·B·C +A·B·C +A·B·C +A·B·C,

A B C f (A, B, C)
0 0 0 0
0 0 1 0
0 1 0 1
0 1 1 1
1 0 0 0
1 0 1 1
1 1 0 0
1 1 1 1

Table 2.3: Truth table for the function f (A, B, C) = A · B · C + A · B · C + A · B · C + A · B · C.

Dr Jon Shiach 22 Computing Mathematics Lecture Notes


Back to Table of Contents Chapter 2. Logic Circuits

The minterms are the product of the inputs where f (A, B, C) = 1 and the maxterms are the sum of the
inputs where f (A, B, C) = 0. So to find an expression in POS form that is equivalent to f (A, B, C) we
find the maxterms and invert each input.
To convert from SOP to POS:
1. find the minterms, mi ;
2. identify the maxterm indices by removing the minterm indices from {0, 1, . . . , 2n − 1};
3. convert maxterm indices to binary, find the complement and convert to decimal j;
4. change the to and mi to Mj ;
P Q

For example, consider the SOP expression

f (A, B, C) = A · B · C + A · B · C + A · B · C + A · B · C.

Expressing f (A, B, C) using the minterms gives

f (A, B, C) ≡ m111 + m011 + m101 + m110


≡ m7 + m3 + m5 + m2

Since there are 3 inputs so the set of all possible minterms is {0, 1, 2, 3, 4, 5, 6, 7} so the maxterms are M0 ,
M1 , M4 and M6 .

f (A, B, C) ≡ M000 · M001 · M010 · M110


≡ M111 · M110 · M101 · M001
≡ (A + B + C) · (A + B + C) · (A + B + C) · (A + B + C).

To convert from POS to SOP we use the same steps for conversion between SOP to POS with the exception
that we change to . For example, consider following expression in POS form
Q P

f (A, B, C) = (A + B + C) · (A + B + C) · (A + B + C) · (A + B + C)

Expressing f (A, B, C) using the maxterms gives

f (A, B, C) ≡ M100 · M001 · M101 · M110


≡ M4 · M1 · M 5 · M6 .

Here the minterms are m0 , m2 , m3 and m7 so

f (A, B, C) ≡ m000 + m010 + m011 + m111


≡ m111 + m101 + m100 + m000
≡ A · B · C + A · B · C + A · B · C + A · B · C.

Example 2.3

For each Boolean expression below write it in SOP and POS forms.
1. f (A, B) = A · (A + B);
2. f (A, B, C) = (A + C) · (A + B).

Solution:

Dr Jon Shiach 23 Computing Mathematics Lecture Notes


Chapter 2. Logic Circuits Back to Table of Contents

1.

f (A, B) ≡ A · (A + B)
≡A·A+A·B
≡A·B

Expressing f (A, B) using minterms gives

f (A, B) ≡ m00 = m0

so the maxterms are M1 , M2 and M3 therefore

f (A, B) ≡ M01 · M10 · M11


≡ M10 · M01 · M00
≡ (A + B) · (A + B) · (A + B).

2.

f (A, B, C) = (A + C) · (A + B)
≡A·A+A·B+A·C +B·C
≡A·B+A·C +B·C
≡ A · B · (C + C) + A · C · (B + B) + B · C · (A + A)
≡A·B·C +A·B·C +A·B·C +A·B·C +A·B·C +A·B·C
≡A·B·C +A·B·C +A·B·C +A·B·C +A·B·C

Expressing f (A, B, C) using minterms we have

f (A, B, C) ≡ m111 + m110 + m011 + m001 + m101


≡ m7 + m6 + m3 + m1 + m5

so the maxterms are M0 , M2 and M4 therefore

f (A, B, C) ≡ M000 · M010 · M100


≡ M111 · M101 · M011
≡ (A + B + C) · (A + B + C) · (A + B + C).

2.7 Karnaugh maps


Simplifying Boolean expressions can often be a long winded procedure requiring knowing all of the laws of
Boolean algebra. Karnaugh maps (also known as K-maps) were introduced by American mathematician
Maurice Karnaugh in 1953 and are a useful tool for simplifying Boolean expressions since it uses our inherent
pattern recognition ability.

Consider the truth table of the Boolean expression A + B shown in table 2.4. We can represent this
as a Karnaugh map by forming a grid with the inputs represented by the rows and columns of the grid

Dr Jon Shiach 24 Computing Mathematics Lecture Notes


Back to Table of Contents Chapter 2. Logic Circuits

Table 2.4: Truth table for A + B.

A B A+B
0 0 0
0 1 1
1 0 1
1 1 1

(figure 2.11). The outputs of the Boolean expression are represented by 0s and 1s in the Karnaugh map
corresponding to their inputs.

B
A 0 1
0 0 1

1 1 1

Figure 2.11: Karnaugh map for A + B.

2.7.1 Finding the Boolean expression from a Karnaugh map


When presented with a Karnaugh map we can establish the Boolean expression that it represents by
grouping grid cells that contain adjacent 1s that form a rectangle. Each group of 1s corresponds to a
minterm of the Boolean expression. For example, consider the Karnaugh map in figure 2.12 where the 1s
in the second vertical column and the 1s in the second horizontal row of the Karnaugh map for A + B
(figure 2.11) have been grouped. The rules for groupings in Karnaugh maps are given in section 2.7.2.

B
A 0 1
0 0 1

1 1 1

Figure 2.12: The Karnaugh map for A + B with the 1s grouped.

Taking the vertical column (blue) in figure 2.12 to start with here we see that the the input value A is 1
for both the elements in the group and the input values B is both 0 and 1. What this means is that this
group represents the minterm A since it is alway true.
Now looking at the horizontal row (red) we see that the input value A is both 0 and 1 whilst the input
value B is 1 for both of the 1s in the group. Therefore this group represents the minterm B.
Since each grouping of a Karnaugh map represents a minterm for the Boolean expression it represents then
the expression for this Karnaugh maps is A + B as expected..
Alternatively, groups of 0s in a Karnaugh map corresponds to the maxterms in a Boolean expression which
is the complement of the expression that the Karnaugh map represents f (A1 , A2 , . . . , An ). The Karnaugh
map in figure 2.13 is the Karnaugh map for A+B (figure 2.11) with the 0s grouped. This group corresponds
to the maxterm A · B which is equivalent A + B, i.e., the complement of A + B.

2.7.2 Rules of Karnaugh maps


When implementing Karnaugh maps we have to abide by the following rules (Belton 1998):

Dr Jon Shiach 25 Computing Mathematics Lecture Notes


Chapter 2. Logic Circuits Back to Table of Contents

B
A 0 1
0 0 1

1 1 1

Figure 2.13: The Karnaugh map for A + B with the 0s grouped.

• Input numbering can only change by a single bit from one row or column to the next.

correct incorrect
BC BC
00 01 11 10 00 01 10 11
A A
0 0 0 0 0 0 0 0 0 0

1 1 1 0 0 1 1 1 0 0

• Groups may not include any cell containing a zero

correct incorrect
BC BC
A 00 01 11 10 A 00 01 11 10
0 0 0 0 0 0 0 0 0 0

1 1 1 0 0 1 1 1 0 0

• Groups can be horizontal or vertical but not diagonal.

correct incorrect
BC BC
A 00 01 11 10 A 00 01 11 10
0 0 0 1 0 0 0 0 1 0

1 1 1 0 0 1 1 1 0 0

• The number of cells in a group must be 2n , i.e., 1, 2, 4, 8, . . .

correct incorrect
BC BC
A 00 01 11 10 A 00 01 11 10
0 0 0 0 0 0 0 0 0 0

1 1 1 1 0 1 1 1 1 0

2 groups of 2 1 group of 3

• Each group should be as large as possible (not doing this will not break the laws of Boolean algebra
but it wouldn’t result in the simplest form).

Dr Jon Shiach 26 Computing Mathematics Lecture Notes


Back to Table of Contents Chapter 2. Logic Circuits

correct incorrect
BC BC
A 00 01 11 10 A 00 01 11 10
0 1 1 1 1 0 1 1 1 1

1 0 0 1 1 1 0 0 1 1

• Groups may wrap around the edges of the Karnaugh map. The leftmost column may be grouped
with the rightmost column and the top row may be grouped with the bottom row.
correct incorrect
BC BC
A 00 01 11 10 A 00 01 11 10
0 1 0 0 1 0 1 0 0 1

1 1 0 0 1 1 1 0 0 1

• Each cell containing a 1 must be in at least one group.


• Groups may overlap.
• There should be as few groups as possible as long as the previous rules are not contradicted.

2.7.3 Simplify Boolean expressions using Karnaugh maps


Consider the Boolean expression A · B + A · B + A · B. The minterms are A · B, A · B and A · B so the
Karnaugh maps has 1s where A = B = 0, A = 0 and B = 1 and A = B = 1.
B
A 0 1
0 1 1

1 0 1

We can form two groups:


B
A 0 1
0 1 1

1 0 1

The horizontal (blue) group corresponds to the minterm A and the vertical (red) group corresponds to the
minterm B so this Karnaugh map represents A + B. If we simplify the original expression using the laws
of Boolean algebra

A · B + A · B + A · B ≡ A · (B + B) + A · B (distributivity law)
≡A+A·B (complement law)
≡ (A + A) · (A + B) (distributivity law)
≡ A + B. (complement law)

Which shows that the simplification using the Karnaugh map is correct. Note that Karnaugh maps give
the Boolean expression in SOP form which may be simplified further by use of the laws of Boolean algebra.

Dr Jon Shiach 27 Computing Mathematics Lecture Notes


Chapter 2. Logic Circuits Back to Table of Contents

Example 2.4

For each of the Boolean expressions below:


1. f (A, B) = A · B + A · B + A · B;
2. f (A, B, C) = A · B + B · C + B · C + A · B · C;
do the following:
(a) construct a truth table;
(b) use a Karnaugh map to find the SOP form;
(c) use a Karnaugh map to find the POS form;
(d) construct a truth table for the SOP and POS forms.

Solution:
1. (a)
A B A·B+A·B+A·B
0 0 1
0 1 1
1 0 1
1 1 0
(b)
B
A 0 1
0 1 1

1 1 0

f (A, B) = A + B
(c)
A
B 0 1
0 1 1

1 1 0

f (A, B) ≡ A · B
f (A, B) ≡ A · B
≡A+B

(d)
A B A+B
0 0 1
0 1 1
1 0 1
1 1 0
2. (a)

Dr Jon Shiach 28 Computing Mathematics Lecture Notes


Back to Table of Contents Chapter 2. Logic Circuits

A B C A·B+B·C +B·C +A·B·C


0 0 0 0
0 0 1 0
0 1 0 1
0 1 1 1
1 0 0 1
1 0 1 0
1 1 0 1
1 1 1 1
(b)
BC
A 00 01 11 10
0 0 0 1 1

1 1 0 1 1

f (A, B, C) ≡ B + A · C
(c)
BC
A 00 01 11 10
0 0 0 1 1

1 1 0 1 1

f (A, B, C) ≡ (A · B) + (B · C)
f (A, B, C) ≡ (A · B) · (B · C)
≡ (A · B) + (B · C)
≡ (A + B) · (B + C)
≡ (A + B) · (B + C)

(d)
A B C B+A·C (A + B) · (B + C)
0 0 0 0 0
0 0 1 0 0
0 1 0 1 1
0 1 1 1 1
1 0 0 1 1
1 0 1 0 0
1 1 0 1 1
1 1 1 1 1

Dr Jon Shiach 29 Computing Mathematics Lecture Notes


Chapter 2. Logic Circuits Back to Table of Contents

2.8 Half and full adders


The purpose of logic gates is to build circuits that perform arithmetic operations. The two basic arithmetic
operations are addition and subtraction, from these we can derive multiplication, division, exponents and
roots. The half adder of full adder are logic circuits that are used to sum two or more binary inputs.

2.8.1 Half adder


Consider the binary addition of two inputs A + B (here the + symbol represents addition and not the OR
gate), the four possible combinations are

0 + 0 = 00,
0 + 1 = 01,
1 + 0 = 01,
1 + 1 = 10.

We refer to the digit on the right of the output as the sum or S and the digit on the left as the carry or
C. The first three cases we only need 1 bit to store the output since the value of the carry is 0 but in
the fourth case where the value of the carry is 1 we will require 2 bits to store the output. So we need to
create a logic circuit that can perform this addition and output the sum and the carry. The truth table for
the addition of two 1-bit numbers is shown in table 2.5.
Table 2.5: Truth table for the addition of two 1 bit numbers.

inputs outputs
A B C S
0 0 0 0
0 1 0 1
1 0 0 1
1 1 1 0

The corresponding Karnaugh map for the S and C columns from table 2.5 are shown in figure 2.14.

B B
A 0 1 A 0 1
0 0 0 0 0 1

1 0 1 1 1 0

(a) carry C (b) sum S

Figure 2.14: Karnaugh maps for the sum and carry column for the addition of two 1 bit numbers.

Grouping the 1s in figure 2.14(a) gives

S = A · B, (2.3)

and grouping the 1s in figure 2.14(b) gives

C =A·B+A·B
≡ A ⊕ B, (2.4)

which is an XOR gate. Therefore the logic circuit for adding two 1 bit numbers is shown in figure 2.15.
This circuit is known as the half adder.

Dr Jon Shiach 30 Computing Mathematics Lecture Notes


Back to Table of Contents Chapter 2. Logic Circuits

A
S

C
B

Figure 2.15: The half adder logic circuit.

2.8.2 Full adder


The problem with the half adder is that whilst it is able to sum two 1 bit inputs it cannot be combined so
that more than two bit inputs are summed. Consider the binary addition 01 + 11.

0 1
1 1 +
carry
sum

Starting at with the right column, we have the sum 1 + 1 = 10 so we put 0 in the sum and we carry 1 over
to the next column to the left.

0 1
1 1 +
carry 1
sum 0

Moving to the next column to the left we have the sum 0 + 1 + 1 = 10 so again we put 0 in the sum and
carry the 1 to the next column on the left.

0 1
1 1 +
carry 1 1
sum 0 0

We now have no more columns and the answer can be read as the last carry value along with the sum
which is 100 as required (i.e., 01 + 11 in binary is equivalent to 1 + 3 in decimal which is 4 and the binary
equivalent is 100).
To build circuits that can sum 2 or more bit numbers we need to be above to sum to 1 bit numbers and
a carry from previous sums. This circuit is known as the full adder which takes inputs of two 1 bit values
A and B as well as a carry value Cin which may have come from a previous calculation. The truth table
for a full adder is shown in table 2.6 and the Karnaugh maps for the Cout and S columns are shown in
figure 2.16.

Table 2.6: The truth table for a full adder.

inputs outputs
A B Cin Cout S
0 0 0 0 0
0 0 1 0 1
0 1 0 0 1
0 1 1 1 0
1 0 0 0 1
1 0 1 1 0
1 1 0 1 0
1 1 1 1 1

Dr Jon Shiach 31 Computing Mathematics Lecture Notes


Chapter 2. Logic Circuits Back to Table of Contents

BCin BCin
A 00 01 11 10 A 00 01 11 10
0 0 0 1 0 0 0 1 0 1

1 0 1 1 1 1 1 0 1 0

(a) carry Cout (b) sum S

Figure 2.16: Karnaugh maps for the sum and carry column for a full adder.

Grouping the 1s in figure 2.16(a) gives the Boolean expression A·B +B ·Cin +A·C which can be simplified
using the distributivity law to
Cout = A · B + Cin · (A + B). (2.5)
Grouping the 1s in figure 2.16(b) (which is trivial as we only have groups of size 1) gives the Boolean
expression A · B · C + A · B · C + A · B · C + A · B · C. Simplifying using the laws of Boolean algebra
S =A·B·C +A·B·C +A·B·C +A·B·C
≡ C · (A · B + A · B) + C · (A · B + A · B) (distributivity law)
≡ C · (A · B + A · B) + C · (A · B + A · B) (see below)
≡ C · (A ⊕ B) + C · (A ⊕ B) (definition of A ⊕ B)
≡ A ⊕ B ⊕ C. (definition of A ⊕ B) (2.6)

It might not immediately be obvious to you that A · B + A · B ≡ A · B + A · B in the third line so let’s
take moment here to check this
A·B+A·B ≡A+B·A+B (De Morgan’s law)
≡ (A + B) · (A + B) (De Morgan’s law)
≡A·A+A·B+A·B+B·B (distributive law)
≡0+A·B+A·B+0 (complement law)
≡ A · B + A · B. (identity law)
So we now have two Boolean expressions for Cout and S in equations (2.5) and (2.6) for a full adder. One
thing to note that the A · B term in equation (2.5) is the carry output from the half adder equation (2.3)
and the A ⊕ B in S is the sum output from the half adder equation (2.4). Therefore the circuit for the full
adder can be built by combining two half adder circuits where the sum output from the first half adder is
combined with Cin in the second half adder where the sum output from the second half adder is S and the
two carry outputs are combined using an XOR gate to give Cout . The circuit for the full adder is shown in
figure 2.17.

Cin
S

A
Cout

Figure 2.17: Two half adders combined to make a full adder.

Note that we could replace the XOR gate connecting the two carry outputs with an OR gate and not affect
the circuit, however this would mean using three different types of gates (XOR, AND and OR) when for
manufacturing reasons fewer types is often preferable.

Dr Jon Shiach 32 Computing Mathematics Lecture Notes


Back to Table of Contents Chapter 2. Logic Circuits

2.8.3 The ripple carry adder


We can string together multiple full adders to create a circuit capable of computing the addition of n bit
numbers. For each bit we require a full adder where the inputs are the two bits that are being added
together along with the carry output from the previous digit.

A3 B 3 A2 B2 A1 B 1 A0 B 0

full C2 full C1 full C0 full


C3 0
adder 3 adder 2 adder 1 adder 0

S3 S2 S1 S0

Figure 2.18: Four single bit full adders combined to make a ripple adder to compute the sum of
two 4 bit numbers.

Consider the ripple carry adder shown in figure 2.18. This consists of four single bit full adders so it can
perform the addition of two 4 bit binary numbers A3 A2 A1 A0 and B3 B2 B1 B0 . The inputs to full adder 0
are the rightmost digits of the two numbers, A0 and B0 , and 0 (note that full adder 0 could also be a half
adder since the Cin input is 0 but it is simpler just to use all full adders here). The outputs for adder 0 are
the rightmost digit of the sum S0 and the carry C0 . The inputs to adder 1 are the next two digits to the
left, A1 and B1 , and the carry from adder 0. The outputs for adder 1 are the next digit to the left of the
sum S1 and the carry C1 . This pattern continues along the chain until all bits have been added together,
the digits of the final sum are C3 S3 S2 S1 S0 .

1 1 0 0 0 1 1 1

full 0 full 1 full 1 full


1 0
adder 3 adder 2 adder 1 adder 0

0 1 0 0

Figure 2.19: The addition of A = 1001 and B = 1011 using a ripple carry adder.

For example, consider the sum 1001 + 1011.


• adder 0: inputs are A0 = 1, B0 = 1 and 0 so S0 = 0 and C0 = 1;
• adder 1: inputs are A1 = 0, B1 = 1 and C0 = 1 so S1 = 0 and C1 = 1;
• adder 2: inputs are A2 = 0, B2 = 0 and C1 = 1 so S2 = 1 and C2 = 0;
• adder 3: inputs are A3 = 1, B3 = 1 and C2 = 0 so S3 = 0 and C3 = 1.
Therefore the answer to the sum is C3 S3 S2 S1 S0 = 10100. The decimal equivalent to this sum is 9 + 11 =
20 = 1 × 16 + 0 × 8 + 1 × 4 + 0 × 2 + 0 × 1 given the binary equivalent 10100.

2.8.4 Representing the half adder and adder using NAND gates
As mentioned in the previous section, it is preferable to use fewer types of gates in a logic circuit where
possible. Since NAND gates are universal gates we can replace the XOR and AND gates in figures 2.15
and 2.17 with the NAND gates equivalents from section 2.5 to give the circuit diagrams shown in figures 2.20
and 2.21.

Dr Jon Shiach 33 Computing Mathematics Lecture Notes


Chapter 2. Logic Circuits Back to Table of Contents

Figure 2.20: The half adder constructed using only NAND gates.

Cout
Cin
A

Figure 2.21: The full adder constructed using only NAND gates.

Dr Jon Shiach 34 Computing Mathematics Lecture Notes


Back to Table of Contents Chapter 2. Logic Circuits

2.9 Tutorial exercises


Exercise 2.1. Complete the truth tables for the following circuits:
(a)
A B C Q
A C
Q 0 0
B 0 1
1 0
1 1
(b)

A C
A B C Q
Q
0 0
B 0 1
1 0
1 1
(c)

A C
Q

A B C D Q
B 0 0
D 0 1
1 0
1 1
Exercise 2.2. Draw circuit diagrams for the following Boolean expressions:
(a) A + B · B; (b) (A · B) · (A + C); (c) A · B + C + A · B.
Exercise 2.3. Simplify the following Boolean expressions. For each step of the simplification, state which
laws you have used.
(a) A · (A + B); (b) (A + AB) · (C + B · C);

(c) (A + B) · (A + B); (d) (A + B · C) · (A + B).


Exercise 2.4. Simplify the Boolean expression for the circuit below so that it uses the fewest possible
number of logic gates and draw the simplified circuit diagram.
A
B

Exercise 2.5. Prove that the NOR gate is a universal gate.


Exercise 2.6. For each of the Boolean expressions below find the SOP and POS forms.
(a) f (A, B) = A · (A + B);

(b) f (A, B, C) = A · (B + C);

Dr Jon Shiach 35 Computing Mathematics Lecture Notes


Chapter 2. Logic Circuits Back to Table of Contents

(c) f (A, B, C) = (A + B) · (B + C).


Exercise 2.7. For each of the truth tables below, draw the corresponding Karnaugh map and use them to
find the SOP and POS forms.
(a) (b) (c)
A B Q A B C Q A B C Q
0 0 1 0 0 0 0 0 0 0 1
0 1 0 0 0 1 1 0 0 1 0
1 0 1 0 1 0 1 0 1 0 1
1 1 1 0 1 1 1 0 1 1 0
1 0 0 1 1 0 0 0
1 0 1 0 1 0 1 1
1 1 0 0 1 1 0 0
1 1 1 0 1 1 1 1
Exercise 2.8. Given the following truth table:
A B C f (A, B, C)
0 0 0 0
0 0 1 1
0 1 0 0
0 1 1 0
1 0 0 1
1 0 1 1
1 1 0 0
1 1 1 0
(a) design a circuit that uses the fewest possible logic gates to produce the output f (A, B, C);

(b) draw the circuit using only NAND gates.


The solutions to these exercises are given on page 91.

Dr Jon Shiach 36 Computing Mathematics Lecture Notes


Chapter 3

Algorithms

Learning outcomes
On successful completion of this chapters students will be able to:
• read an algorithm from pseudocode and implement it;
• apply the bubble sort, quicksort and merge sort algorithm to sort a list of numbers into ascending
order;
• understand and apply recursive operations;
• compare the time cost of algorithms using big-O notation.

3.1 Greatest common divisor algorithm


An algorithm is a series of steps that is used to perform computation, usually with the intention to
solve a problem. The word algorithm is derived from the work of the 9th Century Persian mathemati-
cian Muh.ammad ibn Mūsā al-Khwūrizmı̄ which when translated to Latin the word algoritmi (Wikipedia
contributors 2001).
A well known algorithm is Euclid’s algorithm that is used to calculate the Greatest Common Divisor (GCD)
of two numbers a and b. At the first step (known as step k = 0) the smaller number, b say, is subtracted
from the larger number a. At the start of the second step (k = 1) one of the values of a or b has changed
during the first step and we repeat the subtraction of the small number from the larger number. The steps
are repeated until a = b which is the GCD.
For example, using Euclid’s algorithm to calculate the GCD of 315 and 588

k = 0, a = 588, b = 315, =⇒ a ← 588 − 315 = 273,


k = 1, a = 273, b = 315, =⇒ b ← 315 − 273 = 42,
k = 2, a = 273, b = 42, =⇒ a ← 273 − 42 = 231,
k = 3, a = 231, b = 42, =⇒ a ← 231 − 42 = 189,
k = 4, a = 189, b = 42, =⇒ a ← 189 − 42 = 147,
k = 5, a = 147, b = 42, =⇒ a ← 147 − 42 = 105,
k = 6, a = 105, b = 42, =⇒ a ← 105 − 42 = 63,
k = 7, a = 63, b = 42, =⇒ a ← 63 − 42 = 21,
k = 8, a = 21, b = 42, =⇒ b ← 42 − 21 = 21,
k = 9, a = 21, b = 21.

So GCD(315, 588) = 21 (note that the a ← x notation means a is defined to be equal to x). Here steps
k = 2 to k = 7 involved repeated subtraction of 42 which eventually resulted in a assuming the value of

37
Chapter 3. Algorithms Back to Table of Contents

21. This is the same as dividing 273 by 42 and finding the remainder which is 21, i.e.,

273 = 6 × 42 + 21.

Since a takes on the value of the remainder and b is now larger than a then the next calculation is to set
b to b − a. To ensure we are always doing the same calculation we can set b to the remainder and a to b.
Therefore Euclid’s algorithm can be rewritten so that the values at the k th step are

ak ← bk−1 ,
bk ← mod(ak−1 , bk−1 ),

where the subscript k − 1 denotes the values from the previous step and mod(a, b) is the modulo operator
which returns the remainder of a ÷ b. The algorithm terminates when a = b which will be when mod = 0.
Applying the rewritten Euclid’s algorithm to a = 588 and b = 315

k = 0, a = 588, b = 315, =⇒ a ← 315, b ← mod(588, 315) = 273,


k = 1, a = 315, b = 273, =⇒ a ← 273, b ← mod(315, 273) = 42,
k = 2, a = 273, b = 42, =⇒ a ← 42, b ← mod(273, 42) = 21,
k = 3, a = 42, b = 21, =⇒ a ← 21, b ← mod(41, 21) = 0,

so GCD(588, 315) = 21 as before.


The Euclid’s algorithm is presented in pseudocode in algorithm 1. Pseudocode is a method of presenting
an algorithm in a way that resembles the commands used by a programming language but without using
the particular syntax of any programming language. This way the algorithm is written so that it can be
applied to a problem or translated to a specific programming language.

Algorithm 1 Pseudocode for Euclid’s algorithm


function GCD(a, b)
if a < b then
t←a . swap a and b
a←b
b←t
end if
while b 6= 0 do
t←b . temp is a temporary variable used to store the value of b
b ← mod(a, b)
a←t
end while
return a
end function

3.1.1 Python and MATLAB code


Python and MATLAB code for Euclid’s algorithm are shown in listings 3.1 and 3.2. Note that since Python
can perform multiple operations on a single line we do not need to use a temporary variable for swapping
a and b.

Listing 3.1: Python code for Euclid’s algortihm.


def gcd (a , b ) :
if a < b :
a, b = b, a
while b != 0:
a, b = b, a % b
return a

Dr Jon Shiach 38 Computing Mathematics Lecture Notes


Back to Table of Contents Chapter 3. Algorithms

Listing 3.2: MATLAB code for Euclid’s algortihm.


function a = gcd (a , b )

if a < b
t = a;
a = b;
b = t;
end

while b ~= 0
t = b;
b = mod (a , b ) ;
a = t;
end

end

3.2 Sorting algorithms


One of the most common problems encountered in computing is the sorting of items into ascending or
descending order.

3.2.1 Bubble sort


Bubble sort is the simplest sorting algorithm. Given a list of numbers, the algorithm loops through pairs
of numbers and swaps them if the first number has a higher value than the second number. This will
require several passes through the list and the algorithm terminates when no swaps have occurred in the
last pass through. This has the effect that the higher value numbers will gradually migrate to the end of
the list similar to bubbles rising to the top of a liquid, which is how the algorithm gets it’s name.
For example, lets apply the bubble sort algorithm to the list of numbers [6, 1, 5, 3, 7, 2, 4] as shown in
figure 3.1.

6 1 5 3 7 2 4 1 5 3 6 2 4 7 1 3 5 2 4 6 7 1 3 2 4 5 6 7

1 6 5 3 7 2 4 1 5 3 6 2 4 7 1 3 5 2 4 6 7 1 3 2 4 5 6 7

1 5 6 3 7 2 4 1 3 5 6 2 4 7 1 3 5 2 4 6 7 1 2 3 4 5 6 7

1 5 3 6 7 2 4 1 3 5 6 2 4 7 1 3 2 5 4 6 7

1 5 3 6 7 2 4 1 3 5 2 6 4 7 1 3 2 4 5 6 7

1 5 3 6 2 7 4 1 3 5 2 4 6 7

1 5 3 6 2 4 7 1 3 5 2 4 6 7 1
(a) Pass 1 (b) Pass 2 (c) Pass 3 (d) Pass 4

Figure 3.1: Implementation of the bubble sort algorithm.

In the first pass, figure 3.1(a), five swaps were required to move 6 and 7 up the list. In the second pass,
figure 3.1(b), since we know that the largest number in the list must now be at the top we have one fewer
pairs to check. After the second pass we have the highest two numbers in the correct place. The algorithm
requires a further two passes through the list to sort it into the correct order and another pass (not shown)
that would not have any swaps so the algorithm terminates.

Dr Jon Shiach 39 Computing Mathematics Lecture Notes


Chapter 3. Algorithms Back to Table of Contents

Example 3.1

Use the bubble sort algorithm to sort the numbers [3, 1, 6, 5, 4, 2].

Solution:
Pass 1: there are 6 numbers in the list so we need to check 5 pairs in pass 1
[3, 1, 6, 5, 4, 2] swap 3 and 1
[1, 3, 6, 5, 4, 2] do not swap 3 and 6
[1, 3, 6, 5, 4, 2] swap 6 and 5
[1, 3, 5, 6, 4, 2] swap 6 and 4
[1, 3, 5, 4, 6, 2] swap 6 and 2
[1, 3, 5, 4, 2, 6]
Pass 2: need to check 4 pairs
[1, 3, 5, 4, 2, 6] do not swap 1 and 3
[1, 3, 5, 4, 2, 6] do not swap 3 and 5
[1, 3, 5, 4, 2, 6] swap 5 and 4
[1, 3, 4, 5, 2, 6] swap 5 and 2
[1, 3, 4, 2, 5, 6]
Pass 3: need to check 3 pairs
[1, 3, 4, 2, 5, 6] do not swap 1 and 3
[1, 3, 4, 2, 5, 6] do not swap 3 and 4
[1, 3, 4, 2, 5, 6] swap 4 and 2
[1, 3, 2, 4, 5, 6]
Pass 4: need to check 2 pairs
[1, 3, 2, 4, 5, 6] do not swap 1 and 3
[1, 3, 2, 4, 5, 6] swap 3 and 2
[1, 2, 3, 4, 5, 6]
Pass 5: need to check 1 pair
[1, 2, 2, 4, 5, 6] do not swap 1 and 3
[1, 2, 3, 4, 5, 6]

The bubble sort algorithm is presented in pseudocode in algorithm 2.

Dr Jon Shiach 40 Computing Mathematics Lecture Notes


Back to Table of Contents Chapter 3. Algorithms

Algorithm 2 Pseudocode for the bubble sort algorithm


function Bubblesort(X)
n ← length of X
k←0
swap ← True . swap is a flag that shows if a swap has occurred
while swap = True do
swap ← False
for i = 1 . . . n − k − 1 do
if X(i) > X(i + 1) then
t ← X(i) . temp is a temporary variable used to store the value of X(i)
X(i) ← X(i + 1)
X(i + 1) ← t
swap ← True
end if
end for
k ←k+1
end while
return X
end function

3.2.2 Quicksort
The major disadvantage with the bubble sort algorithm is that it is slow to implement. A quicker algorithm is
quicksort which is a popular sorting algorithm developed by British computer scientist Tony Hoare (1961).
The algorithm works by choosing a number from the list to be the pivot, the list is then partitioned about
the pivot so that those numbers that are less than the pivot number are moved to the left of the pivot and
those numbers that are greater than the pivot are moved to the right of the pivot. This process is repeated
for the two sub-lists which are less than or greater than the pivot. Eventually the sub-lists will contain just
element and the list will be in sorted order.
The choice of the pivot element is arbitrary, in the examples presented here the last element in the list
is chosen for simplicity. The partitioning of the list is done setting an index i to 1 (the index of the first
element in the loop so for a Python implementation this would be 0) and look for an element in the list
that is less than the pivot element. We swap this element with the one with index i and increment i by 1.
This continues until all non-pivot elements have been checked and then we swap the pivot element with
the one with index i which places the pivot between the left and right sub-lists.
The pseudocode for the quicksort algorithm is shown in algorithm 3. The algorithm uses two functions,
quicksort is a function that calls itself recursively starting with l = 1 and r = n (the index of the last
element in the list). The function partition performs that partitioning of the lists about the pivot.
For example, consider the implementation of the quicksort algorithm used to sort the list [6, 1, 5, 3, 7, 2,
4] as shown in figure 3.2. The steps of the algorithm are:
(1) X = [6, 1, 5, 3, 7, 2, 4]: l = 1 and r = 2 then l < r so swap 6 and 2;
(2) X = [2, 1, 5, 3, 7, 6, 4]: l = 3 and r = 4 then l < r so swap 5 and 3;
(3) X = [2, 1, 3, 5, 7, 6, 4]: l = 4 and r = 3 then l > r so swap 5 with the pivot 4;
(4) X = [2, 1, 3, 4, 7, 6, 5]: partitioning is complete for pivot 4, apply quicksort to sub-lists [2, 1, 3] and
[7, 6, 5]
(5) X = [2, 1, 3]: l = 3 and r = 1 then l > r so swap 3 with the pivot (which is itself);
X = [5, 6, 7]: l = r = 7 so swap 7 with the pivot 5;
(6) X = [2, 1, 3]: partitioning is complete for pivot 3, apply quicksort to the sub-list [2, 1];

Dr Jon Shiach 41 Computing Mathematics Lecture Notes


Chapter 3. Algorithms Back to Table of Contents

X = [5, 6, 7]: partitioning is complete for pivot 5, apply quicksort to the sub-list [6, 7];

(7) X = [1, 2]: l = r = 1 so swap 2 with the pivot 1;

X = [6, 7]: l = 7 and r = 6 then l > r so swap 7 with the pivot (which is itself);

(8) X = [1, 2]: partitioning is complete for pivot 2, apply quicksort to the sub-list [2];

X = [6, 7]: partitioning is complete for pivot 7, apply quicksort to the sub-list [6];

(9) X = [2]: list is of length 1 so exit;

X = [6]: list of of length 1 so exit and terminate algorithm. The final sorted list is X =
[1, 2, 3, 4, 5, 6, 7].

l r
1 6 1 5 3 7 2 4
l r
2 2 1 5 3 7 6 4
r l
3 2 1 3 5 7 6 4

4 2 1 3 4 7 6 5
r l l, r
5 2 1 3 4 7 6 5

6 2 1 3 4 5 6 7
l, r r l
7 2 1 3 4 5 6 7

8 1 2 3 4 5 6 7

9 1 2 3 4 5 6 7

1 2 3 4 5 6 7

Figure 3.2: Implementation of the quicksort algorithm.

Example 3.2

Use the quicksort algorithm to sort the numbers [3, 1, 6, 5, 4, 2].

Solution:
Choosing the last number in the list to be the pivot.

Dr Jon Shiach 42 Computing Mathematics Lecture Notes


Back to Table of Contents Chapter 3. Algorithms

[3, 1, 6, 5, 4, 2] swap 3 and 1


[1, 3, 6, 5, 4, 2] swap 3 and 2]
[1, 2, 3, 6, 5, 4] partition sub-lists [1] and [3, 6, 5, 4]
[ [1], 2, [3, 6, 5, 4] ] sub-list [1]: do nothing, sub-list [3, 6, 5, 4]: swap 6 and 4
[[1], 2, [3, 4, 6, 5] ] partition sub-lists [3] and [6, 5]
[[1], 2, [ [3], 4, [6, 5] ]] sub-list [3]: do nothing, sub-list [6, 5]: swap 6 and 5
[[1], 2, [[3], 4, [5, 6] ]] partition sub-list [6]
[[1], 2, [[3], 4, [5, [6] ]]] sub-list [6]: do nothing

All sub-lists only have one elements so the sorted list is [1, 2, 3, 4, 5, 6].

Algorithm 3 Pseudocode for the quicksort algorithm


function partition(X, l, r)
pivot ← X(r) . store the partition index in p
while l < r do
while X(l) < X(pivot) do . move l to the right
l =l+1
end while
while X(r) ≥ X(pivot) and r ≥ 1 do . move r to the left
r =r−1
end while
if l < r then . swap X(l) and X(r)
temp = X(l)
X(l) = X(r)
X(r) = temp
end if
end while
temp ← X(l) . move the pivot between the two sub-lists
X(l) ← X(pivot)
X(pivot) ← temp
return X and l
end function

function quicksort(X, l, r)
if l < r then
X, pivot ← quicksort(X, l, r) . move list elements either side of the pivot
X ← quicksort(X, l, p − 1) . repeat algorithm for left sub-list
X ← quicksort(X, p + 1, r) . repeat algorithm for right sub-list
end if
return X
end function

3.2.3 Merge sort


Quicksort is what is known as a “divide and conquer” method since the list is recursively partitioned until
we have lists containing two elements which are sorted in ascending order. Another divide and conquer
sorting method is merge sort. Merge sort is similar to quicksort in that we divide the list into two sub-lists
A and B but instead of populating the sub-lists depending on the value of the elements compared to a
pivot, the list is simply split at the element that is midway along the list so that A contains the numbers
in the lower half (including the midpoint) and B contains the numbers in the upper half. This splitting
continues until A and B contain just one element. The sub-lists are then merged by populating a sub-list

Dr Jon Shiach 43 Computing Mathematics Lecture Notes


Chapter 3. Algorithms Back to Table of Contents

by taking the next smallest element from A or B. The final merged list will contain the sorted list.

For example, consider the implementation of the merge sort algorithm used to sort the list [6, 1, 5, 3, 7,
2, 4] as shown in figure 3.3. The steps of the algorithm are:

(1) List [6, 1, 5, 3, 7, 2, 4] – the midpoint of the list is 3 the index of which is found using i =
int((listlength + 1)/2). The list is split into two sub-lists [6, 1, 5, 3] (containing the midpoint since
there are an odd number of elements) and [7, 2, 4].

(2) Sub-list [6, 1, 5, 3] – the midpoint is 1 so the list is split into the sub-lists [6, 1] and [5, 3].

(3) Sub-list [6, 1] – the midpoint is 6 so the list is split into the sub-lists [6] and [1].

(4) The sub-lists [6] and [1] are merged into the sub-list [1, 6].

(5) Sub-list [5, 3] – the midpoint is 5 so the list is split into the sub-lists [5] and [3].

(6) The sub-lists [5] and [3] are merged into the sub-list [3, 5].

(7) The sub-lists [1, 6] and [3, 5] are merged into the sub-list [1, 3, 5, 6].

(8) Sub-list [7, 2, 4] – the midpoint is 2 so the list is split into the sub-lists [7, 2] and [4].

(9) Sub-list [7, 2] – the midpoint is 7 so the list is split into the sub-lists [7] and [2].

(10) The sub-lists [7] and [2] are merged into the sub-list [2, 7].

(11) The sub-lists [2, 7] and [4] are merged into the sub-list [2, 4, 7].

(12) the sub-lists [1, 3, 5, 6] and [2, 4, 7] are merged into the sorted list [1, 2, 3, 4, 5, 6, 7].

midpoint

6 1 5 3 7 2 4
1

6 1 5 3 7 2 4
2 8

6 1 5 3 7 2 4
3 5 9

6 1 5 3 7 2
4 6 10

1 6 3 5 2 7
7 11

1 3 5 6 2 4 7
12

1 2 3 4 5 6 7

Figure 3.3: Implementation of merge sort.

Dr Jon Shiach 44 Computing Mathematics Lecture Notes


Back to Table of Contents Chapter 3. Algorithms

Example 3.3

Use the merge sort algorithm to sort the numbers [3, 1, 6, 5, 4, 2].

Solution:
Underlined numbers denote the midpoint.
[3, 1, 6, 5, 4, 2] partition list
[ [3, 1, 6], [5, 4, 2] ] partition sub-lists [3, 1, 6], [5, 4, 2]
[[ [3, 1], [6] ], [ [5, 4], [2] ]] partition sub-lists [3, 1] and [5, 4]
[[[ [3], [1] ], [6]], [[ [5], [4] ], [2]]] merge [3] with [1], and [5] with [4]
[[ [1, 3], [6]] , [ [ 4, 5 ], [2] ]] merge [1, 3] with [6] and [4, 5] with [2]
[ [1, 3, 6 ], [2, 4, 5 ] ] merge [1, 3, 6] with [2, 4, 5]
[1, 2, 3, 4, 5, 6]

The pseudocode for the merge sort algorithm is shown in algorithm 4

Algorithm 4 Pseudocode for the merge sort algorithm


function merge(L, R)
i←1
j←1
X ← empty list
while i < length(L) and j < length(R) do
if L(i) < R(i) then
append L(i) to X
i←i+1
else
append R(j) to X
j ←j+1
end if
end while
if i < length(L) then . L still has elements not in X
append the rest of L to the end of X
else . R still has elements not in X
append the rest of R to the end of X
end if
return X
end function

function mergesort(X)
if length(X) > 1 then
set m ← int((length(X) + 1)/2) . calculate index of midpoint
call L ← mergesort(X(1 : m)) . apply merge sort to left-hand list
call R ← mergesort(X(m + 1 : end)) . apply merge sort to right-hand list
call X ← merge(L, R) . merge left and right-hand lists
end if
return X
end function

Dr Jon Shiach 45 Computing Mathematics Lecture Notes


Chapter 3. Algorithms Back to Table of Contents

3.3 Recursion
The pseudocode for the quicksort and merge sort algorithms shown in algorithms 3 and 4 both involve a
function making a call to itself. This is known as recursion (see section 3.31 ) and is often used in divide
and conquer algorithms.
When using recursion in an algorithm the following structure is used:
• base case – this is a command used when a recursive step is not required and the current scenario
should terminate. For example, in algorithm 4, the mergesort function will return the list X if its
length is equal to 1, i.e., the list cannot be partitioned further.
• recursive step – this is a set of commands that are applied which will eventually result in the base
case being met. For example, in algorithm 4 if the length of the list X is greater than 1 then X is
split into two and the mergesort function is called recursively to each sub-list.
Consider the Fibonacci series which is defined by

0,

 if n = 0,
Fn = 1, if n = 1,

n−2 + Fn−1 , ifi > 1.

F

This gives the series

F0 = 0, F1 = 1, F2 = 1, F3 = 2, F4 = 3, F5 = 5, ...

The pseudocode for computing the Fibonacci number Fn is shown in algorithm 5

Algorithm 5 Pseudocode for computing Fibonacci numbers


function Fibonacci(n)
if n < 2 then
return n
else
F0 ← 0
F1 ← 1
for i = 1 . . . n do
F2 ← F0 + F1
F0 ← F1
F1 ← F2
end for
return F1
end if
end function

We can write an algorithm for computing the Fibonacci number Fn using recursion is shown in algorithm 6.
The base case is when n < 2 and the algorithm returns Fn = n (i.e.. F0 = 0 or F1 = 1). The recursive
step is implemented when n ≥ 2 and
We can visualise recursion using a tree structure. Consider the Fibonacci number F5 calculated using
recursion as shown in algorithm 6 which is represented using a tree in figure 3.4. Since n ≥ 2 then
F5 = F3 + F4 which also require a recursive step to calculate F3 = F1 + F2 and F4 = F2 + F3 and
so on. Where a base case is encountered we have a leaf node (see chapter 4 for details on trees) where
F0 = 0 and F1 = 1. The values of the parent nodes are the sum of the values of the child nodes, e.g.,
F2 = F0 + F1 = 0 + 1 = 1. Computing the values for all nodes in the tree gives F5 = 5.
1
This is a joke stolen from Google where if you search for ‘recursion’ it returns a message above the search results that
states “Did you mean: recursion” which links to the same page.

Dr Jon Shiach 46 Computing Mathematics Lecture Notes


Back to Table of Contents Chapter 3. Algorithms

Algorithm 6 Pseudocode for computing Fibonacci numbers using recursion


function Fibonacci(n)
if n < 2 then
return n
end if
return fibonacci(n − 1) + fibonacci(n − 2)
end function

F5 = 5

F3 = 2 F4 = 3

F1 = 1 F2 = 1 F2 = 1 F3 = 2

F0 = 1 F1 = 1 F0 = 0 F1 = 1 F1 = 1 F2 = 1

F0 = 0 F1 = 1

Figure 3.4: Tree representing the calculation of the Fibonacci number F5 .

Example 3.4

Write pseudocode that uses recursion to compute the sum of the numbers between 1 and n.

Solution:
We need to write a function sum(n) which will return the value 1 + 2 + · · · + n. So if n is the input
we need to go through the numbers in descending order, i.e.,

sum(n) = n + sum(n − 1)
= n + (n − 1) + sum(n − 2)
..
.
= n + (n − 1) + · · · + 2 + sum(1)

So the base case is when n = 1 and we return the value of n and in the recursive step we need
to add the current number n to the sum of all of the numbers less than n, i.e., n + sum(n − 1).
Therefore the pseudocode is
function sum(n)
if n = 1 then
return n
end if
return n + sum(n − 1)
end function

Dr Jon Shiach 47 Computing Mathematics Lecture Notes


Chapter 3. Algorithms Back to Table of Contents

3.4 Complexity
Complexity is the cost of implementing an algorithm and is based on the size of the input n. Even though
different algorithms may achieve the same results, they may have required more steps or computations to
get there. For example, in the previous section we looked three different algorithms for performing the
same action, sorting a list of numbers into ascending order. These algorithms were coded in Python and
used to sort an lists containing 1000 random integers. The times taken to sort the list are shown below.

Listing 3.3: Times taken for sorting algorithms to sort 1000 random numbers.
Bubble sort : 0.3325 s
Quicksort : 0.0552 s
Merge sort : 0.0054 s

So the bubble sort algorithm took the longest, followed by the quicksort algorithm and merge sort algorithm
was the quickest. So different algorithms take different amounts of time to run on a computer. The reason
for this is that they use different number of arithmetic operations and different memory requirements. It
is useful to know which algorithm will perform the quickest when presented with a task.
The factors affecting the speed which an algorithm runs on a computer are:
(i) the speed of the computer hardware;
(ii) the efficiency programming language used;
(iii) the efficiency of the program (i.e., the programming skills of the programmer);
(iv) the hardware requirements of the tasks running in the background (i.e., other programs running at
the same time);
(v) the complexity of the algorithm;
(vi) the size of the input.
Here (i) to (iv) will vary so can be ignored when analysing the speed of an algorithm where we focus on (v)
and (vi). The speed of computer hardware tends to increase over time so we need a way of measuring the
cost of an algorithm that is independent of time. For this reason we count the number of times a “principal
activity” is performed in an algorithm. The principal activity may depend on the type of algorithm, for
example in the bubble sort algorithm we were comparing pairs of numbers whereas in Euclid’s algorithm
we we calculating the modulo between two numbers.
Another thing we need to consider is that an algorithm may require different number of operations for
different inputs, for example, using the quicksort algorithm to sort an list that has already been sorted
would require no swaps so would be performed quicker than when used to sort and unsorted list. So we
think of the worst case, best case and average case
• worst case – this is the maximum number of times the principal activity has been performed for all
inputs of size n;
• best case – the minimum number of times the principal activity is performed for specific inputs of
size n;
• average case – the number of times the principal activity is performed on average.
The average case is the most useful measure as in practice we will rarely have an input that results in the
worst or best cases. However, this is difficult to measure since we are unlikely to know how the input set
will be distributed.

3.4.1 Big-O notation


Let’s determine the number of principal activities required to perform the bubblesort algorithm on a list
of n numbers. If we consider the principal activity to be checking whether a pair of numbers requires

Dr Jon Shiach 48 Computing Mathematics Lecture Notes


Back to Table of Contents Chapter 3. Algorithms

swapping then if we have n numbers in the list on the first pass we have n − 1 pairs to check. At the end
of the first pass we know that the largest number in the list is in the correct position at the end so on the
second pass we only need to check n − 2 pairs. Assuming that each pass requires at least one swap then

number of checks = 1 + 2 + · · · + (n − 2) + (n − 1).

This is the sum of an arithmetic series with the first term a = 1 and common difference d = 1 so
n
number of checks = (2a + (n − 1)d)
2
(n − 1)
= (2 + ((n − 1) − 1))
2
1 1
= n2 − n.
2 2
So the cost of the worst case for the bubble sort algorithm is 12 n2 − 12 n. Let t(n) be the time taken for a
computer to apply the bubble sort algorithm, if we have two computers, A and B, where computer B is
10 times faster than computer A then

computer A : t(n) = 10n2 − 10n,


computer B : t(n) = n2 − n.

Given an input of size n = 1000 we have

computer A : t(n) = 10(10002 ) − 10(1000) = 9990000,


computer B : t(n) = 10002 − 1000 = 999000.

Note that for both computers, the value of the first term, 107 and 106 , is much larger than the value of
the second term, 104 and 103 so we say that the first term dominates. When talking about the complexity
of an algorithm, we ignore all but the first term, n2 , and make the statement
“t(n) grows like n2 as n increases”
What this means is that if we apply the bubblesort algorithm to sort two lists one twice as long as the
other, then we would expect the larger list to take 22 = 4 times as long as the smaller list. Also, note that
this statement is independent of the speed of different computers.
This concept represented mathematically using big-O notation where if the value of the function t(n)
grows like n2 as n increases then we say

t(n) = O(n2 ).

Some examples of different complexities are:


• O(1) constant time: the time taken to perform an algorithm does not change as the size of the
input changes
• O(n) linear time: the time taken to perform and algorithm is directly proportional to the size of the
input n. Consider an algorithm that calculates the numbers of 0s in a list of numbers by checking
each one. This would have complexity O(n).
• O(n2 ) quadratic time: the time taken to perform an algorithm is proportional to the square of the
size of the input n. We have seen the the bubble sort has quadratic time complexity;.
• O(log n) logarithmic time: the time taken for each step of an algorithm decreases. For example,
imagine you are looking up a word in a dictionary (not on a computer). Checking each page for the
word you are looking for starting at the beginning would be O(n) is if we increased the number of
pages in our dictionary the complexity would increase proportionally. However, if we used the more
sensible method of opening the dictionary halfway and then splitting the half which will contain our
word halfway again we will soon find our word since the number of pages in our splits will halve each
time.

Dr Jon Shiach 49 Computing Mathematics Lecture Notes


Chapter 3. Algorithms Back to Table of Contents

• O(n!) factorial time: the time taken for each step of an algorithm is proportional to the factorial
of the size of the input n. Take the classic travelling salesman problem where a sales man wants to
find a path that visits a number of cities with the shortest travelling distance (we look at shortest
path problems in chapter 4). If we were to exhaustively check each possible path then for n cities
we have n! permutations, i.e., for 10 cities we have 10! = 3628800 permutations to consider but for
20 cities we have 20! = 2.4 × 1018 permutations (to put this number into perspective if it took 1
millisecond to check each permutation then it would take 77 million years to check them all).
The sorting algorithms studied in this chapter have the following complexities:
• Bubblesort
– worst case O(n2 )
– average case O(n2 )
– best case O(n)
• Quicksort
– worst case O(n2 )
– average case O(n log n)
– best case O(n log n)
• Merge sort
– worst case O(n log n)
– average case O(n log n)
– best case O(n log n)

Dr Jon Shiach 50 Computing Mathematics Lecture Notes


Back to Table of Contents Chapter 3. Algorithms

3.5 Tutorial Exercises


Exercise 3.1. Use Euclid’s algorithm to find the greatest common divisor between:
(a) 48 and 102; (b) 585 and 1027.
Exercise 3.2. Showing each step of the algorithm, use bubble sort to sort the numbers [5, 1, 3, 6, 2, 4] into
ascending order.
Exercise 3.3. Repeat exercise exercise 3.2 using quicksort.
Exercise 3.4. Repeat exercise exercise 3.2 using merge sort.
Exercise 3.5. Write a Python or MATLAB function that uses the bubble sort algorithm to sort a list of
numbers. Use your function to sort the numbers in exercise 3.2.
Exercise 3.6.
(a) Write down the pseudocode for a function minrecursion(X, n) that uses recursion to find the
smallest number in the list X which has n elements.

(b) Write down the steps used by your algorithm to find the minimum value for the list [5, 6, 3, 7, 4].

(c) Code your function in Python or MATLAB and text your code on the list in part (b).
Exercise 3.7. The pseudocode for Gaussian elimination, which solves a system linear equations of the form
Ax = b, is given in algorithm 7.

Algorithm 7 Gaussian elimination


function GaussianElimination(A, b)
for j = 1 . . . n − 1 do
for i = j + 1 . . . n do . row operations
r ← ai,j /aj,j
for k = j . . . n do
ai,k ← ai,k − raj,k
end for
bi ← bi − rbj
end for
end for
x←b
for i = n . . . 1 do . back substitution
for j = i + 1 . . . n do
xi ← xi − ai,j xj+1
end for
xi ← xi /ai,i
end for
return x
end function

When using Gaussian elimination to solve a linear system of n equations in n unknowns:


(a) how many division operations are required?
(b) how many subtraction operations are required?
(c) how many multiplication operations are required?
(d) what is the complexity of Gaussian elimination?
The solutions to these exercises is given in appendix A.3.

Dr Jon Shiach 51 Computing Mathematics Lecture Notes


Chapter 4

Graph Theory

Learning outcomes
On successful completion of this chapters students will be able to:

• identify graphs, trees, walks trails and paths;

• represent a graph using an adjacency matrix;

• perform depth-first and breadth-first search to obtain node ordering and a spanning tree;

• solve shortest path problems using Dijkstra’s, Bellman-Ford and A* algorithms.

4.1 Graphs
In mathematics a graph is an ordered pair G = (V, E) where

• V = {v1 , v2 , . . . , vn } are a set of nodes (also called vertices ;

• E = {(u, v) : edge joining node u to node v} is a set of edges.

A graph is represented diagrammatically using circles to represent the nodes and lines to represent the
edges (figure 4.1). The configuration of nodes does not matter as long as the lines that represent the edges
connect the correct nodes.

C D

A B

Figure 4.1: An example of a graph.

Definition 4.1: Adjacent nodes

Two nodes are said to be adjacent (or incident) if they are connected by an edge. For example,
nodes A and D are adjacent to node A in figure 4.1.

53
Chapter 4. Graph Theory Back to Table of Contents

Definition 4.2: Degree

The degree of a node vi , denoted by deg(vi ) is the number of nodes that are adjacent to node vi .
For example, the node D in figure 4.1 has degree deg(D) = 2. In the case where an edge is a loop
that connects a node with itself we add 2 to the degree of the node. The node A in figure 4.1 has
degree deg(A) = 5.

Definition 4.3: Leaf nodes


A node with a degree of 1 is called a leaf node.

4.1.1 Walks, trails and paths


We use the terms walk, trails and paths to describe the traversal of a graph:
• A walk is a sequence of edges that joins nodes of a graph. Let G = (V, E) be a graph then a walk
is denoted using the ordered set W = (v1 , v2 , . . . , vn ). The walk is closed if v1 = vn and is open
otherwise.
• A trail is a walk where no two edges are repeated.
• A path is a trail where no two nodes are repeated.
• A cycle is a trail where the start and end node are the same.
For example, in the graph figure 4.1
• (B, A, C, D, A, B) is a walk but not a cycle since the edge joining nodes A and B is traversed twice;
• (A, D, C, A, B) is a trail but not a walk since node A is visited twice;
• (C, A, A, D, C) is a cycle.

Definition 4.4: Parent node


A parent node is a node that precedes the current node in a walk.

The origins of graph theory can be traced to swiss mathematician Leonard Euler (1707 – 1783) where in
1736 he used it to solve the problem of whether it is possible to walk over the seven bridges of Königsberg
crossing each one only once (now known as an ‘Eulerian path’). To solve this problem Euler created the
first mathematical graph where the map of Königsberg (figure 4.2(b)) is represented as a graph (figure 4.2).
The seven bridges are the edges of the graph which join four nodes which are the land masses that are
separated by the river Pregel where node A is the island in the centre, node B is the north of the city,
node C is the south of the city and node D is the west of the city.

A D

(a) Map of Königsberg (b) Graph for the bridge of Königsberg problem

Figure 4.2: The bridges of Königsberg problem.

Dr Jon Shiach 54 Computing Mathematics Lecture Notes


Back to Table of Contents Chapter 4. Graph Theory

In his solution, Euler noticed that in order to traverse an edge just once, zero or two nodes of the graph
must have an odd degree. Since all four nodes in figure 4.2 have an odd degree then it is not possible to
create a path that crosses each of the seven bridges just once.

4.1.2 The adjacency matrix


A convenient way to mathematically describe a graph is using a adjacency matrix. The adjacency matrix
is a square matrix A where the element aij denotes the number of edges connecting the nodes Vi to Vj .
Where an edge exits a node, loops round and re-enters the same node the convention is to assign a value
of 2 for that edge. For example the adjacency matrix for the graph shown in figure 4.2 is

A B C D
A 0 2 2 1
 

A = B 2 0 0 1.

C 2 0 0 1
D 1 1 1 0

Theorem 4.1: Properties of the adjacency matrix

The adjacency matrix A has the following properties:


• The adjacency matrix for a non-directed graph is symmetric;
• If there are no loops in the graph then the main diagonal elements are all zero;
• The number of non-zero elements on the main diagonal is the number of loops in the graph;
• The value of [An ]ij is equal to the number of walks of length n from vi to vj .
• The degree of node vi is equal to the sum of row i or column i.

Definition 4.5: Connected graphs

A graph G is said to be connected if a path exists between any two nodes in G. If a graph is not
connected then it is disconnected.

Example 4.1

Given the graph below:


4

1 2 3

find:
1. the adjacency matrix A;
2. the matrix giving the number of 3 step walks.a
a
This problem is from the film Good Will Hunting where M.I.T. professor Gerald Lambeau poses this problem to
his class of graduate students claiming that it is “an advanced Fourier system” (there is no such thing as a ‘Fourier
system’) and that whomever solves it will have their name mentioned in M.I.T. Tech.

Dr Jon Shiach 55 Computing Mathematics Lecture Notes


Chapter 4. Graph Theory Back to Table of Contents

Solution:
1.
 
0 1 0 1
 
1 0 2 1
A=
 

0 2 0 0
 
1 1 0 0

2. The number of walks of length 3 is given by A3


    
0 1 0 1 0 1 0 1 2 1 2 1
    
1 0 2 1
 1 0 2 1
  1 6 0 1
2
A = =
  
 
0 2 0 0
 0 2 0 2 0 4
0  2
 
 
1 1 0 0 1 1 0 0 1 1 2 2
    
0 1 0 1 2 1 2 1 2 7 2 3
    
1 0 2 1
 1 6 0 1
  7 2 12 7
A3 =  =
  
 
0 2 0 0
 2 0 4 2 12 0
2  2
 
 
1 1 0 0 1 1 2 2 3 7 2 2

I.e., there are 12 walks of length 3 between nodes 2 and 3.

4.1.3 Weighted graphs


A weighted graph is a graph G = (V, E) where V = {v1 , v2 , . . . , vn } is a set of nodes and E = {w(u, v) :
a weighted edge between nodes u and v}. w(u, v) is a numerical weight assigned to an edge joining nodes
u and v (figure 4.3).

4
B D
3 6

A 1 F
6
4 3
C E
5

Figure 4.3: A weighted graph.

The elements in the adjacency matrix for a weighted graph are the weights assigned to the edges, i.e.,
[A]ij = w(i, j). For example, the adjacency matrix for the weighted graph in figure 4.3 is

Dr Jon Shiach 56 Computing Mathematics Lecture Notes


Back to Table of Contents Chapter 4. Graph Theory

A B C D E F
A 0 3 4 0 0 0
B 3 0 0 4 6 0
 
A= C 
4 0 0 0 5 0
D 
0 4 0 0 1 6
E 0 6 5 1 0 3
F 0 0 0 6 5 0

4.1.4 Directed graphs


A directed graph (also known as a digraph) is a graph where the edges have a direction. The edges of a
directed graphs are represented diagrammatically by arrows indicating the direction of the arrow (figure 4.4).

1
B D
5 3
4
A 2 -1 F

-2 1
C E
5

Figure 4.4: A directed graph.

The adjacency matrix for a directed graph may not be symmetric. For example, the edge joining nodes A
to B has weight w(A, B) = 5 but there is no edge joining B to A so w(B, A) = 0. The adjacency matrix
for the weighted graph shown in figure 4.4 is

A B C D E F
A 0 5 −2 0 0 0
B 0 0 0 1 0 0
 
A= C
0 2 0 4 5 0
 
D 0
 0 0 0 −1 3
E 0 0 0 0 0 1
F 0 0 0 0 0 0

4.1.5 Trees
A tree is a connected graph, G, that satisfies the following:
• Node G contains no cycles;
• adding an edge between any two nodes in G a cycle is created;
• removing any edge in G creates a disconnected graph;
• any two nodes can be connected by a single path;
• if G has n nodes then it has n − 1 edges.
For example, the graph in figure 4.5 is a tree.

Definition 4.6: Spanning tree

A spanning tree is a tree that contains all of the nodes in a graph.

Dr Jon Shiach 57 Computing Mathematics Lecture Notes


Chapter 4. Graph Theory Back to Table of Contents

A D G

B F

C E H

Figure 4.5: A tree.

Definition 4.7: Minimum spanning tree

A minimum spanning tree is a spanning tree for a weighted graph where the sum of the weights
of the edges are the minimum possible for the tree.

4.2 Depth-first and breadth-first search


Depth-first and search are algorithms for traversing a graph and producing spanning trees.

4.2.1 Depth-first search (DFS)


The concept behind depth-first search is that a path is traversed as far as possible until either a leaf node
is reached or all nodes adjacent to the current nodes have been visited. Then we return to the last node
that has an unvisited adjacent node and traverse as far as possible again. We continue to do this until
all nodes in the graph have been visited. The spanning tree is defined by the edges that are traversed to
visited each node.
Given an n-node connected graph G = (V, E) the depth-first search proceeds as follows:
1. Assume all nodes are classified as ‘unvisited’ and create an empty list called closedList which will
contain the visited nodes.
2. Create an empty list called openList will contain the nodes that are to be checked. Put the start
node into openList.
3. Create an n-element list parent = [∅, . . . ∅] which contains null entries. This will contain the parent
nodes for the nodes added in openList.
4. If openList 6= ∅ remove the last node u and check whether it is in closedList (i.e., has it already
been visited). If u is not in closedList add it to closedList and add u to the spanning tree.
5. Append all of the nodes adjacent to u that are not in closedList to openList and set their parent
node to u.
6. Repeat steps 3 and 4 until the openList is empty.
For example, consider the graph in figure 4.6 with node A as the start node. Initialise the closedList and
openList lists

closedList = ∅, openList = (A).

1. Remove node A from openList. Node A is not in closedList so we add it. Node B is adjacent to
node A and not in closedList so we add it to closedList and to the spanning tree.

closedList = {A}, openList = (B).

Dr Jon Shiach 58 Computing Mathematics Lecture Notes


Back to Table of Contents Chapter 4. Graph Theory

A B C

D E F

G H I

Figure 4.6: Graph used in the search algorithms.

2. Remove node B from openList. Node B is not in closedList so we add it to closedList and to the
spanning tree (node A is the parent node). Nodes A, C, D and E are adjacent to node B but node
A is in closedList so we add nodes C, D, E to openList

closedList = {A, B}, openList = (C, D, E).

A B

3. Remove node E from openList. Node E is not in closedList so we add it to closedList and the
spanning tree (node B is the parent node). Nodes B, C, D, F and H are adjacent to node E but
node B is in closedList so we add nodes C, D, F, H to openList

closedList = {A, B, E}, openList = (C, D, C, D, F, H).

A B E

4. Remove node H from openList. Node H is not in closedList so add it to closedList and to the
spanning tree (node E is the parent node). Nodes D, E, G and I are adjacent to node H but node
E is in closedList so add nodes D, G, I to openList

closedList = {A, B, E, H}, openList = (C, D, C, D, F, D, G, I).

A B E H

5. Remove node I from openList. I is not in closedList so add it to closedList and to the spanning
tree (node H is the parent node). Nodes F and H are adjacent to I but H is in closedList so add
F to openList

closedList = {A, B, E, H, I}, openList = (C, D, C, D, F, D, G, F ).

A B E H I

6. Remove F from openList. F is not in closedList so add it to closedList and to the spanning tree
(node I is the parent node). Nodes E and I are adjacent to F but both are in closedList so we
cannot add them

closedList = {A, B, E, H, I, F }, openList = (C, D, C, D, F, D, G).

A B E H I F

7. Remove G from openList. G is not in closedList so add it to closedList and to the spanning tree
(node H is the parent node). Node H is adjacent to G but is in closedList so we cannot add it to
openList

closedList = {A, B, E, H, I, F, G}, openList = (C, D, C, D, F, D).

Dr Jon Shiach 59 Computing Mathematics Lecture Notes


Chapter 4. Graph Theory Back to Table of Contents

I F

A B E H

8. Remove D from openList. D is not in closedList so add it to closedList and to the spanning tree
(node H is the parent node). Nodes B, E and H are adjacent to D but are in closedList so we
cannot add them

closedList = {A, B, E, H, I, F, G, D}, openList = (C, D, C, D, F ).

I F

A B E H G

9. Remove F from openList. F is in closedList so we do not add it to closedList or the spanning tree.

closedList = {A, B, E, H, I, F, G, D}, openList = (C, D, C, D).

10. Remove D from openList. D is in closedList so we do not add it to closedList or the spanning tree.

closedList = {A, B, E, H, I, F, G, D}, openList = (C, D, C).

11. Remove C from openList. C is not in closedList so we add it to closedList and to the spanning
tree (node E is the parent node). All nodes are now in closedList so the algorithm terminates.

I F

H G

A B E

C D

The ordering of the nodes by the depth-first search algorithm starting at node A is

closedList = {A, B, E, H, I, F, G, D, C}.

Example 4.2

Starting at node A, use depth-first search to produce a spanning tree for the graph below.
B
D

E
C

Dr Jon Shiach 60 Computing Mathematics Lecture Notes


Back to Table of Contents Chapter 4. Graph Theory

Solution:

closedList = ∅, openList = [A],


closedList = {A}, openList = [B, C, D],
closedList = {A, D}, openList = [B, C, C],
closedList = {A, D, C}, openList = [B, C],
closedList = {A, D, C, B}, openList = [E],
closedList = {A, D, C, B, E}, openList = ∅.

D C

B E

The pseudocode for the depth-first search algorithm is shown in algorithm 8.

Algorithm 8 Pseudocode for the depth-first search algorithm


function dfs(A, startnode)
n ← number of rows in A
closedList ← empty list
openList ← [startnode]
parent ← [−1, . . . , −1] . parent is an n-element array
while openList 6= ∅ do
u ← last element in openList
remove u from openList
if u 6∈ closedList then
append u to closedList
for v = 1, . . . , n do
if G(u, v) 6= 0 and G(u, v) ∈
/ closedList then
append node v to openList
parent(v) ← u
end if
end for
end if
end while
return closedList and parent
end function

4.2.2 Python and MATLAB code for depth-first search


Python and MATLAB code for depth-first search are shown in listings 4.1 and 4.2. Both codes define
a function called DFS which takes inputs of the adjacency matrix A and the index of the starting node
startnode and returns the node order in closedList and the parent nodes for each node in parent.

Listing 4.1: Python code for depth-first search.


def DFS (A , startnode ) :

# Initialise lists
closedList , openList , parent = [] , [ startnode ] , [ -1] * len ( A )

Dr Jon Shiach 61 Computing Mathematics Lecture Notes


Chapter 4. Graph Theory Back to Table of Contents

while openList :

# Get last node from the open list


u = openList . pop ()

# Check if current node has not already been visited


if u not in closedList :

# Add current node to closed list


closedList . append ( u )

# Look for unvisited nodes adjacent to the current node


for v in range ( len ( A ) ) :
if A [u , v ] != 0 and v not in closedList :
openList . append ( v )
parent [ v ] = u

return closedList , parent

Listing 4.2: MATLAB code for depth-first search.


function [ closedList , parent ] = DFS (A , startnode )

% Initialise lists
closedList = []; openList = [ startnode ]; parent = - ones (1 , size (A , 1) ) ;

while isempty ( openList ) == false

% Get last node from open list


u = openList ( end ) ;
openList ( end ) = [];

% Check if current node has not already been visited


if ismember (u , closedList ) == false

% Append current node node to closed list


closedList = [ closedList , u ];

% Look for unvisited nodes adjacent to the current node


for v = 1 : size (A , 1)
if A (u , v ) ~= 0 && ismember (v , closedList ) == false
openList = [ openList , v ];
end
end
end
end

end

4.2.3 Breadth-first search


Breath-first searchbreadth-first is another algorithm for traversing a graph and producing a spanning tree.
Starting at a chosen node in the graph, we visit all nodes adjacent to the current node that has not yet
been visited. For each new visited node we repeat this process until all nodes have been visited.
Given an n-node connected graph G = (V, E) and a start node, depth-first search proceeds as follows:
1. Assume all nodes are classified as ‘unvisited’ and create an empty list closedList which will contain
the visited nodes.
2. Create an empty list openList will contain a queue of nodes to be checked. Put the start node into
openList.

Dr Jon Shiach 62 Computing Mathematics Lecture Notes


Back to Table of Contents Chapter 4. Graph Theory

3. Create an n-element list parent = [∅, . . . ∅] which contains null entries. This will contain the parent
nodes for the nodes added to openList.
4. If openList 6= ∅ remove the first node u from openList and check whether it is in closedList. If u
is not in closedList add it to closedList and add u to the spanning tree.
5. Append all of the nodes v adjacent to u that are not in closedList to openList and, if this is the
first time a node has been added to openList, set their parent node to u.
6. Repeat steps 3 and 4 until the openList is empty.
Note that the breadth-first search algorithm is very similar to the depth-first search algorithm, the difference
being is that we remove the first node from the list. For example, consider applying breath-first search to
the graph in figure 4.6 with A as the start node.
1. Node A is the start node so we initialise the closedList and openList lists

closedList = ∅, openList = (A).

2. Remove node A from openList. Node A is not in closedList so add it to closedList and to the
spanning tree. Node B is adjacent to node A and not in closedList so it is added to openList

closedList = {A}, openList = (B).

3. Remove node B from openList. Node B is not in closedList so add it to closedList and to the
spanning tree (node A is the parent node). Nodes A, C, D and E are adjacent to node B but node
A is in closedList so add nodes C, D, E to openList

closedList = {A, B}, openList = (C, D, E).

A B

4. Remove node C from openList. Node C is not in closedList so add it to closedList and to the
spanning tree (node B is the parent node). Nodes B and E are adjacent to node C but node B is
in closedList so add node E to openList

closedList = {A, B, C}, openList = (D, E, E).

A B C

5. Remove node D from openList. Node D is not in closedList so add it to closedList and to the
spanning tree (node B is the parent node). Nodes B, E and H are adjacent to node D but node B
is in closedList so add nodes E, H to openList

closedList = {A, B, C, D}, openList = (E, E, E, H).

A B

6. Remove E from openList. Node E is not in closedList so add it to closedList and to the spanning
tree (node B is the parent node). Nodes B, C, D, F and H are adjacent to node E but nodes B,
D and C are in closedList so add nodes F, H to openList

closedList = {A, B, C, D, E}, openList = (E, E, H, F, H).

Dr Jon Shiach 63 Computing Mathematics Lecture Notes


Chapter 4. Graph Theory Back to Table of Contents

A B D

7. Remove node E from openList. Node E is in closedList so do not add to closedList.

closedList = {A, B, C, D, E}, openList = (E, H, F, H).

8. Remove node E from openList. Node E is in closedList so do not add to closedList.

closedList = {A, B, C, D, E}, openList = (H, F, H).

9. Remove node H from openList. Node H is not in closedList so add it to closedList and to the
spanning tree (node D is the parent node). Nodes D, E, G and I are adjacent to node H but nodes
D and E are in closedList so add nodes G, I to openList

closedList = {A, B, C, D, E, H}, openList = (F, H, G, I).

A B D H

10. Remove node F from openList. Node F is not in closedList so add it to closedList and to the
spanning tree. Nodes E and I are adjacent to node F but node E is in closedList so add node I to
openList

closedList = {A, B, C, D, E, H, F }, openList = (H, G, I, I).

A B D H

E F

11. Remove node H from openList. H is in closedList so do not add to closedList.

closedList = {A, B, C, D, E, H, F }, openList = (G, I, I).

12. Remove node G from openList. Node G is not in closedList so add it to closedList and to the
spanning tree (node H is the parent node). Node H is adjacent to node G but is already in closedList
so do not add to openList

closedList = {A, B, C, D, E, H, F, G}, openList = (I, I).

Dr Jon Shiach 64 Computing Mathematics Lecture Notes


Back to Table of Contents Chapter 4. Graph Theory

A B D H G

E F

13. Remove Node I from openList. Node I is not in closedList so add it to closedList and to the
spanning tree (node H is the parent node). Nodes F and H are adjacent to node I but are already
in closedList so do not add to openList. All nodes are now in closedList so the algorithm terminates.

closedList = {A, B, C, D, E, H, F, G, I}.

A B D H

E F

Algorithm 9 Pseudocode for the breadth-first search algorithm


function BFS(A, startnode)
n ← number of rows in A
closedList ← ∅
openList ← [startnode]
parent ← [−1, . . . , −1] . parent is an n-element array
while openList 6= ∅ do
u ← last node in openList
remove u from openList
if v 6∈ closedList then
append u to closedList
for v = 1 . . . n do
if A(u, v) 6= 0 and v 6∈ closedList then
append node v to openList
if parent(v) = −1 then
parent(v) ← u . update parent(v) the first time v is added to openList
end if
end if
end for
end if
end while
return closedList
end function

Example 4.3

Starting at node A, use breadth-first search to produce a spanning tree for the graph below.

Dr Jon Shiach 65 Computing Mathematics Lecture Notes


Chapter 4. Graph Theory Back to Table of Contents

B
D

E
C

Solution:

closedList = ∅, openList = [A],


closedList = {A}, openList = [B, C, D],
closedList = {A, B}, openList = [C, D, E],
closedList = {A, B, C}, openList = [D, E, D],
closedList = {A, B, C, D}, openList = [E, D],
closedList = {A, B, C, D, E}, openList = [D].

A B E

4.3 Shortest path problems


The shortest path problem is the problem of finding the shortest path between two nodes on a graph.
Let graph G = (V, E) be defined by a set of nodes, V = {v1 , v2 , . . . , vn } and a set of weighted edges,
E = {w(u, v) : weighted edge joining u to v}. The shortest path between the two nodes v0 and vm is
defined as the path P = (v0 , v1 , v2 , . . . , vm ) that minimises the sum of edges that form P , i.e.,
m−1
X
f (P ) = w(vi , vi+1 ).
i=1
When each edge has a weight of w(u, v) = 1 then the shortest path is the one with the fewest edges.
For example consider the weighted graph in figure 4.3. There are numerous possible paths from node A to
node F . The path P1 = (A, B, D, F ) gives f (P1 ) = 3 + 4 + 6 = 13 but the path P2 = (A, C, E, F ) gives
f (P2 ) = 4 + 5 + 3 = 12 so its easy to see that path P2 is the shorter path. The question is whether P2 is
the shortest possible path from A to F . Since this graph contains a small number of nodes it is possible
for us to see that the that P = (A, B, D, E, F ) that gives f (P ) = 3 + 4 + 1 + 3 = 11 is the shortest path.
The question is, if we were presented with a weighted graph that has thousands of nodes how would we
be able to determine which is the shortest path?

4.4 Dijkstra’s algorithm


Dijkstra’s algorithm was developed by Dutch computer scientist Edsger Dijkstra (1930 – 2002) (Dijkstra
1959). Given a graph G = (V, E) with n nodes and the start and end nodes startnode and endnode then

Dr Jon Shiach 66 Computing Mathematics Lecture Notes


Back to Table of Contents Chapter 4. Graph Theory

the steps of the algorithm are as follows:


1. Create empty openList and closedList lists and put the start node into openList.
2. Create an n-element array d = [∞, . . . , ∞] and set d(startnode) = 0. The d array is used to contain
the shortest distance for the path from the start node to each node, hence why the start node has a
distance of zero.
3. Create another n-element array parent = [∅, . . . , ∅] with null entries for each element. The parent
array is used to contain the parent node, the node on the shortest path that precedes each node, for
the nodes in openList.
4. Choose the node u from openList which has the smallest distance value. Move u from openList to
closedList.
5. If u = endnode exit the algorithm.
6. For each node v that is adjacent to u and not in closedList. If v is not in openList and the
distance through from the start through u is less than d(v) then update d(v) ← d(u) + w(u, v) and
parent(v) = u. If v is not in openList add it.
7. Repeat steps 4 to 6 until the end node is reached.
The distance values now define the minimum spanning tree (a tree which contains all of the nodes of
the graph where the sum of the weights is minimum) for G which includes the start and end nodes. The
shortest path is found by initialising P = (endnode) and backtracking to the start node and pre-pending
the adjacent node which gives the correct shortest distance through the node.
For example, consider the implementation of Dijkstra’s algorithm to the shortest path problem between
nodes A and F in figure 4.7(a). Initialising openList ← {A} and using the notation (d(node), parent(node))
to record the distance and parent node we have
iteration A B C D E F
0 (0, ∅) (∞, ∅) (∞, ∅) (∞, ∅) (∞, ∅) (∞, ∅)
Iteration 1 (figure 4.7(b)):
• The node in openList with the smallest distance is node A so we move this to closedList;
• Node B: d(A) + w(A, B) = 0 + 5 = 5 < ∞ so we update d(B) = 5 and parent(B) = A;
• Node C: d(A) + w(A, C) = 0 + 2 = 2 < ∞ so we update d(C) = 2 and parent(C) = A;
iteration A B C D E F
0 (0, ∅) (∞, ∅) (∞, ∅) (∞, ∅) (∞, ∅) (∞, ∅)
1 (0, ∅) (5, A) (2, A) (∞, ∅) (∞, ∅) (∞, ∅)

Here the underline (0, ∅) denotes that the node has been added to closedList.
Iteration 2 (figure 4.7(c)):
• The node in openList with the smallest distance is node C so we move this to closedList;
• Node B: d(C) + w(C, B) = 2 + 2 = 4 < 5 so we update d(B) = 4 and parent(B) = C;
• Node D: d(C) + w(C, D) = 2 + 4 = 6 < ∞ so we update d(D) = 6 and parent(D) = C;
• Node E: d(C) + w(C, E) = 2 + 5 = 7 < ∞ so we update d(E) = 7 and parent(E) = C.
iteration A B C D E F
0 (0, ∅) (∞, ∅) (∞, ∅) (∞, ∅) (∞, ∅) (∞, ∅)
1 (0, ∅) (5, A) (2, A) (∞, ∅) (∞, ∅) (∞, ∅)
2 (0, ∅) (4, C) (2, A) (6, C) (7, C) (∞, ∅)

Iteration 3 (figure 4.7(d)):

Dr Jon Shiach 67 Computing Mathematics Lecture Notes


Chapter 4. Graph Theory Back to Table of Contents

• The node in openList with the smallest distance is node B so we move this to closedList;
• Node D: d(B) + w(B, C) = 4 + 1 = 5 < 6 so we update d(D) = 5 and parent(D) = B .
iteration A B C D E F
0 (0, ∅) (∞, ∅) (∞, ∅) (∞, ∅) (∞, ∅) (∞, ∅)
1 (0, ∅) (5, A) (2, A) (∞, ∅) (∞, ∅) (∞, ∅)
2 (0, ∅) (4, C) (2, A) (6, C) (7, C) (∞, ∅)
3 (0, ∅) (4, C) (2, A) (5, B) (7, C) (∞, ∅)

Iteration 4 (figure 4.7(e))


• The node in openList with the smallest distance is node D so we move this to closedList;
• Node E: d(D) + w(D, E) = 5 + 1 = 6 < 7 so we update d(E) = 6 and parent(E) = D;
• Node F : d(D) + w(D, F ) = 5 + 3 = 8 < ∞ so we update d(F ) = 8 and parent(E) = D.
iteration A B C D E F
0 (0, ∅) (∞, ∅) (∞, ∅) (∞, ∅) (∞, ∅) (∞, ∅)
1 (0, ∅) (5, A) (2, A) (∞, ∅) (∞, ∅) (∞, ∅)
2 (0, ∅) (4, C) (2, A) (6, C) (7, C) (∞, ∅)
3 (0, ∅) (4, C) (2, A) (5, B) (7, C) (∞, ∅)
4 (0, ∅) (4, C) (2, A) (5, B) (6, D) (8, D)

Iteration 5 (figure 4.7(f)):


• The node in openList with the smallest distance is node E so we move this to closedList;
• Node F : d(E) + w(E, F ) = 6 + 1 = 7 < 8 so we update d(F ) = 7 and parent(F ) = E.
iteration A B C D E F
0 (0, ∅) (∞, ∅) (∞, ∅) (∞, ∅) (∞, ∅) (∞, ∅)
1 (0, ∅) (5, A) (2, A) (∞, ∅) (∞, ∅) (∞, ∅)
2 (0, ∅) (4, C) (2, A) (6, C) (7, C) (∞, ∅)
3 (0, ∅) (4, C) (2, A) (5, B) (7, C) (∞, ∅)
4 (0, ∅) (4, C) (2, A) (5, B) (6, D) (8, D)
5 (0, ∅) (4, C) (2, A) (5, B) (6, D) (7, E)
Iteration 6:
• The node in openList with the smallest distance is node F so we move this to closedList;
• Node F is the end node so the algorithm terminates.
The shortest path is found by backtracking through the minimal spanning tree starting at the end node
which is node F so P = (F ). The parent node for node F is node E which is prepended to the path
P = (E, F ). The parent node for node E is node D so P = (D, E, F ). Continuing to prepend the parent
nodes until the start node A is reached gives
P = (A, C, B, D, E, F ).

4 1 5
B D
5 3
4
0 A 2 1 F 7

2 1
C E
2 5 6

Figure 4.8: The shortest path between nodes A and F .

Dr Jon Shiach 68 Computing Mathematics Lecture Notes


Back to Table of Contents Chapter 4. Graph Theory

∞ 1 ∞ 5 1 ∞
B D B D
5 3 5 3
4 4
0 A 2 1 F ∞ 0 A 2 1 F ∞

2 1 2 1
C E C E
∞ 5 ∞ 2 5 ∞
(a) Initial graph (b) iteration 1
4 1 6 4 1 5
B D B D
5 3 5 3
4 4
0 A 2 1 F ∞ 0 A 2 1 F ∞

2 1 2 1
C E C E
2 5 7 2 5 7
(c) iteration 2 (d) iteration 3
4 1 5 4 1 5
B D B D
5 3 5 3
4 4
0 A 2 1 F 8 0 A 2 1 F 7

2 1 2 1
C E C E
2 5 6 2 5 6
(e) iteration 4 (f) iteration 5

Figure 4.7: Implementation of Dijkstra’s algorithm to find the shortest path from A to F .

The pseudocode for Dijkstra’s algorithms is shown in algorithm 10.

Dr Jon Shiach 69 Computing Mathematics Lecture Notes


Chapter 4. Graph Theory Back to Table of Contents

Algorithm 10 Pseudocode for Dijkstra’s algorithm


function dijkstra(A, startnode, endnode)
n ← number of rows in A
closedList ← empty list
openList ← [startnode]
d ← [∞, . . . , ∞] . d and parent are n-element arrays
parent ← [−1, . . . , −1]
d(startnode) ← 0
while openList is not empty do
u ← node in openList with the smallest d value
append u to closedList
remove u from openList
if u = endnode then
exit while loop
end if
for v = 1 . . . n do
if A(u, v) 6= 0 and v 6∈ closedList and d(v) > d(u) + A(u, v) then
d(v) ← d(u) + A(u, v)
parent(v) ← u
if v 6∈ openList then
append v to openList
end if
end if
end for
end while
path ← [endnode]
i ← endnode
while parent(i) 6= −1 do . backtrack through spanning tree
path ← [parent(i), path] . prepend parent(i) to path
i ← parent(i)
end while
return path
end function

Dr Jon Shiach 70 Computing Mathematics Lecture Notes


Back to Table of Contents Chapter 4. Graph Theory

Example 4.4

Use Dijkstra’s algorithm to find the shortest path between nodes A and F in the graph below.
5 2
A C E

1
3 3 4

B D F
2 3

Solution:
iteration A B C D E F
0 (0, ∅) (∞, ∅) (∞, ∅) (∞, ∅) (∞, ∅) (∞, ∅)
1 (0, ∅) (3, A), (5, A) (∞, ∅) (∞, ∅) (∞, ∅)
2 (0, ∅) (3, A) (4, B) (5, B) (∞, ∅) (∞, ∅)
3 (0, ∅) (3, A) (4, B) (5, B) (6, C) (∞, ∅)
4 (0, ∅) (3, A) (4, B) (5, B) (6, C) (3, D)
5 (0, ∅) (3, A) (4, B) (5, B) (6, C) (3, D)
6 (0, ∅) (3, A) (4, B) (5, B) (6, C) (3, D)
4
5 2
0 A C E 6
1
3 3 4

3 B D F 8
2 3
5

4.5 The Bellman-Ford algorithm


The Bellman-Ford algorithm is an algorithm for finding the shortest path on a weighted graph. Where
the Bellman-Ford algorithm differs from Dijkstra’s algorithm is that the Bellman-Ford algorithm can handle
negative weights. Named after American mathematicians Richard Bellman (1920 – 1984) and Lester
Randolph Ford Jr. (1927 - 2017) who discovered it separately in 1958 and 1956 respectively (Bellman
1958; Ford 1956)
Given an n-node graph G and the start and end nodes, startnode and endnode, the steps of the algorithm
are as follows:
1. Create an n-element array d ← [∞, . . . , ∞] and set d(startnode) ← 0. The d array is used to
contain the shortest distance for the path from the start node to each node.
2. Create another n-element array parent ← [∅, . . . , ∅] with null entries in each element. The parent
array is used to contain the parent nodes for each node.
3. For each edge w(u, v) in the graph, if the distance to v through u is less than the current distance
d(v) then we update d(v) = d(u) + w(u, v) and parent(v) = u. This is called relaxing the distances.
4. Perform step 3 for a total of n − 1 times.
After n − 1 iterations of the relaxation process all distances will be the minimum from the start node and
define the minimum spanning tree. The shortest path is found using backtracking in the same way as for
Dijkstra’s algorithm.
For example, consider the implementation of the Bellman-Ford algorithm to the shortest path problem

Dr Jon Shiach 71 Computing Mathematics Lecture Notes


Chapter 4. Graph Theory Back to Table of Contents

1
B D
5 3
4
A 2 -1 F

-2 1
C E
5

Figure 4.9: A directed graph.

between nodes A and F in figure 4.9. We initialise the distances to ∞ except for the start node A
(figure 4.10(a))
step A B C D E F
0 (0,∅) (∞,∅) (∞,∅) (∞,∅) (∞,∅) (∞,∅)
Iteration 1:
• Node A is the start node so we don’t update d(A);
• Node B: d(A) + w(A, B) = 0 + 5 = 5 < ∞ and d(C) + w(C, B) = ∞ + 2 = ∞ so we update
d(B) = 5 and parent(B) = A;
• Node C: d(A) + w(A, C) = 0 − 2 = −2 < ∞ so we update d(C) = −2 and parent(C) = A;
• Node D: d(B) + w(B, D) = ∞ + 1 = ∞ and d(C) + w(C, D) = ∞ + 4 = ∞ so we do not update
d(D) or parent(E);
• Node E: d(C) + w(C, E) = ∞ + 5 = ∞ and d(D) + w(D, E) = ∞ − 1 = ∞ so we do not update
d(E) or parent(E);
• Node F : d(D) + w(D, F ) = ∞ + 3 = ∞ and d(E) + w(E, F ) = ∞ + 1 = ∞ so we do not update
d(F ) or parent(F ).
iteration A B C D E F
0 (0,∅) (∞,∅) (∞,∅) (∞,∅) (∞,∅) (∞,∅)
1 (0,∅) (5,A) (-2,A) (∞,∅) (∞,∅) (∞,∅)
After the first step the distances for all paths at most 1 edge from the start node are found. This means
we don’t need to check nodes A or C again.
Iteration 2:
• Node B: d(A) + w(A, B) = 0 + 5 = 5 6< 5 and d(C) + w(C, B) = −2 + 2 = 0 < 5 so we update
d(B) = 0 and parent(B) = C;
• Node D: d(B) + w(B, D) = 5 + 1 = 6 < ∞ and d(C) + w(C, D) = −2 + 4 = 2 < ∞ so we update
d(D) = 2 and parent(D) = C;
• Node E: d(C) + w(C, E) = −2 + 5 = 3 < ∞ and d(D) + w(D, E) = ∞ − 1 = ∞ so we update
d(E) = 3 and parent(E) = C;
• Node F : d(D) + w(D, F ) = ∞ + 3 = ∞ and d(E) + w(E, F ) = ∞ + 1 = ∞ so we do not update
d(F ) or parent(F ).
iteration A B C D E F
0 (0,∅) (∞,∅) (∞,∅) (∞,∅) (∞,∅) (∞,∅)
1 (0,∅) (5,A) (-2,A) (∞,∅) (∞,∅) (∞,∅)
2 (0,∅) (0,C) (-2,A) (2,C) (3,C) (∞,∅)
After the second iteration the distances for all paths at most 2 edges from the start node are found so we
no longer need to check node B again.

Dr Jon Shiach 72 Computing Mathematics Lecture Notes


Back to Table of Contents Chapter 4. Graph Theory

Iteration 3:
• Node D: d(B) + w(B, D) = 0 + 1 = 1 < 2 and d(C) + w(C, D) = −2 + 4 = 2 6< 2 therefore we
update d(D) = 1 and parent(D) = B;
• Node E: d(C) + w(C, E) = −2 + 5 = 3 6< 3 and d(D) = 2 − 1 = 1 < 3 therefore we update
d(E) = 1 and parent(E) = C;
• Node F : d(D) + w(D, F ) = 2 + 3 = 5 < ∞ and d(E) + w(E, F ) = 3 + 1 = 4 < ∞ therefore we
update d(F ) = 4 and parent(F ) = E.
iteration A B C D E F
0 (0,∅) (∞,∅) (∞,∅) (∞,∅) (∞,∅) (∞,∅)
1 (0,∅) (5,A) (-2,A) (∞,∅) (∞,∅) (∞,∅)
2 (0,∅) (0,C) (-2,A) (2,C) (3,C) (∞,∅)
3 (0,∅) (0,C) (-2,A) (1,B) (1,C) (4, E)
Now we know the distances to all paths at most 3 edges from node A so we no longer need to check node
D again.
Iteration 4:
• Node E: d(C) + w(C, E) = −2 + 5 = 3 > 1 and d(D) = 1 − 1 = 0 < 1 therefore update d(E) = 0
and parent(E) = D;
• Node F : d(D) + w(D, F ) = 1 + 3 = 4 6< 4 and d(E) + w(E, F ) = 1 + 1 = 2 < 4 therefore we
update d(F ) = 2 and the parent node is unchanged.
iteration A B C D E F
0 (0,∅) (∞,∅) (∞,∅) (∞,∅) (∞,∅) (∞,∅)
1 (0,∅) (5,A) (-2,A) (∞,∅) (∞,∅) (∞,∅)
2 (0,∅) (0,C) (-2,A) (2,C) (3,C) (∞,∅)
3 (0,∅) (0,C) (-2,A) (1,B) (1,C) (4, E)
4 (0,∅) (0,C) (-2,A) (1,B) (0,D) (2, E)
Now we know the distances to all paths at most 4 edges from node A so we no longer need to check node
E.
Iteration 5:
• Node F : d(D) + w(DF ) = 1 + 3 = 4 and d(E) + w(EF ) = 0 + 1 = 1 therefore we update d(F ) = 1
and the parent node is unchanged.
step A B C D E F
0 (0,∅) (∞,∅) (∞,∅) (∞,∅) (∞,∅) (∞,∅)
1 (0,∅) (5,A) (-2,A) (∞,∅) (∞,∅) (∞,∅)
2 (0,∅) (0,C) (-2,A) (2,C) (3,C) (∞,∅)
3 (0,∅) (0,C) (-2,A) (1,B) (1,C) (4, E)
4 (0,∅) (0,C) (-2,A) (1,B) (0,D) (2, E)
5 (0,∅) (0,C) (-2,A) (1,B) (0,D) (1, E)
Since we have done n − 1 steps of the algorithm none of the distances can be updated so we stop here.
The shortest path is found by back tracking from the end node and prepending the parent nodes in the
same way as used in Dijkstra’s algorithm which gives P = (A, C, B, D, E, F ) (figure 4.11).

Dr Jon Shiach 73 Computing Mathematics Lecture Notes


Chapter 4. Graph Theory Back to Table of Contents

∞ 1 ∞ ∞ ∞
B D 1
B D
5 3 5 3
4 4
0 A 2 -1 F ∞ 0 A 2 -1 F ∞

-2 1 -2 1
C E C E
∞ 5 ∞ ∞ 5 ∞
(a) initial graph (b) iteration 1
5 1 ∞ 0 1 1
B D B D
5 3 5 3
4 4
0 A 2 -1 F ∞ 0 A 2 -1 F ∞

-2 1 -2 1
C E C E
−2 5 ∞ −2 5 3

(c) iteration 2 (d) iteration 3


0 1 1 0 1 1
B D B D
5 3 5 3
4 4
0 A 2 -1 F 4 0 A 2 -1 F 2

-2 1 -2 1
C E C E
−2 5 0 −2 5 0
(e) iteration 4 (f) iteration 5

Figure 4.10: Implementation of the Bellman-Ford algorithm to find the shortest path from A to
F on a directed graph with negative weights.

0 1 1
B D
5 3
4
0 A 2 -1 F 1

-2 1
C E
−2 5 0

Figure 4.11: The shortest path from node A to node F found using the Bellman-Ford algorithm.

Dr Jon Shiach 74 Computing Mathematics Lecture Notes


Back to Table of Contents Chapter 4. Graph Theory

4.5.1 Negative cycles


A negative cycle is a cycle where the weights sum to a negative number. A directed graph which contains a
negative cycle does not have a shortest path because the negative cycle will cause a shortest path algorithm
to enter a cycle. The directed graph shown in figure 4.12 contains the negative cycle (B, C, D).

2
A B

−4
2

C D
1

Figure 4.12: A directed graph with a negative cycle.

To find out whether a directed graph has a negative cycle we simply attempt to relax the distance values
for the node once again. If one of the distances can be reduced further then we have a negative cycle.
This is because after n − 1 iterations all distances should be minimised, so if we can relax a distance value
then we must have a negative cycle.

The pseudocode for the Bellman-Ford algorithm is shown in algorithm 11.

Algorithm 11 Pseudocode for the Bellman-Ford algorithm


function bellmanford(A, startnode, endnode)
n ← number of rows in A
d ← [∞, . . . , ∞]
parent ← [−1, . . . , −1] . d and parent are n element arrays
d(startnode) ← 0
for k = 1, . . . , n − 1 do . iterate n − 1 times
for u = 1 . . . n do
for v = 1 . . . n do
if A(u, v) 6= 0 and d(v) > d(u) + A(u, v) then
d(v) ← d(u) + A(u, v) . relax distances
parent(v) ← u . update parent node
end if
end for
end for
end for
for u = 1 . . . n do
for v = 1 . . . n do . check for negative cycles
if d(v) > d(u) + A(u, v) then
return error . negative cycle found
end if
end for
end for
path ← [endnode] . backtrack from endnode through the parent nodes
i ← endnode
while parent(i) 6= −1 do . stop when no parent exists
path ← [parent(i), path] . prepend parent(i) to path
i ← parent(i)
end while
return path
end function

Dr Jon Shiach 75 Computing Mathematics Lecture Notes


Chapter 4. Graph Theory Back to Table of Contents

4.6 The A* algorithm


The A* algorithm (Hart et al. 1968), pronounced as “A star”, is an algorithm for finding the shortest
path on a weighted graph. Unlike Dijkstra’s algorithm and the Bellman-Ford algorithm where all nodes in
a graph are considered before finding the shortest path, the A* algorithm attempts to find the shortest
path by only considering nodes which are close to the shortest path. To do this, the algorithm makes a
choice as to which adjacent node to move to using an estimate of the cost required to extend the path to
the end node and then chooses the adjacent node with the smallest cost.
The estimated cost for node n, f (n), is calculated using

f (n) = g(n) + h(n)

where g(n) is the distance from the start node1 and h(n) is a heuristic2 that estimates the cost of the
cheapest path from node n to the end node.
Given a weighted graph G and the start and end nodes, startnode and endnode, the steps of the A*
algorithm are:
1. Create two lists, openList and closedList, where openList will contain the vertices that have been
evaluated by the heuristic function and closedList contains those nodes that have been visited. Put
the start node startnode into openList.
2. Create n-element lists parent = [∅, . . . ∅], g = [∞, . . . , ∞] and f = [∞, . . . , ∞]. parent contains
the parent nodes, g contains the shortest distance from the start node and f contains the estimated
cost.
3. Pick node u from the openList list with the smallest value of f (u) and move this to the closedList
list.
4. If u = endnode exit the algorithm.
5. For each node v that is adjacent to u and not in closedList. If the distance from the start node through
u is less than g(v) then update parent(v) = u, g(v) ← g(u) + w(u, v) and f (v) ← g(v) + h(v).
If v is not in openList then add it.
6. Repeat steps 3 and 4 until the end node has been moved to closedList.
The shortest path is found using back tracking through the parent list in the same way as in Dijkstra’s and
the Bellman-Ford algorithm.
For example, consider the implementation of the A* algorithm to the shortest between nodes A and G in
the graph shown in figure 4.13. Here the heuristic values have been given for each node.
We start by putting the start node A into openList and initialising g(A) = 0 and f (A) = h(A) = 7.
Iteration 1 (figure 4.14(a)):
• The node in openList with the minimum f (n) value is node A so we move this to closedList;
• Node B is not in openList so we add it and set parent(B) = A, g(B) = g(A)+w(A, B) = 0+4 = 4
and f (B) = g(B) + h(B) = 4 + 5 = 9;
• Node C is not in openList so we add it and set parent(C) = A, g(C) = g(B)+w(A, C) = 0+6 = 6
and f (C) = g(C) + h(C) = 6 + 6 = 12;
• Node D is not in openList so we add it and set parent(D) = A, g(A) = g(A)+w(A, D) = 0+4 = 4
and f (D) = g(D) + h(D) = 4 + 4 = 8:
1
So far in this chapter I have used d(n) to denote the distance from the start node which is the game as g(n). Here I have
chosen to use g(n) as this is what is commonly used when presenting the A* algorithm.
2
A heuristic is a method for giving a solution to a problem that does not necessarily give the optimum solution but one
that is good enough for our needs.

Dr Jon Shiach 76 Computing Mathematics Lecture Notes


Back to Table of Contents Chapter 4. Graph Theory

h=5
B
4 5
h=3
3
h=7 A E
4 4 4
D G h=0
6
h=4 5
6 6 7

8
C F
h=6 h=4

Figure 4.13: A weighted graph with heuristic values for each node.

Iteration 2 (figure 4.14(a)):


• The node in openList with the minimum f (n) value is node D so we move this to closedList;
• Node A is in closedList so we do nothing;
• Node B is already in open. g(D) + w(D, B) = 4 + 3 = 7 > 4 so we do not update parent(B), g(B)
or f (B);
• Node C is already in openList. g(D) + w(D, C) = 4 + 6 = 10 > 6 so we do not update parent(C),
g(C) or f (C);
• Node E is not in openList so we add it and set parent(E) = D, g(E) = g(D)+w(D, E) = 4+4 = 8
and f (E) = g(E) + h(E) = 8 + 3 = 11;
• Node F is not in openList so we add it and set parent(F ) = D, g(F ) = g(D)+w(D, F ) = 4+6 = 10
and f (F ) = g(F ) + h(F ) = 10 + 4 = 14;
Iteration 3 (figure 4.14(a)):
• The node in openList with the minimum f (n) value is node B so we move this to closedList;
• Node A and D are in closed so we do nothing;
• Node E is already in openList. g(B) + w(B, E) = 4 + 5 = 9 > 8 so we do not update parent(E),
g(E) or f (E);
Iteration 4 (figure 4.14(a)):
• The node in openList with the minimum f (n) value is node E so we move this to closedList;
• Nodes B and D are already in closedList so we ignore these;
• Node F is already in openList. g(E) + w(E, F ) = 8 + 5 = 13 > 10 so we do not update parent(F ),
g(F ) or f (F );
• Node G is not in openList so we add it and set parent(G) = E, g(G) = g(E)+w(E, G) = 8+4 = 12
and f (G) = g(G) + h(G) = 12 + 0 = 12.
Iteration 5:
• The node in openList with the minimum f (n) value is node G so we move this to closedList;
• Node G is the end node so the algorithm terminates.
Back tracking from G using the parent nodes gives the shortest path P = (A, D, E, G).

Dr Jon Shiach 77 Computing Mathematics Lecture Notes


Chapter 4. Graph Theory Back to Table of Contents

g = 4 g = 4
h = 5 h = 5
f = 9 f = 9
B B g = 8
4 5 4 5 h = 3
h = 3
3 f = 11
h = 7 A E h = 7 A E
4 4 4 4 3 4 4
D G h = 0 D G h = 0
6 6
g = 4 5 g = 4 5
6 h = 4 6 7 h = 4 6 7
f = 8
f = 8 6
8 8
C F C F
g = 6 g = 6 g = 10
h = 4 h = 6 h = 4
h = 6
f = 12 f = 14
f = 12

(a) iteration 1 (b) iteration 2


g = 4 g = 4
h = 5 h = 5
f = 9 f = 9
B g = 8 B g = 8
4 5 h = 3 4 5 h = 3
f = 11 f = 11
h = 7 A E h = 7 A E
4 3 4 4 4 3 4 4
g = 12 g = 12
D G h = 0 D G h = 0
6 f = 12 6 f = 12
g = 4 5 g = 4 5
h = 4 6 7 h = 4 6 7
f = 8 f = 8
6 6
8 8
C F C F
g = 6 g = 10 g = 6 g = 10
h = 6 h = 4 h = 6 h = 4
f = 12 f = 14 f = 12 f = 14

(c) iteration 3 (d) iteration 4

Figure 4.14: Implementation of the A* algorithm to find the shortest path between nodes A and
G.

The pseudocode for the A* algorithm is shown in algorithm 12.

4.6.1 Heuristics
One of the problems with implementing the A* algorithm is how do we calculate the value of the heuristic
h(n)? The heuristic is the cost of extending the path to the end node but since the algorithm adds one
node at a time we do not know what nodes will be selecting in future steps so we do not know what this
cost will be.
Some of the possible solutions for calculating the heuristic are:
• Exact heuristic – h(n) is calculated for all possible paths from each node to the end node is
calculated. Whilst this is possible for graphs will a small number of nodes the computational cost
soon becomes prohibitive for graphs with relatively modest number of nodes.
• Manhattan distance – where nodes are positioned due to some location metric, e.g., the location
of a road junction on a map or a position in a virtual world, we calculates h(n) are the sum of the
distance in the x and y directions. If the start and end nodes have co-ordinates (xs , ys ) and (xe , ye )
respectively then the heuristic is calculated using

h(n) = |xe − xs | + |ye − ys |.

This method is named after the block like structure of roads in Manhattan in New York.
• Euclidean distance – the straight line distance between the location of two nodes. If the start and
end nodes have co-ordinates (xs , ys ) and (xe , ye ) respectively then the heuristic is calculated using
q
h(n) = (xe − xs )2 + (ye − ys )2 .

Dr Jon Shiach 78 Computing Mathematics Lecture Notes


Back to Table of Contents Chapter 4. Graph Theory

Algorithm 12 Pseudocode for the A* algorithm


function Astar(A, startnode, endnode)
n ← number of rows in A
openList ← ∅
closedList ← ∅
g ← [∞, . . . , ∞] . g, f and parent are n-element arrays
f ← [∞, . . . , ∞]
parent ← [−1, . . . , −1]
g(start) ← 0
f (start) ← 0
while openList 6= ∅ do
u ← node in openList with the smallest f value
append u to closedList and remove u from openList
if u = endnode then
exit while loop
end if
for v = 1 . . . n do
if A(u, v) 6= 0 and v 6∈ closedList and g(v) > g(u) + A(u, v) then
parent(v) ← u
calculate heuristic h(v)
g(v) ← g(u) + A(u, v)
f (v) ← g(v) + h(v)
if v 6∈ openList then
append v to openList
end if
end if
end for
end while
end function

Dr Jon Shiach 79 Computing Mathematics Lecture Notes


Chapter 4. Graph Theory Back to Table of Contents

4.7 Applications of shortest path problems


4.7.1 Map directions
The most common application of the A* algorithm is finding directions from one point on a map to another.
Consider a map of a city, we can represent the map as a graph where each road junction is a node and
the roads between junctions the edges between nodes. The weight associated with each edge could be the
distance between the junctions or the time taken to travel the distance depending on whether we require
the shortest route in space or time. Given a starting location and a destination, we can apply the A*
algorithm to the graph to give the best route.
Consider figure 4.15 which shows a map of the area of Manchester between the John Dalton Building and
Piccadilly train station. Overlaid onto the map is a graph with the node A located at the John Dalton
building and node P located at Piccadilly train station. The remaining nodes are located at road junctions
and the co-ordinates of each node relative to node A are shown next to each node.

(400, 590) L
P (460, 400)
(220, 510) K O
(460, 480) M (550, 420)
(80, 440) G N (590, 420)
(300, 400) J

(100, 340) F
(−180, 320) C I (320, 320)

(120, 240) E
H (380, 180)
(−80, 120) B
D (200, 100)

(0, 0) A

Figure 4.15: <caption>

Applying the A* algorithm to this graph using the Euclidean distance to node P as the heuristic results in
the graphs shown in figure 4.16.

Dr Jon Shiach 80 Computing Mathematics Lecture Notes


Back to Table of Contents Chapter 4. Graph Theory

g = 1205 g = 1188 g = 1205 g = 1188


h = 199 h = 92 h = 199 h = 92
f = 1404 f = 1280 f = 1404 f = 1280
g = 1082 g = 1082
h = 264 h = 264
f = 1346 215 L 199 g = 1280 f = 1346 215 L 199 g = 1280
108 P h = 0
108 P h = 0
208 K 125 O f = 1280
208 K 125 O f = 1280
92 92
213 M 213 M
G 215 40 G 215 40
g = 1080 N g = 1080 N
358 241 J h = 80 358 241 J h = 80
g = 867 82 f = 1160 g = 1104 g = 867 82 f = 1160 g = 1104
204 204
g = 794
F
h = 160 319 h = 132 g = 794
F
h = 160 319 h = 132
f = 1027 g = 785 f = 1236 f = 1027 g = 785 f = 1236
C h = 365
251 I h = 161
C h = 365
251 I h = 161
f = 1159 f = 1159
f = 946 f = 946
260 g = 534
260 g = 534
E h = 376 268 E h = 376 268
224 233 f = 910 g = 801 224 233 f = 910 g = 801
H h = 234 H h = 234
g = 144 f = 1035 g = 144 f = 1035
310 310
h = 608 B 577
h = 608 B 577
f = 752 D f = 752 D
g = 224 g = 224
144 224 h = 397 144 224 h = 397
f = 621 f = 621
A A

(a) iteration 1 (b) iteration 2


g = 1205 g = 1188 g = 1205 g = 1188
h = 199 h = 92 h = 199 h = 92
f = 1404 f = 1280 f = 1404 f = 1280
g = 1082 g = 1082
h = 264 h = 264
f = 1346 215 L 199 g = 1280 f = 1346 215 L 199 g = 1280
108 P h = 0
108 P h = 0
208 K 125 O f = 1280
208 K 125 O f = 1280
92 92
213 M 213 M
G 215 40 G 215 40
g = 1080 N g = 1080 N
358 241 J h = 80 358 241 J h = 80
g = 867 82 f = 1160 g = 1104 g = 867 82 f = 1160 g = 1104
204 204
g = 794
F
h = 160 319 h = 132 g = 794
F
h = 160 319 h = 132
f = 1027 g = 785 f = 1236 f = 1027 g = 785 f = 1236
C h = 365
251 I h = 161
C h = 365
251 I h = 161
f = 1159 f = 1159
f = 946 f = 946
260 g = 534
260 g = 534
E h = 376 268 E h = 376 268
224 233 f = 910 g = 801 224 233 f = 910 g = 801
H h = 234 H h = 234
g = 144 f = 1035 g = 144 f = 1035
310 310
h = 608 B 577
h = 608 B 577
f = 752 D f = 752 D
g = 224 g = 224
144 224 h = 397 144 224 h = 397
f = 621 f = 621
A A

(c) iteration 3 (d) iteration 4


g = 1205 g = 1188 g = 1205 g = 1188
h = 199 h = 92 h = 199 h = 92
f = 1404 f = 1280 f = 1404 f = 1280
g = 1082 g = 1082
h = 264 h = 264
f = 1346 215 L 199 g = 1280 f = 1346 215 L 199 g = 1280
108 P h = 0
108 P h = 0
208 K 125 O f = 1280
208 K 125 O f = 1280
92 92
213 M 213 M
G 215 40 G 215 40
g = 1080 N g = 1080 N
358 241 J h = 80 358 241 J h = 80
g = 867 82 f = 1160 g = 1104 g = 867 82 f = 1160 g = 1104
204 204
g = 794
F
h = 160 319 h = 132 g = 794
F
h = 160 319 h = 132
f = 1027 g = 785 f = 1236 f = 1027 g = 785 f = 1236
C h = 365
251 I h = 161
C h = 365
251 I h = 161
f = 1159 f = 1159
f = 946 f = 946
260 g = 534
260 g = 534
E h = 376 268 E h = 376 268
224 233 f = 910 g = 801 224 233 f = 910 g = 801
H h = 234 H h = 234
g = 144 f = 1035 g = 144 f = 1035
310 310
h = 608 B 577
h = 608 B 577
f = 752 D f = 752 D
g = 224 g = 224
144 224 h = 397 144 224 h = 397
f = 621 f = 621
A A

(e) iteration 5 (f) iteration 6


g = 1205 g = 1188
h = 199 h = 92
f = 1404 f = 1280
g = 1082
h = 264
215 L 199 P
f = 1346 g = 1280 O
108 P h = 0
M
208 K 125 O f = 1280
92
213 M
40 J
G 215 g = 1080 N
358 241 J h = 80
g = 867 82 f = 1160 g = 1104
204 I
g = 794
F
h = 160 319 h = 132
f = 1027 g = 785 f = 1236
C h = 365
251 I h = 161
f = 1159
f = 946 E
260 g = 534
E h = 376 268
224 233 f = 910 g = 801
H h = 234
g = 144 f = 1035 D
310
h = 608 B 577
f = 752 D
g = 224
144 h = 397 A
224 f = 621
A

(g) iteration 7 (h) shortest path

Figure 4.16: Implementing the A* algorithm to find the shortest path between nodes A to P .

Dr Jon Shiach 81 Computing Mathematics Lecture Notes


Chapter 4. Graph Theory Back to Table of Contents

4.7.2 Path finding


The A* algorithm is commonly used in computer games in path finding for Non-Player Characters (NPCs).
Consider a situation where a NPC wants to move to a position in a virtual world where there are obstacles
in the way (figure 4.17). By dividing the world into discrete areas we can form a graph where each area
is represented by a node in the graph. The edges join the nodes in the eight directions left, right, top,
bottom, top-left, top-right, bottom-left, bottom-right, with the exception of nodes adjacent to an obstacle
or boundary of the
√ world. The weights for the edges are either 1 for an edge parallel to the horizontal or
vertical axis and 2 ≈ 1.4 for diagonal edges.

Target

NPC

Figure 4.17: The path a non-play character will take to reach a target.

Consider the implementation of the A* algorithm for the path finding problem shown in figure 4.17 with
h(n) calculated using the Manhattan distance. The start node has co-ordinates (1, 3) and is moved to
closedList = {(1, 3)}. The start node has 5 adjacent nodes with co-ordinates (1, 4), (2, 4), (2, 3), (2, 2)
and (1, 2).
• (1, 4): g(1, 4) = 0 + 1 = 1, h(1, 4) = |8 − 1| + |4 − 4| = 7, f (1, 4) = 1 + 7 = 8;
• (2, 4): g(2, 4) = 0 + 1.4 = 1.4, h(2, 4) = |8 − 2| + |4 − 4| = 6, f (2, 4) = 1.4 + 6 = 7.4;
• (2, 3) : g(2, 3) = 0 + 1 = 1, h(2, 3) = |8 − 2| + |4 − 3| = 7, f (2, 3) = 1 + 7 = 8;
• (2, 2): g(2, 2) = 0 + 1.4 = 1.4, h(2, 2) = |8 − 2| + |4 − 2| = 8, f (2, 2) = 1.4 + 8 = 9.4;
• (1, 2): g(1, 2) = 0 + 1 = 1, h(1, 2) = |8 − 1| + |4 − 2| = 9, f (1, 2) = 1 + 9 = 10.
All of the g(n) values were updated for these nodes so we record their parent node as (1, 3) and add them
to openList. The node in openList with the smallest f (n) value is (2, 4) so we move this to closedList
(figure 4.18(a))

closedList = {(1, 3), (2, 4)}, openList = {(1, 4), (2, 3), (2, 2), (1, 2)}.

The node at (2, 4) has 4 adjacent nodes not in closedList with co-ordinates (1, 5), (2, 5), (2, 3) and (1, 4).
• (1, 5): g(1, 5) = 1.4 + 1.4 = 2.8, h(1, 5) = |8 − 1| + |4 − 5| = 8, f (1, 5) = 2.8 + 8 = 10.8;
• (2, 5): g(2, 5) = 1.4 + 1 = 2.4, h(2, 5) = |8 − 2| + |4 − 5| = 7, f (2, 5) = 2.4 + 7 = 9.4;
• (2, 3): g(2, 3) = 1.4 + 1 = 2.4 which is not less than the current value of g(2, 3) = 1 so f (2, 3) = 8
is unchanged;
• (1, 4): g(1, 4) = 1.4 + 1 = 2.4 which is not less than the current value of g(1, 4) = 1 so f (1, 4) = 8
is unchanged.
The nodes at (1, 5) and (2, 5) had their g(n) values updated so we update their parent node to the node
at (2, 4) and add them to openList. The node in openList with the smallest f (n) value are nodes (2, 3)
and (1, 4). We could select any of these to going into closedList, we will choose (1, 4) (choosing (2, 3)

Dr Jon Shiach 82 Computing Mathematics Lecture Notes


Back to Table of Contents Chapter 4. Graph Theory

will not affect the final path) so we move this to closedList (figure 4.18(b))

closedList = {(1, 3), (2, 4), (1, 4)}, openList = {(2, 3), (2, 2), (1, 2)}.

The node at (1, 4) has 3 adjacent nodes not in closedList with co-ordinates (1, 5), (2, 5), (2, 3).
• (1, 5): g(1, 5) = 1 + 1 = 2 which is less than the current value g(1, 5) = 7 so we update f (1, 5) =
2 + 7 = 9;
• (2, 5): g(2, 5) = 1 + 1.4 = 2.4, h(2, 5) = |8 − 2| + |4 − 5| = 7, f (2, 5) = 2.4 + 7 = 9.4;
• (2, 3): g(2, 3) = 1 + 1.4 = 2.4 which is not less than the current value of g(2, 3) = 1 so f (2, 3) = 8
is unchanged.
The nodes at (1, 5) and (2, 5) had their g(n) values updated so we update their parent node to the node
at (1, 4) and add them to openList. The node in openList with the smallest f (n) value is the node at
(2, 5) so we move this to closedList (figure 4.18(c))

closedList = {(1, 3), (2, 4), (1, 4), (2, 5)}, openList = {(2, 3), (2, 2), (1, 2), (1, 5), (2, 5)}.

g = 2.4 g = 2.4
5 5 h = 8 h = 7
f = 10.4 f = 9.4

g = 1 g = 1.4 g = 1 g = 1.4
4 h = 7
f = 8
h = 6
f = 7.4
4 h = 7
f = 8
h = 6
f = 7.4

g = 0 g = 1 g = 0 g = 1
3 h = 8
f = 8
h = 7
f = 8
3 h = 8
f = 8
h = 7
f = 8

g = 1 g = 1.4 g = 1 g = 1.4
2 h = 9 h = 8
f = 10 f = 9.4
2 h = 9 h = 8
f = 10 f = 9.4

1 1
1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9
(a) iteration 0 (b) iteration 1
g = 2 g = 2.4 g = 3.4 g = 2 g = 2.4 g = 3.4 g = 4.4 g = 5.4
5 h = 8 h = 7 h = 6
f = 10 f = 9.4 f = 9.4 5 h = 8 h = 7 h = 6 h = 5 h = 4
f = 10.8 f = 9.4 f = 9.4 f = 9.4 f = 9.4

g = 1 g = 1.4 g = 1 g = 1.4 g = 5.4 g = 5.4 g = 12.2 g = 11.8 g = 12.2


4 h = 7
f = 8
h = 6
f = 7.4 4 h = 7 h = 6 h = 4 h = 3 h = 1 h = 0 h = 1
f = 8 f = 7.4 f = 9.4 f = 8.4 f = 13.2 f = 11.8 f = 13.2

g = 0 g = 1 g = 0 g = 1 g = 6.8 g = 6.4 g = 10.4 g = 10.8 g = 11.8


3 h = 8
f = 8
h = 7
f = 8
3 h = 8 h = 7 h = 5 h = 4 h = 2 h = 1 h = 2
f = 8 f = 8 f = 11.8 f = 10.4 f = 12.4 f = 11.8 f = 13.8

g = 1 g = 1.4 g = 1 g = 1.4 g = 7.8 g = 7.4 g = 8.4 g = 9.4 g = 10.4 g = 12.2


2 h = 9 h = 8
f = 10 f = 9.4
2 h = 9 h = 8 h = 6 h = 5 h = 4 h = 3 h = 2 h = 3
f = 10 f = 9.4 f = 13.8 f = 12.4 f = 12.4 f = 12.4 f = 12.4 f = 15.2

g = 8.8 g = 8.4 g = 8.8 g = 9.8 g = 10.8


1 1 h = 7 h = 6 h = 5 h = 4 h = 3
f = 15.8 f = 14.4 f = 13.8 f = 13.8 f = 13.8

1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9
(c) iteration 2 (d) iteration 10

Figure 4.18: Using the A* algorithm for path finding.

Continuing to apply the algorithm until the end node is reached gives

closedList = {(1, 3), (2, 4), (1, 4), (2, 5), (3, 5), (4, 5), (5, 4), (5, 3), (5, 2), (6, 2), (7, 2), (8, 3), (8, 4)}

The path between the NPC and the target is found by back tracking through the parent nodes to give the
path shown in figure 4.19. Note that the node at (1, 4) was in closedList but not in the path.

Dr Jon Shiach 83 Computing Mathematics Lecture Notes


Chapter 4. Graph Theory Back to Table of Contents

1
1 2 3 4 5 6 7 8 9

Figure 4.19: The path between the NPC and the target is found by backtracking through the
parent nodes.

Dr Jon Shiach 84 Computing Mathematics Lecture Notes


Back to Table of Contents Chapter 4. Graph Theory

4.8 Tutorial exercises


Exercise 4.1. Draw the graph G = (V, E) where V and E are given by

V = {A, B, C, D, E, F },
E = {(A, C), (A, D), (A, F ), (B, C), (B, D), (C, E), (D, E), (E, F )}.

Exercise 4.2. For the graph in exercise 4.1:


(a) Find a walk of length 7 between nodes B and C;
(b) Find a path of length 5 between nodes E and F .
(c) Find a cycle of length 4 starting and ending at node C.
Exercise 4.3. Write down the degree of each node for the graph in exercise 4.1. Does this graph have an
Eulerian cycle, if so what is it?
Exercise 4.4. Write down the adjacency matrix for the graph in exercise 4.1. How many walks of length
4 are there between C and F ?
Exercise 4.5. Draw all non-isomorphic (not having the same form) trees which have 5 nodes. (Hint: there
are only three in total).
Exercise 4.6. A graph is defined by the following adjacency matrix

A B C D E F G H I
A 0 1 0 1 1 0 0 0 0
B 1

0 1 0 1 0 0 0 0
C 0
 1 0 0 1 0 0 0 0
D 1
 0 0 0 1 0 1 0 0
E 1 1 1 1 0 1 0 1 1.
 
 
F 0
 0 0 0 1 0 0 0 1
G 0
 0 0 1 0 0 0 0 0
H 0 0 0 0 1 0 0 0 0
I 0 0 0 0 1 1 0 0 0

(a) Draw the graph;


(b) Starting at node A and adding nodes to the queue in alphabetical order, write down the order of the
nodes visited using depth-first search;
(c) Draw the minimal spanning tree produced by the depth-first search.
Exercise 4.7. Repeat parts (b) and (c) from exercise 4.6 using breadth-first search.
Exercise 4.8. A weighted graph is defined by the following adjacency matrix

A B C D E F G
A 0 4 3 7 4 0 0
 

B 4
 0 0 2 5 0 0
C 3
 0 0 2 0 0 0
D 7 2 2 0 5 3 0 .
 
 
E 4
 5 0 5 0 0 6
F 0 0 0 3 0 0 5
G 0 0 0 0 6 5 0

Use Dijkstra’s algorithm to determine the shortest path between A and G.

Dr Jon Shiach 85 Computing Mathematics Lecture Notes


Chapter 4. Graph Theory Back to Table of Contents

Exercise 4.9. A directed graph is defined by the following adjacency matrix

A B C D E
A 
0 2 1 0 0
B 0
 0 −2 0 4
C 0
 2 0 3 0
D 0 −1 0 0 2
E 0 0 0 0 0

Use the Bellman-Ford algorithm to determine the shortest path between nodes A and E.
Exercise 4.10. Use the A* algorithm to determine the shortest path between the start cell and the target
cell.

4
target

2
start

1 2 3 4 5

Exercise 4.11. Below is a map of a part of England with the towns and cities on the main road network
marked using nodes of a graph and the distances, in kilometres, are given as weights on the graph. Using
the Manhattan distance to calculate the heuristic, use the A* algorithm to find the shortest path between:
(a) London to Manchester;
(b) Exeter to Rugby.

Manchester Sheffield
61

111
143
125
Birmingham 60 Rugby Huntingdon

103
116 87 80
81
Bicester
Bristol 70 30
Hermitage 80 London
114 95
110

Southampton

Exeter

The solutions are given in the appendices on page 97.

Dr Jon Shiach 86 Computing Mathematics Lecture Notes


Bibliography

Bellman, R. (1958). “On a routing problem”. In: Quarterly of Applied Mathematics 16.

Belton, D. (1998). Karnaugh Maps - Rules of Simplification. url: http://www.ee.surrey.ac.uk/


Projects/Labview/minimisation/karrules.html (visited on 02/03/2022).

Dijkstra, E.W. (1959). “A note on two problems in connexion with graphs”. In: Numerische Mathematik
1, pp. 269–271.

Ford, L.R. (1956). Network Flow Theory. Tech. rep. RAND Corporation.

Hart, P.E., Nilsson, N.J., and Raphael, B. (1968). “A Formal Basis for the Heuristic Determination of
Minimum Cost Paths”. In: IEEE Transactions on Systems Science and Cybernetics 2.4, pp. 100–107.

Hoare, C.A.R. (1961). “Algorithm 64: Quicksort”. In: Comm. ACM 7.4, p. 321.

Karnaugh, M. (1953). “The map method for synthsis of combinational logic circuits”. In: Transactions of
the American Institute of Electrical Engineers, Part I: Communication and Electronics 5.72, pp. 593–599.

Tocci, R.J., Widmer, N.S., and Moss, G.L. (2007). Digital systems.

Wikipedia contributors (2001). Algorithm — Wikipedia, The Free Encyclopedia. url: https : / / en .
wikipedia.org/wiki/Algorithm (visited on 02/07/2022).

87
Appendix A

Exercise solutions

A.1 Mathematics Fundamentals


These are the solutions to the exercises on mathematics fundamentals on page 9.
Solution 1.1.

(a) 11; (b) 26; (c) 106; (d) 137.

Solution 1.2.

(a) 1001; (b) 10111; (c) 1010000; (d) 10100011.

Solution 1.3.

(a) 16; (b) 22; (c) 2836; (d) 43981.

Solution 1.4.

(a) 18; (b) 80; (c) 1E5; (d) 59B;

Solution 1.5. 185110 = 21121203


Solution 1.6.
1 0 1 1 0 1
+ 1 0 0 1 1 1
carry 1 1 1 1 1
sum 1 0 1 0 1 0 0

Solution 1.7.
1 C E 4 F
1 0 A 5 4
carry 1 1
sum 2 D 8 A 3

Solution 1.8.

(a) (b)

p q ¬p ∨ (p ∧ q) p q ¬(p ∨ q) ∧ (p ∨ ¬q)
0 0 1 0 0 0
0 1 0 0 1 0
1 0 1 1 0 0
1 1 1 1 1 1

89
Appendix A. Exercise solutions Back to Table of Contents

(c)

p q q ¬(p ∧ ¬(q ∨ ¬r))


0 0 0 1
0 0 1 1
0 1 0 0
0 1 1 1
1 0 0 1
1 0 1 1
1 1 0 1
1 1 1 1

Solution 1.9.

(a) (b)

¬p ∨ (p ∧ q) ¬(p ∨ q) ∧ (p ∨ ¬q)
≡ (¬p ∨ p) ∧ (¬p ∨ q) (distributive law) ≡ ¬p ∧ ¬q ∧ (p ∨ ¬q) (De Morgan’s law)
≡ 1 ∧ (¬p ∨ q) (complement law) ≡ ¬p ∧ ¬q (absorption law)
≡ ¬p ∨ q (identity law)

(c)

¬(p ∧ ¬(q ∨ ¬r))


≡ ¬p ∨ ¬¬(q ∨ ¬r) (De Morgan’s law)
≡ ¬p ∨ q ∨ ¬r (double negation)

Dr Jon Shiach 90 Computing Mathematics Lecture Notes


Back to Table of Contents Appendix A. Exercise solutions

A.2 Logic Circuits


The are the solutions to the exercises on logic circuits on page 35.

Solution 2.1.

(a) (b) (c)

A B C Q A B C Q A B C D Q
0 0 0 1 0 0 0 0 0 0 1 1 0
0 1 1 0 0 1 0 1 0 1 1 0 1
1 0 1 0 1 0 0 0 1 0 1 1 0
1 1 1 0 1 1 1 1 1 1 0 0 1

Solution 2.2.

(a) (b)

A A
Q B Q

B
C

(c)

A
Q

Solution 2.3.

(a)

A · (A + B) ≡ A · A + A · B (distributivity law)
≡0+A·B (complement law)
≡A·B (definition of AND)

(b)

(A + A · B) · (C + B · C) ≡ A · C + A · B · C + A · B · C + A · B · C (distributivity law)
=A·C +A·B·C +A·B·C (idempotence law).

(c)

(A + B) · (A + B) ≡ A + B + A + B (de Morgan’s law)


≡A·B+A·B (de Morgan’s law)
≡A·B+A·B (double negation)
≡A⊕B (definition of XOR).

Dr Jon Shiach 91 Computing Mathematics Lecture Notes


Appendix A. Exercise solutions Back to Table of Contents

(d)

(A + B · C) · (A + B) ≡ A · A + A · B + A · B · C + B · B · C (distributivity law)
≡A+A·B+A·B·C +B·B·C (idempotence law)
≡A+A·B+A·B·C +0·C (complement law)
≡A+A·B+A·B·C (definition of AND).

Solution 2.4. Q ≡ B · C

B
Q

Solution 2.5. We need to show that the other logic gates can be constructed from NOR gates.

• NOT gate: A + A ≡ A;

• AND gate: A + A + B + B ≡ A + B ≡ A · B ≡ A · B;

• OR gate: A + B + A + B ≡ A + B = A + B;

• XOR gate:

A+A+B+A+B+B+A+A+B+A+B+B

≡A+A+B+A+B+B
≡A+A+B+A+B+B
≡A·A+B+A+B·B
≡ A · (A + B) + B · (A + B)
≡A·A+A·B+A·B+B·B
≡A·B+A·B
≡ A ⊕ B.

• NAND gate: A + A + B + B + A + A + B + B ≡ A + B + A + B ≡ A + B ≡ A + B ≡ A · B

Therefore the NOR gate is a universal gate.

Solution 2.6.

(a) SOP: f (A, B, C) ≡ A · B;


POS: f (A, B, C) ≡ (A + B) · (A + B) · (A + B).

(b) SOP: f (A, B, C) ≡ A · B + A · C;


POS: f (A, B, C) ≡ (A + B + C) · (A + B + C) · (A + B + C) · (A + B + C) + (A + B + C).

(c) SOP: f (A, B, C) ≡ A · B · C + A · B · C + A · B · C + A · B · C;


POS: POS: f (A, B, C) ≡ (A + B + C) · (A + B + C) · (A + B + C) · (A + B + C).

Solution 2.7.

Dr Jon Shiach 92 Computing Mathematics Lecture Notes


Back to Table of Contents Appendix A. Exercise solutions

(a) SOP and POS: f (A, B) ≡ A + B.

A A
B 0 1 B 0 1

0 1 0 0 1 0

1 1 1 1 1 1

(b) SOP: f (A, B, C) ≡ A · C + A · C + B · C;


POS: f (A, B, C) ≡ (A + B + C) · (A + B) · (A + C).

AB AB
Cin 00 01 11 10 Cin 00 01 11 10

0 1 0 1 1 0 1 0 1 1

1 0 1 0 0 1 0 1 0 0

(c) SOP: f (A, B, C) ≡ A · C + A · C;


POS: f (A, B, C) ≡ (A + C) · (A + C).

AB AB
Cin 00 01 11 10 Cin 00 01 11 10

0 1 0 0 1 0 1 0 0 1

1 0 1 1 0 1 0 1 1 0

Solution 2.8.

(a)

A
C f (A, B, C)

(b)

C f (A, B, C)

Dr Jon Shiach 93 Computing Mathematics Lecture Notes


Appendix A. Exercise solutions Back to Table of Contents

A.3 Algorithms
These are the solutions to the exercises on graph theory on page 51.
Solution 3.1.

1.

k = 0, a = 102, b = 48, =⇒ a ← 48, b ← mod(102, 48) = 6,


k = 1, a = 48, b = 6, =⇒ a ← 6, b ← mod(48, 6) = 0

Therefore gcd(48, 102) = 6


2.

k = 0, a = 1027, b = 585, =⇒ a ← 585, b ← mod(1027, 585) = 442,


k = 1, a = 585, b = 442, =⇒ a ← 442, b ← mod(585, 442) = 143,
k = 2, a = 442, b = 143, =⇒ a ← 143, b ← mod(442, 143) = 13,
k = 3, a = 143, b = 13, =⇒ a ← 13, b ← mod(143, 13) = 0.

Therefore gcd(585, 1027) = 13

Solution 3.2.
Pass 1: Pass 2:

[5, 1, 3, 6, 2, 4] swap 5 and 1 [1, 3, 5, 2, 4, 6] do not swap 1 and 3


[1, 5, 3, 6, 2, 4] swap 5 and 3 [1, 3, 5, 2, 4, 6] do not swap 3 and 5
[1, 3, 5, 6, 2, 4] do not swap 5 and 6 [1, 3, 5, 2, 4, 6] swap 5 and 2
[1, 3, 5, 6, 2, 4] swap 6 and 2 [1, 3, 2, 5, 4, 6] swap 5 and 4
[1, 3, 5, 2, 6, 4] swap 6 and 4 [1, 3, 2, 4, 5, 6]
[1, 3, 5, 2, 4, 6]
Pass 3: Pass 4:

[1, 3, 2, 4, 5, 6] do not swap 1 and 3 [1, 2, 3, 4, 5, 6] do not swap 1 and 2


[1, 3, 2, 4, 5, 6] swap 3 and 2 [1, 2, 3, 4, 5, 6] do not swap 2 and 3
[1, 2, 3, 4, 5, 6] do not swap 3 and 4
[1, 2, 3, 4, 5, 6]
No swaps needed in last pass so sorted list is [1, 2, 3, 4, 5, 6]
Solution 3.3.
Underlined numbers denote the pivot.
[5, 1, 3, 6, 2, 4] swap 5 and 2
[2, 1, 3, 6, 5, 4] swap 6 and 4
[2, 1, 3, 4, 5, 6] partition sub-lists [2, 1, 3] and [5, 6]
[ [2, 1, 3], 4, [5, 6] ] sub-list [2, 1, 3]: swap 2 and 1, sub-list [5, 6]: do nothing
[ [1, 2, 3], 4, [5, 6] ] partition sub-lists [1, 2] and [5]
[[ [1], [2], 3], 4, [ [5], 6]]
All sub-lists only have one element so sorted list is [1, 2, 3, 4, 5, 6].
Solution 3.4.
Underlined numbers denote the midpoint.
[5, 1, 3, 6, 2, 4] partition list
[ [5, 1, 3], [6, 2, 4] ] partition sub-lists [5, 1, 3] and [6, 2, 4]
[[ [5, 1], [3] ], [ [6, 2], [4] ]] partition sub-lists [5, 1] and [6, 2]
[[[ [5], [1] ], [3]], [[ [6], [2] ], [4]]] merge [5] with [1] and [6] with [2]
[[ [1, 5], [3]], [ [2, 6], [4]]] merge [1, 5] with [3] and [2, 6] with [4]
[ [1, 3, 5], [2, 4, 6] ] merge [1, 3, 5] with [2, 4, 6]
[1, 2, 3, 4, 5, 6]

Dr Jon Shiach 94 Computing Mathematics Lecture Notes


Back to Table of Contents Appendix A. Exercise solutions

Solution 3.5. Python:


def bubblesort ( X ) :

k , swap = 0 , True
while swap :
swap = False
for i in range ( len ( X ) - k - 1) :
if X [ i ] > X [ i +1]:
X [ i ] , X [ i +1] = X [ i +1] , X [ i ]
swap = True

k += 1

return X

# Define list
X = [5 , 1 , 3 , 6 , 2 , 4]

# Sort list
print ( f ’X = { X }\ nsorted X = { bubblesort ( X ) } ’)

MATLAB:
% Define list
X = [5 , 1 , 3 , 6 , 2 , 4]

% Sort list
bubblesort ( X )

function X = bubblesort ( X )

k = 0;
swap = true ;
while swap
swap = false ;
for i = 1 : length ( X ) - k - 1
if X ( i ) > X ( i + 1)
t = X(i);
X ( i ) = X ( i + 1) ;
X ( i + 1) = t ;
swap = true ;
end
end
k = k + 1;
end

end

Solution 3.6.

(a)
function minrecursion(X, n)
if n = 1 then
return X(0) . assuming zero indexing (X(0) is the first element)
else
Xmin ← minrecursion(X, n − 1)
if Xmin < X(n − 1) then
return Xmin
else
return X(n − 1)
end if
end if

Dr Jon Shiach 95 Computing Mathematics Lecture Notes


Appendix A. Exercise solutions Back to Table of Contents

end function
(b) Call the function minrecursion([5, 6, 4, 7, 3], 5)
• minrecursion([5, 6, 3, 7, 4], 5): n > 1 so call minrecursion([5, 6, 3, 7, 4], 4)
• minrecursion([5, 6, 3, 7, 4], 4): n > 1 so call minrecursion([5, 6, 3, 7, 4], 3)
• etc.
• minrecursion([5, 6, 3, 7, 4], 1): n = 1 so return Xmin = 5
• minrecursion([5, 6, 3, 7, 4], 2): Xmin = 5 < X(1) = 6 so return Xmin = 5
• minrecursion([5, 6, 3, 7, 4], 3): Xmin = 5 > X(2) = 4 so return Xmin = 4
• minrecursion([5, 6, 3, 7, 4], 4): Xmin = 4 < X(3) = 7 so return Xmin = 4
• minrecursion([5, 6, 3, 7, 4], 5): Xmin = 4 > X(4) = 3 so return Xmin = 3
(c) Python:
def minrecursion (X , n ) :

if n == 0:
return X [0]
else :
Xmin = minrecursion (X , n - 1)
if Xmin < X [n -1]:
return Xmin
else :
return X [n -1]

print ( f ’ Xmin = { minrecursion ([5 , 6 , 7 , 4 , 3] , 5) } ’)

MATLAB:
fprintf ( ’ Xmin = % i ’ , minrecursion ([5 , 6 , 4 , 7 , 3] , 5) )

function Xmin = minrecursion (X , n )

if n == 1
Xmin = X (1) ;
else
Xmin = minrecursion (X , n - 1) ;
if Xmin > X ( n )
Xmin = X ( n ) ;
end
end

end

Solution 3.7.

(a) 1 2
2n + 21 n;
(b) 1 3
3n + 31 n2 − 56 n;
(c) 1 3
3n + 21 n2 − 56 n;
(d) O(n3 ).

Dr Jon Shiach 96 Computing Mathematics Lecture Notes


Back to Table of Contents Appendix A. Exercise solutions

A.4 Graph theory


These are the solutions to the exercises on graph theory on page 85.

Solution 4.1.
B

A C

F D

Solution 4.2. There are multiple correct answers to this question.

(a) W = (B, D, A, F, E, D, B, C);

(b) P = (E, C, B, D, A, F );

(c) C = (C, E, D, B).

Solution 4.3. deg(A) = deg(E) = deg(D) = deg(C) = 3, deg(B) = deg(F ) = 2.

This graph does not have an Eulerian cycle since the number of nodes with an odd degree is not zero or two.

Solution 4.4.
   
0 0 1 1 0 1 22 16 0 0 22 0
0 0 1 1 0 0 16 12 0 0 16 0
   
1 1 0 0 1 0 0 0 22 22 0 16
A=
1
, A4 =  .
 1 0 0 1 0
0
 0 22 22 0 16
0 0 1 1 0 1 22 16 0 0 22 0
1 0 0 0 1 0 0 0 16 16 0 12

There are 16 walks of length 4 between C and F .

Solution 4.5.

Solution 4.6.

(a)

A B C

D E F

G H I

(b) closedList = (A, E, I, F, H, D, G, C, B)

(c)

Dr Jon Shiach 97 Computing Mathematics Lecture Notes


Appendix A. Exercise solutions Back to Table of Contents

A B C

D E F

G H I

Solution 4.7.

(b) closedList = (A, B, D, E, C, G, F, H, I)

(c)

A B C

D E F

G H I

Solution 4.8.

4 9
5
B E
4 5 6
2
7
0 A D G 13
5
3 2 3 5
C F
3 8

Shortest path: P = (A, C, D, F, G)

Solution 4.9.

2 4
B E 5
2 -1
2
0 A 2 -2 D
3
1 3
C
0

Shortest path: P = (A, B, C, D, E)

Solution 4.10. Shortest path: P = ((1, 2), (2, 3), (2, 4), (2, 5), (3, 5), (4, 5), (5, 4))

Dr Jon Shiach 98 Computing Mathematics Lecture Notes


Back to Table of Contents Appendix A. Exercise solutions

g = 3.82 g = 3.41 g = 4.41 g = 5.41 g = 6.41


5 h = 5 h = 4 h = 3 h = 2 h = 1
f = 8.82 f = 7.41 f = 7.41 f = 7.41 f = 7.41

g = 2.82 g = 2.41 g = 6.41 g = 6.82


4 h = 4 h = 3 h = 1 h = 0
f = 6.82 f = 5.41 f = 7.41 f = 6.82

g = 1 g = 1.41
3 h = 5 h = 4
f = 6 f = 5.41

g = 0 g = 1
2 h = 6 h = 5
f = 6 f = 6

g = 1 g = 1.41
1 h = 7 h = 6
f = 8 f = 7.41

1 2 3 4 5

Solution 4.11.

(a) Shortest path: P = (E, H, G, J); (b) Shortest path: P = (A, C, D, E, I).

g = 274 61 g = 228 61
h = 0 J K h = 70 J K
f = 274 f = 298

143 143
111 125 111 125
g = 103
g = 163 h = 205 g = 80 g = 230 g = 367 g = 344
h = 125 G H f = 308 I h = 265 h = 140 G H h = 60 I h = 0
f = 288 60 f = 345 f = 370 60 f = 427 f = 344

87 103 87 103
g = 81 g = 214 81 80
116 F 81 80 116
h = 120
F
h = 245
f = 326 f = 334
30 g = 114
30 g = 264
C D E h = 245 C D E h = 85
70 g = 80 80 f = 359 70 g = 184 80 f = 349
h = 270 h = 155
114 f = 350 114 f = 339
95 95
110 110
g = 110 g = 279
A h = 335 B A h = 230 B
f = 445 f = 509

Dr Jon Shiach 99 Computing Mathematics Lecture Notes


Index

A* algorithm, 76 decimal number, 1


Euclidean distance heuristic, 78 depth-first search, 58
exact heuristic, 78 MATLAB code, 62
heuristic, 76, 78 pseudocode, 61
Manhattan distance heuristic, 78 Python code, 61
pseudocode, 78 Dijkstra’s algorithm, 66
absorption law, 7 pseudocode, 69
adjacency matrix, 55 distributive law, 7
average case, 48 double negation, 7

base case, 46 factorial time, 50


base-10, 1 full adder, 31
base-16, 4
base-2, 1 graph, 53
Bellman-Ford algorithm, 71 connected graph, 55
pseudocode, 75 cycle, 54
best case, 48 directed graph, 57
big-O notation, O(n), 49 disconnected graph, 55
binary number, 1 edge, 53
Boolean algebra, 14 minimum spanning tree, 58, 67
absorption law, 15 spanning tree, 57
associative law, 15 trail, 54
commutative law, 15 tree, 57
complement law, 15 vertex, 53
De Morgan’s law, 15 walk, 54
distributive law, 15 weight, 56
double negation, 15 weighted graph, 56
idempotence law, 15 half adder, 30
identity law, 15 hexadecimal, 4
breadth-first search, 58
pseudocode, 65 idempotence law, 7
identity law, 7
canonical normal form inputs, 13
canonical conjunctive normal form, 21
canonical disjunction normal form, 20 Karnaugh maps, 24
product of sums (POS), 21 rules of Karnaugh maps, 25
sum of products (SOP), 20
carry, 3 linear time, 49
circuit diagram, 13 logarithmic time, 49
complexity, 48 logic, 5
constant time, 49 associative law, 7
commutative law, 7
De Morgan’s law, 7 compound proposition, 5

101
Index Back to Table of Contents

conjunction, 5 adjacent node, 53


connectives, 5 degree, 54
disjunction, 5 leaf node, 54
laws, 7 parent node, 54
negation, 6
logic circuits, 13 outputs, 13
logic gates, 11
path finding, 82
AND gate, 11
product, 11
exclusive OR gate, 12
propagation delay, 16
inverter, 11
NAND gate, 12 quadratic time, 49
NOR gate, 13
NOT gate, 11 recursion, 46
OR gate, 12 recursive step, 46
universal gates, 19 ripple carry adder, 33
XOR gate, 12
shortest path problem, 66
maxterm, 21 sum, 12
minterm, 21 sum of products (SOP), 20

node, 53 worst case, 48

Dr Jon Shiach 102 Computing Mathematics Lecture Notes

You might also like