You are on page 1of 153

Theory Of Computation for the

new A Level Computer Science


AQA and OCR
Python programming
Notes
www.interactivepython.org has been a major
source for putting this presentation together.
Although mentioned in the Bibliography I
thought they deserved a special mention here
also.
This presentation is mainly for the instructors
it can then be adapted section by section for
your learners.
AQA vs OCR

There is a large overlap!


Session 1: ABSTRACTION &
AUTOMATION

Why should we use abstraction?


Different types of abstraction as applied to
real problems
Worked examples using different types of
abstraction
Understanding the term algorithm (in Session
II)
OCR

AQA vs OCR
3 vital pre-programming skills
1. Computational & Algorithmic Thinking:
what is the problem to solve
2. Abstraction
How to encapsulate it without any frills
3. Writing pseudo-code
Algorithm turned into pseudocode
Syllabus Content of this course
overlaps:
AQA OCR
4.2.4 Graphs 2.1.1 Thinking abstractly
4.2.5 Trees 2.2.2 Computational methods
4.3.1Graph Traversal 2.3.1 Algorithms
Analysis & design of algorithms
4.3.2 Tree Traversal
Suitability for a task
4.3.4 Searching algorithms Big O notation
4.3.5 Sorting Algorithms Comparison of algorithms
4.3.6 Optimisation Algorithms Algorithms for Abstract Data
Structures
4.4.1 Abstraction & Standard sorting /searching
Automation algorithms
Session 1: ABSTRACTION &
AUTOMATION

Why should we use abstraction?


Different types of abstraction as applied to
real problems
Worked examples of different types of
abstraction
4.4.1.1 Develop and check solutions to
simple logic problems
http://codingbat.com/python
Probably one of the best sites for practice but
dont let your students loose on it you can
give them the activities to do
It does tend to reveal the solution rather too
easily!
Example of a simple logic problem

Given an integer n, return the absolute


difference between n and 21, except return
double the absolute difference if n is over 21.

diff21(19) 2
diff21(10) 11
diff21(21) 0
Be able to check solutions to simple logic
problems
Why is Abstraction so useful in
computational thinking?
Humans are amazing at Abstraction
2 main types of Abstraction
Be familiar with the concept of abstraction and
know that
Representational abstraction is a
representation arrived at by removing
unnecessary detail
Abstraction by generalisation/categorisation
is a grouping by common characteristics to
arrive at a hierarchical relationship
Abstraction (GCSE definition)
create a general idea of
what the problem is
and how to solve it
Link: Algebra

BBC Definition
Find a Formal Definition of
Abstraction Appendix A
Definition Link with Programming:
Abstraction is used to remove If in the course of planning your
program (a process known as
unnecessary information analysis and design), you find
helps solve the problem. good abstractions to the problem
you are trying to solve, your
programs become shorter, hence
easier to write and - maybe more
Allows the process of simple importantly - easier to read.
ideas in order to solve other The way to do this is to try and
grasp the major concepts that
problems which are similar in define your problems -- as in the
nature. (simplified) example of drawing a
house, this was squares and
triangles, to draw a village it was
houses.
Formal Definition
Abstraction is the purposeful suppression, or
hiding, of some details of a process or artefact,
in order to bring out more clearly other aspects,
details or structure.
(Nameless scholarly article online)
Why is The London Underground a good
example of Representational Abstraction?
Representational abstraction
Example 2
So why are we not jumping straight
into the code soup?
Perhaps by now obvious
What have we gained by turning the problem
into its abstract form first?
Answer:
Stripping to bare essentials
Find out what needs automation
Think about algorithm to solve the problems,
iterations
Abstraction by generalisation
In abstraction we simplify the description of
something to those aspects that are relevant
to the problem at hand eg London
Underground
In Generalisation we find and exploit the
common properties in a set of abstractions
Hierarchy, polymorphism, genericity &
patterns now become important
Hierarchy
Here we exploit an is-a-kind-of relationship
among kinds of entities to allow related kinds to
share properties and implementation
Examples:
a Director is an Employee
A swallow is a bird
More interested in the taxonomy of entities
So if we know the general properties of a bird
then we can surmise correctly about the general
properties of a swallow
AQA Past Exam Question
Solution: is-a-kind-of
Information Hiding
Be familiar with the process of hiding all details
of an object that do not contribute to its
essential characteristics.
Information Hiding 2 Definitions
Definition 1: Information hiding is the purposeful
omission of details in the development of an
abstract representation
Definition 2: Showing only those details to the
outside world which are necessary for the outside
world and hiding all other details from the outside
world.
Scholarly article online:
http://www.tonymarston.co.uk/php-
mysql/abstraction.txt
Information Hiding - Rationale
Need to know basis:

if code chunk A doesn't really need to know something about how


code chunk B (which it calls) does its job, don't make it know it.
Then, when that part of B changes, you don't have to go back and
change A. (similar to data redundancy in database design)
Information Hiding - OOP

Hiding the object details (state and


behaviour) from the users
Here by users we mean an object of another
class that is calling functions of this class using
the reference of this class object or it may be
some other program in which we are using
this class.
How to achieve this in OOP
Information Hiding is achieved in Object Oriented
Programming using the following principles:

All information related to an object is stored within the


object
It is hidden from the outside world
It can only be manipulated by the object itself
Information Hiding vs Encapsulation

Info Hiding Encapsulation


Info Hiding is the idea that a Encapsulation means
design decision should be drawing a boundary around
hidden from the rest of the something. It means being
system to prevent able to talk about the inside
unintended coupling. and the outside of it.
Info Hiding is achieved via Object-oriented languages
encapsulation provide a way to do this.
It is a design principle Encapsulation is a
programming language/
implementation feature.
Encapsulation vs Information Hiding
Encapsulation is the grouping of related ideas
into one unit, which can thereafter be referred to
by a single name.
Single name is here the key word to get an insight
of what encapsulation is about: a subroutine (or
function as we call it nowadays), for example, is a
name that refers to a unit of code.
Something that instead of being repeated over
and over in the source code is factored out in a
single place.
Procedural Abstraction
Know that procedural abstraction represents a computational
method

The result of abstracting away the actual values used in any


particular computational patter or method - a procedure
Procedural Abstraction- definition
A function encapsulates a bundle of program
statements (code):
Solution with and without def
Advantages of Procedural Abstraction
The function gives us a name for the statements,
a way to invoke them at a high-level.
The name of the function becomes an
abstraction that can be used as we reason
through a complex program.

Our program becomes a more manageable set of


functions, instead of a flat sequence of thousands
of statements. It is easier to understand, build,
and maintain.
Functional Abstraction
Know that for functional abstraction the
particular computational method is hidden
The result of procedural abstraction is a
procedure, not a function
To get a function requires another abstraction
that disregards the particular computational
method (Black Box).
This is functional abstraction
Functional Abstraction
Define Problem P
Define its subtasks S(i)
Non trivial and implies each S(i) is mapped to
a function/procedure f(i)
Example of Functional Abstraction
consider the Python math module. Once we
import the module, we can perform
computations such as
>>> import math
>>> math.sqrt(16)
4.0
>>>
Fruitful functions vs procedures
Draw a square is an abstraction, or a mental
chunk, of a number of smaller steps
What are these steps?
Functions that return values are called fruitful
functions
In some programming languages, a function that
does not return a value is called a procedure,
since it is used to implement procedural
abstraction, but in Python both constructs are
just called functions.
User defined Example: Sum of Squares
Examples of Functional Abstraction

>>> abs(5)
5
>>> abs(-5)
5
Division into sub-tasks
Rule 1: using functions helps in the top down design of
programs.

Rule 2: During design a problem is subdivided into tasks


(and then into sub-tasks, sub-sub-tasks, etc.) .
the problem solver (programmer) should have to consider only
what a function is to do and not be concerned about the details
of the function.
Rule 3: The function name and comments at the beginning
of the function should be sufficient to inform the user as to
what the function does.
As users of the function, we neither know nor care why
they work.
Very Comprehensive Example
COMP1 2015 Capture the Sarrum
Data Abstraction
Definition: Any representation of data in which the
implementation details are hidden (abstracted).
Abstract data types and objects are the two primary forms
of data abstraction. (Stacks, queues, lists, linked lists)

An abstract data type, sometimes abbreviated ADT, is a


logical description of how we view the data and the
operations that are allowed without regard to how they will
be implemented.
This means that we are concerned only with what the
data is representing and not with how it will eventually be
constructed.
Info Hiding vs Encapsulation (again)
By providing this level of abstraction, we are
creating an encapsulation around the data.
The idea is that by encapsulating the details of
the implementation, we are hiding them from
the users view. This is called information
hiding.
Recall in top down design
function is the simplest level of encapsulation
and abstraction:
a designer can refer to it by name as a single
entity and even forget the details of the
internals and only remember its interface
Same principle is used for data abstraction
Example of Data Abstraction
Data Abstraction
(https://sites.google.com/site/usfcomputerscience/introduction-
to-object)
Functions allow us to clump together code, but what about data? What
about a large program that requires lots of variables? Data Abstraction
gives us a way to bundle variables

For example, consider the data needed to track someone's account in a


social network. You might create a data abstraction that bundles
together the data for a person:

After creating this Person abstraction, you could use it in your program,
e.g., send a Person as a parameter to a function instead of five separate
variables.
Easier to send one parameter
Activity
What data abstractions might you define in a
Twitter app?
What data abstractions might you have defined in
the Mastermind program?
What data abstractions might you have defined
in the Hangman program?
What data abstractions might you define for a
polygon drawing program?
(https://sites.google.com/site/usfcomputerscienc
e/introduction-to-object)
Problem Abstraction/Reduction
The lambda operator or lambda function is a
way to create small anonymous functions, i.e.
functions without a name.
These functions are throw-away functions, i.e.
they are just needed where they have been
created.
>>>f = lambda x, y : x + y

>>> f(1,1) 2
Decomposition
Procedural decomposition: following two are
well worth reading for going deeper into the
topic
http://www.nltk.org/book_1ed/ch04.html
http://wla.berkeley.edu/~cs61a/fa11/lectures
/objects.html
Its easier to implement the smaller tasks in
isolation than it is to implement the entire
process at once.
A good definition
Session II- Algorithms
Understanding the term algorithm
Constructing algorithms using standard
constructs: sequence, assignment, selection,
iteration
Converting an algorithm to pseudo-code
Hand tracing or dry-running sample
algorithms
For your students
An excellent teaching tool is this presentation:

http://www.slideshare.net/DamianGordon1/pseudocode-10373156
Definition of Algorithm
An algorithm is a procedure for solving a
problem in terms of the actions to be
executed and the order in which those
actions are to be executed.
An algorithm is merely the sequence of steps
taken to solve a problem. The steps are
normally "sequence," "selection, "
"iteration," and a case-type statement.
From algorithm to pseudocode- Chess
example
place initial pieces in their locations
show board
while game is not over
make move and display board
prompt user for move
make users move
This is structured English encourage
students to start here!
Definition of pseudocode
Pseudocode is an artificial and informal
language that helps programmers develop
algorithms. Pseudocode is a "text-based"
detail (algorithmic) design tool.
The rules of Pseudocode are reasonably
straightforward. All statements showing
"dependency" are to be indented. These
include while, do, for, if.
Following and writing algorithms
Give students various algorithms without
saying which is which and have them hand-
trace to find out what they do
Algorithm to Pseudocode :use GCSE examples
to start and then build up into more complex
A Level examples
Activity : Stepwise refinement of an
algorithm

This is the top-down


approach which breaks the
problem into successively
smaller steps adding
greater detail at each level
Consider the calculation of
the sum of any two
numbers supplied by a
given person
Create an algorithm for
this task
Sum & Average Algorithm
Level 0: Calculate Sum of two LINK with each section
Numbers
1: Get two numbers
1.1 Read(No1)
1.1 Read(No2)
2: Sum two numbers
2.1 Sum No1 + No2
3 Display Sum
3.1 Write(Sum)
Baby Mod Algorithm
Solution to Baby Mod Algorithm
Answer Count Remainder
True - -
2 7 mod 2 = 1
3 7 mod 3 = 1
4 7 mod 4 = 3
5 7 mod 5 = 2
6 7 mod 6 = 1

