Algorithm Analysis II.pdf - Q Overall Asymptotic Runtime Bound for dup1 Demo Q Print Party Attempt 1 Find a simple f(N such that the runtime R(N ∈

Algorithm Analysis II.pdf - Q Overall Asymptotic Runtime...

This preview shows page 1 out of 4 pages.

You've reached the end of your free preview.

Want to read all 4 pages?

Unformatted text preview: Q Overall Asymptotic Runtime Bound for dup1 Demo Q Print Party: Attempt 1 Find a simple f(N) such that the runtime R(N) ∈ Θ(f(N)). Give an overall asymptotic runtime bound for R as a combination of Θ, O, and/or Ω notation. Take into account both the best and the worst case runtimes (Rbest and Rworst). A. 1 B. log N C. N D. N log N E. N2 F. Other void printParty(int N) { for (int i = 1; i <= N; i *= 2) { for (int j = 0; j < i; j += 1) { System.out.println("hello"); } } } Note that there’s only one case. No separate case analysis! 3 Q1: Give an overall asymptotic runtime bound for R as a combination of Θ, O, and/or Ω notation. Take into account both the best and the worst case runtimes (Rbest and Rworst). 8 Q1: Find a simple f(N) such that the runtime R(N) ∈ Θ(f(N)). ?: How do we know that there’s only one case to consider? void printParty(int N) { 0 1 i Repeat After Me… for (int i = 1; i <= N; i *= 2) 2 for (int j = 0; j < i; j += 1) 3 System.out.println("hello"); 4 There is no magic shortcut for these problems (except in a few well-behaved cases). We’ll expect you to know these two summations since they’re common patterns. } 5 0 1 2 3 4 Demo Find a simple f(N) s.t. the runtime R(N) ∈ Θ(f(N)). 5 j N : 1 2 3 4 5 6 7 C(N): 1 3 3 7 7 7 7 15 15 15 15 15 15 15 15 31 31 31 8 Strategies. 9 10 11 12 13 14 15 16 17 18 Print Party: Attempt 2 1. Find the exact count of steps. 2. Write out examples. 3. Use a geometric argument–visualizations! 10 14 Numerical Linear Algebra (Lloyed N. Trefethen, David Bau, III/SIAM) Let the cost model C(N) be the number of calls to println for a given N. This is our representative operation for figuring out the runtime. Real world programs are often messy and difficult to model. ?: What’s different between these two summations? ?: For each N, predict C(N). ?: How did we apply these strategies to analyze printParty? Q Informal Recursion Analysis if (n <= 1) Find a simple f(N) such that the runtime R(N) ∈ Θ(f(N)). Inspect the example and give the order of growth of the runtime as a function of N. A. 1 B. log N C. N D. E. N 2 Recursion and Exact Counts public static int f3(int n) { if (n <= 1) Find a simple f(N) such that the runtime R(N) ∈ Θ(f(N)). return 1; return f3(n-1) + f3(n-1); Approach 2: Count number of calls to f3, given by C(N). } return 1; return f3(n-1) + f3(n-1); } 4 4 3 1 2 1 3 3 2 2 N Q public static int f3(int n) { 1 2 1 1 Give a simple, exact expression for C(N). 2 1 1 2 1 1 3 2 1 1 2 1 16 1 2 1 1 1 19 ?: What does each node represent in the tree on the right? ?: What is the exact value of the last term in the sum for C(N)? Q1: Find a simple f(N) such that the runtime R(N) ∈ Θ(f(N)). Q1: Give a simple, exact expression for C(N). The Merge Operation Merge Sort Merge sort algorithm merges every layer. Given two sorted arrays, the merge operation combines them into a single sorted array by successively copying the smallest item from the two arrays into a target array. 2 3 6 10 11 4 5 7 M ~64 1. 2. 3. 4. 8 2 3 4 5 6 7 8 10 11 Top layer: ~64 AU Second layer: 2(~32 AU) = ~64 AU Third layer: 4(~16 AU) = ~64 AU ith layer: 2i - 1(~64 AU / 2i - 1) = ~64 AU M M If array is of size 1, return. Merge sort the left half. Merge sort the right half. Merge the two sorted halves. ~32 N=32 M ~16 For N = 64, the total runtime is ~384 AU. • • • • N=64 M 16 16 M ~8 ~32 N=32 M 16 M 8 8 ··· ··· 25 ?: What is a cost model that we can use to evaluate the runtime of the merge operation? M 16 29 ?: How does the call tree for merge sort differ from the example we saw in f3? ?: How do these differences affect our runtime analysis? ...
View Full Document

  • Spring '14

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors