0% found this document useful (0 votes)
3 views

Unit 12 Algorithms Teaching Guide

This document outlines the copyright and licensing terms for educational materials provided by PG Online Limited, specifically for a unit on algorithms. It includes a detailed teacher's guide with lesson plans, learning outcomes, and assessments for teaching algorithms, including searching and sorting techniques, as well as graph traversal algorithms. The document also provides vocabulary, suggested resources, and assessment methods to evaluate student understanding.

Uploaded by

imanimeboi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Unit 12 Algorithms Teaching Guide

This document outlines the copyright and licensing terms for educational materials provided by PG Online Limited, specifically for a unit on algorithms. It includes a detailed teacher's guide with lesson plans, learning outcomes, and assessments for teaching algorithms, including searching and sorting techniques, as well as graph traversal algorithms. The document also provides vocabulary, suggested resources, and assessment methods to evaluate student understanding.

Uploaded by

imanimeboi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Copyright

© 2016 PG Online Limited

The contents of this unit are protected by copyright.

This unit and all the worksheets, PowerPoint presentations, teaching guides and other
associated files distributed with it are supplied to you by PG Online Limited under licence
and may be used and copied by you only in accordance with the terms of the licence. Except
as expressly permitted by the licence, no part of the materials distributed with this unit may
be used, reproduced, stored in a retrieval system, or transmitted, in any form or by any
means, electronic or otherwise, without the prior written permission of PG Online Limited.

Licence agreement
This is a legal agreement between you, the teaching institution, and PG Online Limited. PG
Online Limited grants to you a non-exclusive, non-transferable, revocable licence to use this
unit and all the worksheets, PowerPoint presentations, teaching guides and other associated
files distributed with it in the course of teaching by your teachers and/or employees.

The materials distributed with this unit may be copied and used by your teachers and/or
employees on a single site only in the course of their teaching. You warrant that you shall
not, and shall procure that each of your teachers and/or employees shall not, share in any
way any of the materials or part of the materials with any third party, including users on
another site or individuals who are teachers and/or employees of a separate institution. You
acknowledge and agree that the materials must remain with you, the teaching institution, and
no part of the materials may be transferred to another institution. You also warrant that you
shall not, and shall procure that each of your teachers and/or employees shall not, procure,
authorise, encourage, facilitate or enable any third party to reproduce these materials in
whole or in part without the prior permission of PG Online Limited.

In consideration of the licence granted to you, you shall indemnify PG Online Limited against
all liabilities, costs, expenses, damages and losses (including but not limited to any direct,
indirect or consequential losses, loss of profit, loss of reputation and all interest, penalties
and legal costs and all other professional costs and expenses) suffered or incurred by PG
Online Limited arising out of or in connection with the exercise by you of your rights granted
under this licence.

OCR A Level Unit 12 Algorithms © 2016 PG Online Ltd 2


Algorithms
Teacher’s Guide
Introduction
This teacher’s guide contains a detailed lesson plan to accompany the set of PowerPoint
slides and worksheets for each lesson.
The lessons are designed to form a basis for ideas for the teacher and should be adapted to
suit the teaching style and preferences of the individual teacher, and the resources and
nature of the individual school or Computing / ICT department.
The material supplied for this unit includes:
• 6 PowerPoint presentations, each designed to cover one lesson
• 6 worksheets
• 6 homework sheets
• An end-of-unit test for assessment purposes
• Python programs implementing pseudocode

Summary
This is a theoretical unit covering Section 2.3 Algorithms (except algorithms for stacks,
queues, trees and linked lists which are covered in Unit 7). Searching and sorting algorithms
(bubble sort, insertion sort, merge sort, quick sort) are explained in an interactive and
practical way, with reference to Big-O notation in terms of time and space complexity. Topic
5 tackles standard algorithms for depth-first and breadth-first graph traversals. Optimisation
algorithms, such as Dijkstra’s shortest path algorithm and the A* algorithm are covered along
with a discussion of intractable problems, in the final topic.