Purpose: To find the remainder upon division of 7 by 1-6.


Looped Modular Arithmetic Algorithm
MOD Algorithm
Dry Run - Solution
ISBN Algorithm
Dry run of ISBN Algorithm
Use the following digits to see if they form a
valid ISBN sequence or not:

9, 7, 8, 0, 0,9, 9, 4, 1, 0, 6, 7, 6 (in the order


given)
ISBN Algorithm - Solution
Count 1 2 3 4 5 6 7 8 9 10 11 12 13
ISBN 9 7 8 0 0 9 9 4 1 0 6 7 6

Calculated Count CD+ISBN[Count] Count CD+ISBN[Count]*3 Count


Digit
0 1 0+9=9 2 9+(7*3)=9+21=30 3
30 3 30+8=38 4 38+(0*3)=38 5
38 5 38+0=38 6 38+(9*3)=38+27=65 7
65 7 65+9=74 8 74+(4*3)=74+12=86 9
86 9 86+1=87 10 87+(3*0)=87 11
87 11 87+6=93 12 93+(7*3)=93+21=114 13
Break out of loop!!
Solution

1 0 0

1 2 0

1 1 1
Session III: PRACTICAL
understanding & writing some well-known
searching & sorting algorithms including
Insertion Sort & Bubble Sort
Advanced practice with Graph & Tree
Algorithms
Big O Notation
Linear Search
Linear search (aka Sequential Search) is the most fundamental and
important of all algorithms.
It is simple to understand and implement, yet there are more
subtleties to it than most programmers realize.
The input to linear search is a sequence (e.g. an array, a collection, a
string, an iterator, etc.) plus a target item.
The output is true if the target item is in the sequence, and false
otherwise.
If the sequence has n items, then, in the worst case, all n items in
the sequence must be checked against the target for equality.
Under reasonable assumptions, linear search does O(n)
comparisons on average.
In practice, this is often too slow, and so, for example, Binary
Searching is a speedier alternative.
Sequential Search
When data items are stored in a collection such
as a list, we say that they have a linear or
sequential relationship.
Each data item is stored in a position relative to
the others. In Python lists, these relative
positions are the index values of the individual
items. Since these index values are ordered, it is
possible for us to visit them in sequence.
This process gives rise to our first searching
technique, the sequential search
Sequential search
Linear Search - Algorithm
abstractly:
sequential search(list L,item x)
for (each item y in the list)
if y =x
return y
return no match
How efficient/fast is this algorithm?
Simple answer = depends on the size of the
array
And
Position of element sought in array
Best
12 41 41

23 23 23
average
10 10 10

1 1 12

4 4 4

2 2 2

41 12 2
worst
Average Case O(n)
Best Case Find at first place - one comparison
Worst Case Find at nth place or not at all - n
comparisons
Average Case In considering the average case there
are n cases that can occur, i.e. find at the first place,
the second place, the third place and so on up to the
nth place.
If found at the ith place then i comparisons are
required. Hence the average number of comparisons
over these n cases is:
average = (1+2+3.....+n)/n = (n+1)/2where the result
was used that 1+2+3 ...+n is equal to n(n+1)/2.
Linear Search Worst case O(n)
Linear Search - Python
Binary Tree Search
To do a Binary Tree Search we first need to
know what a Tree is.

And to know what a Tree is we need to know


about Graphs first.
Basic Graph Types AQA Wiki
(COMP3)
Definition of a Tree
A connected undirected graph with no cycles
Root, leaves, subtrees
Root node = 8

Left sub tree= 5,7,12

Right sub tree: 4,11

Leaves: 9,1,2,3
Binary Trees
Look closely at this tree - can you see a pattern in
addition to the fact that there are cycles?
Exercise
Create a binary tree for
this data:

8,3,1,6,4,7,10,14,13
Create a binary tree for this data:
Elephant
Cat
Dog
Hippo
Giraffe
Lion
Bear
Binary Tree Search
Having seen the properties of a Binary Tree we
can now look at its related searching
algorithm
For an excellent treatise look at Victor
Adamchiks site Carnegie Mellon University
http://www.cs.cmu.edu/~adamchik/15-
121/lectures/Trees/trees.html
Properties of a BST
In a BST the nodes are ordered as follows:

