ECE250: Algorithms and Data Structures Midterm Review Ladan Tahvildari, PEng, SMIEEE Associate Professor Software Technologies Applied Research (STAR) Group Dept. of Elect. & Comp. Eng. University of Waterloo
Materials from CLRS/Weiss Chapter CLRS 2 CLRS 3, A.1 CLRS 4 CLRS 10 CLRS 11 CLRS 12, B.5 CLRS 18.1, 18.2 Weiss 4 Topics Algorithmic problems, insertion sort, merge sort Asymptotic notation, useful math Recurrences Arrays, lists, stacks, queues Hashing Binary Search Trees (BST) B-Trees (Search and Insertion) AVL Trees Lecture 16 ECE250 2
Running Time - Kinds of Analyses v Worst-case: (usually) Ø T(n): maximum time of algorithm on any input of size n. v Average-case: (sometimes) Ø T(n): expected time of algorithm over all inputs of size n. Need assumption of statistical distribution of inputs. v Best-case: (NEVER) Ø Cheat with a slow algorithm that works fast on some input. Lecture 16 ECE250 3
Ο Notation Ο(g(n)) = { f (n) : there exist positive constants c and n 0 such that 0 f (n) cg(n) for all n n 0 } Example: 2n 2 = Ο( n with c 3 ) = 1, n 0 = 2 g(n) is an asymptotic upper bound for f(n) Lecture 16 ECE250 4
Ω Notation Ω (g(n)) = { f (n) : there exist positive constants c and n 0 such that 0 cg(n) f (n) for all n n 0 } Example: n = Ω(log n) with c = 1, n 0 = 16 Lecture 16 ECE250 5
Θ Notation Θ(g(n)) = { f (n) : there exist positive constants c 1, c 2, and n 0 such that 0 c 1 g(n) f (n) c 2 g(n) for all n n 0 } Example: 1 2 n 3n 2 with = Θ( n Lecture 16 ECE250 6 c 1 = 1 14, c 2 2 ) = 1 2, n g(n) is an asymptotic tight bound for f(n) 0 = 7
Methods for Solving Recurrences v Master Method Ø Running time dominated by cost at leaves Ø Running time evenly distributed throughout the tree Ø Running time dominated by cost at the root In each case compare f( n) with v Repeated Substitution v Recursion-Tree Method On logb a ( ) Lecture 16 ECE250 7
Elementary Data Structures v A stack is a container of objects that are inserted and removed according to the last-in-first-out (LIFO) principle. Ø Push (S:Stack, x:element) - Inserts o onto top of S Ø Pop (S:Stack) - Removes the top object of stack S v A queue differs from a stack in that its insertion and removal routines follows the first-in-first-out (FIFO) principle. Ø Elements are inserted at the rear (Enqueued) Ø Elements are removed from the front (Dequeued) Lecture 16 ECE250 8
Hashing v Collision Resolution Ø Chaining Ø Open Addressing Linear Probing Double Hashing v Hash Functions Ø Division Method: h(k) = k mod m Ø Multiplication Method: h(k) = m (k A mod 1) Lecture 16 ECE250 9
Trees v Binary Search Trees (BST) Ø Tree Walks: Preorder, Inorder, Postorder Ø Search, Insert, Delete (three cases) Ø Min, Max, Successor, Predecessor v AVL Trees Ø Single/Double Rotations and their effects Ø Insertion (four cases), Deletion (three cases) v B-Trees Ø Searching (simple), Creating an empty tree (trivial) Ø Splitting nodes and insertion keys Lecture 16 ECE250 10
Preparation for the Exam v First, concentrate on the lecture notes v Second, studying the CLRS/Weiss books wherever are needed (although you have to know the concepts!) v Third, solving exercises by yourself Ø When solving write your solutions down! Lecture 16 ECE250 11
More Materials (46 Questions) v Study all solved exercises in the tutorials/lectures (11) http://www.stargroup.uwaterloo.ca/~ece250/materials/ v Study solutions for the first assignment (5) http://www.stargroup.uwaterloo.ca/~ece250/assignments/ v ECE250 Previous Midterms (21) http://www.stargroup.uwaterloo.ca/~ece250/exams/ v Study solutions for sample questions (9) http://www.stargroup.uwaterloo.ca/~ece250/materials/lecture17-samplequestions.pdf Lecture 16 ECE250 12
Office Hours (October 16-25; 15 hours) Mon Oct. 16 Tue Oct. 17 Wed Oct. 18 Thu Oct. 19 Mon Oct. 23 Tue Oct. 24 Wed Oct. 25 Mahsa 12:30-1:30 DC-3574 Ladan 12:30-1:30 EIT-4136 Mahsa 5:30-6:30 DC-3574 Jun 12:30-1:30 DC-3574 Mahsa 10-11 DC-3574 Jun 11-12 DC-3574 Ladan 10-11 EIT-4136 Jun 5:30-6:30 DC-3574 Jun 5:30-6:30 DC-3574 Ladan 5:30-6:30 EIT-4136 Mahsa 1-2 DC-3574 Jun 1-2 DC-3574 Ladan 11-12 EIT-4136 Mahsa 2-3 DC-3574 Ladan 3-4 EIT-4136 ECE250 Fall 2017
ECE250 Midterm Exam v Date: Wednesday, October 25, 2017 v Time: 8:45pm-10:00pm v Locations: Ø RCH 301 Student ID from 20446508 to 20660661 Ø RCH 302 Student ID from 20660914 to 20750989 NOTE: Standard calculator allowed but no additional materials allowed. Lecture 16 ECE250 14
Potential Structure for Midterm v Four questions with two/three sub-problems 1. Algorithm Analysis (~30 marks) (CLRS 2, 3, 4, A.1) 2. Linked Lists, Stacks, and Queues ( ~20 marks) (CLRS 10) 3. Trees and Tree Traversals (~30 marks) (CLRS 12, 18.1, 18.2, B.5; Weiss 4) 4. Hashing (~20 marks) (CLRS 11) Lecture 16 ECE250 15
Sample Questions
Question 1 - Algorithm Analysis v Consider the sorting algorithm given below. This algorithm is designed to sort an array of integers, a[ 0], a[1],..., a[ n 1] where n is assumed to be a power of two. 1. void Merge (int a[], int pos1, int pos2, int len); 2. 3. void MSort (int a[], int n) 4. { 5. for (int s = 1; s <= n/2; s = 2*s) 6. for (int i = 0; i < n; i = i + 2*s) 7. Merge (a, i, i + s, s); 8. } Lecture 16 ECE250 17
Question 1 (Continue) v This algorithm makes use of a routine called Merge which merges the two sorted sub-sequences of array a, each of length len, which start at array indices pos1 and pos2 as shown below: len len a pos1 pos2 v Derive a tight, big-oh asymptotic bound on the running time of the MSort routine as a function of n. Assume that the running time of Merge is O ( 2 len). Lecture 16 ECE250 18
Question 2 Stacks and Queues v Propose a data structure that supports Push and Pop operations, and a third operation Find-Max, which returns the largest element in the data structure, all in O(1) worst case time. Lecture 16 ECE250 19
Question 3 Tree Traversals v Consider the binary tree shown below: h c j a e i b d g f Lecture 16 ECE250 20
Question 3 (Continue) v For each of the following traversals, list the order in which the nodes are visited. Pre-order Traversal In-order Traversal Post-order Traversal Lecture 16 ECE250 21
Question 4 Binary Trees v A full node in a binary tree is a node with exactly two children. Give an expression for the number of full nodes in a binary tree as a function of the number of leaf nodes l. Hint: Consider the number of null pointers in a tree with n nodes. Lecture 16 ECE250 22
Question 5 - BST v Show the tree after deleting 50 and 30. Use the successor of a node if it is needed 50 30 90 10 70 95 Lecture 16 ECE250 23 80
Question 6 AVL Trees v What is the minimum number of nodes in an AVL tree of height three? What is the maximum number of nodes in an AVL tree of height three? v Let n be the minimum number of nodes in an AVL tree of height three. Draw the AVL tree of height three that contains the keys. 1, 2,3,..., n Lecture 16 ECE250 24
Question 7 B-Trees v Consider the following B-Tree of order 2, called X. Show the resulting B-tree when the key C is inserted into B-tree called X. Lecture 16 ECE250 25
Question 8 - Recurrences v Give asymptotic upper and lower bounds for the following recurrence. Assume that is constant for sufficiently small n. Make your bounds as tight as possible, and justify your answer. Lecture 16 ECE250 26
Question 9 - Hashing v Give an algorithm that uses hashing to check whether all elements of an array are distinct. Lecture 16 ECE250 27