Professional Documents
Culture Documents
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
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:
>>> abs(5)
5
>>> abs(-5)
5
Division into sub-tasks
Rule 1: using functions helps in the top down design of
programs.
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
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.
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:
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
0156423 0123456
Bubble Sort Algorithm