Coursework 02 Revision
Recurrence Relations
- Is a recursively-defined function. Applied to the case when the function is some measure of resources.
- The runtime of a program is , then a recurrence relation will express , then a recurrence relation will express in terms of its values at other (smaller) values of .
- Solving Recurrence:
- Starting from the base case, use the recurrence to work out many cases, by directly substituting and working upwards in values of n.
- Inspect the result, look for a pattern and make a hypothesis for the general results
- Attempt to prove the hypothesis - typically using some form of induction
- Often then extract the large n behaviour using big-Oh family. Can be long, tedious, and error-prone, but many cases are covered by a general rule with the name of "Master Theorem"
Master Theorem
-> Results are split into 3 cases, according to comparing the growth rate of to
- Case 1: grows slowly. Recurrence term dominates. Solution ignores
- is with
- is
- Case 2: grows same - up to log factors - "mix of recurrence with a,b, and also the term"
- is with and
- is
- Case 3: grows faster. "Solution ignores recurrence terms and a,b"
- is with
- satisfies the regularity condition
- ) for some
Vectors and amortised complexity
- Vector - Abstract Data Type, ADT, corresponding to generalising the notion of the Array. Stores a sequence of arbitrary objects
- Rank - Number of elements preceding it (index)
- Application - No automatic limit on the storage size, direct applications (sorted collection of objects), indirect applications (auxiliary data structure)
- Operation
elemAtRank(r)
- - Insertion -
insertAtRank(r,o)
, need to make room for the new element by shifting forward then-r
elements. Worst case takes - Deletion -
removeAtRank(r)
, need to fill the hole left by the removed element by shifting backward the elements. Worst case takes - Performance - In the array-based implementation of a Vector
- Space used by the data structure is
size
,isEmpty
,elemAtRank
andreplaceAtRank
run ininsertAtRank
andremoveAtRank
run in timepush
runs in time, as do not need to move elementspop
runs in time
- Growable Array-based Vector - In a push operation, when the array is full, instead of throwing an exception, can replace the array with a larger one. Has a high cost of .
- Incremental Strategy - Increase size by a constant c
- Doubling Strategy - Double the size
- Amortize - Time of a push operation the average time taken by a push over the series of operations
Why is amortised analysis different from the average case analysis
Amortised - (long) real sequence of dependent operations Average - Set of (possibly independent) operations
ADTs/CDTs Stacks/Queues using linked lists
ADT - Only concerned with specifying the interface. Behaviour as seen from the outside. Specifies the methods provided - and possibly requirements on their complexity (Interface) CDT - Some structure and algorithm used for an actual implementation. Behaviour as seen from the outside (Implementation)
Performance and Limitation of Array-based Stack
Performance
- Let be the number of elements in the stack
- The space used is
- Each operation runs in time
- Max size of the array-based stack must be defined in advance and cannot be changed dynamically
- Trying to push a new element into a full stack causes an implementation-specific exception
Narrow - Small set of methods. Stack ADT, less flexible to use, more flexible to implement, maybe more efficient Wide - Large set of methods. Java Stack, more flexible to use, possibly more difficult to implement efficiently
Linked List vs. Array based CDTs
Array
- Con - Fixed size - might need a lot of unused space
- Pro - Contiguous in memory. Localised memory access, gives better use of the (CPU) cache - e.g. machine level "pre-fetch" will tend to do the right thing Linked List
- Pro - size grows and shrinks automatically
- Con - Can be scattered around memory, and give poor usage of the cache
- Con - Storage of the next references doubles the space usage
Priority Queues and Heaps
Can use a heap to implement a priority. We store a (key, element) item at each node. We keep track of the position of the last node.
Implementing PQ with a Heap
- Initialise a heap
- Insert in the head
- Ask for the value of the root of the heap (minimal key)
- Remove the root ad return the value stored there (dequeue the highest priority)
Heap-sort
- Consider a priority queue with items implemented by means of a heap
- Space used is
- methods
insert
andremoveMin
take time - methods
size
,isEmpty
, andmin
take time
- Using a heap based priority queue, can sort a sequence of elements in time