OCR A Level Unit 12 Algorithms © 2016 PG Online Ltd 3


Learning Outcomes for the unit
At the end of this Unit all students should be able to:
• state the order in which nodes are visited in pre-order and post-order tree traversals
• give examples of linear, polynomial, exponential and logarithmic functions
• compare two algorithms in terms of efficiency
• explain the principles of a linear and binary search
• explain how an insertion sort works
• state the two basic steps in a merge sort
• show the result of merging two sorted lists
• state a possible order in which nodes are visited in depth first and breadth first graph
traversals
• state applications of each graph traversal
• state the purpose and applications of Dijkstra’s shortest path algorithm
• Explain what is meant by a tractable or intractable problem

Most students will be able to:


• state the time complexity of an algorithm
• write an algorithm for a linear search
• trace through a bubble sort algorithm
• trace through an insertion sort algorithm
• explain how the merge sort works and analyse its time complexity
• explain how the quicksort works
• describe applications of each graph traversal
• be able to trace Dijkstra’s shortest path algorithm
• Give examples of intractable problems
• Describe briefly the A* algorithm and its purpose

Some students will be able to:


• write a recursive algorithm to solve a simple problem
• show the changing contents of a call stack as a recursive routine is executed
• derive the time complexity of an algorithm
• write an algorithm for a binary search
• write an algorithm to merge two lists
• trace through the merge sort algorithm
• trace through the quicksort algorithm
• trace depth-first and breadth-first graph traversal algorithms

OCR A Level Unit 12 Algorithms © 2016 PG Online Ltd 4


Previous Learning
Students need to have studied the first year of the A Level course and have some
programming experience.

Suggested Resources
No specific software is required for this unit beyond a standard office suite of applications for
the presentation and printing of provided resources.

The textbook OCR AS and A Level Computer Science by PM Heathcote and RSU
Heathcote provides comprehensive coverage of all the theoretical topics in the OCR
specification (H446) and complements the unit series.

Each of the twelve sections in the book corresponds to one of the teaching units in this
series and will be extremely useful as a course textbook and also as a revision guide.
Sample questions, many taken from past exam papers, are included at the end of each
chapter and can be set as additional homework.

The book is published by PG Online in a printed and electronic edition. Please refer to
www.pgonline.co.uk for ordering and pricing details.

OCR A Level Unit 12 Algorithms © 2016 PG Online Ltd 5


Vocabulary
Vocabulary associated with this Unit, such as:
algorithm, brute force method, divide and conquer, decrease and conquer recursion,
recursive subroutine
Big-O notation, linear, polynomial, exponential, logarithmic functions, permutation, time
complexity
linear search, binary search, binary tree search, bubble sort, insertion sort, merge sort, quick
sort
depth-first traversal, breadth-first traversal, pre-order and post-order tree traversal
optimisation problem, Dijkstra’s shortest path algorithm, A* algorithm
limits of computation, travelling salesman problem (TSP), computational problem, tractable
and intractable problems, heuristic solution, computable and non-computable problems

Assessment
Assessment will be by means of regular homework and a test with examination style
questions.

OCR A Level Unit 12 Algorithms © 2016 PG Online Ltd 6


Topic plans
Topic 1 Analysis and design of algorithms

Learning Objectives:
• Analyse the suitability of different algorithms for a given task and data set
• Be familiar with measures and methods to determine the efficiency of different
algorithms
• Define constant, linear, polynomial, exponential and logarithmic functions
• Use Big-O notation to compare the time complexity of algorithms
• Be able to derive the time complexity of an algorithm

Content Resources

Starter PowerPoint Guide:


