1. Convert the following decimal numbers to binary and back again.

Size: px
Start display at page:

Download "1. Convert the following decimal numbers to binary and back again."

Transcription

1 Exercises 1 - number representations Questions 1. Convert the following decimal numbers to binary and back again. (a) 34 (b) The algorithm for addition of two positive integers is as easy with the binary representation as it is with the decimal representation. Show that = 53, by converting 26 and 27 to binary, computing the sum, and then converting back to decimal. 3. A byte is n = 8 bits. When the bits are interpreted as positive 8 bit integers, the values range from 0 to 255. Write out the bytes (binary numbers) that correspond to integers 127 to Integers are typically represented in a computer using a fixed number of bits, namely 8, 16, 32, or 64. In Java, these correspond to primitive types byte, short, int, and long. To represent positive integers one has to declare the type to be unsigned. What is the positive largest number that can be represented using the above lengths, and roughly how much is each (written in decimal)? 5. We represent numbers in base 8 as follows: ( a[n 1] a[2] a[1] a[0] ) 8 = a[n 1] 8 N a[2] a[1] 8 + a[0] where the a[i] are all in { 0, 1, 2,, 7. Convert (736) 8 and (1205) 8 from base 8 to decimal. 6. Perform the sum (1205) 8 + (736) Perform the following sum. The two numbers are represented in base 5: (2430) 5 + (2243) 5. Do not convert the numbers from base 5 to decimal! 8. Convert 238 from decimal to base 5, that is, write 238 as a sum of powers of 5.

2 Exercises 1 - number representations Answers 1. (a) = = (b) = = To perform the addition, use the same algorithm that you use with decimal, except that you are only allowed 0 s and 1 s. Whenever the sum in a column is 2 or more, you carry to the next column, since it contributes to the next power of 2: So, carry bits i + 2 i = 2 i+1 In fact, this is basically how computers do arithmetic as you will learn in COMP is , 128 is , 129 is , 130 is The largest positive integer you can represent with N bits is 2 N 1. Since , we have = 64, = 2 2 (10 3 ) 3 = = 16 (10 3 ) 6 = which is a very big number. 5. (736) 8 = = (478) 10 (1205) 8 = = (645) (1205)_8 + (736)_ (2143)_8 which is 1123 in base 10.

3 Exercises 1 - number representations 7. The answer is (10223) <--- carry If you would like to practice converting between different bases, see the website: 8. Apply the same algorithm we saw for binary, but now we divide by 5 at each step and write down the remainder. decimal base 5 coefficients i 238 a[i] and so (238) 10 = (1423) 5 = =

4 Exercises 2 - arrays and array lists September, 2016 Questions 1. Give an algorithm for reversing all the elements of an array list, which uses a constant amount of additional space (other than the array itself). That is, you are not allowed to use a new array for your solution. 2. Give a O(N) algorithm (pseudocode) for removing the first instance of a given object e in a list, assuming the list is represented as an array list and the size of the list is N. That is, give an algorithm for remove(e). In your answer, you can use methods given in the lecture. [ASIDE (added Sept 16): In the lecture, I presented a remove(i) algorithm which removes the element at index i in the list. In the above question, I m asking for a remove(e) algorithm which removes a particular object (if it happens to be there). Having two methods with the same name but different argument types is called overloading a method. Its very common in Java.] 3. As discussed in the lectures, an important property of arrays is that they have constant time access. This property follows from the fact that all array slots have the same memory size. What about an array of strings, in which the strings have possibly different lengths? Does this contradict the property just mentioned? Does one still have constant time access to an element in an array of strings? last updated: 16 th Sept, 2016 at 15:28 1

5 Exercises 2 - arrays and array lists September, 2016 Answers 1. Here I sketch the idea with pseudocode. The main idea is to use a swap method. You should have seen in COMP 202 how to swap the references of two values. Recall that you need to use a tmp variable to do so. swap(j, k){ tmp = a[j] a[j] = a[k] a[k] = tmp The algorithm then swaps the first and the last, the second and second last, etc. reversearraylist() { for (i = 0; i < size/2; i++){ swap(i, size-1-i) If the list has a odd number of elements, then it doesn t touch the middle one, which is fine. For example, consider the case i = 13. It swaps 0,1,2,3,4,5 with 12,11,10,9,8,7, respectively, and doesn t touch The following algorithm loops through the list and examines each element at most once. Hence it takes time proportional to N in the worst case, and so we say it is O(N). remove( e ) { i = 0 found = false while ((i < size) and (found == false)){ if a[i] == e found = true return remove(i) // else i++ // means i = i + 1 print("sorry pal, I couldn t find your e") this method was discussed in the lecture 3. The slots in the array of strings don t contain strings, but rather they contain references to (addresses of) strings. The strings themselves are objects. Each string has a location somewhere in memory. So there is no problem here. (Note: saying that the array slots all have constant size is fine. The slots contain references or addresses, which all use the same number of bits. The number of bits depends on which machine we are talking about. ) last updated: 16 th Sept, 2016 at 15:28 2

6 Exercises 3 - linked lists September, 2016 Questions 1. Consider the following Java code for a singly linked list method getindexof(e e) that returns the index of the first occurrence of given element in a singly linked list if that element is present, and it returns -1 if the element is not present. For example, if the given argument e is at the front of the list then the method returns index 0. You may assume the list is not empty, and the field head is defined in the usual way. Provide the missing Java code of this method below. public int getindexof(e SNode<E> cur = head; int index = 0; e){ // ADD YOUR CODE HERE if (cur.element == e) return index; else return -1; 2. Fill in the code of the following Java method reverse() which reverses the order of nodes in a singly linked list, by changing next references so that they go in the opposite direction in the list. The head and tail references must be swapped too. The idea of the solution below is to iterate from the head node to the tail node. While doing so, partition of the nodes into two lists: the (reversed) nodes up to the current node, and the (not yet reversed) nodes beyond the current node. The heads of the two lists are headlist1 and headlist2. To answer such a question, many people find it helpful to visualize the situation with boxes (nodes) and arrows, as done in the lectures. Doing it in your head is likely to be too difficult. public void reverse(){ SNode<E> headlist1, headlist2, cur; if (head!= null){ headlist1 = null; headlist2 = head; // ADD YOUR CODE HERE. In formulating your solution, try to focus // first on the general case, rather than the two edge cases. tail = head; head = headlist1; // the tail of the original list last updated: 19 th Nov, 2016 at 09:23 1

7 Exercises 3 - linked lists September, [EDITED Nov 19. After presenting lecture 29 on interfaces, I went back and reviewed the Iterator methods for the singly linked list class that I had presented earlier, and I rewrote it. The link below is to the solution code only. I am figuring that at this point in the semester, if you haven t done this exercise by now then you probably aren t going to do it at all. Next time I teach the course, I will add stubs mentioned below. In the meantime, enjoy reading the solution code.] See the Java code which contains a partially implemented singly linked list class. The java files with stubs in the file names contains some partially implemented methods: remove(i) add(i,e) reverse() i.e. question 2 above Your task is to implement these methods. A solution file is provided. Note: Assignment 1 will use linked lists, but it does not require you to practice the under the hood manipulations of linked lists. These exercises give you an opportunity for doing so. 4. The URL in the previous question also contains a partially implemented doubly linked list class (DLinkedList stubs.java) along with a fulling implemented class. Implement the following methods in the (DLinkedList stubs.java) class: addbefore(e e, DNode node) This is a helper method that is used by various add methods, namely addfirst(), addlast(), add() reverse() i.e. same as in Questions 2 and 3, but now with a doubly linked list. 5. Consider the Java code: public void display( LinkedList<E> list ){ for (int i = 0; i < list.size(); i++){ System.out.println( list.get(i).tostring() ); How does the number of steps of this method depend on N, the number of elements in the list. Unlike in the example in lecture 5, consider the fact that the get(i) method will start from the tail of the list in the case that i is greater than N/2. 6. Can you have a loop in a singly linked list? That is, if you follow the next references, then can you reach a node that you have already visited (and hence loop around infinitely many times if you keep advancing by following the next reference? last updated: 19 th Nov, 2016 at 09:23 2

8 Exercises 3 - linked lists September, 2016 Solutions 1. The solution is to advance by following the next pointers. However, be careful not to advance past the last element of the list because you need to call cur.getelement after the code you add. while ((cur.element!= e) && (cur.next!= null)){ cur = cur.next; index++; 2. In the general case, headlist1 is pointing to the node which came before it in the original list (since list 1 had already been reversed) and headlist2 is pointing to the node that came after it in the original list (since list 2 hasn t been reversed yet). The goal is to remove the first element from list 2 and add it to list 1. We need a temporary variable to help us, and we can use cur which has been declared. while (headlist2!= null){ // the condition here handles an edge case: // namely we re done when list 2 is empty cur = headlist2.next; headlist2.next = headlist1; headlist1 = headlist2; headlist2 = cur; 3. See the implemented method in the SLinkedList1 class which is part of the zip file that I have provided in the question. 4. See the implemented methods in the DLinkedList.java file. 5. For any index i the first half of the list, it takes i steps to get to the node. So the number of steps total for nodes in the first half of the list is: ( N 2 ) = N 2 ( N 2 + 1) 2 For nodes in the second half of the list, we start from the tail instead of the head, but the idea is the same, so it takes ( N 2 ) = N 2 ( N 2 + 1) 2 steps in total to reach those nodes. Thus, in total the number of steps is the sum of the above, or N 2 (N 2 + 1). This is about twice as fast as using the inefficient getnode() method, but it is still O(N 2 ). last updated: 19 th Nov, 2016 at 09:23 3

9 Exercises 3 - linked lists September, Linked list data structures do allow for a loop, in the sense that there is nothing stopping the next field of some node from referencing a node earlier in the list. However, in this case, the data structure will not be a list, in the sense of having a well defined ordering from 0, 1,..., size -1. If the linked list class is properly implemented, then the methods should not allow this to happen. last updated: 19 th Nov, 2016 at 09:23 4

10 Appendix: Q2 figures The code starts off like this: if (head!= null){ headlist1 = null; headlist2 = head; That s just saying that if the list is not empty, then there is something to do (namely reverse the list). In that case, we re going to initialize headlist1 and headlist2 as above. That gives us the situation shown on the next slide. headlist1 null head headlist2

11 Suppose we have the general situation below in which the list is partly reversed. headlist1 is the head of the part that has been reversed and headlist2 is the head of the part that hasn t yet been reversed. (At the situation in the previous slide, none of the list has been reversed yet, and so headlist1 points to null. In this case you think of the red nodes as not being there. ) Then, supposed we run the following four instructions. I claim that this will transfer one node from list2 to list1. headlist1 headlist2 cur = headlist2.next; headlist2.next = headlist1; headlist1 = headlist2; headlist2 = cur; headlist1 cur = headlist2.next; headlist2.next = headlist1; headlist1 = headlist2; headlist2 = cur; headlist2 cur

12 headlist1 cur = headlist2.next; headlist2.next = headlist1; headlist1 = headlist2; headlist2 = cur; headlist2 cur headlist1 cur = headlist2.next; headlist2.next = headlist1; headlist1 = headlist2; headlist2 = cur; headlist2 cur

13 headlist1 cur = headlist2.next; headlist2.next = headlist1; headlist1 = headlist2; headlist2 = cur; headlist2 cur After the four instructions, we have one more node in the reversed part of the list (list1), and one fewer node in the not-yet-reversed part of the list (list2). These four instructions are repeated over an over again in a while loop, until eventually head2list2 is null and headlist1 references the last node in the original list. headlist1 headlist2 null

14 Exercises - quadratic sorting September, 2016 Questions 1. Consider the insertion-sort method which sorts an array from smallest to largest. Show the contents of the array after each pass through the for loop. index initial after 1 after I will try to add more later. last updated: 24 th Sept, 2016 at 11:01 1

15 Exercises - quadratic sorting September, 2016 Answers 1. The * indicates that the array is sorted up to that element. index initial after 1 after 2 after 3 after * * * * last updated: 24 th Sept, 2016 at 11:01 2

16 Exercises 5 - stacks, queues September, 2016 Questions 1. Consider the following sequence of stack operations: push(d), push(h), pop(), push(f), push(s), pop(), pop(), push(m). (a) Assume the stack is initially empty, what is the sequence of popped values, and what is the final state of the stack? (Identify which end is the top of the stack.) (b) Suppose you were to replace the push and pop operations with enqueue and dequeue respectively. What would be the sequence of dequeued values, and what would be the final state of the queue? (Identify which end is the front of the queue.) 2. Use a stack to test for balanced parentheses, when scanning the following expressions. Your solution should show the state of the stack each time it is modified. The state of the stack must indicate which is the top element. Only consider the parentheses [,],(,),{,. Ignore the variables and operators. (a) [ a + { b / ( c - d ) + e / (f + g ) - h ] (b) [ a { b + [ c ( d + e ) - f ] + g 3. Suppose you have a stack in which the values 1 through 5 must be pushed on the stack in that order, but that an item on the stack can be popped at any time. Give a sequence of push and pop operations such that the values are popped in the following order: (a) 2, 4, 5, 3, 1 (b) 1, 5, 4, 2, 3 (c) 1, 3, 5, 4, 2 It might not be possible in each case. 4. (a) Suppose you have three stacks s1, s2, s2 with starting configuration shown on the left, and finishing condition shown on the right. Give a sequence of push and pop operations that take you from start to finish. For example, to pop the top element of s1 and push it onto s3, you would write s3.push( s1.pop()). start finish A A B B C D D C s1 s2 s3 s1 s2 s3 last updated: 2 nd Oct, 2016 at 06:37 1

17 Exercises 5 - stacks, queues September, 2016 (b) Same question, but now suppose the finish configuration on s3 is BDAC (with B on top)? 5. Consider the following sequence of stack commands: push(a), push(b), push(c), pop(), push(d), push(e), pop(), pop(), pop(), pop(). (a) What is the order in which the elements are popped? (Give a list and indicate which was popped first.) (b) Change the position of the pop() commands in the above sequence so that the items are popped in the following order: b,d,c,a,e. You are not allowed to change the ordering of the push commands. I briefly discussed the next two questions in the lecture (slides only) 6. Assume you have a stack with operations: push(), pop(), isempty(). How would you use these stack operations to simulate a queue, in particular, the operations enqueue() and dequeue()? Hints: Use two stacks one of which is the main stack and one is a temporary one. You are allowed use a while loop. If you have no idea how to do this question or what is even being asked, then look at the solutions. Once you understand the solutions, try to do the next question and resist looking at the solution for that one. 7. Assume you have a queue with operations: enqueue(), dequeue(), isempty(). How would you use the queue methods to simulate a stack, in particular, push() and pop()? Hint: use two queues, one of which is the main one and one is temporary. The solution requires that at least one of the push() or pop() operations is O(N) where N is the size of the queue. last updated: 2 nd Oct, 2016 at 06:37 2

18 Exercises 5 - stacks, queues September, 2016 Answers 1. (a) Sequence of popped values: h,s,f. State of stack (from top to bottom): m, d (b) Sequence of dequeued values: d,h,f. State of queue (from front to back): s,m. 2. (a) - means empty stack [ [{ [{( TOP OF STACK IS ON THE RIGHT [{ [{( [{ [ - empty stack, so brackets match (b) - [ [{ [{[ TOP OF STACK IS ON THE RIGHT [{[( [{[ [{ [ stack not empty, so brackets don t match push 1 push 1 push 1 push 2 pop pop pop push 2 push 2 push 3 push 3 push 3 push 4 push 4 pop pop push 5 push 4 push 5 pop push 5 pop pop pop pop x pop pop (not possible) pop last updated: 2 nd Oct, 2016 at 06:37 3

19 Exercises 5 - stacks, queues September, (a) s2.push( s1.pop() ) s2.push( s1.pop() ) s3.push( s1.pop() ) s3.push( s1.pop() ) s3.push( s2.pop() ) s3.push( s2.pop() ) (b) s2.push( s1.pop() ) s2.push( s1.pop() ) s3.push( s1.pop() ) s1.push( s2.pop() ) s3.push( s2.pop() ) s2.push( s1.pop() ) s3.push( s1.pop() ) s3.push( s2.pop() ) 5. (a) c (popped first), e, d, b, a (b) push(a), push(b), pop(), push(c), push(d), pop(), pop(), pop() push(e), pop() 6. Here I present two solutions. Note that each requires that at least one of the enqueue() or dequeue() operation is O(N) where N is the size of the queue. Solution 1 The first solution is to implement enqueue(e) simply by pushing the new element onto the main stack s. enqueue(e){ s.push(e) In this solution, the bottom of the stack would be the front of the queue (containing the element that has been in the queue longest) and the top of the stack is the back of the queue (containing the least recently added element). How can we implement dequeue, that is, how do we remove the bottom element of the stack? The idea is to use a second stack tmps. We first pop all items from the main stack s, and push each popped element directly onto the second stack tmps. We then pop the top element of the second stack (which is the oldest element in the set). Finally, we refill the main stack s by popping all elements from the second stack and pushing each back on to the first stack. dequeue(){ tmps <- new empty stack while!s.isempty(){ tmps.push( s.pop() ) last updated: 2 nd Oct, 2016 at 06:37 4

20 Exercises 5 - stacks, queues September, 2016 returnvalue <- tmps.pop() // the dequeued element while!(tmps.isempty()){ s.push( tmps.pop() ) return returnvalue Solution 2 Here we let dequeue() be simple and just pop the stack. For this to work, the stack needs to store the elements such that the oldest element is on top of the stack and the most recently added element is at the bottom of the stack. dequeue(){ return s.pop() With this solution, enqueue(e) needs to do the heavy lifting: enqueue uses a temporary stack to invert the order of elements currently in the main stack, so that the newest element is on top of this temporary stack. Then it pushes the new element onto the empty main stack. Then it copies all the elements back from the temporary stack to the main stack, using pop-push. This recreates the original stack, but now the newest element has been added on the bottom. enqueue(e){ tmp <- new empty stack while! (s.isempty()){ tmps.push( s.pop() ) s.push(e) while! (tmps.isempty()){ s.push( tmps.pop()) last updated: 2 nd Oct, 2016 at 06:37 5

21 Exercises 5 - stacks, queues September, The concepts are similar to the previous question. Solution 1 push(e){ q.enqueue(e) pop(){ tmpq <- new empty queue while! (q.isempty()){ tmpe <- q.dequeue() if! (q.isempty()) tmpq.enqueue( tmpe ) else{ while!( tmpq.isempty()) q.enqueue( tmpq.dequeue()) return tmpe Solution 2 Here the idea is similar, but now push does most of the work. pop(){ q.dequeue() push(e){ make a new empty queue qtmp qtmp.enqueue(e) while!q.isempty(){ qtmp.enqueue( q.dequeue() ) q <- qtmp return qtmp last updated: 2 nd Oct, 2016 at 06:37 6

22 Exercises 6 - induction & recursion Questions 1. (a) Suppose you wish to count down the numbers from a given n down to 1. You can use a while loop to do this: countdown(n){ while (n > 0) { print n n-- Write a recursive version of this algorithm. (b) Now write a recursive countup(n) algorithm which counts up from 1 to n. This is slightly trickier than it seems. 2. Write a recursive algorithm that prints out a consecutive sequence of elements in an array. The method has three parameters: the array name, and the first and last indices to print from i.e. displayarray( a, first, last) 3. Here is an alternative reverse method for the SLinkedList<E> class (see online code from linked list exercises), which reverses the order of nodes in a singly linked list. The method calls a recursive helper method reverserecursivehelper which does most of the work. Write this helper method. If solution is different from the given one, then you should add your method to the SLinkedList class and test/debug it. [MODIFIED Nov. 12, 2016] public void reverserecursive(){ SNode<E> oldhead = head; reverserecursivehelper(head); head = tail; tail = oldhead; 4. Consider a simplified version of the game 20 questions, in which one person has a number in mind from 0 to You can easily find that number with your twenty questions e.g. by asking for the ith bit of the number. (This is essentially binary search.) Here is a slightly different game. Suppose I am thinking of a positive integer n but I don t give you a bound on the number. It is easy for you figure out the number using n questions, namely question i is is it the number i?. Give a faster algorithm, namely one that runs in time proportional to log n. 5. Suppose that you are given an array of n different numbers that strictly increase from index 0 to index m, and strictly decrease from index m to index n 1, where n is known but m is unknown. Note that there is a unique largest number in such a list, and it is at index m. Here are a few examples. last updated: 12 th Nov, 2016 at 07:18 1

23 Exercises 6 - induction & recursion m n [ 3, 5, 17, 18, 21, 6 ] 4 6 [ -3, 5 ] 1 2 [ 12, 7, 4, 2, -5 ] 0 5 Provide the missing pseudocode below of a recursive algorithm that returns the index m of the largest number in the array, in time proportional to log n. The algorithm is initially called with low = 0, high = n-1. findm( a, low, high ){ // array is a[ ], assume low <= high if (low == high) return low else{ // ADD YOUR CODE HERE (AND ONLY HERE) 6. Use mathematical induction to prove the following: For all n 1, (2n 1) = n [ADDED Nov 12, 2016] Recall the mergesort algorithm which recursively partitions a list of size n into two sub-lists of half the size, sorts the two sublists, and then merges them Show the order of the list elements after all merges of lists of size 1 to 2 have been completed, and then after all merges of lists of size 2 to lists of size 4 have been completed: ( 6, 5, 2, 8, 4, 3, 7, 1 ) original list, n=8 (,,,,,,, ) merges from n=1 to n=2 completed (,,,,,,, ) merges from n=2 to n=4 completed ( 1, 2, 3, 4, 5, 6, 7, 8 ) final sorted list last updated: 12 th Nov, 2016 at 07:18 2

24 Exercises 6 - induction & recursion Answers 1. (a) countdown(n){ if (n > 0) { print n countdown(n - 1) Did you remember the base case? (b) countup(n){ if (n > 0) { countup(n - 1) print n Did you get the order of the instructions correct? 2. displayarray( a, first, last){ print a[first]) if (first < last) displayarray(a, first+1, last) Alternatively, you could do it like this: displayarray( a, first, last){ if (first < last) displayarray(a, first, last-1); print a[last]) 3. The following two implementations do essentially the same thing, namely they advance head to the next node in the list, recursively reverse the list from the new head, and clean up the references that involve the original head. private void reverserecursivehelper(snode<e> head){ if (head.next!= null){ reverserecursivehelper1(head.next); head.next.next = head; head.next = null; last updated: 12 th Nov, 2016 at 07:18 3

25 Exercises 6 - induction & recursion private void reverserecursivehelper2(snode<e> head){ SNode<E> tmp = head; if (head.next!= null){ head = head.next; reverserecursivehelper2(head); head.next = tmp; tmp.next = null; 4. The idea of the algorithm is to guess increasing powers of 2 until the power of 2 is bigger than the number. Then do a binary search (backwards). guess = 1 answer = false while (answer == false){ answer = (guess > n) // i.e. evaluate boolean expression guess = guess*2 // To reach here, we must have guess/2 <= n < guess binarysearch(n, [guess/2, guess] ) Both the while loop and the binary search take time proportional to log n. 5. Here are two different solutions. // SOLUTION 1 if (high-low == 1){ if (a[low] < a[high]) return high else return low else{ mid = (low + high)/2 if (a[mid-1] < a[mid]){ return findm(a, mid, high) else return findm(a, low, mid) last updated: 12 th Nov, 2016 at 07:18 4

26 Exercises 6 - induction & recursion // SOLUTION mid = (low + high)/2 if (a[mid] < a[mid+1]){ return findm(a, mid+1, high) else return findm(a, low, mid) last updated: 12 th Nov, 2016 at 07:18 5

27 Exercises 6 - induction & recursion 6. The base case of n 0 = 1 is obvious, since there is only a single term on the left hand side, i.e. 1 = 1 2. The induction hypothesis is the statement P (k): P (k) (2k 1) = k 2 To prove the induction step, we show that if P (k) is true, then P (k + 1) must also be true. k+1 k (2i 1) = 2(k + 1) 1 + (2i 1) i=1 i=1 = 2(k + 1) 1 + k 2, by the induction hypothesis = 2k k 2 = (k + 1) 2. Thus, the induction step is also proved, and so we re done. 7. Below I have grouped into four lists of size two, and then these four lists of size two are merged into two lists of size four. ( 5,6, 2,8, 3,4, 1,7 ) after merging lists of size 2 to lists of size 2 ( 2,5,6,8, 1,3,4,7 ) after merging lists of size 2 to lists of size 4 last updated: 12 th Nov, 2016 at 07:18 6

28 Exercises 7 - recurrences Oct, 2016 Questions For the questions below, solve the recurrence assuming n is a power of 2. t(1) = 1. For Q1-Q4, assume 1. t(n) = t( n 2 ) + n + 2. This is similar to binary search, but now we have to do n operations during each call. What do you predict? Is this O(log 2 n) or O(n) or O(n 2 ) or what? 2. t(n) = t( n 2 ) + n Compare with the previous question. What is the effect of having an n 2 term instead of n? 3. t(n) = 2t( n 2 ) + n2 This similar to mergesort except we need to do n 2 operations at each call, instead of n. 4. t(n) = 3 t( n 2 ) + cn This recurrence arises in an algorithm for fast multiplication of two n digit numbers, which is faster than the grade school algorithm. The method is called Karatsuba multiplication. I mentioned it earlier in the course, and you may see it again in COMP 251. See if you are interested in the details. 5. When we considered the mergesort algorithm in class, we had a base case of n = 1. What if we had stopped the recursion at a larger base case? For example, suppose that when the list size has been reduced to 4 or less, we switch to running bubble sort instead of mergesort. Since bubble sort is O(n 2 ), one might ask whether this would increase the O( ). Would this modified mergesort become an O(n 2 ) algorithm? Solve the recurrence for mergesort by assuming a base case where t(n) = n 2 for the case of n 4. Note: while this might seem like a toy problem, it makes an important point. Sometimes when we write recursive methods, we find that the base case can be tricky to encode. If there is a slower method available that can be used for small instances of the problem, and this slower method is easy to encode, then use it! last updated: 13 th Oct, 2016 at 06:54 1

29 Exercises 7 - recurrences Oct, 2016 Answers 1. Here we are cutting the problem in half, like in a binary search, but we need to do n operations to do so. This term will give us an n + n + n = 2n 1 effect. The constant 2 will 2 4 give us a log n effect since it has to be done in each recursive call. Formally, we have: t(n) = t( n 2 ) + n + 2 = [t( n 4 ) + n 2 + 2] + n + 2 = [t( n 8 ) + n 4 + 2] + n n + 2 = t( n 2 k ) + n 2 k n 2 + n + 2k = t(1) n 2 + n + 2 log(n) log n = i + 2 log(n) = i=1 log n 2 i + 2 log(n),, see geometric series formula below i=0 = (2 log n+1 1)/(2 1) + 2 log(n) = (2 log n 2 1)/(2 1) + 2 log(n) = 2n log(n) This is O(n) because the largest term that depends on n is the 2n term. The formula for the geometric series is : N 1 i=0 x i = xn 1 x 1 Here, I am using x = 2, N = log 2 n. The general formula is derived in lecture 2 on page 5. last updated: 13 th Oct, 2016 at 06:54 2

30 Exercises 7 - recurrences Oct, This is basically the same as the previous problem except that now we have to do half as much work ( n ) instead of n at each call. Will this give us sub-linear behavior? No, it won t 2 since even at the first call we have a term n. 2 This is O(n). t(n) = t( n 2 ) + n = (t( n 4 ) + n 4 + 2) + n = (t( n 8 ) + n 8 + 2) + n 4 + 2) + n = (t( n n ) + n n + 2) + + n n n = t(1) n log n = t(1) + log n 2 i=0 2 i + 2 log(n) = t(1) + (2 log n 1)/(2 1) + 2 log(n) = n + 2 log n last updated: 13 th Oct, 2016 at 06:54 3

31 Exercises 7 - recurrences Oct, The first term of the recurrence is similar to mergesort, but the second term is different since it is now quadratic rather than linear in n. What is the effect? Again, we let n = 2 k and t(1) = 1. t(n) = 2t( n 2 ) + n2 = 2[2t( n 2 2 ) + (n 2 )2 ] + n 2 = 2 2 t( n 2 2 ) + n2 2 + n2 = 2 2 [2t( n 2 3 ) + ( n 2 2 )2 ] + n2 2 + n2 = 2 3 t( n 2 3 ) + n2 4 + n2 2 + n2 = 2 k t( n 2 ) + n2 n2 n k 2k 1 2k n2 log(n) 1 = n t(1) + n i i=0 = n + n 2 (1 ( 1 2 )log n )/(1 1 2 ) = n + 2n 2 (1 1 n ) = n + 2n 2 2n = 2n 2 n Here it is somewhat surprising that the answer is O(n 2 ). In eyeballing the given recurrence, you might have guessed that there would be a further dependence on log n. But that is not what happens. The many small versions of the problem that exist with the recursive calls end up costing not much. The reason, roughly speaking, is that n 2 costs much more for larger problems than smaller problems. last updated: 13 th Oct, 2016 at 06:54 4

32 Exercises 7 - recurrences Oct, Assume n = 2 k, i.e. n is a power of 2. t(n) = 3 t( n 2 ) + cn = 3 [3 t( n 4 ) + cn 2 ] + cn = 3 2 t( n 4 ) + 3cn 2 + cn = 3 2 [3t( n 8 ) + cn 4 ] + cn3 2 + cn = 3 3 t( n 8 ) + cn(3 2 )2 + 3c n 2 + cn = 3 k t( n 2 ) + cn ((3 k 2 )k ( 3 2 ) ) = 3 k t(1) + cn (( 3 2 )k 1)/( 3 2 1) = 3 log 2 n t(1) + 2cn (( 3 2 )log 2 n 1) Using the fact that (see properties of logarithms reviewed in lectures): 3 log 2 n = n log 2 3 and so Thus, ( 3 2 )log 2 n = nlog log 2 n = n(log 2 3) 1. t(n) = n log 2 3 t(1) + 2cn n (log 2 3 1) 2cn = n log 2 3 t(1) + 2c n log 2 3 2cn which is O(n log 2 3 ). Note that n log 2 3 > n, so the dominant term is n log 2 3 and subtracting cn is negligible effect when n is large. last updated: 13 th Oct, 2016 at 06:54 5

33 Exercises 7 - recurrences Oct, Assume n is a power of 2 (to simplify the argument). We would have: t(n) = 2t( n 2 ) + cn = 2( 2t( n 4 ) + cn 2 ) + cn = 4t( n ) + c(n + n) 4 = 8t(n/8) + c(n + n + n) = 16 t(n/16) + c(n + n + n + n) = 2 k t( n 2 k ) + ckn We want to stop at t(4) on the right side. So we let k be such that n = 4, that is, 2 k = n, 2 k 4 which gives t(n) = n 4 t(4) + cn log n 2 4 Now the tricky part of this problem is that you may be thinking that you need to somehow put an n 2 dependence in at this point to account for the switch to insertion sort. But you don t need to do this! Since we are only switching to insertion sort when n 4, the term t(4) is a constant. This is the time it takes to solve a problem of size 4. By inspection of the last line above, we see that the biggest term that depends on n is n log n which is the same as mergesort. Thus, switching to insertion sort when the problem is reduced to size n = 4 does not change the dependence on n. last updated: 13 th Oct, 2016 at 06:54 6

34 Exercises 8 - big O, Ω, Θ Questions 1. Is n! in O((n + 2)!)? 2. Is (n + 2)! in O(n!)? 3. Is 9 n O(12 n )? 4. Is 12 n O(9 n )? 5. Let Show that t(n) is O(3 n ). t(n) = n 3 i. i=0 6. In the lecture on mathematical induction, I showed that for all n, F ib(n) < 2 n. Thus, F ib(n) is O(2 n ). Use mathematical induction to prove a tighter bound, namely F ib(n) is O( ( 7 4 )n ). 7. Use mathematical induction to prove a lower bound: F ib(n) Ω(( 3 2 )n ). 8. Let t(n) = n log n. Prove that t(n) is Ω(log(n!)). 9. Let t(n) = log n 1, where the notation means floor i.e. we are rounding down. Prove that t(n) is Ω(log n) 10. If t(n) O(g(n)), may we conclude that g(n) Ω(t(n))? 11. If t(n) Ω(g(n)), may we conclude that g(n) O(t(n))? 12. Show 2 n is O(n!). 13. Prove that t(n) is Ω(n 2 ), where t(n) = n log n Show that t(n) is Ω(g(n)), where t(n) = 1 log(n 8) 5 g(n) = log(n). 15. Let t(n) = (n + 8) n + 5. Prove that t(n) is O(n 1.3 ). 16. Show that 31n + 12n log n + 57 is O( n log n). 17. Show that (n 3 log n) n is Ω(n 1.6 ). last updated: 14 th Nov, 2016 at 06:49 1

35 Exercises 8 - big O, Ω, Θ 18. Suppose you have three n n arrays, call them a[][], b[][], and c[][]. Consider the following. (Those you familiar with linear algebra will recognize this as matrix multiplication.) for i = 1 to n for j = 1 to n for k = 1 to n{ c[i][j] = a[i][k] * b[k][j]; 19. If f(n) is O(g(n)), can we conclude that 2 f(n) is O(2 g(n) )? 20. Is t(n) = 1 n in Ω(1)? last updated: 14 th Nov, 2016 at 06:49 2

36 Exercises 8 - big O, Ω, Θ Answers 1. We need to show there exists c, n 0 0 such that n! < c(n + 2)(n + 1) n! for all n n 0. Letting c = 1 and n 0 = 1 and dividing by n!, we now need to show that 1 < (n + 1)(n + 1). But this is obviously true. Thus n! O((n + 2)!) 2. Here we need to find a c, n 0 > 0 such that (n + 2)(n + 1) n! < c(n!) for all n > n 0. Choose any c, n 0. Then, dividing by n!, we would now need to show that (n + 1)(n + 2) < c for all n n 0. But this is clearly false, since the left side grows without bound as n grows. Thus, (n + 2)! is not O(n!). 3. Yes, and it is trivial. Since 9 < 12, it follows that 9 n < 12 n and so c = 1 and n 0 = 1 does the job. 4. We want to show there exists c, n 0 > 0 such that 12 n < c9 n for all n n 0. But, letting denote if and only if, we have 12 n < c9 n ( 12 9 )n < c But this inequality cannot be true for all n n 0 obviously, since the left side grows without bound. Thus, 12 n O(9 n ). 5. Recall the formula for a geometric series n i=0 a i = an+1 1 a 1. Then, n i=1 3 i = 3n which is O(3 n ), i.e. take c = 3 2 and n 0 = 1. = 3 2 (3n 1 3 ) 6. We need to find an n 0 and c such that, for all n n 0, F (n) < c( 7 4 )n. Try c = 1. The base case is trivial since F (0) = 0 < ( 7 4 )0 and F (1) = 1 < 7. So let s 4 hypothesize that F (n) < ( 7 4 )n for all n up to some k 1 and see if it follows for n = k + 1. F (k + 1) = F (k) + F (k 1) < ( 7 4 )k + ( 7 4 )k 1 by the induction hypothesis, = ( )(7 4 )k 1 last updated: 14 th Nov, 2016 at 06:49 3

37 Exercises 8 - big O, Ω, Θ But it is easy to verify that < ( 7 4 )2 and so (from the induction hypothesis) we get F (k + 1) < ( 7 4 )2 ( 7 4 )k 1 = ( 7 4 )k+1. This proves the induction step, and so we are done. 7. We need to find an n 0 and c such that F (n) > c( 3 2 )n for all n n 0. Let s first establish a base case. We can t have a base case for n = 0 since F (0) = 0 and so it will be impossible for F (0) > c( 3 2 )0 for c > 0. Instead, we try to find a c and use the base case(s) n = 1, 2. If we let c = ( 2 3 )2, then indeed we have F (n) > c( 3 2 )n for n = 1, 2. So let s try using that c and proving the induction step. We assume the induction hypothesis, namely we assume that F (n) > c( 3 2 )n for n = k 1, k. We want to show it follows that F (k + 1) > c( 3 2 )k+1. F (k + 1) = F (k) + F (k 1) > c( 3 2 )k + c ( 3 2 )k 1 by induction hypothesis = c ( ) (3 2 )k 1 > c ( 3 2 )2 ( 3 2 )k 1, since 5 2 > 9 4 = c ( 3 2 )k+1 Thus, both the base case and induction step are proved and so we are done. 8. We need to show there exist two positive constants c, n 0 such that, for all n n 0, n log n > c log(n!). Try c = 1. Since n log n = log(n n ), and since log x is monotonically increasing, it is enough for us to show that there exist n 0 such that, for all n n 0, n n > n! But it is easy to see that nn n! > 1 since both numerator and denominator have n terms each, and if we take corresponding terms, we notice that the ratio is greater than or equal to 1 for each. Thus, the product of the ratios is greater than or equal to We need to show there exist two positive constants c, n 0 such that, for all n n 0, log n 1 > c log n. But log n 1 > log n 2. last updated: 14 th Nov, 2016 at 06:49 4

38 Exercises 8 - big O, Ω, Θ Let s guess at a constant c < 1 and show we can find an n 0 to get what we want. Guess c = 1 2, and check if there is an n 0 such that, for all n n 0, log n 2 > 1 2 log n. But this inequality is the same as the following: 1 2 log n > 2. which is true when n > 16. So take n 0 = 16 and we are done. 10. The answer is yes, and here is the proof. If t(n) O(g(n)), then there exists a constant c and n 0 such that, for all n n 0, t(n) < cg(n) or equivalently g(n) > 1 c t(n). Hence the constants n 0 and 1 c exist for g(n) is Ω(t(n)). 11. The answer is yes, and the proof is exactly as in the previous question. If t(n) Ω(g(n)), then there exists a constant c and n 0 such that, for all n n 0, t(n) > cg(n) or equivalently Hence the constants n 0 and 1 c g(n) < 1 c t(n). exist for g(n) is O(t(n)). 12. We want to show that there exist two constants c > 0 and n 0 > 0 such that, for all n n 0, 2 n c n! or, equivalently, 2 n 2 n 1 2 n c. On the left side, the numerator and denominator have n terms each. We pair them up and note that numerator terms are all less than or equal to their corresponding denominator terms, except for the last pair ( 2 ). We take the last pair to the other side, 1 2 n 2 n 1 2 n c 2. The terms on the left side are for n 2. If n = 1, then the left side is 1. So, if we let c = 2 and n 0 = 2, then this inequality indeed is true for all n n 0 since the right side is 1 and the left side is a product of terms that are each less than or equal to 1. last updated: 14 th Nov, 2016 at 06:49 5

39 Exercises 8 - big O, Ω, Θ 13. Here are two ways to do it. The first way: t(n) = n2 2 n log n for n 1 Since we are looking for a lower bound, let s try a constant c < 1 2, specifically take c = 1 4. We want to find an n 0 such that, for all n n 0, n > n 4 or equivalently n 2 4 > 40 We see n 0 = 13 does the job, since 13 2 = 169 > 160 = The second way to do it is to guess c = 1 and then find an n 2 0 such that 3 log n 40 > 0 for all n > n 0. Choosing n 0 = does the job. 14. We are looking for a lower bound so let s try some constant c < 1 1. Let s try c = log(n 8) > 1 10 log n log(n 8) > 1 2 log n log(n 8) > log n n 8 > n But the last inequality is true if n is sufficiently large, since n grows faster than n. We still need to choose an n 0. The inequality holds for n 0 = 16 since 8 > 4. Moreover, dividing both sides by n gives n > n which holds for all n > 16 since the left side is increasing and the right side is decreasing. So, n 0 = 16 does the job (and c = 1 10 ). 15. We need to show there exists two positive constants c, n 0 such that, for all n n 0, (n + 8) n + 5 < cn 1.3. (n + 8) n + 5 < (2n) n + 5, if n 8 < 4n n n 1.3, since < 2 2 = 4 = 12n 1.3 So, take n 0 = 8 and c = 12. last updated: 14 th Nov, 2016 at 06:49 6

40 Exercises 8 - big O, Ω, Θ 16. We want to show there exists a c > 0 and n 0 1 such that, for all n n 0, 31n + 12n log n + 57 < c n log n. But 31n + 12n log n + 57 < 31n log n + 12n log n + 57n log n, when n > 2 = 100n log n = 10 n log n < 10 n log n, when n > 2 where the last line follows from the fact that x < x when x > 1. So, take n 0 = 3 and c = Using the definition of Ω( ), we want to show there exists a c > 0 and n 0 1 such that, for all n n 0, (n 3 log n) n > cn 1.6. First notice that (n 3 log n) n > (n 3 log n) 1.6, for n 1 and so it will be enough to find a suitable lower bound for the expression on the right side, in particular, we want to show that there exists a c > 0 and n 0 1 such that, for all n n 0, Take both sides to the power 1 1.6, gives (n 3 log n) 1.6 > cn 1.6. n 3 log n > c 1/1.6 n Let C = c 1/1.6, so we want: (1 C)n > 3 log n. Try C = 1 2 (or c = ). The inequality becomes n > 6 log n. Trying powers for 2, we see that inequality does not hold for n = 16 but it does hold for n = 32, i.e. 32 > 30. Is the inequality true for larger powers of n? When I originally posted these solutions, I just wrote that it is but didn t give adequate justification. Sorry about that! Below is the full justification. It relies on Calculus 1. The proof below is more than most of you want, so feel free to skip it. It is not something I would expect you to do on your own (although many of you could do it, I m sure). Rewrite the inequality using the log property log 2 n = (log 2 e)(ln n), and replace n with x because we are going to take a derivative: last updated: 14 th Nov, 2016 at 06:49 7 x > 6(log 2 e) ln x.

41 Exercises 8 - big O, Ω, Θ To show that this inequality is true for any x > 32, I m going to show that the derivative of the left side is bigger than the derivative of the right side. Let f(x) = x be the left side and g(x) = 6(log 2 e) ln x be the right side. We know f(32) > g(32) and I am going to show f (t) > g (t) for all t > 32. It will then follow that f(x) = x 32 f (t)dt > x 32 g (t)dt = g(x). But f (x) = 1 and g (x) = 6 (log 2 e) 1 x. and so f (x) > g (x) when x > 9 and thus it also holds for x > The algorithm is O(n 3 ). Why? For each value of i, we run the two inner loops (j and k). There are n values of i, so the number of steps is n times the number of steps in the two inner loops. The two inner loops take n 2 steps (by similar reasoning, namely for each value of j, we run through all n values of k). Thus, the number of steps is O(n n 2 ) = O(n 3 ). 19. No. Take f(n) = 2n and g(n) = n. However, 2 2n = 4 n which is not O(2 n ). 20. The definition of Ω() requires c > 0. However, for any such c that we choose, there will be an n 0 such that t(n) < c when n n 0, namely n 0 = 1. The idea here is that t(n) is not c asymptotically bounded below by a strictly positive constant. last updated: 14 th Nov, 2016 at 06:49 8

42 Exercises 9 - trees Oct 2016 Questions 1. What is the sequence of nodes visited for the following tree for a preorder, postorder, or breadth first traversal. j / \ f c e / \ d a h i g b 2. Give the order of nodes visited in a pre-, in-, post-, and level-order traversal. 3 / \ 6 2 / \ \ / / Consider the polynomial 5y 2 3y + 2. (a) Write the polynomial as an expression tree that obeys the usual ordering of operations. Hint: to clarify the ordering for yourself, first write the expression with brackets indicating the order of operations. (b) Write the polynomial as postfix expression. 4. Convert the following infix expressions to postfix expressions. Assume the usual ordering of operations: multiple +, - (or *, / ) are evaluated left to right, and *, / has precedence over +, -. (a) a*b/(c-d) (b) a/b+(c-d) (c) a/b+c-d 5. Evaluate the following postfix expressions made out of single digit numbers and the usual integer operators. last updated: 13 th Nov, 2016 at 06:10 1

43 Exercises 9 - trees Oct 2016 (a) / (c) 234-/5* (d) (b) 234*5*- 6342^*+5-6. Two binary trees A and B are said to be isomorphic if, by swapping the left and right subtrees of some nodes of A, one can obtain a tree identical to B. For example, the following two binary trees are isomorphic. 5 5 / \ / \ / \ / \ / \ / \ Write a recursive algorithm isisomorphic( n1, n2) for checking if two trees are isomorphic. The arguments should be references to the root nodes of the two trees. To test if two non-null nodes are equal, assume each node has a key and check if the keys of the two nodes are equal. That way, our definition of equal does not involve other fields defined at each node, namely references to children. 7. Prove that an in-order traversal of a binary search tree visits the nodes in order defined by the keys. Hint: what is the variable used for the proposition? The number of nodes of the tree? Something else? 8. Give a non-recursive version of the binary search tree operations find, findmin, findmax 9. Consider the binary tree (with null child references not shown): "easy" / \ "in" "november" \ / \ "can" "be" "quizzes" / "fun" (a) What is the order of nodes visited in a pre-order traversal? (b) What is the order of nodes visited in a post-order traversal? last updated: 13 th Nov, 2016 at 06:10 2

44 Exercises 9 - trees Oct 2016 (c) Transform this binary tree into a binary search tree of height 2, defined by the natural ordering on strings. (d) Represent this binary tree using an array (of references to strings), such that the parent/child indices in the array are the same as that used in a heap. (Heaps will be covered in upcoming lectures.) 10. (a) Draw the binary tree whose in-order traversal is DBEAFC and whose pre-order traversal is ABDECF. (b) What is the post-order traversal of this tree? (c) Draw all binary search trees of height 2 that can be made from all the letters ABCDEF, assuming the natural ordering. 11. How many binary search trees can you make from a,b,c,d assuming their natural ordering? 12. (a) Form a binary search tree of strings from the sentence: Form a binary search tree of strings In particular, the tree must be constructed by adding the words in the order they are found in the sentence, namely first add the word Form to an empty tree, then add the word a, etc. (b) What is the tree that results, if you run remove( Form ) on the tree in (a)? 13. A tree can be represented using lists, as follows: tree = root ( root listofsubtrees ) listofsubtrees = tree tree listofsubtrees Draw the tree that corresponds to the following list, where the root elements are single digits. ( 6 ( ) 3 ( 4 5 ) ( ) ) Consider two solutions. The first uses a separate edge for each parent/child pair. The second uses the first child, next sibling representation. last updated: 13 th Nov, 2016 at 06:10 3

45 Exercises 9 - trees Oct 2016 Answers 1. preorder: jfdgceahib post: gdfcahbiej breadth: jfcedahigb 2. pre-order: in-order: post-order: level order: (a) Writing with brackets, we get ((5 (y 2 )) (3 y)) + 2, and the corresponding expression tree is: + / \ - 2 / \ * * / \ / \ 5 ^ 3 y / \ y 2 (b) The postfix representation of this tree is: 5 y 2 ^ * 3 y * The solution is the right column. I have written intermediate solutions which is what you should go through (in your head, or explicitly as I have done). IN-FIX BRACKETED IN-FIX BRACKETED POST-FIX POSTFIX (a) a*b/(c-d) ((a*b)/(c-d)) ((ab*)(cd-)/) ab*cd-/ (b) a/b+(c-d) ((a/b)+(c-d)) ((ab/)(cd-)+) ab/cd-+ (c) a/b+c-d (((a/b)+c)-d) (((ab/)c+)d-) ab/c+d- 5. (a) -4 i.e. 8 / (-2) (b) -58 i.e (c) -10 i.e. 2 * -5 (d) 49 i.e. 6 + (3*(4^2)) -5 To solve these problems, you use a stack. (You might solve the probelm in your head, but be aware that your solution is using a stack.) For example, the last problem is done like this: last updated: 13 th Nov, 2016 at 06:10 4

46 Exercises 9 - trees Oct First we push the first four numbers on the stack.. 6,3,4,2 Then apply ^ to the two two elements (4^2)... 6,3,16 Then apply * to the top two elements (3*16)... 6,48 Then apply + to the top two elements (6+48) Then push 5 onto the stack... 54,5 Then apply - to the top two elements (54-5) The idea of the algorithm is to check the various possible situations. First check that the roots match, namely both are not null and the keys are identical. Once the two trees pass that test, you compare the left and right children of the two trees. For the two trees to be isomorphic, either (1.) the left subtree of first tree is isomorphic to the left subtree of the second tree AND the right subtree of first tree is isomorphic to the right subtree of the second tree, or (2) the left subtree of first tree is isomorphic to the right subtree of the second tree AND the right subtree of first tree is isomorphic to the left subtree of the second tree. Here is Java code to solve this problem: isisomorphic( n1, n2){ if(n1 == null && n2==null) return true; else if(n1 == null n2==null) return false; if( n1.key!= n2.key ) return false; if( isisomorphic(n1.getrightchild(), n2.getrightchild()) && isisomorphic(n1.getleftchild(), n2.getleftchild())){ return true if( isisomorphic(n1.getrightchild(), n2.getleftchild()) && isisomorphic(n1.getleftchild(), n2.getrightchild())){ return true return false; last updated: 13 th Nov, 2016 at 06:10 5

47 Exercises 9 - trees Oct The induction variable is the height of the tree. The base case is that the height is 0, so there is just the root node. Obviously the claim is true for the base case. Suppose that for any binary search tree of height k, an in-order traversal visits the nodes in their correct order. Consider now a binary search tree of height k + 1. The inorder traversal first visits all the nodes in the left subtree and, by the induction hypothesis, these nodes are visited in order (since the left subtree is of height no more than k). It then visits the root node. The root element is greater than all nodes visited in the subtree, by definition. Then it visits all the nodes in the right subtree. These nodes are visited in order (by induction hypothesis) and the right subtree nodes have keys that are all greater than the root. 8. find(root,key){ cur = root while ((cur!= null) or (cur.key!= key)) if (key < cur.key) cur = cur.left else cur = cur.right return cur findmin(root){ if (root == null) return null else{ cur = root while (cur.left!= null){ cur = cur.left return cur findmax(root){ if (root == null) return null else{ cur = root while (cur.right!= null) cur = cur.right return cur 9. (a) easy, in, can, november, be, fun, quizzes (b) can, in, fun, be, quizzes, november, easy last updated: 13 th Nov, 2016 at 06:10 6

48 Exercises 9 - trees Oct 2016 (c) fun / \ can november / \ / \ be easy in quizzes (d) easy in november * can be quizzes * * * * fun where * is a null reference. Note that the strings are not stored in the array, but rather the array stores references to the strings. 10. (a) A / \ B C / \ / D E F (b) (c) DEBFCA D D C C / \ / \ / \ / \ B F B E B E A E / \ / / \ \ / / \ \ / \ A C E A C F A D F B D F 11. The systematic way to do this would be to write down all the BST s with a at the root, b at the root, c at the root, d at the root. It turns out there are = 14 possibilities. For the BSTs with a at the root, the keys b, c, d would all be in the right subtree. There are 5 ways see below.similarly, there are 5 ways to have d in the root. (not shown) a a a a a \ \ \ \ \ b b c d d \ \ / \ / / c d b d c b \ / / \ d c b c What about b in the root? There are 2 ways. (Similarly there are 2 ways to have c in the root not shown.) b b / \ / \ a c a d \ / d c last updated: 13 th Nov, 2016 at 06:10 7

49 Exercises 9 - trees Oct (a) Form / \ a search \ / \ binary of tree / strings (b) of / \ a search \ \ binary tree / strings 13. Here is the solution for having a separate (parent-child) edge for each child. 6 / \ \ / \ / \ Here is the solution for the first child - next sibling representation. 6 / / / \ last updated: 13 th Nov, 2016 at 06:10 8

50 Exercises 10 - heaps Questions 1. (a) Use the upheap() algorithm to build a heap out of the characters of the word: computed. (b) What is the result of applying the removemin() method to the heap in (a)? (c) Give an example of a heap that uses the characters of the word computed, and that has the following additional property: for any node with two children, the left child is less than the right child. (d) Give an example of a complete binary tree that contains the characters of the word computed and is also a binary search tree. (Such a tree is not a heap.) 2. Suppose you have an array with the following characters in it. f b u e l a k d w Show how the array evolves after each of the n = 9 loops of the slow buildheap method. 3. The removemin() method puts the last element of the heap into the root. Will this element necessarily need to be moved down in the heap? The answer is obviously no if there are only 2 elements in the heap. So consider the more interesting case that the heap had at least 4 elements in it when the removemin() was called. If this is obvious to you, then you should still try to write down your argument why. 4. Give a O( ) bound for heapsort. When does this worst case occur. 5. Suppose you want to see if a heap contains some element and, if so, then return the index of the element. Give an algorithm for find(element) that is as efficient as you can. last updated: 12 th Nov, 2016 at 08:47 1

51 Exercises 10 - heaps Answers 1. (a) c / \ d e / \ / \ o u t m / p (b) d / \ o e / \ / \ p u t m (c) The easiest way to solve this one is to sort the elements from smallest to largest. The resulting sequence, if written as a array, defines a heap. c / \ d e / \ / \ m o p t / u (d) The tree node structure is determined by the number of nodes. The only question is where to place the elements. But remember that when you traverse a binary search tree in order, you visit the elements in order. So you need to sort the elements of the string: cdemoptu and place them into the tree in order. o / \ e t / \ / \ d m p u / c last updated: 12 th Nov, 2016 at 08:47 2

52 Exercises 10 - heaps 2. The boundary between the heap and non-heap elements is marked with f b u e l a k d w f b u e l a k d w (added f) b f u e l a k d w (added b) b f u e l a k d w (added u) b e u f l a k d w (added e) b e u f l a k d w (added l) a e b f l u k d w (added a) a e b f l u k d w (added k) a d b e l u k f w (added d) a d b e l u k f w (added w) 3. Yes, it will necessarily need to be moved down. If the last element in the heap is at level l where l > 1, then it will have an ancestor that is a child of the root. (This ancestor may be its parent, or grandparent, etc.) But in a heap, a node is always greater than all of its ancestors. So, when the last element is moved to the root and becomes a parent of one of its (former) ancestors, this new root will be greater than its (former) ancestor, which is now its child, and so this new root will not satisfy the heap property. 4. We can ignore the buildheap() step since I showed in class this was O(n). The heapsort algorithm consists of a loop that passes n times. In pass k, starting at k = 0, there is a heap with n k elements and the algorithm removes the minimum element. Removing the minimum element takes the last element, moves it to the root, and then down heaps it, which swaps it at most floor(log 2 n) times. So, we just need to sum this total number of swaps. But we know this is just the sum we saw in the slow buildheap algorithm which was O(nlog 2 n). Verify with examples the worst case indeed occurs if the initial built heap happened to be sorted from smallest to largest. (Note: this is not a proof, but it will hopefully give the idea of what goes wrong in this case!) 5. Unlike binary search trees, heaps do not have enough structure to support finding a particular element. Instead, one has to traverse the whole tree in the worst case, which is O(n). last updated: 12 th Nov, 2016 at 08:47 3

53 Exercises 11 - hashing Questions 1. [EDITED: Nov. 19] Both hashing and binary search trees allow you to store and access map entries in an efficient manner. What are the advantages/disadvantages of each which would make you choose one over the other? 2. Consider a hash table with capacity m and with n entries, i.e. the load factor is n/m. Give O() and Ω() bounds for an iterator that visits all entries. 3. Suppose we were to define a hash code on strings s by: n 1 h(s) = s[i] x i i=0 where s[i] is the 16-bit unicode value of the character at position i in the string, n is the length of s, and x is some positive integer. Give an upper bound on the number of bits needed for the hash code as a function of x and n (the length of the string). 4. Suppose you have approximately 1000 images that you would like to store. Rather than labelling them using a string (i.e. filename) and indexing them based on filename, you would like to label them using small images called thumbnails. Let s say each thumbnail image is pixels, and each pixel can have intensity values from 0 to 255 (ignore color here.) We want to use the thumbnail images as keys in a hash function. Suggest a suitable hash function, namely one that avoids collisions and that doesn t take too much space. 5. Canadian postal codes are of the form L 1 D 1 L 2 D 2 L 3 D 3 where L is always a letter (A-Z) and D is always a digit (0-9). Suppose you have your own company and you wish to index your customer addresses using the postal code as the key. Let the digits A-Z be coded with numbers 1 to 26, for example, code(b) = 2. The codes of the digits are just the digits themselves. (a) Define a hash function: 3 h(l 1 D 1 L 2 D 2 L 3 D 3 ) = ( code(l i ) + i=1 3 D i ) mod 10. i=1 Give an example of a postal code that begins with H3A and that collides with H3A2A7. (b) Give an example of a hash function that would never result in a collision. How large would the hash table need to be? last updated: 19 th Nov, 2016 at 14:55 1

54 Exercises 11 - hashing Answers 1. A BST generally gives slower access than a hash table. Why? With a BST, we would search for a key by following a path from the root towards the leaves. For each node in the BST we encounter, we do a key comparison (<, =, >). If the BST is balanced (best case), then on average it will take us O(log n) steps to find a key, or determine that there is no matching key, where n is the number of keys in the BST. So, for example, if n = 2000 and the tree is balanced, then it will take us on average roughly 10 comparisons to find an item (i.e , and about that half the nodes in a complete binary tree are leaves.) If the BST is not balanced 1, then it will take longer to find the key. This is faster than using a linked list, but still slow relative to a hash table. With hashing, there is a function h(key) which provides a hash value which is a number from 0 to m 1 where m n. Given a key, k, we compute h(k) using some formula or algorithm, and then we go directly to the entry h(k) in the hash table and search through a (typically very short) list for key k. If the hash function is easy to compute, then you can find the key more quickly than with a BST. Note that the BST requires a comparison to be made at each node, and these comparisons can be as expensive as compute a hash value. Another way to think about the difference is to note that binary search trees do a sequence of comparisons, i.e. each comparison returns one of three values (<, =, >), which is relatively little information. (We only need 2 bits to specify one of three values.) By contrast, a hash function gives you log m bits of information, namely it specifies one of m values. You still need to scan the entries in the bucket at index h(k). But if the hash function does a good job, then most of the buckets will have very few elements. So is there any advantage of a binary search tree over the hash table? Yes. The BST may be traversed to list off all elements in order. (The basic idea is that one can start at any node in the BST and continue traversing in order from there and stop when one wants.) With a hash table, one doesn t reprsent the key orderings at all, so reading off an ordered list of keys cannot be done. 2. The simplest way to define the iterator would be to step through each bucket in the hash table. There are m buckets and each needs to be examined, both in the best case and worst case. For each non-empty bucket, the (key,value) pairs ( entries ) in the bucket must each be visited i.e. the list in each bucket needs to be traversed. These traversal require a total of n visits, regardless of how the entries are distributed over the buckets. Thus, the best case is the same as the worst case, namely Ω(m + n) and O(m + n). 3. The largest hash code is 2 16 (x 0 + x x n 1 ) = 2 16 ( xn 1 x 1 ) < 216 x n. Taking the log gives the number of bits of the hashcode 16 + n log 2 x. For example, if x = 31 (as in Java s String class es hashcode method), the hashcode would be about n bits. Here is an example with n = 4. **************** s[0] x 31^0 1 keep in mind that it takes extra work to keep it balanced wait for COMP 251 last updated: 19 th Nov, 2016 at 14:55 2

55 Exercises 11 - hashing ********************* s[1] x 31^1 ************************** s[2] x 31^2 + ******************************* s[3] x 31^ ********************************* hashcode( s ) 4. Let s make a hash table with m = 2000 entries so that we are sure there will be plenty of buckets with no elements (and hence collisions are relatively rare). For the hash function, we need to map the pixel colors to a number larger than m = To do so, we could define a hash code to be the sum of the intensity values at the pixels. Assume the average intensity value is 128, we would get a number on average of which is much bigger than m. Then, to get the hash value, we could take the sum of the color values and compute sum mod m. 5. (a) h(h3a2a7) = ( ) mod 10 = 2 So, you need to come up with a postal code D 2 L 3 D 3 such that D 2 + code(l 3 ) + D 3 mod 10 is the same as mod 10. The latter is 0. So, for example, if you take 3A6, then D 2 + code(l 3 ) + D 3 mod 10 is 0, and h(h3a3a6) = ( ) mod 10 = 2 (b) One simple solution is to use base b = 26 and define: h(l 1 D 1 L 2 D 2 L 3 D 3 ) code(l 1 ) + D 1 b + code(l 2 )b 2 + D 2 b 3 + code(l 3 )b 4 + D 3 b 5 In these cases, the postal code Z9Z9Z9 would give the largest hash value, and you can plug in the numbers and letters to get the largest value. That is how big the hash table would need to be for this hash code. Notice that there are possible postal codes. You could come up with a hash function that maps each postal code to one of the numbers from 0 to For example, h(l 1 D 1 L 2 D 2 L 3 D 3 ) (D 1 +D 2 10+D )+10 3 (code(l 1 )+code(l 2 ) 26+code(L 3 ) 26 2 ). last updated: 19 th Nov, 2016 at 14:55 3

56 Exercises 12 - graphs Questions 1. A weighted graph is a graph such that each edge has a weight (a number). The weights can be represented with an adjacency matrix, whose elements contain the weights (instead of boolean values). Note that this representation does not distinguish the case that there is an edge with weight 0 from the case that there is no edge. For this reason, we interpret a 0 in the matrix to mean that there is no edge. Let A be the adjacency matrix of a weighted graph, G, with the columns and rows labelled, in order, by the vertices of the set V = {v 0, v 1, v 2, v 3, v 4, v 5 That is, the first row of the matrix represents the adjacencies of vertex v 0, the second row those of vertex v 1, etc. Note that there are some non-zero diagonals which means that there are some edges of the form (v, v). A = (a) Write down the adjacency list for the graph, such that the edges in the list are ordered by increasing weight. (b) Perform a (preorder) depth first search on G, beginning at vertex v 0. Use the ordering in the adjacency list to determine the ordering of vertices visited. (c) Perform a breadth first search of the graph, G, again beginning at v 0 and using the ordering in the adjacency list. 2. Would you use the adjacency list structure or the adjacency matrix structure in each of the following cases? Justify your choice. (a) The graph has 10,000 vertices and 20,000 edges and it is important to use as little space as possible. (b) The graph has 10,000 vertices and 20,000,000 edges, and it is important to use as little space as possible. (c) You need to answer the query areadjacent() as quickly as possible, no matter how much space you use. (Two vertices u and v are adjacent if either (u, v) or (v, u) is an edge in the graph.) (d) You need to perform operation insertvertex. (e) You need to perform operation removevertex. 3. Explain why the depth first traversal runs in O(n 2 ) time on an n-vertex graph that is represented with an adjacency matrix structure. last updated: 12 th Dec, 2016 at 16:17 1

57 Exercises 12 - graphs 4. The figure below shows a set of rectangles whose overlap relationships can be represented with a directed graph. Each rectangle is represented by one vertex, and there is a directed edge whenever one rectangle overlaps another rectangle. For example, there is an edge (A,B) but no edge (B,A). (a) Give the adjacency list for this graph. The vertices must be ordered alphabetically. (b) Give the ordering of vertices visited in a breadth first traversal of the graph, starting from vertex C. Show the BFT tree. NOTE: In this question and the next, there is only one answer since you must order the vertices alphabetically. (c) Give the ordering of vertices visited in a preorder depth first search traversal, starting from vertex C. Show the DFT tree. (d) Give a new example having four rectangles I,J,K,L, such that corresponding graph contains a cycle. Draw the rectangles and the graph. 5. The depth first traversal algorithms that I presented in class were all preorder since vertices were visited before their children. Is a postorder graph recursive traversal possible? If so, how? If not, why not? last updated: 12 th Dec, 2016 at 16:17 2

push(d), push(h), pop(), push(f), push(s), pop(), pop(), push(m).

push(d), push(h), pop(), push(f), push(s), pop(), pop(), push(m). Questions 1. Consider the following sequence of stack operations: push(d), push(h), pop(), push(f), push(s), pop(), pop(), push(m). (a) Assume the stack is initially empty, what is the sequence of popped

More information

n 1 x i = xn 1 x 1. i= (2n 1) = n 2.

n 1 x i = xn 1 x 1. i= (2n 1) = n 2. Questions 1. Use mathematical induction to prove that, for any n 1 n 1 x i = xn 1 x 1. (Recall that I proved it in a different way back in lecture 2.) 2. Use mathematical induction to prove that, for all

More information

COMP 250 Fall recurrences 2 Oct. 13, 2017

COMP 250 Fall recurrences 2 Oct. 13, 2017 COMP 250 Fall 2017 15 - recurrences 2 Oct. 13, 2017 Here we examine the recurrences for mergesort and quicksort. Mergesort Recall the mergesort algorithm: we divide the list of things to be sorted into

More information

COMP 250 Winter 2010 Exercises 6 - trees March 2010

COMP 250 Winter 2010 Exercises 6 - trees March 2010 Questions 1. Consider the polynomial 5y 2 3y + 2. (a) Write the polynomial as an expression tree that obeys the usual ordering of operations. Hint: to clarify the ordering for yourself, first write the

More information

Algorithms and Data Structures

Algorithms and Data Structures Algorithms and Data Structures Spring 2019 Alexis Maciel Department of Computer Science Clarkson University Copyright c 2019 Alexis Maciel ii Contents 1 Analysis of Algorithms 1 1.1 Introduction.................................

More information

Section 05: Solutions

Section 05: Solutions Section 05: Solutions 1. Memory and B-Tree (a) Based on your understanding of how computers access and store memory, why might it be faster to access all the elements of an array-based queue than to access

More information

COMP 250 Fall priority queues, heaps 1 Nov. 9, 2018

COMP 250 Fall priority queues, heaps 1 Nov. 9, 2018 COMP 250 Fall 2018 26 - priority queues, heaps 1 Nov. 9, 2018 Priority Queue Recall the definition of a queue. It is a collection where we remove the element that has been in the collection for the longest

More information

COMP 250 Fall binary trees Oct. 27, 2017

COMP 250 Fall binary trees Oct. 27, 2017 The order of a (rooted) tree is the maximum number of children of any node. A tree of order n is called an n-ary tree. It is very common to use trees of order 2. These are called binary trees. Binary Trees

More information

Insertion Sort: an algorithm for sorting an array

Insertion Sort: an algorithm for sorting an array Insertion Sort: an algorithm for sorting an array Let s use arrays to solve a problem that comes up often in programming, namely sorting. Suppose we have an array of objects that is in no particular order

More information

Arrays. myints = new int[15];

Arrays. myints = new int[15]; Arrays As you know from COMP 202 (or equivalent), an array is a data structure that holds a set of elements that are of the same type. Each element in the array can be accessed or indexed by a unique number

More information

COMP 250 Winter generic types, doubly linked lists Jan. 28, 2016

COMP 250 Winter generic types, doubly linked lists Jan. 28, 2016 COMP 250 Winter 2016 5 generic types, doubly linked lists Jan. 28, 2016 Java generics In our discussion of linked lists, we concentrated on how to add or remove a node from the front or back of a list.

More information

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A; How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A; } if (n

More information

Data Structures Question Bank Multiple Choice

Data Structures Question Bank Multiple Choice Section 1. Fundamentals: Complexity, Algorthm Analysis 1. An algorithm solves A single problem or function Multiple problems or functions Has a single programming language implementation 2. A solution

More information

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators) Name: Email address: Quiz Section: CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators) Instructions: Read the directions for each question carefully before answering. We will

More information

Section 05: Solutions

Section 05: Solutions Section 05: Solutions 1. Asymptotic Analysis (a) Applying definitions For each of the following, choose a c and n 0 which show f(n) O(g(n)). Explain why your values of c and n 0 work. (i) f(n) = 5000n

More information

Basic Data Structures (Version 7) Name:

Basic Data Structures (Version 7) Name: Prerequisite Concepts for Analysis of Algorithms Basic Data Structures (Version 7) Name: Email: Concept: mathematics notation 1. log 2 n is: Code: 21481 (A) o(log 10 n) (B) ω(log 10 n) (C) Θ(log 10 n)

More information

Solutions. (a) Claim: A d-ary tree of height h has at most 1 + d +...

Solutions. (a) Claim: A d-ary tree of height h has at most 1 + d +... Design and Analysis of Algorithms nd August, 016 Problem Sheet 1 Solutions Sushant Agarwal Solutions 1. A d-ary tree is a rooted tree in which each node has at most d children. Show that any d-ary tree

More information

asymptotic growth rate or order compare two functions, but ignore constant factors, small inputs

asymptotic growth rate or order compare two functions, but ignore constant factors, small inputs Big-Oh 1 asymptotic growth rate or order 2 compare two functions, but ignore constant factors, small inputs asymptotic growth rate or order 2 compare two functions, but ignore constant factors, small inputs

More information

COMP 250 F 17 1 grade school arithmetic algorithms Sept. 7, 2017

COMP 250 F 17 1 grade school arithmetic algorithms Sept. 7, 2017 COMP 250 F 17 1 grade school arithmetic algorithms Sept. 7, 2017 Addition Let s try to remember your first experience with numbers, way back when you were a child in grade school. In grade 1, you learned

More information

Assume you are given a Simple Linked List (i.e. not a doubly linked list) containing an even number of elements. For example L = [A B C D E F].

Assume you are given a Simple Linked List (i.e. not a doubly linked list) containing an even number of elements. For example L = [A B C D E F]. Question Assume you are given a Simple Linked List (i.e. not a doubly linked list) containing an even number of elements. For example L = [A B C D E F]. a) Draw the linked node structure of L, including

More information

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS Lecture 03-04 PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS By: Dr. Zahoor Jan 1 ALGORITHM DEFINITION A finite set of statements that guarantees an optimal solution in finite interval of time 2 GOOD ALGORITHMS?

More information

INSTITUTE OF AERONAUTICAL ENGINEERING

INSTITUTE OF AERONAUTICAL ENGINEERING INSTITUTE OF AERONAUTICAL ENGINEERING (Autonomous) Dundigal, Hyderabad - 500 043 COMPUTER SCIENCE AND ENGINEERING TUTORIAL QUESTION BANK Course Name Course Code Class Branch DATA STRUCTURES ACS002 B. Tech

More information

CS301 - Data Structures Glossary By

CS301 - Data Structures Glossary By CS301 - Data Structures Glossary By Abstract Data Type : A set of data values and associated operations that are precisely specified independent of any particular implementation. Also known as ADT Algorithm

More information

CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014

CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014 CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014 Study: Chapter 4 Analysis of Algorithms, Recursive Algorithms, and Recurrence Equations 1. Prove the

More information

CS126 Final Exam Review

CS126 Final Exam Review CS126 Final Exam Review Fall 2007 1 Asymptotic Analysis (Big-O) Definition. f(n) is O(g(n)) if there exists constants c, n 0 > 0 such that f(n) c g(n) n n 0 We have not formed any theorems dealing with

More information

Summer Final Exam Review Session August 5, 2009

Summer Final Exam Review Session August 5, 2009 15-111 Summer 2 2009 Final Exam Review Session August 5, 2009 Exam Notes The exam is from 10:30 to 1:30 PM in Wean Hall 5419A. The exam will be primarily conceptual. The major emphasis is on understanding

More information

Faculty of Science FINAL EXAMINATION COMP-250 A Introduction to Computer Science School of Computer Science, McGill University

Faculty of Science FINAL EXAMINATION COMP-250 A Introduction to Computer Science School of Computer Science, McGill University NAME: STUDENT NUMBER:. Faculty of Science FINAL EXAMINATION COMP-250 A Introduction to Computer Science School of Computer Science, McGill University Examimer: Prof. Mathieu Blanchette December 8 th 2005,

More information

CS171 Midterm Exam. October 29, Name:

CS171 Midterm Exam. October 29, Name: CS171 Midterm Exam October 29, 2012 Name: You are to honor the Emory Honor Code. This is a closed-book and closed-notes exam. You have 50 minutes to complete this exam. Read each problem carefully, and

More information

The divide and conquer strategy has three basic parts. For a given problem of size n,

The divide and conquer strategy has three basic parts. For a given problem of size n, 1 Divide & Conquer One strategy for designing efficient algorithms is the divide and conquer approach, which is also called, more simply, a recursive approach. The analysis of recursive algorithms often

More information

Algorithm Analysis. (Algorithm Analysis ) Data Structures and Programming Spring / 48

Algorithm Analysis. (Algorithm Analysis ) Data Structures and Programming Spring / 48 Algorithm Analysis (Algorithm Analysis ) Data Structures and Programming Spring 2018 1 / 48 What is an Algorithm? An algorithm is a clearly specified set of instructions to be followed to solve a problem

More information

Instructions. Definitions. Name: CMSC 341 Fall Question Points I. /12 II. /30 III. /10 IV. /12 V. /12 VI. /12 VII.

Instructions. Definitions. Name: CMSC 341 Fall Question Points I. /12 II. /30 III. /10 IV. /12 V. /12 VI. /12 VII. CMSC 341 Fall 2013 Data Structures Final Exam B Name: Question Points I. /12 II. /30 III. /10 IV. /12 V. /12 VI. /12 VII. /12 TOTAL: /100 Instructions 1. This is a closed-book, closed-notes exam. 2. You

More information

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 The data of the problem is of 2GB and the hard disk is of 1GB capacity, to solve this problem we should Use better data structures

More information

O(n): printing a list of n items to the screen, looking at each item once.

O(n): printing a list of n items to the screen, looking at each item once. UNIT IV Sorting: O notation efficiency of sorting bubble sort quick sort selection sort heap sort insertion sort shell sort merge sort radix sort. O NOTATION BIG OH (O) NOTATION Big oh : the function f(n)=o(g(n))

More information

Priority Queues. 1 Introduction. 2 Naïve Implementations. CSci 335 Software Design and Analysis III Chapter 6 Priority Queues. Prof.

Priority Queues. 1 Introduction. 2 Naïve Implementations. CSci 335 Software Design and Analysis III Chapter 6 Priority Queues. Prof. Priority Queues 1 Introduction Many applications require a special type of queuing in which items are pushed onto the queue by order of arrival, but removed from the queue based on some other priority

More information

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct. MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct. In linked list the elements are necessarily to be contiguous In linked list the elements may locate at far positions

More information

Largest Online Community of VU Students

Largest Online Community of VU Students WWW.VUPages.com http://forum.vupages.com WWW.VUTUBE.EDU.PK Largest Online Community of VU Students MIDTERM EXAMINATION SEMESTER FALL 2003 CS301-DATA STRUCTURE Total Marks:86 Duration: 60min Instructions

More information

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours TED (10)-3071 Reg. No.. (REVISION-2010) (Maximum marks: 100) Signature. FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours PART

More information

COMP Data Structures

COMP Data Structures COMP 2140 - Data Structures Shahin Kamali Topic 5 - Sorting University of Manitoba Based on notes by S. Durocher. COMP 2140 - Data Structures 1 / 55 Overview Review: Insertion Sort Merge Sort Quicksort

More information

Figure 4.1: The evolution of a rooted tree.

Figure 4.1: The evolution of a rooted tree. 106 CHAPTER 4. INDUCTION, RECURSION AND RECURRENCES 4.6 Rooted Trees 4.6.1 The idea of a rooted tree We talked about how a tree diagram helps us visualize merge sort or other divide and conquer algorithms.

More information

COMP 250 Fall Recursive algorithms 1 Oct. 2, 2017

COMP 250 Fall Recursive algorithms 1 Oct. 2, 2017 Recursion Recursion is a technique for solving problems in which the solution to the problem of size n is based on solutions to versions of the problem of size smaller than n. Many problems can be solved

More information

The Limits of Sorting Divide-and-Conquer Comparison Sorts II

The Limits of Sorting Divide-and-Conquer Comparison Sorts II The Limits of Sorting Divide-and-Conquer Comparison Sorts II CS 311 Data Structures and Algorithms Lecture Slides Monday, October 12, 2009 Glenn G. Chappell Department of Computer Science University of

More information

Algorithm efficiency can be measured in terms of: Time Space Other resources such as processors, network packets, etc.

Algorithm efficiency can be measured in terms of: Time Space Other resources such as processors, network packets, etc. Algorithms Analysis Algorithm efficiency can be measured in terms of: Time Space Other resources such as processors, network packets, etc. Algorithms analysis tends to focus on time: Techniques for measuring

More information

CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis. Aaron Bauer Winter 2014

CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis. Aaron Bauer Winter 2014 CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis Aaron Bauer Winter 2014 Previously, on CSE 373 We want to analyze algorithms for efficiency (in time and space) And do so generally

More information

Lecture 2: Getting Started

Lecture 2: Getting Started Lecture 2: Getting Started Insertion Sort Our first algorithm is Insertion Sort Solves the sorting problem Input: A sequence of n numbers a 1, a 2,..., a n. Output: A permutation (reordering) a 1, a 2,...,

More information

COMP 250. Lecture 6. doubly linked lists. Sept. 20/21, 2017

COMP 250. Lecture 6. doubly linked lists. Sept. 20/21, 2017 COMP 250 Lecture 6 doubly linked lists Sept. 20/21, 2017 1 Singly linked list head tail 2 Doubly linked list next prev element Each node has a reference to the next node and to the previous node. head

More information

Test 1 SOLUTIONS. June 10, Answer each question in the space provided or on the back of a page with an indication of where to find the answer.

Test 1 SOLUTIONS. June 10, Answer each question in the space provided or on the back of a page with an indication of where to find the answer. Test 1 SOLUTIONS June 10, 2010 Total marks: 34 Name: Student #: Answer each question in the space provided or on the back of a page with an indication of where to find the answer. There are 4 questions

More information

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators) Name: Email address: Quiz Section: CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators) Instructions: Read the directions for each question carefully before answering. We will

More information

Lecture 15 : Review DRAFT

Lecture 15 : Review DRAFT CS/Math 240: Introduction to Discrete Mathematics 3/10/2011 Lecture 15 : Review Instructor: Dieter van Melkebeek Scribe: Dalibor Zelený DRAFT Today slectureservesasareviewofthematerialthatwillappearonyoursecondmidtermexam.

More information

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May www.jwjobs.net R10 SET - 1 II B. Tech I Semester, Supplementary Examinations, May - 2012 (Com. to CSE, IT, ECC ) Time: 3 hours Max Marks: 75 *******-****** 1. a) Which of the given options provides the

More information

Department of Computer Science and Technology

Department of Computer Science and Technology UNIT : Stack & Queue Short Questions 1 1 1 1 1 1 1 1 20) 2 What is the difference between Data and Information? Define Data, Information, and Data Structure. List the primitive data structure. List the

More information

COMP 250 Fall heaps 2 Nov. 3, 2017

COMP 250 Fall heaps 2 Nov. 3, 2017 At the end of last lecture, I showed how to represent a heap using an array. The idea is that an array representation defines a simple relationship between a tree node s index and its children s index.

More information

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17 601.433/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17 5.1 Introduction You should all know a few ways of sorting in O(n log n)

More information

UNIVERSITY REGULATIONS

UNIVERSITY REGULATIONS CPSC 221: Algorithms and Data Structures Midterm Exam, 2013 February 15 Name: Student ID: Signature: Section (circle one): MWF(201) TTh(202) You have 60 minutes to solve the 5 problems on this exam. A

More information

DATA STRUCTURES AND ALGORITHMS

DATA STRUCTURES AND ALGORITHMS DATA STRUCTURES AND ALGORITHMS For COMPUTER SCIENCE DATA STRUCTURES &. ALGORITHMS SYLLABUS Programming and Data Structures: Programming in C. Recursion. Arrays, stacks, queues, linked lists, trees, binary

More information

Faculty of Science FINAL EXAMINATION

Faculty of Science FINAL EXAMINATION Faculty of Science FINAL EXAMINATION COMPUTER SCIENCE COMP 250 INTRODUCTION TO COMPUTER SCIENCE Examiner: Prof. Michael Langer April 27, 2010 Associate Examiner: Mr. Joseph Vybihal 9 A.M. 12 P.M. Instructions:

More information

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted, [1] Big-O Analysis AVERAGE(n) 1. sum 0 2. i 0. while i < n 4. number input_number(). sum sum + number 6. i i + 1 7. mean sum / n 8. return mean Revision Statement no. of times executed 1 1 2 1 n+1 4 n

More information

Analyze the obvious algorithm, 5 points Here is the most obvious algorithm for this problem: (LastLargerElement[A[1..n]:

Analyze the obvious algorithm, 5 points Here is the most obvious algorithm for this problem: (LastLargerElement[A[1..n]: CSE 101 Homework 1 Background (Order and Recurrence Relations), correctness proofs, time analysis, and speeding up algorithms with restructuring, preprocessing and data structures. Due Thursday, April

More information

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR STUDENT IDENTIFICATION NO MULTIMEDIA COLLEGE JALAN GURNEY KIRI 54100 KUALA LUMPUR FIFTH SEMESTER FINAL EXAMINATION, 2014/2015 SESSION PSD2023 ALGORITHM & DATA STRUCTURE DSEW-E-F-2/13 25 MAY 2015 9.00 AM

More information

Analysis of Algorithms. CS 1037a Topic 13

Analysis of Algorithms. CS 1037a Topic 13 Analysis of Algorithms CS 1037a Topic 13 Overview Time complexity - exact count of operations T(n) as a function of input size n - complexity analysis using O(...) bounds - constant time, linear, logarithmic,

More information

COS 226 Midterm Exam, Spring 2009

COS 226 Midterm Exam, Spring 2009 NAME: login ID: precept: COS 226 Midterm Exam, Spring 2009 This test is 10 questions, weighted as indicated. The exam is closed book, except that you are allowed to use a one page cheatsheet. No calculators

More information

Course Review. Cpt S 223 Fall 2009

Course Review. Cpt S 223 Fall 2009 Course Review Cpt S 223 Fall 2009 1 Final Exam When: Tuesday (12/15) 8-10am Where: in class Closed book, closed notes Comprehensive Material for preparation: Lecture slides & class notes Homeworks & program

More information

FINAL EXAMINATION. COMP-250: Introduction to Computer Science - Fall 2010

FINAL EXAMINATION. COMP-250: Introduction to Computer Science - Fall 2010 STUDENT NAME: STUDENT ID: McGill University Faculty of Science School of Computer Science FINAL EXAMINATION COMP-250: Introduction to Computer Science - Fall 2010 December 20, 2010 2:00-5:00 Examiner:

More information

Course Review for Finals. Cpt S 223 Fall 2008

Course Review for Finals. Cpt S 223 Fall 2008 Course Review for Finals Cpt S 223 Fall 2008 1 Course Overview Introduction to advanced data structures Algorithmic asymptotic analysis Programming data structures Program design based on performance i.e.,

More information

Big-O-ology 1 CIS 675: Algorithms January 14, 2019

Big-O-ology 1 CIS 675: Algorithms January 14, 2019 Big-O-ology 1 CIS 675: Algorithms January 14, 2019 1. The problem Consider a carpenter who is building you an addition to your house. You would not think much of this carpenter if she or he couldn t produce

More information

COE428 Lecture Notes Week 1 (Week of January 9, 2017)

COE428 Lecture Notes Week 1 (Week of January 9, 2017) COE428 Lecture Notes: Week 1 1 of 10 COE428 Lecture Notes Week 1 (Week of January 9, 2017) Table of Contents COE428 Lecture Notes Week 1 (Week of January 9, 2017)...1 Announcements...1 Topics...1 Informal

More information

MIDTERM EXAM (HONORS SECTION)

MIDTERM EXAM (HONORS SECTION) Data Structures Course (V22.0102.00X) Professor Yap Fall 2010 MIDTERM EXAM (HONORS SECTION) October 19, 2010 SOLUTIONS Problem 1 TRUE OR FALSE QUESTIONS (4 Points each) Brief justification is required

More information

Overview of Data. 1. Array 2. Linked List 3. Stack 4. Queue

Overview of Data. 1. Array 2. Linked List 3. Stack 4. Queue Overview of Data A data structure is a particular way of organizing data in a computer so that it can be used effectively. The idea is to reduce the space and time complexities of different tasks. Below

More information

Week - 03 Lecture - 18 Recursion. For the last lecture of this week, we will look at recursive functions. (Refer Slide Time: 00:05)

Week - 03 Lecture - 18 Recursion. For the last lecture of this week, we will look at recursive functions. (Refer Slide Time: 00:05) Programming, Data Structures and Algorithms in Python Prof. Madhavan Mukund Department of Computer Science and Engineering Indian Institute of Technology, Madras Week - 03 Lecture - 18 Recursion For the

More information

Binary Search Trees Treesort

Binary Search Trees Treesort Treesort CS 311 Data Structures and Algorithms Lecture Slides Friday, November 13, 2009 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks CHAPPELLG@member.ams.org 2005 2009

More information

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU Course Review for Midterm Exam 1 Cpt S 223 Fall 2011 1 Midterm Exam 1 When: Friday (10/14) 1:10-2pm Where: in class Closed book, closed notes Comprehensive Material for preparation: Lecture slides & in-class

More information

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: September 28, 2016 Edited by Ofir Geri

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: September 28, 2016 Edited by Ofir Geri CS161, Lecture 2 MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: September 28, 2016 Edited by Ofir Geri 1 Introduction Today, we will introduce a fundamental algorithm design paradigm,

More information

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures MIDTERM EXAMINATION Spring 2010 CS301- Data Structures Question No: 1 Which one of the following statement is NOT correct. In linked list the elements are necessarily to be contiguous In linked list the

More information

CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK

CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK Page 1 UNIT I INTRODUCTION 2 marks 1. Why is the need of studying algorithms? From a practical standpoint, a standard set of algorithms from different

More information

(Refer Slide Time: 1:27)

(Refer Slide Time: 1:27) Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi Lecture 1 Introduction to Data Structures and Algorithms Welcome to data

More information

Algorithm Efficiency & Sorting. Algorithm efficiency Big-O notation Searching algorithms Sorting algorithms

Algorithm Efficiency & Sorting. Algorithm efficiency Big-O notation Searching algorithms Sorting algorithms Algorithm Efficiency & Sorting Algorithm efficiency Big-O notation Searching algorithms Sorting algorithms Overview Writing programs to solve problem consists of a large number of decisions how to represent

More information

Fun facts about recursion

Fun facts about recursion Outline examples of recursion principles of recursion review: recursive linked list methods binary search more examples of recursion problem solving using recursion 1 Fun facts about recursion every loop

More information

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: April 1, 2015

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: April 1, 2015 CS161, Lecture 2 MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: April 1, 2015 1 Introduction Today, we will introduce a fundamental algorithm design paradigm, Divide-And-Conquer,

More information

Module 1: Asymptotic Time Complexity and Intro to Abstract Data Types

Module 1: Asymptotic Time Complexity and Intro to Abstract Data Types Module 1: Asymptotic Time Complexity and Intro to Abstract Data Types Dr. Natarajan Meghanathan Professor of Computer Science Jackson State University Jackson, MS 39217 E-mail: natarajan.meghanathan@jsums.edu

More information

ECE250: Algorithms and Data Structures Midterm Review

ECE250: Algorithms and Data Structures Midterm Review 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

More information

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees Computer Science 0 Data Structures Siena College Fall 08 Topic Notes: Trees We ve spent a lot of time looking at a variety of structures where there is a natural linear ordering of the elements in arrays,

More information

Pseudo code of algorithms are to be read by.

Pseudo code of algorithms are to be read by. Cs502 Quiz No1 Complete Solved File Pseudo code of algorithms are to be read by. People RAM Computer Compiler Approach of solving geometric problems by sweeping a line across the plane is called sweep.

More information

Points off Total off Net Score. CS 314 Final Exam Spring 2016

Points off Total off Net Score. CS 314 Final Exam Spring 2016 Points off 1 2 3 4 5 6 Total off Net Score CS 314 Final Exam Spring 2016 Your Name Your UTEID Instructions: 1. There are 6 questions on this test. 100 points available. Scores will be scaled to 300 points.

More information

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Summer I Instructions:

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Summer I Instructions: VIRG INIA POLYTECHNIC INSTITUTE AND STATE U T PROSI M UNI VERSI TY Instructions: Print your name in the space provided below. This examination is closed book and closed notes, aside from the permitted

More information

CSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting. Aaron Bauer Winter 2014

CSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting. Aaron Bauer Winter 2014 CSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting Aaron Bauer Winter 2014 The main problem, stated carefully For now, assume we have n comparable elements in an array and we want

More information

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Spring Instructions:

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Spring Instructions: VIRG INIA POLYTECHNIC INSTITUTE AND STATE U T PROSI M UNI VERSI TY Instructions: Print your name in the space provided below. This examination is closed book and closed notes, aside from the permitted

More information

21# 33# 90# 91# 34# # 39# # # 31# 98# 0# 1# 2# 3# 4# 5# 6# 7# 8# 9# 10# #

21# 33# 90# 91# 34# # 39# # # 31# 98# 0# 1# 2# 3# 4# 5# 6# 7# 8# 9# 10# # 1. Prove that n log n n is Ω(n). York University EECS 11Z Winter 1 Problem Set 3 Instructor: James Elder Solutions log n n. Thus n log n n n n n log n n Ω(n).. Show that n is Ω (n log n). We seek a c >,

More information

Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi

Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi Lecture 20 Priority Queues Today we are going to look at the priority

More information

Introduction to Data Structure

Introduction to Data Structure Introduction to Data Structure CONTENTS 1.1 Basic Terminology 1. Elementary data structure organization 2. Classification of data structure 1.2 Operations on data structures 1.3 Different Approaches to

More information

COMP 161 Lecture Notes 16 Analyzing Search and Sort

COMP 161 Lecture Notes 16 Analyzing Search and Sort COMP 161 Lecture Notes 16 Analyzing Search and Sort In these notes we analyze search and sort. Counting Operations When we analyze the complexity of procedures we re determine the order of the number of

More information

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Divide and Conquer

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Divide and Conquer Computer Science 385 Analysis of Algorithms Siena College Spring 2011 Topic Notes: Divide and Conquer Divide and-conquer is a very common and very powerful algorithm design technique. The general idea:

More information

10/5/2016. Comparing Algorithms. Analyzing Code ( worst case ) Example. Analyzing Code. Binary Search. Linear Search

10/5/2016. Comparing Algorithms. Analyzing Code ( worst case ) Example. Analyzing Code. Binary Search. Linear Search 10/5/2016 CSE373: Data Structures and Algorithms Asymptotic Analysis (Big O,, and ) Steve Tanimoto Autumn 2016 This lecture material represents the work of multiple instructors at the University of Washington.

More information

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination University of Illinois at Urbana-Champaign Department of Computer Science Second Examination CS 225 Data Structures and Software Principles Spring 2014 7-10p, Tuesday, April 8 Name: NetID: Lab Section

More information

University of Waterloo CS240 Winter 2018 Assignment 2. Due Date: Wednesday, Jan. 31st (Part 1) resp. Feb. 7th (Part 2), at 5pm

University of Waterloo CS240 Winter 2018 Assignment 2. Due Date: Wednesday, Jan. 31st (Part 1) resp. Feb. 7th (Part 2), at 5pm University of Waterloo CS240 Winter 2018 Assignment 2 version: 2018-02-04 15:38 Due Date: Wednesday, Jan. 31st (Part 1) resp. Feb. 7th (Part 2), at 5pm Please read the guidelines on submissions: http://www.student.cs.uwaterloo.ca/~cs240/

More information

1. Attempt any three of the following: 15

1. Attempt any three of the following: 15 (Time: 2½ hours) Total Marks: 75 N. B.: (1) All questions are compulsory. (2) Make suitable assumptions wherever necessary and state the assumptions made. (3) Answers to the same question must be written

More information

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305 Q.1 If h is any hashing function and is used to hash n keys in to a table of size m, where n

More information

Divide & Conquer. 2. Conquer the sub-problems by solving them recursively. 1. Divide the problem into number of sub-problems

Divide & Conquer. 2. Conquer the sub-problems by solving them recursively. 1. Divide the problem into number of sub-problems Divide & Conquer Divide & Conquer The Divide & Conquer approach breaks down the problem into multiple smaller sub-problems, solves the sub-problems recursively, then combines the solutions of the sub-problems

More information

Lecture Notes for Advanced Algorithms

Lecture Notes for Advanced Algorithms Lecture Notes for Advanced Algorithms Prof. Bernard Moret September 29, 2011 Notes prepared by Blanc, Eberle, and Jonnalagedda. 1 Average Case Analysis 1.1 Reminders on quicksort and tree sort We start

More information

CSE 373 Spring Midterm. Friday April 21st

CSE 373 Spring Midterm. Friday April 21st CSE 373 Spring 2006 Data Structures and Algorithms Midterm Friday April 21st NAME : Do all your work on these pages. Do not add any pages. Use back pages if necessary. Show your work to get partial credit.

More information

CISC

CISC CISC-235 20180115+17+19 Much of the material we covered this week was already posted in the notes for last week. These notes take up where those left off, and fill in some gaps. We have discussed the notation

More information

1 Introduction. 2 InsertionSort. 2.1 Correctness of InsertionSort

1 Introduction. 2 InsertionSort. 2.1 Correctness of InsertionSort CS 161, Lecture 2 MergeSort, Recurrences 101, and Asymptotic Analysis Scribes: Michael Kim (2015), Ofir Geri (2016), M. Wootters (2017) Date: September 27, 2017 Adapted From Virginia Williams lecture notes

More information