Lecture Notes COMP3506
Lecture Notes COMP3506
31/7/2014
Topic: Stack and heap memory
Array
Contiguous uniform storage
Pros:
Constant time access to indexed memory location: O(1)
Memory efficient
Cons:
Resizing requires copying to new array
Worst case for sorted insert is must shuffle entire array
Linked List
Pros:
Insertion: O(1)
Cons:
Does not allow access via index must traverse entire list: O(n)
Uses more memory
Types:
Single linked list
Double linked list
o Easy to insert new node before a certain node
Recursion
Linear recursion
Tail recursion
Binary recursion
Multiple recursion
In Java: interface
Factory Pattern can be used to allow different ADT implementations to be
instantiated at runtime
f ( n ) is O ( g ( n ) )
f ( n ) cg ( n )
(Big Omega)
Big-
n n0
cg ( n ) f ( n)
for
cn 0 such that
Big-
(Big Theta)
c 1 g ( n ) f ( n ) c2 g(n)
Describes a tight bound
g(n)
Lecture 6:
7/8/2014
Insertion/deletion: LIFO
Main operations:
o push(object)
o pop()
Auxiliary operations:
o top()
o size()
o isEmpty()
For empty stack, pop and top return null
Array location
queue
Lecture 9: Trees
18/8/2014
Tree: Hierarchical structure of nodes with parent-child relation
Traversal
Preorder traversal
o A node is visited before its descendants
Postorder traversal
Binary tree
Each node has at most two children: left child and right child
Proper binary tree: each node has exactly zero or two children
Inorder traversal
o A node is visited after its left subtree and before its right subtree
Node stores:
o Element
o Parent node
o Sequence of children nodes
For binary tree, node stores:
o Element
o Parent node
o Left child node
o Right child node
Node implement position ADT
May be ok to use if every row is filled. Saves a little space by not needing
links
Priority Queue
Priority Queue ADT
Entry: pair(key, value)
Main methods:
o insert(k, v)
o removeMin(): removes and returns entry with smallest key, or null if
empty
Additional methods:
o min()
o size()
o isEmpty()
Ordering
x y y x
Comparability:
Antisymmetric property:
Transitive property:
Entry ADT
Methods:
getKey()
getValue()
x y y x x= y
x y y z x y
Comparator ADT
Compare(x,y) returns i such that:
i< 0 if a<b
i=0 if a=b
i> 0 if a>b
Runs in
O ( n2 )
time
Insertion-Sort
PQ-sort function implemented with sorted sequence
Runs in
O(n2 ) time
In-place insertion-sort
Use swaps
2i
nodes at
Height of heap:
O ( log n )
Insertion: Insert after last node, then restore heap-order by swapping with
parent
Removal: Replace root with last node, then set new last node. Restore
heap-order (downheap)
Updating the last node:
o If previous left node is left child, go to right child. Otherwise:
o Go up from previous last node until left child or root
o If left child is reached, to the right child
o Go down left until a leaf is reached
o If root is reached, go left until new row
o
n
log
O
Heap-Sort
O ( n)
Space:
Heap-sort:
O ( log n )
O ( 1)
O ( n log n )
O(n)
time
Location-aware list
o
o
o
key
value
position (or rank)
ADT
get(k)
put(k,v)
remove(k)
size(), isEmpty()
entrySet()
keyset()
values()
Hash function
Usually specified as composition of hash code and compression:
O(n)
Lecture 13
1/9/2014
Collision handling
Separate chaining: let each cell in table point to linked list of entries
Open addressing: place colliding item in different cell
Linear probing
o Place colliding item in next (circularly) available table cell
o Future collisions longer sequence of probes (inspections)
o remove(k): if entry (k,o) is found replace with DEFUNCT and return
element o, else return null
Double hashing
o Have secondary hash function d(k)
( h ( k ) + jd ( k ) ) mod N , j=1,2,3,
Performance of hashing
O(n)
(i.e.
=n /N
Load factor:
1/(1 )
O(1)
In practice hashing is very fast provided load factor is not close to 100%
Set
Set ADT
S T
addAll(T):
retainAll(T):
removeAll(T):
S T
ST
Implementations
Sorted list
o
O ( n A +n B )
O ( 1)
Generic Merging
Runs in
O ( n A +n B )
Multimap
Similar to map, but can have multiple entries with same key
Skip Lists
O(1)
A series of lists
Sh
S0 , S1 , , Sh
Implementation
Quad-node: stores entry, link to nodes prev, next, below, above
Define special keys PLUS_INF, MINUS_INF
Space Usage
h
2ni n 21i 2 n O ( n )
i=0
i=0
Height
log n
O )
Time
Search time proportional to number of drop-downs + number of scanforwards.
O(log n)
O(log n)
(with high
probability)
Binary search
O(log n)
Search tables
Ordered map implemented by sorted sequence
O ( log n )
Insert/remove:
Good for small maps or when search is the most common operation
O ( n)
key ( u ) v key ( w )
Deletion
Performance
For ordered map with
Space used is
O ( n)
Height is
O(n)
O ( h ) time
O(log n)
in best
case
Search Tree
8/9/2014
AVL Tree
Binary search tree with height balance: for every internal node v, heights
of children of v can differ by at most 1
n
log
O
Search Trees
11/9/2014
keys is
(2, 4) tree
A multi-way search tree with:
o Node-Size property: Every internal node has at most four children
o Depth property: All external nodes have the same depth
h log ( n+1 )
search takes
O(log n)
Splay tree
18/10/2014
O(n)
O ( log n )
Splaying
Move a node to root using rotation
Performed after all operations
See slides for
o Which node to splay
o Zig/zag operations. Note: easier to understand with actual numbers
for nodes
Splaying costs
o O(h) rotations, each O(1). Still O(n) worst-case
o Amortised cost is O(log n)
Time T(n) needed to perform a series of n splay operations,
divided by n, i.e. T(n)/n
Red-Black Trees
18/9/2014
o
o
Height
Height O(log n)
o Proof: height is at most twice the height of associated (2,4) tree,
which is O(log n)
Search works the same way as for binary search tree. O(log n)
Insertion
Colour the newly inserted node red, unless root.
If there is double red,
Divide: partition
into
S1
each.
Conquer: merge
S 1 and
S2
and
S1
S2
and
S2
Quick-Sort
Expected height is O(log n), work done at nodes of same depth is O(n)
Expected running time of quick-sort is O(n log n)
O(n )
In-place quick-sort
See slides
Shortest Paths
13/10/2014
Dijkstras Algorithm
Solves single-source shortest path problem
Similar to uniform cost search, but no goal vertex
Used on weighted graph with non-negative edge weights
Edge relaxation:
min { d ( z ) , d (u )+ weight ( e ) } d ( z )
Trie: A compact data structure for representing a set of strings, e.g. all the
words in a text
Supports pattern matching queries in time proportional to the pattern size
Standard trie: ordered tree with each node labelled with a character.
Children alphabetically ordered
Compressed trie: Every internal node is at least degree 2. Obtained from
standard trie by compressing chains of redundant nodes.
See slides for time complexity
Compact representation
Compact representation of compressed trie, store ranges of indices at
nodes instead of strings.
Suffix Trie