Algorithms T1 Design of
Ask the class to give a definition of an “algorithm”. Invite algorithms
suggestions for real-world algorithms that have “changed the
world” – problems that are solved by computer which we take
for granted today. There are thousands of them – for example,
pattern recognition is used in scanning everything from bar
codes to fingerprints to detecting when a platform on an
underground railway has become too full for safety. More
suggestions are given on the next slide.
Main
Ask the students to discuss with each other and come up with
a list of properties that a good algorithm should have. There
are well-defined properties which are given on the next slide.
How efficient is an algorithm?
Most problems can be solved in different ways, and some
algorithms are far more efficient than others. Computer
scientists need a way of measuring the time efficiency of a
given algorithm, and a basic measure is to count the number of
statements that will be performed in relation to the number of
items in the data set.
Mathematics revision
Before moving on to Big-O notation, students need to be
reminded of different types of function, and the shapes of the
graphs that represent them.
An understanding of linear, quadratic and logarithmic functions
is necessary in order to understand the Big-O notation, a
measure of time complexity, or how execution time increases
with the number of items to be processed.

Big-O notation

OCR A Level Unit 12 Algorithms © 2016 PG Online Ltd 7


O stands for Order. Explain the basic concept of Big-O notation
– it could be, for example O(n) if n items take n times as long
to process as one item, or O(n2) if n items take n2 times as long
to process as one item.
Deriving Big-O http://logbase2.blogspot.
co.uk/2008/08/log-
Show how to derive the Big-O time complexity for a given calculator.html
algorithm. This basically involves counting the number of steps
as a function of n. The coefficient of n and any terms other
than the dominant one become irrelevant as n becomes large,
so an algorithm with, for example, 2n + 6 steps is just O(n).
An algorithm with 5n2 + 2n + 6 has time complexity O(n 2)
Logarithmic functions
Students should be familiar with the binary search algorithm.
The time taken to search for a particular item increases only
very slowly as the number of items doubles, so this is a very
efficient algorithm.
Algorithms Worksheet 1
Hand out Worksheet 1 and ask students to do the questions in Analysis and design of
Task 1. algorithms
Go over the answers when they have finished. Algorithms Worksheet 1
Permutations Answers
More Maths here, the amount of time you will need to spend on
it will depend on how much Maths students have done,
Some examples of permutations are given – you can probably
think of others.
With repetition
From a pot of 10 numbers (0 – 9):
Possible choices for 1 digit = 10 1 = 10
Possible choices for 2 digits = 101 + 101 = 102 = 100
Possible choices for 3 digits = 10 1 + 101 + 101 = 103 = 1000
Possible choices for 4 digits = 10 1+101+101+101=104=10,000
Ask if students can identify the Big-O for this problem, just by
looking at the pattern.
Without repetition
The example with 3 colours is small enough that students can
check their logic by drawing out all the different permutations.
Once they’re convinced that 3 x 2 x 1 is correct, have them
generalise the logic by using 5 and 7 differently coloured balls.
They may need reminding that this formula is a factorial.

Compare the different time complexities – note how very much


more efficient an algorithm of O(n) or O(log n) is than one of
O(2n) or O(n!). Algorithms of this order are all but useless for a
problem with other than a very small dataset.
Ask students to do Task 2 on the worksheet.

OCR A Level Unit 12 Algorithms © 2016 PG Online Ltd 8


At the end of this worksheet there is a multiple choice quiz
on Big-O notation.
You could give this to the students now, or keep it for a
revision lesson.

Plenary
Go over the answers. Show the final two plenary slides to
recap the lesson. Algorithms Homework 1
Analysis and design of
Hand out Homework 1. algorithms
Algorithms Homework 1
Answers

OCR A Level Unit 12 Algorithms © 2016 PG Online Ltd 9


Topic 2 Searching algorithms

Preparation:
• The Word documents Girls names (10) and Girls names (20) contain two sets of
girls’ names. You need one set for each student, preferably laminated and cut up in
advance. These will be used in kinaesthetic exercises to manually search and sort a
list.
Learning Objectives:
• Write and trace algorithms for linear search and binary search
• Analyse the time complexity of the linear search and binary search algorithms
• Describe and trace the binary tree search algorithm

