Professional Documents
Culture Documents
5/12/16
Learning Outcomes
Object
Oriented
Programmi
ng
Concepts
Recursion
Linked
Lists, Trees
and Graphs
5/12/16
Algorithms
and their
Complexity
Sorting
Algorithms
Object Oriented
Programming Concepts
5/12/16
Object Oriented
Programming Concepts
5/12/16
Object Oriented
Programming Concepts
Main concepts
Encapsulatio
n
Inheritance
Polymorphis
m
Abstraction
5/12/16
Object Oriented
Programming Concepts
Inheritance
5/12/16
Object Oriented
Programming Concepts
Object Oriented
Programming Concepts
Object Oriented
Programming Concepts
Object Oriented
Programming Concepts
Abstract class
Object Oriented
Programming Concepts
Abstract class
abstract class GraphicObject
{
int x, y;
void moveTo(int newX, int newY) { ... }
abstract void draw();
abstract void resize();
}
5/12/16
11
Object Oriented
Programming Concepts
Abstract class
class Circle extends GraphicObject
{
void draw() { ... }
void resize() { ... }
}
class Rectangle extends GraphicObject
{
void draw() { ... }
void resize() { ... }
}
5/12/16
12
Object Oriented
Programming Concepts
Encapsulation
Encapsulation in Java is a mechanism of wrapping the
data (variables) and code acting on the data (methods)
together as a single unit.
In encapsulation the variables of a class will be hidden
from other classes, and can be accessed only through
the methods of their current class, therefore it is also
known as data hiding.
To achieve encapsulation in Java
1. Declare the variables of a class as private.
2. Provide public setter and getter methods to modify
and view the variables values.
5/12/16
13
Object Oriented
Programming Concepts
Object Oriented
Programming Concepts
5/12/16
15
Object Oriented
Programming Concepts
Polymorphism
16
Object Oriented
Programming Concepts
17
5/12/16
18
Almost anything.
We use the ideas ofTuring Completenessand
Turing equivalencyto describe computers, processors an
programming languages (all the same thing from a certain
abstract view)
To be equivalent to all other machines, we need:
1. Conditional branching.
2. Modifiable state.
5/12/16
19
Modifiable state:
1. Variables or just plain memory access.
2. Or a disk.
3. Or any other way to hold data.
A program can be described as a series of changes to state
The data of a program can be referred to as its "state space
If we can't remember any numbers or change some output
we can't do much.
5/12/16
20
21
1. Assignment
Pseudo-code uses for assignment instead of =
Sometimes written as < = is used for comparison instead of ==
Multiple assignment is achieved with:ab0
(a and b are variables)
Arrays in Pseudo-code sometimes start at 1, unlike m
programming languages which start at 0.
5/12/16
22
https://www.youtube.com/watch?
v=6hfOvs8pY1k
5/12/16
24
5/12/16
25
5/12/16
26
5/12/16
27
5/12/16
28
29
5/12/16
30
5/12/16
(n times)
(n times)
(n times)
(n*n times)
(n*n times)
(n*n times)
(n times)
32
5/12/16
33
34
35
Activity 1
5/12/16
36
Activity 1 Solution
5/12/16
, , [, ] ,
37
Activity 2
5/12/16
38
Activity 2 Solution
B
C
D
E
5/12/16
39
Recursion
What is recursion?
5/12/16
40
Recursion
Example
Factorial:
FACTORIAL(n)
if(n < 2)
return 1
else
return FACTORIAL(n-1) * n
The recursion stops when n is 1.
5/12/16
41
Recursion
Example
5/12/16
42
Recursion
Another example
Binary search:
http://www.cs.armstrong.edu/liang/animation/web/BinaryS
earch.html
BINARY_SEARCH(A, v, first, last)
if last < first
return false
i first + (last - first)/2
if A[i] == v
//Base case 1
//Base case
2
return true
if A[i] > v
BINARY_SEARCH(A,v,first,i-1)
//Recursion
case 1
5/12/16
else
43
Recursion
Another example
Towers of Hanoi:
http://www.dynamicdrive.com/dynamicindex12/towerh
anoi.htm
5/12/16
44
Sorting Algorithms
Introduction
Sorting data is a fundamental problem in software
development
and also a core topic in computer science.
If we consider that the "information age" is all about
data,
and we have huge amounts of it then dealing with
the data
efficiently is critical.
Today, we will be looking at a selection of sorting
algorithms.
5/12/16
45
Sorting Algorithms
Bogosort
5/12/16
46
Sorting Algorithms
Bogosort
5/12/16
47
Sorting Algorithms
Insertion sort
Pick the first item.
Keep swapping it with its left neighbour until the
swap would put it to the left of a number smaller
than it.
Pick the next number along.
Repeat.
5/12/16
48
Sorting Algorithms
Insertion sort
5/12/16
49
Sorting Algorithms
Insertion sort
INSERTION-SORT(A)
for i 1 to length[A]
key A[i]
ji
while j > 0 and A[j-1] > key
A[j] A[j-1]
jj-1
A[j] key
Best case performance: O()
Worst case performance: O()
Average case performance: O()
5/12/16
50
Sorting Algorithms
Bubble sort
Examine each consecutive pair of items.
If they are out of order, swap them.
Repeat until no swaps are made.
5/12/16
51
Sorting Algorithms
Bubble sort
5/12/16
52
Sorting Algorithms
Bubble sort
BUBBLE(A)
swapTrue
while swap is True:
swapFalse
for i 0 to length(A)-1
if A[i] > A[i+1]
swapTrue
swap(A[i],A[i+1])
Best case performance: O()
Worst case performance: O()
Average case performance: O()
5/12/16
53
Sorting Algorithms
Merge sort
Divide the unsorted list into nsublists, each
containing 1 element (a list of 1 element is
considered sorted).
Repeatedlymergesublists to produce new sorted
sublists until there is only 1 sublist remaining.
This final list will be the sorted list.
5/12/16
54
Sorting Algorithms
Merge sort
5/12/16
55
Sorting Algorithms
Merge sort
MERGE-SORT(l):
if length(l) <= 1
return l
al[0 .. length(l)/2]
bl[length(l)/2 .. length(l)]
aMERGE-SORT(a)
bMERGE-SORT(b)
return MERGE(a,b)
5/12/16
56
Sorting Algorithms
Merge sort
MERGE(a,b)
out array of length length(a)+length(b)
i0
j0
for x 0 length(out)
if a[i] < b[j]
out[x] = a[i]
i++
else
out[x] = b[j]
j++
5/12/16
57
Sorting Algorithms
Merge sort
58
Sorting Algorithms
Merge sort
5/12/16
59
Sorting Algorithms
Quick sort
Pick a "pivot.
Create two empty lists, one for all items less than
the pivot, one for all greater.
For each item in the original list, move it to the
correct new list.
Now sort each half (by applying this all over again).
5/12/16
60
Sorting Algorithms
Quick sort
5/12/16
61
Sorting Algorithms
Quick sort
function QUICKSORT(A)
if length(A)<=1
return A
pivot A[?]
L []
G []
for each x in array
if x pivot
append x to L
else
append x to G
return concatenate(quicksort(L), A[pivot],quicksort(
5/12/16
62
Sorting Algorithms
Quick sort
Best option: pick the value int he middle of the
range.
But how? If we have to hunt through to be sure, it
can slow things down a lot.
So pick the first or last item? If the data isn't sorted,
it doesn't matter, so this would be fine.
Except: what if itissorted? If it's sorted, then we
end up withO(n2)performance.
Take the one in the middle? Better, but could be
given data made specifically to slow it down.
Better: take a random sample, making it
nondeterministic.
Better
5/12/16
63
still: take the median of three random items.
Sorting Algorithms
Conclusions
64
Sorting Algorithms
Comparison
5/12/16
65
Sorting Algorithms
Comparison
5/12/16
66
Sorting Algorithms
Quick sort
5/12/16
67
Sorting Algorithms
Bubble sort
5/12/16
68
Sorting Algorithms
Merge sort
5/12/16
69
Sorting Algorithms
Insertion sort
5/12/16
70
Activity 3
Examine the pseudocode below and describe the
effect it has on the given input sequence.
REARRANGE(A)
n <- length(A)
for j <- 0 to n
for i <- 0 to n-1
if (A[i] < A[i+1])
swap (A[i], A[i+1])
5/12/16
71
Activity 3 Solution
5/12/16
72
Activity 4
Use pseudocode to describe an algorithm that finds
the smallest number in a sequence.
Write a function in the programming language of your
choice that takes two sequences of numbers as
parameters and returns a sequence that contains the
non-unique numbers from each input. That is, those
that are in the first and the second lists. There should
be no duplicates in the output. For example, given the
sequences [1, 2, 3, 4, 5] and [4, 5, 6, 7], the answer is
[4, 5].
5/12/16
73
Activity 4 Solution
FIND SMALLEST(seq)
if size(seq)<1
raise error
smallest <- seq[0]
for all numbers i in seq
if i<smallest
smallest <- I
return smallest
5/12/16
74
Activity 4 Solution
def non_unique(a,b):
result = []
for i in a:
if i in b and not i in
result:
result.append(i)
return result
5/12/16
75
5/12/16
76
5/12/16
77
79
5/12/16
80
5/12/16
81
5/12/16
82
5/12/16
83
5/12/16
84
5/12/16
85
86
87
5/12/16
88
89
5/12/16
90
5/12/16
BIN-TREE-INSERT(tree,item)
IF tree =
tree=Node(item)
ELSE
IF tree.value > target
IF tree.left 0
tree.left=Node(item)
ELSE
BIN-TREEINSERT(tree.left,item)
ELSE
IF tree.right 0
tree.right=Node(item)
ELSE
BIN-TREE-
91
92
94
Preorder
10, 8, 5, 9, 14, 12, 11, 13, 17
Postorder
5, 9, 8, 11, 13, 12, 17, 14, 10
Breadth first
10, 8, 14, 5, 9, 12, 17, 11, 13
In order
5, 8, 9, 10, 11, 12, 13, 14, 17
5/12/16
95
5/12/16
96
5/12/16
97
Activity 4
5/12/16
98
5/12/16
99
5/12/16
101
102
103
5/12/16
104
5/12/16
105
5/12/16
106
5/12/16
107
5/12/16
108
5/12/16
109
110
5/12/16
111
5/12/16
112
5/12/16
113
5/12/16
114
5/12/16
115
.
shttps://www.cs.usfca.edu/~galles/visualization/DFS.htm
http://www.comp.nus.edu.sg/~stevenha/visualization/df
5/12/16
116
5/12/16
117
5/12/16
118
5/12/16
119
.
http://www.comp.nus.edu.sg/~stevenha/visualization/df
https://www.cs.usfca.edu/~galles/visualization/BFS.htm
http://joseph-harrington.com/2012/02/breadth-first-sear
5/12/16
120
5/12/16
121
Activity 5
Describe how a breadth first search (BFS) is performed
on a graph and state the order in which the nodes of
the graph below will be visited, beginning with A.
Assume that visited nodes will be remembered and not
visited again, and that left-hand edges are selected
first.
5/12/16
122
Activity 5 Solution
ABCEFGDHIJ
5/12/16
123
Discussion
5/12/16
124