{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Handout 20

# Handout 20 - Mehran Sahami CS103B Handout#20 February 6...

This preview shows pages 1–3. Sign up to view the full content.

Mehran Sahami Handout #20 CS103B February 6, 2009 Practice Midterm Problem Solutions (including some hints and shortcuts you might find useful for exams) 1. Order in which nodes are processed if we were to do a reverse preorder traversal on the tree: a, c, f, j, i, b, e, h, d, g Shortcut: this is just the “Columbus” method going around the tree starting at the right side and going “clock - wise” around the tree. 2. Give a tight big-Oh bound in terms of n for the function foo : To derive the answer we work from the outermost loop inwards. The body of the outermost for loop that is indexed by i executes O(n) times since it simply iterates from 1 up to n, inclusive. The next for loop (working inwards) is indexed by j and executes O( i ) times since it simply iterates from 0 up to ( i -1), inclusive. Thus, the number of times that the body of the for loop indexed by j is executed is determined by the series: 1 + 2 + ... + n = n(n+1)/2 = O(n 2 ). The innermost for loop (indexed by k , which is in the body of the loop indexed by j ), by itself would be executed O(n) times (regardless of the value of i or j ) since it simply iterates from 0 up to n-1, inclusive. Now we combine the number of times that the innermost for loop is executed with the fact that it is in the body of the for loop indexed by j. The instructions in the body of the innermost for loop (indexed by k ) are the most executed. By multiplying the number of times the body of the for loop indexed by j is executed (O(n 2 )) with the number of times that the body of the for loop indexed by k is executed (O(n)), we obtain: O(n 2 ) * O(n) = O(n 3 ) So the overall big-Oh running time of foo is O(n 3 ). Hint: note that sometimes it’s easier to start by analyzing the part of the function that looks familiar to something we’ve seen before (like the outer two for loops that we’ve seen in many examples) and then using that to help derive the final result. 3. Find a tight big-Oh bound for the following recurrence relation using repeated substitution. T(0) = 1 T(n) = T(n-1) + n 2 Using iterative substitution, we get: T(n) = T(n-2) + (n-1) 2 + n 2 T(n) = T(n-3) + (n-2) 2 + (n-1) 2 + n 2 T(n) = T(n-4) + (n-3) 2 + (n-2) 2 + (n-1) 2 + n 2

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 Generalizing in terms of i gives us: T(n) = T(n-i) + 1 0 2 ) ( i j j n Setting i = n, yields: T(n) = T(0) + 1 0 2 ) ( n j j n = 1 + 1 0 2 ) ( n j j n The simplest way to solve this problem at this point, is simply to bound the summation (as tightly as we can without using j ) as follows: 1 0 2 ) ( n j j n <= 1 0 2 ) ( n j n = n 3 This yields: T(n) <= 1 + n 3 = O(n 3 ) The derivation above would be sufficient for full credit, and is what we would probably expect to get as a fully correct answer. However, just for completeness, we actually expand the sum further to show a few more tricks you might find useful.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern