COMP90038
Algorithms and Complexity
Lecture 2: Review of Basic Concepts (with thanks to Harald Søndergaard)
DMD 8.17 (Level 8, Doug McDonell Bldg)
http://people.eng.unimelb.edu.au/tobym @tobycmurray
2
Copyright University of Melbourne 2016, provided under Creative Commons Attribution License
Approaching a problem
Can we cover this board with 31
•
tiles of the following form?
This is the mutilated
•
checkerboard problem.
•
There are only finitely many ways we can arrange the 31 tiles, so there is a brute-force (and very inefficient) way of solving the problem.
3
Copyright University of Melbourne 2016, provided under Creative Commons Attribution License
Transform and Conquer? Use abstraction?
Can we cover this board with
•
31 tiles of the form shown?
Why can we quickly determine
•
that the answer is no?
Hint: Using the way the
•
squares are coloured helps.
4
Copyright University of Melbourne 2016, provided under Creative Commons Attribution License
Algorithms and Data Structures
Algorithms: for solving problems, transforming data.
•
•
Which data structures are you familiar with?
Data structures: for storing data; arranging data in a way that suits an
•
algorithm.
• • •
Linear data structures: stacks and queues Trees and graphs
Dictionaries
Primitive Data Structures: The Array
An array corresponds to a sequence of consecutive cells in memory. Depending on programming language: A[0] up to A[n-1], or A[1]
up to A[n].
Locating a cell, and storing or retrieving data at that cell is very fast.
• •
•
The downside of an array is that maintaining a contiguous bank of cells with
•
information can be difficult and time-consuming.
5
Copyright University of Melbourne 2016, provided under Creative Commons Attribution License
How many bytes does each integer occupy here?
Answer: 2 (16-bit integers)
Primitive Data Structures: The Linked List
An array X: A linked list
X:
Suppose variable X holds the address 42160, then the list could look like this in memory:
null
2
3
5
7
2
3
5
7
6
Copyright University of Melbourne 2016, provided under Creative Commons Attribution License
Terminology
node
pointer
(in Java: “reference”)
X:
X is (a pointer to) the head node of the list
2
2
3
5
7
2
Y: “Y.val” refers to
“Y.next” refers to
7
Copyright University of Melbourne 2016, provided under Creative Commons Attribution License
8
Copyright University of Melbourne 2016, provided under Creative Commons Attribution License
Linked List
• •
Inserting and deleting elements is very fast: just move a few links around. Finding the ith element can be time-consuming.
Often we use a dummy head node that points to the first object, or to a
•
special null object that represents an empty list. This makes it easier to write functions that insert or delete elements.
Iterative Processing: Array
• •
Walk through the array (of length n) For example, to locate item x.
function find(A,x,n) j←0
while j < n if A[j] = x
return j j ← j+1
return -1
Y:
Let’s trace the execution of find(Y,7,7)
(returns 4)
A: Y A[j]
x: 7
n: 7
j: 02341
9
Copyright University of Melbourne 2016, provided under Creative Commons Attribution License
Iterative Processing: List
• •
Walk through a linked list.
For example, to locate item x.
function find(head,x) p ← head
while p ≠ null if p.val = x
return p p ← p.next
return null head:
(note similarity to array version)
def find(p, value):
while p != None:
if p.val == value:
return p
p = p.next
return None
p:
2
3
5
7
10
Copyright University of Melbourne 2016, provided under Creative Commons Attribution License
11
Recursive Processing: Array
•
Solve the problem for a sub-instance and use the solution to solve the full
•
instance
For example, to locate item x.
function find(A,x,lo,hi) if lo > hi
return -1 else if A[lo] = x
return lo else
A: Y A[lo]
x: 7
lo: 32401
hi: 6
Let’s trace the execution of find(Y,7,0,6)
Initial call: find(A,x,0,n-1)
Copyright University of Melbourne 2016, provided under Creative Commons Attribution License
(returns 4)
A[hi]
Y: return find(A,x,lo+1,hi)
Recursive Processing: List
Solve the problem for a sub-instance and use the solution to solve the full (note similarity to array version)
•
instance
function find(p,x) if p = null
return p else if p.val = x
return p else
return find(p.next,x) p: Initial call: find(head,x)
function find(A,x,lo,hi) if lo > hi
return -1 else if A[lo] = x
return lo else
return find(A,x,lo+1,hi)
2
3
5
7
head:
12 Copyright University of Melbourne 2016, provided under Creative Commons Attribution License
We will
discuss recursion properly in Week 3
13
Copyright University of Melbourne 2016, provided under Creative Commons Attribution License
Abstract DataTypes
A collection of data items, and a family of operations that operate on that
Think of an ADT as a set of contracts, an interface
We must still implement these promises, but it is an advantage to separate the implementation of the ADT from the “concept” (i.e. the interface it provides)
Good programming practice is to support this separation
•
data
•
•
•
Nothing outside of the definition of the ADT should refer to anything
•
inside, except through function calls and basic operations
14
Copyright University of Melbourne 2016, provided under Creative Commons Attribution License
Fundamental Data Structure: The Stack
Last-In-First-Out (LIFO) Operations:
CreateStack Push
Pop
Top EmptyStack? …
•
•
•
• • • • •
•
Usually implemented as an ADT
Stack Implementation: Array
top: 65 Push(5)
15 Copyright University of Melbourne 2016, provided under Creative Commons Attribution License
Stack Implementation: Linked List
st:
elt: Push(5)
function push(st,x) elt ← new node elt.val ← x elt.next ← st
st ← elt return st
2
3
5
7
5
See https://www.cs.usfca.edu/~galles/visualization/Algorithms.html
for more visualisations
16
Copyright University of Melbourne 2016, provided under Creative Commons Attribution License
17
Copyright University of Melbourne 2016, provided under Creative Commons Attribution License
Pseudo Code
•
There is no standard for pseudo-code. Use the examples in Levitin as a guide. Cormen et al. pages 20–22 (in Reading Resources) has a list of standard conventions used with pseudo-code which are good to follow, except we use ← as the assignment operator.
On the previous slide, we assumed that a “node” has two attributes: a “val”
•
which is its value, and a “next” which points to the rest of the list.
Fundamental Data Structure: Queues
First-In-First-Out (FIFO) Operations:
CreateQueue Enqueue Dequeue Head EmptyQueue? …
•
•
•
• • • • •
18 Copyright University of Melbourne 2016, provided under Creative Commons Attribution License
19
Copyright University of Melbourne 2016, provided under Creative Commons Attribution License
Other Data Structures
•
•
• •
We will meet many other (abstract) data structures, e.g. The priority queue
Various types of “tree” Various types of “graph”
If you check out algorithm animation tools or advanced algorithm books, you
•
will meet exotic data structures such as splay trees and skip lists.
20
Copyright University of Melbourne 2016, provided under Creative Commons Attribution License
Next Week
Algorithm analysis—how to reason about an algorithm’s resource
•
consumption.