Each node contains one piece of data (or one key)


Keys in the left subtree are less than the key in the
parent node ie L <P
Keys in the right subtree are greater than the parent
key ie P<R
Duplicate keys are not allowed
Solution
Binary Search Complexity

Tree Traversal Algorithms


1. Pre-Order
2. In-Order
3. Post-Order

4. Depth First
5. Breadth First
Pre-Order N L R
1. Visit root
2. Traverse to
left subtree
3. Traverse to
right subtree

F,B,A,D,C,E,G,I,H
In Order L N R
Traverse to left subtree
Visit root node
(generally output this)
Traverse to right
subtree

A, B, C, D, E, F, G, H, I
Post Order - L R N
1.Traverse to left
subtree
2.Traverse to right
subtree
3.Visit root node
(generally output this)

A, C, E, D, B, H, I, G, F
Simple Graph Traversal Algorithms

First we look at the actual traversals then we


compare the algorithms
Depth First Traversal
Dive down as quickly as
possible to the leaf
node
Pre-Order: Node first
then children
abdheijcfkg
Post-Order: Children first
then node
hdijebkfgca
Breadth First Traversal
Traverse the tree (or
graph) level by level,
visiting the nodes of the
tree in this order:
a, b c d e f g h i j k
Breadth First Traversal on Graph
Graph Traversal Maze
Exploring a graph is like
navigating a maze
Which parts of the
graph are reachable
from a given vertex?
each node is a junction of
paths.
D, I, J, L and O are dead
ends, and ** is the goal.
Of course, in your actual
tree, each node has a
possibility of having as
many as three children.
1 Algorithm 2 searches
Consider a tree
In Depth First visits the
nodes:
A , B, D, C, E, F
Work down one branch
In Breadth First we
visit the nodes in the
order:
A, B, C, D, E, F
Work across
Structured English
Difference between the two traversals
For Depth First use a For Breadth First use a
stack queue
FILO FIFO
Depth First
Breadth First
Discussion
Discuss why the Abstract Data Types Stack and
Queue are suitable for the two graph
traversals?
What is their advantage over the other in each
case?
DFS BFS

0156423 0123456
Bubble Sort Algorithm

Know and be able to trace and analyse the time


complexity of the bubble sort algorithm

This is included as an example of a particularly


inefficient sorting algorithm, time-wise.

Time complexity is O(n^2)


How it works
The bubble sort makes multiple passes
through a list or 1D array. [x1,x2,..,xn]
It compares adjacent items and swaps them
round if they are in wrong order [xi,xj]
[xj,xi] where j=i+1
In this way every pass through the list places
the next largest value in its correct position
It is called BubbleSort since each item
essentially bubbles up to the surface
Example : Creating Temp Variables
Write a full English sentence for each of these to
explain what is happening at each step:
Ptr 1
While Ptr < 10 Do
If List[Ptr] > List [Ptr + 1] Then
Temp List [Ptr]
List [Ptr] List [Ptr +1 ]
List[Ptr +1] Temp
EndIf
Ptr Ptr + 1
Endwhile
Temp variable
Bubble Sort
Question
Consider the array: [8 6 4 2 ]

Apply the Bubble Sort and write out the


number of passes required to sort it in
Ascending Order
Solution Bubble Sort
BubbleSort is O(n^2)
We have a pair of nested loops in the algorithm
The outer loop must iterate once for each
element in the data set (of size n) while the inner
loop iterates n times the first time it is entered, n-
1 times the second, and so on.
Therefore, to get all n elements in their correct
places, the outer loop must be executed n times.
The total number of comparisons, therefore, is
(n - 1) + (n - 2)...(2) + (1) = n(n - 1)/2 or O(n 2) .
Merge Sort
Be able to trace and analyse the time
complexity of the merge sort algorithm.

An example of Divide & Conquer


approach to problem solving.

Its time complexity is O(nlogn)


