lecture notes comp3506
DESCRIPTION
Lecture Notes COMP3506 data structures and algorithmsTRANSCRIPT
Lecture 2: Arrays, Linked Lists, and Recursion31/7/2014
Topic: Stack and heap memory
ArrayContiguous 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 ListPros: 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 Easy to insert new node before a certain node
Recursion
Linear recursion Tail recursion Binary recursion Multiple recursion
Lecture 3: Abstract Data Types31/7/2014
Model of data structure that specifies: Type of data stored Operations supported and types of parameters Specifies what each operation does, but not how In Java: interface Factory Pattern can be used to allow different ADT implementations to be instantiated at runtime
Lecture 4 Counting Primitive Operations4/8/2014
Primitive Operations Assumed to take constant time
Big-O if there are positive constants such that for Describes an upper bound
Big- (Big Omega) Describes a lower bound
Big- (Big Theta) Describes a tight bound is asymptotically equal to
Lecture 5: Asymptotic Analysis7/8/2014
Lecture 6:7/8/2014Lecture 7: The Stack ADT and Array-based Stacks11/8/2014
The stack ADT: Insertion/deletion: LIFO Main operations: push(object) pop() Auxiliary operations: top() size() isEmpty() For empty stack, pop and top return null
Java Virtual Machine (JVM) Keeps track of chain of active methods with a stack
Performance and limitations Let n be no. of elements, N >= n fixed sized of array Space: O(N) Time: O(1) Trying to push element into full stack exception
Note: Using linked list instead would give space O(n)
Lecture 8: The Queue ADT and Array-based Queues14/8/2014
Queue ADT: Insertion/deletion: FIFO Main operations: enqueue(object) dequeue() Auxiliary operations: first() size() isEmpty()
Array-based queue Array size N Two vars keep track of front and size Array location is the first empty slot past the rear of the queue
List and Iterator ADTs and Amortisation
Growable Array-based array list: push(o) adds o to the end of list When array is full, replace array with a larger one Incremental strategy: Increase size by constant Doubling strategy: double the size Worst time complexity: O(n). Best: O(1)
Amortized time of a push operation is the average time taken by push: T(n)/nTurns out doubling strategy is better
Positional list ADT: Accessor first() last() before(p) after(p) Update addFirst(e) addLast(e) addBefore(p,e) addAfter(p,e) set(p,e) remove(p) Natural implementation: doubly-linked list
Iterator next() hasNext()
Lecture 9: Trees18/8/2014
Tree: Hierarchical structure of nodes with parent-child relation
Traversal Preorder traversal A node is visited before its descendants
Postorder traversal A node is visited after its descendants
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 A node is visited after its left subtree and before its right subtree
Lecture 10: Trees continued22/8/2014
Evaluate Arithmetic Expressions
Euler Tour Traversal Preorder, postorder, inorder traversals are special cases of this Walk around tree and visit each node three times
Linked Structure for Trees Node stores: Element Parent node Sequence of children nodes For binary tree, node stores: Element Parent node Left child node Right child node Node implement position ADT
Array-Based Binary Tree Node v is stored at A[rank(v)] rank(root) = 0 Left child: rank(node) = 2 * rank(parent(node)) + 1 Right child: rank(node) = 2 * rank(parent(node)) + 2 Worst case space: , all nodes on right side 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: insert(k, v) removeMin(): removes and returns entry with smallest key, or null if empty Additional methods: min() size() isEmpty()
Ordering Comparability: Antisymmetric property: Transitive property:
Entry ADTMethods: getKey() getValue()
Comparator ADTCompare(x,y) returns i such that:
Sequence-based priority queue Unsorted: Insert: O(1) removeMin and min: O(n) Sorted: Insert: O(n) removeMin and min: O(1)
Selection-Sort PQ-sort function implemented with unsorted sequence Running time n insert operations takes O(n) Runs in time
Insertion-Sort PQ-sort function implemented with sorted sequence Runs in time
In-place insertion-sort Use swaps
Lecture 11: Heap25/8/2014
Heap A binary tree storing keys at nodes and satisfying properties: Heap-Order: for every internal node other than root, Complete Binary Tree: Fill top to bottom, left to right. nodes at depth The last node of a heap is the rightmost node of maximum depth Height of heap: 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: If previous left node is left child, go to right child. Otherwise: Go up from previous last node until left child or root If left child is reached, to the right child Go down left until a leaf is reached If root is reached, go left until new row Have to traverse height twice at most, so
Heap-Sort Space: Insert and removeMin: Size, isEmpty, min: Heap-sort:
Array-based heap implementation Can represent heap with n keys by array of length n For node at rank I, left child 2i + 1, right child 2i + 2
Merging two heaps Merge two heaps and key k by storing k as root with the two heaps as subtrees, then perform downheap
Bottom-up heap construction Constructs a heap from n given keys in time
Lecture 12: Adaptable Priority Queue29/08/2014
Adaptable Priority Queue ADT: replaceKey(e, k) - returns previous key of entry e replaceValue(e, v)- returns previous value
Location-aware list Each entry stores: key value position (or rank)
Maps and Hashtables
Map Searchable collection of key-value entries Multiple entries with the same key are not allowed
Map ADT get(k) put(k,v) remove(k) size(), isEmpty() entrySet() keyset() values()
Hash function Usually specified as composition of hash code and compression:
To minimise collisions when combining sequence of components, use polynomial accumulation, e.g.
Polynomials can be evaluated in time with Horners rule
Lecture 131/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 Place colliding item in next (circularly) available table cell Future collisions longer sequence of probes (inspections) remove(k): if entry (k,o) is found replace with DEFUNCT and return element o, else return null Double hashing Have secondary hash function d(k) If collision, place in first available cell at
Performance of hashing Worst case search/insert/remove on hash table is (i.e. everything hashes to same spot) Load factor: Assuming hash values are like random numbers, expected number of probes for an insertion with open addressing is Settle for average In practice hashing is very fast provided load factor is not close to 100%
Set, Multiset, Multimap, Skip Lists10/9/2014SetSet ADT addAll(T): retainAll(T): removeAll(T):
Implementations Sorted list Generic merging: Runs in provided auxiliary methods (>,