Download presentation
Presentation is loading. Please wait.
1
Data Structures and Algorithms
PLSD210 Key Points
2
Lectures 1 & 2 Data Structures and Algorithms
The key to your professional reputation A much more dramatic effect can be made on the performance of a program by changing to a better algorithm than by hacking converting to assembler Buy a text for long-term reference! Professional software engineers have algorithms text(s) on their shelves Hackers have user manuals for the latest software package Which they’ll have to throw away next year anyway!
3
Lecture 2 Most algorithms operate on data collections, so define
Collection Abstract Data Type (ADT) Methods Constructor / Destructor Add / Delete Find Sort (later) ….
4
Lecture 2 Array implementation Add method Find method
Specify maximum size in Constructor Allocate array with sufficient space Add method Takes constant time Independent of current size of collection Find method Linear search Worst case time: n x constant Average time: n/2 x constant (if the key is always found!) è n x constant (depending on probability of finding the key) Worst case analysis preferred - easier and more relevant!
5
Searching Find method n < Binary search
Sort the data into ascending (or descending) order Check the middle item If desired key is less, go left If match, finished! If desired key is greater, go right You can divide n in half: log2n times Therefore binary search takes time = c log2n In “big Oh”, this is O( log n ) n n 8 n 4 n 2 <
6
Binary Search vs Sequential Search
Find method Sequential Worst case time: c1 n Binary search Worst case time: c2 log2n Small problems - we’re not interested! n Large problems - we’re interested in this gap! 4log2n Binary search More complex Higher constant factor
7
Binary Search vs Sequential Search
Logs Base 2 is by far the most common in this course. Assume base 2 unless otherwise noted! Find method Sequential Worst case time: c1 n Binary search Worst case time: c2 log2n Small problems - we’re not interested! n Large problems - we’re interested in this gap! 4log2n Binary search More complex Higher constant factor
8
Lecture 3 - Key Points Linked Lists
Dynamic allocation gives flexibility Use as much or as little space as needed Addition - constant time Searching - proportional to n Deletion - proportional to n Constant in doubly linked if given node Variants LIFO (simplest) FIFO (add tail pointer) Doubly linked Scan in either direction Circularly linked Scan entire list from any point
9
Lecture 4 - Key Points Stacks Recursion
Just a collection with LIFO semantics Add, Delete usually named push, pop Recursion Stack frame to store function context Permits recursive calls Basis for some simple, elegant and efficient solutions! but, sometimes too simple Fibonacci - elegant but not efficient (explanation to come!)
10
Lecture 4 - Key Points Searching Trees O(logn) search time
Binary search - but addition is expensive Trees Recursive Data Structure Sub-trees are themselves trees Searched recursively Search time proportional to height, O(logn)
11
Lecture 10 - Key Points The Sorting Repertoire
Insertion O(n2) Guaranteed Bubble O(n2) Guaranteed Heap O(n log n) Guaranteed Quick O(n log n) Most of the time! O(n2) Bin O(n) Keys in small range O(n+m) Radix O(n) Bounded keys/duplicates O(nlog n) Bin and Radix sort Distinguished by ability to map the key to a small integer or calculate an address from the key Compare only capability -> O(n log n) Pathological case
12
Lecture 10 - Key Points Radix Sort Any suitable radix can be used
Different bases can be used in each phase Memory management must be efficient to achieve O(n)
13
Lecture 13 - Key Points m-way trees
Major use: databases m adjusted so that a node fits in a physical disc block Searching - O( log n ) B-trees m-way tree in which each node is at least half-full B+-trees No data in nodes Leaf nodes Contain pointers to data Linked together to enable fast in-order traversal of tree Insertion Split full nodes, promote a key if necessary
14
Lecture 13 - Key Points Hash Tables
If we can derive an address from a key directly O(1) searching Constraints Limited range - table has to fit in memory Keys dense in range - otherwise too much space wasted Duplicates O(n) in worst case
15
Key Points - Lecture 14 Hash Tables O(1) searching complexity
Address calculated directly from key with hash function Collisions Table Organisation Linked lists Overflow areas Re-hashing Use a second hash function Linear Susceptible to clustering Quadratic
16
Key Points - Lecture 14 Hash Functions Animations!!
Assume we can construct a function to map keys to a range of integers Methods for reducing this range to (0,m] Division mod m Don’t use m = 2p Multiplication Multiply by A and extract p middle bits m = 2p OK Universal Hashing Animations!!
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.