Divide & Conquer Algorithm
Merge sort is a recursive algorithm that
continually splits a list in half.
If the list is empty or has one item, it is sorted by
definition (the base case).
If the list has more than one item, we split the list
and recursively invoke a merge sort on both
halves.
Once the two halves are sorted, the fundamental
operation, called a merge, is performed
Merge Sort Phase I just dividing
Merge Sort Phase II - Sorting
Finally Merging all data
MergeSort is O(n logn)
Two distinct processes:
Splitting in half Process: recall from the Binary Search
algorithm that we can divide a list in half logn times
where n is the length of the list.
The Merge Process: Each item in the list will eventually
be processed and placed on the sorted list.
So the merge operation which results in a list of size n
requires n operations.
The result of this analysis is that logn splits, each of
which costs n for a total of nlogn operations.
A merge sort is an O(nlogn) algorithm.
A great place to practice
http://www.sorting-algorithms.com/
Question 1
Given the following list of numbers:
[21, 1, 26, 45, 29, 28, 2, 9, 16, 49, 39, 27, 43, 34,
46, 40] which answer illustrates the list to be
sorted after 3 recursive calls to merge sort?
Question 2
Given the following list of numbers:
[21, 1, 26, 45, 29, 28, 2, 9, 16, 49, 39, 27, 43, 34,
46, 40] which answer illustrates the first two lists
to be merged?
Optimisation Algorithms
Djikstras Algorithm
Djikstras pseudocode (shortest path)
Interactive Example

A good demonstration example for the


classroom
http://optlab-server.sce.carleton.ca/POAnimations2007/DijkstrasAlgo.html
Solution
Applications of Djikstras Shortest Path
Algorithm
Source: Interactive Python
Insertion Sort O(n^2)
1) We begin by assuming that a list with one item
(position 0 ) is already sorted.
2) On each pass, one for each item 1 through n1 ,
the current item is checked against those in the
already sorted sub-list.
3) As we look back into the already sorted sub-list,
we shift those items that are greater to the
right. When we reach a smaller item or the end
of the sub-list, the current item can be inserted.
Insertion Sort - Visualisation
Insertion Sort
WikiDemo
Suppose A is an array of N values.
We want to sort A in ascending
order.
Insertion Sort is an algorithm to
do this as follows: We traverse
the array and insert each element
into the sorted part of the list
where it belongs. This usually
involves pushing down the larger
elements in the sorted part.
Insertion Sort does roughly N**2
/ 2 comparisons and does up to N
- 1 swaps.
Insertion Sort Python code
Question
Suppose you have the following list of numbers
to sort:
[15, 5, 4, 18, 12, 19, 14, 10, 8, 20]
which list represents the partially sorted list
after three complete passes of insertion sort?
Question
Consider the array: [8 6 4 2]

Write out the number of passes for a


complete Insertion Sort
Selection Sort
The selection sort improves on the bubble sort by
making only one exchange for every pass through the
list.
In order to do this, a selection sort looks for the largest
value as it makes a pass and, after completing the pass,
places it in the proper location.
As with a bubble sort, after the first pass, the largest
item is in the correct place.
After the second pass, the next largest is in place. This
process continues and requires n1 passes to sort n
items, since the final item must be in place after the
(n1) st pass.
Selection Sort
Selection Sort
Selection Sort Pseudocode
Suppose A is an array of N values. We
want to sort A in ascending order. That
is, A[1] should be the smallest and A[N]
should be the largest.

The idea of Selection Sort is that we


repeatedly find the smallest element in
the unsorted part of the array and swap
it with the first element in the unsorted
part of the array.

An alternate way to sort in ascending


order is to find the largest value and
swap with the last element in the
unsorted part of the array.
Selection Sort does roughly N**2 / 2
comparisons and does N swaps.
Question
Consider the array: [8 6 4 2]
Write out the passes for Selection Sort
Solution Selection Sort
Bibliography
http://www.cse.unsw.edu.au/~billw/Justsearch.html
http://www.python-course.eu/lambda.php
www.interactive-python.org
https://en.wikibooks.org/wiki/A-
level_Computing/AQA/Problem_Solving,_Programming,_Operating_Systems,_Dat
abases_and_Networking/Programming_Concepts/Insertion_sort

You might also like