Content Resources

Starter PowerPoint Guide: Algorithms


T2 Searching algorithms.pptx
Hand out the name cards and show the first two slides.
Students can perform a manual search – hopefully a
systematic linear search rather than picking random
cards… this can be a topic for discussion.
On average, students should have had to turn over 5
cards. Ask students how many each turned up and see
if this is true. Worst case scenario is that Lily is the 10th
card to be turned over.
Main
Algorithm for linear search
Give students some time to study the algorithm, write it
down and fill in the missing lines. Answer on next slide,
“Analysing the algorithm”.
Ask them to calculate the number of steps for a list of
size n (worst case scenario) and hence, the Big-O time
complexity of this algorithm.
Answer on next slide.
Binary search
The next few slides explain the binary search algorithm,
Divide and conquer
Students can participate in searching for a given
number in the list 23 27 32 35 37 38 41 42 45 50 52 53
54 58 61 67.
Algorithms Worksheet 2
Give out Worksheet 2 and ask students to do questions Searching algorithms
in Task 1. They need to be able to trace and possibly
write the algorithm for a binary search. Algorithms Worksheet 2
Answers

Binary search tree

OCR A Level Unit 12 Algorithms © 2016 PG Online Ltd 10


Trees were covered in unit 7, so students should know
that in a binary search tree, an inorder traversal will visit
the nodes in alphabetical or numerical order.
The pseudocode for a recursive algorithm for this
traversal is given, and there is a Python program for
tree traversals in the Python programs folder.
TreeTraversals.py
Note that a search of an unbalanced tree will have time
complexity O(n), as it reduces to a linear search. This
tree would result from inserting an already sequenced
list into a tree.
Task 2 on Worksheet 2 gives a little revision on
representing a binary tree as an array of records

Plenary
Go over the answers to Task 2.
Algorithms Homework 2
Give out Homework 2. Searching algorithms
Algorithms Homework 2
Answers

OCR A Level Unit 12 Algorithms © 2016 PG Online Ltd 11


Topic 3 Bubble sort and insertion sort

Preparation:
• The Word documents Girls names (10) and Girls names (20) contain two sets of
girls’ names. You need one set for each student, preferably laminated and cut up in
advance. These will be used in kinaesthetic exercises to manually search and sort a
list.
Learning Objectives:
• Be able to describe the bubble sort and insertion sort algorithms
• Be able to trace the bubble sort and insertion sort algorithms

Content Resources

Starter PowerPoint Guide:


Sorting is a very common activity. Ask students to think of Algorithms T3 Bubble
reasons why data might need to be sorted. For example, a sort and insertion
mailing list might need to be sorted in order of postcode (pre- sort.pptx
sorted bulk mail gets a cheaper rate of postage). If a data file
is to be searched frequently, it needs to be sorted so a binary
search can be carried out. Reports are frequently needed in a
particular order, say in alphabetical order of surname, or Girls names (10)
descending order of amount overdue or date of invoice, so the
most recalcitrant payers can be chased first.
Main
Choosing a sort algorithm
Considerations include the number of items to be sorted and
the amount of memory space needed to sort a large number of
items – the merge sort, for example takes extra memory
space.
Bubble sort
This is the simplest algorithm and the easiest for novice
programmers to understand. However, it is by far the slowest.
Swapping two items requires a temporary variable in which to
store one of the two items.
Temp =a
A=b
B = temp
Where there are two nested loops, the big-O time complexity
is in general, O(n2).
Give out Worksheet 3 and ask students to complete Task 1. Algorithms Worksheet 3
They will use the cards to do a manual binary search. T3 Bubble sort and
insertion sort
Go over the answers.
Algorithms Worksheet 3
Answers
Insertion sort

OCR A Level Unit 12 Algorithms © 2016 PG Online Ltd 12


As with sorting a deck of cards, starting with the second card,
each card is picked up and moved to the correct place if it is
out of order. Students may find this easiest to understand
using the name cards, initially sorted into a random sequence.

The algorithm for the insertion sort is given – note how the
item being looked at is held temporarily in nextItem, while the
other items are moved right until its correct place is found,
when it is re-inserted into the list.
Ask students to do the questions in Task 2.
There is a Python program insertion sort.py in the Python Insertion sort.py
program folder.
Extension task.
If there is time for practical, students could write a program to
time the bubble sort and insertion sort.
Timing sorts.py
See timing sorts.py in the Python Program folder, which also
includes a merge sort.
Time complexity of insertion sort
Let students work this out – the clue is in the two nested loops.
It is O(n2).

Plenary Algorithms Homework 3


Go over the main points of the lesson, and give out T3 Bubble sort and
Homework 3. insertion sort
Algorithms Homework 3
Answers

OCR A Level Unit 12 Algorithms © 2016 PG Online Ltd 13


Topic 4 Merge sort and quick sort

Preparation:
• The Word documents Girls names (10) and Girls names (20) contain two sets of
girls’ names. You need one set for each student, preferably laminated and cut up in
advance. These will be used in kinaesthetic exercises to manually search and sort a
list.
• Go through the animation of the quicksort in the PP slides to make sure you can follow
what is going on and can answer any questions.
Learning Objectives:
• Understand and be able to trace the merge sort and quick sort algorithms

Content Resources

Starter PowerPoint Guide:


Remind students of the two sort algorithms they have already Algorithms T4 Merge sort
seen, bubble sort and merge sort. This week, it’s merge sort and quick sort.pptx
and quick sort – in ever-increasing order of efficiency.
Main
Merge sort Girls names (10)
This is explained in some detail. It is a two-step process – first
of all the list is recursively divided in half until each sublist
consists of just one item. Then a merge process is carried out
to successively merge the sublists in sequence until just one
whole sorted list remains.
Merging two sorted lists
The merge process is explained step-by step.
Algorithms Worksheet 4
Having been through the slides, hand out Worksheet 4 and let Merge sort and quick sort
students carry out a merge sort manually on the girls’ name
cards as described in Task 1. Algorithms Worksheet 4
Answers
An algorithm for the merge sort is given in Task 2.
There are three Python programs called merge sort.py (which
traces through the splitting process), mergesort v2.py (which
uses the same list as in the worksheet) and mergeLists.py in
the Python programs folder and two similar programs in the
Programs vb.net folder.
Ask them to look at the mergeSort procedure and write down
the steps in plain English: e.g.
while list length > 1
Find the midpoint of the list
Save left half in list called lefthalf
Save right half in list called righthalf
Recursively call the procedure with lefthalf as parameter
Recursively call the procedure with righthalf as parameter
endwhile

OCR A Level Unit 12 Algorithms © 2016 PG Online Ltd 14


As an extra task you could ask students to trace though the
the first part of procedure mergeSort (alist) and check their
merge sort.py
results by running one of the programs merge sort with the
same list. mergesort v2.py
Big-O for merge sort mergeLists.py
Time complexity is O(n log n). merge sort.vb
Quicksort mergeLists.vb
This was invented by Sir Tony Hoare in 1960. Even he was
surprised by how fast it was!
A fairly lengthy explanation and visualisation is given.
Sort 13 17 3 10 12 2 20 15
At each stage of the recursive procedure, a split point is found.
The first item in the list, 13, is selected as the pivot. The other
items are put on the left if they are less than the pivot, and on
the right if they are greater than the pivot. The pivot is now at
the split point and the recursion continues with the partitions
on either side of the split point, which is now in its correct
sorted position.
Animated visualisation
An animated visualisation of the quicksort algorithm follows –
click through it, giving time to read the explanation as each
step is performed. You can ask the students at different points:
what happens next? to make sure they are following.
The algorithm for the quicksort is not shown here, but is given
in the homework task, with a few lines to be filled in.
Ask students to try Task 3 on the worksheet.
Go through the answer.
Task 4, looking at a visualisation and comparison of different
sorts, will cement their impressions of which sorts are efficient
and which one is far slower than the others (the Bubble sort).
The Big-O time complexity is O(n log n) in the best case
scenario – which is when the pivot value is the middle item of
the partition each time. In the worst case scenario, for
example if the list is sorted in the reverse order to the one
required, the split point is always at the end of the partition and
the big O is O(n2).
Different methods of picking the pivot value will give better
results in different circumstances.
Plenary Algorithms Homework 4
Merge sort and quick sort
Go over the main points of the lesson, and give out
Homework 4. Algorithms Homework 4
Answers

Topic 5 Graph traversal algorithms

Learning Objectives:

OCR A Level Unit 12 Algorithms © 2016 PG Online Ltd 15


• Be able to trace depth-first and breadth-first algorithms
• Describe typical applications of each

Content Resources

Starter PowerPoint Guide:


As a revision exercise, ask students to draw the graph Algorithms T5 Graph
represented by the adjacency list. traversal algorithms.pptx
Main
Revise the terminology of a graph – edges, nodes or vertices,
neighbours or adjacent nodes, directed and undirected
graphs.
Traversing a graph
Describe the two ways of traversing a graph – depth-first and
breadth-first.
Depth first traversal
A graph can be represented in a programming language using
a data structure. The slide shows how the graph shown would
be represented in Python.
This algorithm uses a stack to keep account of which nodes
have been visited, and a list of visited nodes. A stack is used
to keep track of the previous node visited so that we can
backtrack when the end of a path is reached by popping the
item off the top of the stack, searching for another path.
The algorithm starts by adding the start vertex A to the list of
visited nodes. When going through the algorithm graphically,
each node is coloured dark blue once it has been added to the
visited list.
Subsequent slides show the progress of the algorithm. The
algorithm ends when all nodes have been visited and the
stack is empty.

Algorithm for depth-first traversal


The algorithm is recursive, and a system call stack is
maintained automatically so does not need to be created
manually.
Python and VB programs dfs.py/vb implementing the dfs.py
algorithm are included in the relevant program folders.
dfs.vb
Students will not be expected to reproduce the algorithm but
should understand and be able to trace it through. Practice at
tracing through a depth first algorithm is given both in the
worksheet and in the homework.

Applications of depth-first search

OCR A Level Unit 12 Algorithms © 2016 PG Online Ltd 16


Navigating a maze is a classic example of using a depth-first
search.
Note: If you’re looking for any solution to the maze, then you
can stop the algorithm when the exit node is first encountered.
However, if you’re looking for all possible solutions, then you
have to keep going until all the nodes have been visited.

Give out Worksheet 5 and ask students to complete Task 1.


Algorithms Worksheet 5
The algorithm given is slightly different because it does not
Graph traversal
produce a list of visited nodes, it simply finds a path from the
algorithms
start node to every other node using a depth-first traversal.
Algorithms Worksheet 5
Tracing through a recursive algorithm is daunting at first. The
Answers
best advice for students is that they should understand how
the depth-first traversal works and keep looking at a graphical
image of the graph they are traversing.
Some guidance is given both in the Worksheet answers and in
the Homework answers – you can decide how much help they
need to complete the homework, and whether to give
guidance in advance or let them try without guidance.
Breadth-first traversal
A queue is used instead of a stack as the supporting data
structure. It is necessary to distinguish between nodes which
have been visited (coloured pale green) and added to the
queue, and nodes which have been fully explored (dequeued
and coloured dark green).
Once again a graphical representation of the steps in the
algorithm is given.
Ask students to do Task 2 on the worksheet.
There are Python and VB programs bfspy/vb in the program bfs.py
folders. bfs.vb
The breadth-first traversal can be turned into a depth-first by
simply changing the queue into a stack. This is demonstrated
in the programs dfs or bfs iterative version.py/vb. Simply dfs or bfs iterative
change the statement version.py
currentNode = supporting_adt.pop(0) dfs or bfs iterative
version.vb
to currentNode = supporting_adt.pop
in a depth-first traversal to switch between breadth-first and
depth-first.
Students may like to try running the program.

Applications of breadth-first search


Some applications are given – in the next lesson students will
learn about a well-known algorithm for finding the shortest
path between two nodes.

OCR A Level Unit 12 Algorithms © 2016 PG Online Ltd 17


Time complexity of each search
This depends on the number of edges. In a sparse graph, it is
O(n). In a graph with the maximum number of edges, it is
O(n2).

Depth-first tree traversal


A pre-order tree traversal is a depth-first traversal, which
students can find out by traversing the tree given – or any
other tree. In the case of a binary tree, it will be much easier to
use the tree traversal algorithm given in the program tree
traversals.py than to use the depth first graph search
algorithm, basically:
procedure preorderTraverse(p)
print(tree[p].value)
if tree[p].left <> -1 then
preorderTraverse(tree[p].left)
endif
if tree[p].right <> -1 then
preorderTraverse(tree[p].right)
endif
endprocedure
Ask students to do Task 3 on the worksheet
Plenary
Algorithms Homework 5
Go over the main points and give out Homework 5. Graph traversal
algorithms
Algorithms Homework 5
Answers

OCR A Level Unit 12 Algorithms © 2016 PG Online Ltd 18


Topic 6 Optimisation algorithms

Learning Objectives:
• Understand and be able to trace Dijkstra’s shortest path algorithm
• Be aware of applications of the shortest path algorithm
• Describe the A* algorithm

Content Resources

Starter PowerPoint Guide:


Discuss some typical shortest path problems. Algorithms T6
Optimisation
Main algorithms.pptx
This lesson explains Dijkstra’s shortest path algorithm. Students need to
be able to trace through the algorithm and understand how it works, but
will not be expected to recall the steps.
After showing Slide 4 entitled “Dijkstra’s shortest path algorithm”, you
may find it useful to show a You Tube video showing how a shortest path
algorithm works. There is one at
https://www.youtube.com/watch?v=WN3Rb9wVYDY and there are
several others. This article is the
Or better, watch the video first and then do the stepping through live on description of how
the whiteboard. Dijkstra’s shortest path
moves from the shortest
The steps as described in this video are: distance between any 2
Assign temporary distances or “costs” of 0 to A (starting vertex) ∞ to all nodes and the shortest
other vertices. distance between all
nodes (spanning tree). It
Then visit the vertex with the lowest cost – at the start, this is a, the has an interesting
“current vertex”. kinaesthetic activity
Visit neighbours of a (b and c here) and calculate costs by adding edge described at the top.
weights of ab and ac to cost at current vertex a. http://people.mpi-
If the calculated cost is less than the one at the vertex, replace it. inf.mpg.de/~mehlhorn/ftp/
Toolbox/ShortestPaths.pd
Move to the vertex with the lowest cost, c. f

Cost at c + edge weight bc = 2 + 1 = 3 so replace cost at b with 3.


Replace cost at e with 2 + 10 = 12.
Repeat the steps for each vertex – the next one to be visited is b.
At the end of the algorithm, the distance of the shortest path from the
start to each vertex is marked at the vertex.

OCR A Level Unit 12 Algorithms © 2016 PG Online Ltd 19


When you have finished the explanation, see if students can write down
how it works. Make up another graph with different vertices and edges to
give them practice at tracing through the algorithm.
Implementing the algorithm
The implementation of the algorithm shown on the next few slides is
similar to a breadth-first search but uses a priority queue instead of an
ordinary FIFO queue.
At each stage, the shortest distance from the start node to each of the
neighbours of the current node is calculated. The node with the shortest
distance is moved to the front of the priority queue, if it is not already
there.
This ensures that the next vertex to be dequeued is the neighbour with
the minimum “cost” attached to it.
Algorithms Worksheet 6
Give out Worksheet 6 and ask students to complete the two questions. Optimisation algorithms
These gives practice in following through the algorithm using the priority Algorithms Worksheet 6
queue. Answers
Computable problems
A problem may theoretically have a solution but if it cannot be found in a
reasonable time, it is incomputable in practice.
Discuss the computational problem of cracking a password. How would
students set about this? Are there better ways than the “brute-force”
method of trying every option?
The site https://howsecureismypassword.net/ will tell you how long it will
take to crack a particular password.

Limits of computation
Discuss the limits of computation. These are algorithmic complexity (think
Big-O) and hardware.
There is an interesting history of weather forecasting, once considered
incomputable, at http://www-history.mcs.st-
andrews.ac.uk/HistTopics/Weather_forecasts.html
It would be worth students looking at this.
The travelling salesman problem (TSP)
This is a very well-known problem, which has applications in many fields
from planning the optimum route for the school bus run, to the
manufacture of circuit boards.
Students could Google this to read around the subject.
Tractable problems

OCR A Level Unit 12 Algorithms © 2016 PG Online Ltd 20


Any problem with Big-O of O(n!) or O (2n) is regarded as intractable.
Note that intractable is not the same thing as insoluble. The problem may
be soluble for a small value of n, or by finding a different algorithm.
Ask students to complete Task 2 on the worksheet.
Discuss the answers when completed.
Heuristic methods
Heuristic methods can be used to find a “good enough” solution to an
intractable problem. We all use heuristic methods – such as common
sense, an educated guess or gut instinct!
Some problems really are incomputable – see the “four-tile problem”,
explained at
http://www.cs4fn.org/algorithms/tiles.php

The A* algorithm
This is a general path-finding algorithm which uses a similar method to
Djikstra’s algorithm but includes a heuristic function as well.
It is used, for example, in logistics and gaming.

Plenary Algorithms Homework 6


Optimisation algorithms
Show the Plenary slide and recap the main points of the lesson.
Algorithms Homework 6
Give out Homework 6. Answers

OCR A Level Unit 12 Algorithms © 2016 PG Online Ltd 21


Unit assessment

Learning Outcomes:
Students will
• apply their knowledge in answers to a range of questions
• be able to highlight areas of strength and any gaps in their understanding of computers

Content Resources

Students should complete the Assessment Test. Algorithms Final


Assessment
There are programs in the Python programs folder and programs
VB.NET folder for questions 3 and 5. Algorithms Final
Assessment Answers
bubble sort with flag.py
bubble sort with flag.vb

Big-O calculation.py
Big-O calculation.vb
Final assessment
binary search Qu 2c.py

OCR A Level Unit 12 Algorithms © 2016 PG Online Ltd 22


Acknowledgements
The authors and publisher would like to thank all contributors for their kind permission to
reproduce their photographs or images, screenshots of their websites or other copyright
material in the PowerPoint Guides.

Diagrams © PG Online Ltd


Images otherwise stated © Shutterstock

Every effort has been made to trace the copyright holders and we apologise in advance for
any unintentional omissions. We would be pleased to insert the appropriate
acknowledgement in any subsequent edition of this publication.
This material contains links to relevant websites featured in the teacher’s guide. Every effort
has been made to ensure that at the time of distribution, the links remain unbroken, the
material remains up-to-date and that links are not inadvertently linked to sites that could be
considered offensive. PG Online cannot be held responsible for the content of any website
mentioned in this material. It is sometimes possible to find relocated sites by typing the
original URL into a browser. Any errors should be reported directly to
[email protected] and changes will be made in any subsequent editions of the
material.

Artwork

Evening Walk © 2009 Hilary Turnbull


Mixed media on canvas, 61x61cm
www.hilaryturnbull.co.uk

OCR A Level Unit 12 Algorithms © 2016 PG Online Ltd 23

You might also like