Lecture 3: Asymptotic Data Structures and Analysis + Recurrences Algorithms CSE 373 SU 18 – BEN JONES 1
Warmup – Write a model and find Big-O for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { System.out.println (“Hello!”); } } 𝑜 Summation 𝑗 1 + 2 + 3 + 4 +… + n = 𝑗=1 Definition: Summation 𝑐 = f(a) + f(a + 1) + f(a + 2) + … + f(b -2) + f(b-1) + f(b) 𝑔(𝑗) 𝑗=𝑏 𝑜−1 𝑗−1 T(n) = 𝑑 𝑗=0 𝑘=0 CSE 373 SP 18 - KASEY CHAMPION 2
Simplifying Summations for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { System.out.println (“Hello!”); } } 𝑜−1 𝑗−1 𝑜−1 Summation of a constant T(n) = = 𝑑 𝑑𝑗 𝑗=0 𝑘=0 𝑗=0 𝑜−1 Factoring out a constant = 𝑑 𝑗 𝑗=0 𝑑 𝑜 𝑜 − 1 Gauss’s Identity = 2 2 𝑜 2 − 𝑑 𝑑 = O(n 2 ) 2 𝑜 CSE 373 WI 18 – MICHAEL LEE 3
Function Modeling: Recursion public int factorial(int n) { if (n == 0 || n == 1) { +3 +3 +c +c return 1; +1 +1 } else { return n * factorial(n – 1); +???? ?? } CSE 373 SP 18 - KASEY CHAMPION 4
Function Modeling: Recursion public int factorial(int n) { if (n == 0 || n == 1) { +c +c 1 return 1; } else { return n * factorial(n – 1); +T(n (n-1) 1) } +c +c 2 C 1 when n = 0 or 1 T(n) = otherwise C 2 + T(n-1) Definition: Recurrence Mathematical equation that recursively defines a sequence The notation above is like an if / else statement CSE 373 SP 18 - KASEY CHAMPION 5
Unfolding Method C 1 when n = 0 or 1 T(n) = C 2 + T(n-1) otherwise T(3) = = 2 C 2 + C 1 C 2 + T(3 – 1) = C 2 + ( C 2 + T(2 – 1)) = C 2 + ( C 2 + ( C 1 )) 𝑜−1 T(n) = C 1 + 𝐷 2 𝑗=0 Summation of a constant T(n) = C 1 + (n-1) C 2 CSE 373 SP 18 - KASEY CHAMPION 6
Announcements - Course background survey due by Friday - HW 1 is Due Friday - HW 2 Assigned on Friday – Partner selection forms due by 11:59pm Thursday day https://go //goo.gl/f o.gl/for orms/r ms/rVrV rVUkFDds kFDdsqI qI8pkD2 8pkD2 CSE 373 SU 18 – BEN JONES 7
A Detour on Style - Checkstyle for project - No packages for HW1 - Braces for blocks - Good style is easy to read - Javadoc on public methods (not needed if interface has Javadoc) - Comment non-obvious code - Self-Documenting code is better than commented code - Good variable and method names go a long way towards this - No magic numbers (numbers larger than 2 or 3 should probably be class constants unless there’s a really good reason) - No code duplication - Use Idioms! ex. for (int I = 0; I < 10; i++) instead of for (int I = 0; I == 9; i = i + 1) naming: CONSTANTS_USE_CAPS, ClassName, methodName CSE 373 SU 18 – BEN JONES 8
Tree Method Idea: - Since we’re making recursive calls, let’s just draw out a tree, with one node for each recursive call. -Each of those nodes will do some work, and (if they make more recursive calls) have children. -If we can just add up all the work, we can find a big- Θ bound. CSE 373 SU 18 - ROBBIE WEBER 9
Solving Recurrences I: Binary Search 0. Draw the tree. 1 𝑥ℎ𝑓𝑜 𝑜 ≤ 1 1. What is the input size at level 𝑗 ? 𝑈 𝑜 = 𝑈 𝑜 2𝑈 𝑜 2 + 𝑜 𝑝𝑢ℎ𝑓𝑠𝑥 2 + 1 𝑝𝑢ℎ𝑓𝑠𝑥𝑗𝑡𝑓 2. What is the number of nodes at level 𝑗 ? 3. What is the work done at recursive level 𝑗 ? 4. What is the last level of the tree? 5. What is the work done at the base case? 6. Sum over all levels (using 3,5). 7. Simplify CSE 373 SU 18 - ROBBIE WEBER 10
Solving Recurrences I: Binary Search 0. Draw the tree. 1 𝑥ℎ𝑓𝑜 𝑜 ≤ 1 1. What is the input size at level 𝑗 ? 𝑈 𝑜 = 𝑈 𝑜 2𝑈 𝑜 2 + 𝑜 𝑝𝑢ℎ𝑓𝑠𝑥 2 + 1 𝑝𝑢ℎ𝑓𝑠𝑥𝑗𝑡𝑓 2. What is the number of nodes at level 𝑗 ? 3. What is the work done at recursive level 𝑗 ? 4. What is the last level of the tree? 1 5. What is the work done at the base case? 6. Sum over all levels (using 3,5). 1 7. Simplify 1 … 1 CSE 373 SU 18 - ROBBIE WEBER 11
Solving Recurrences I: Binary Search 0. Draw the tree. 1 𝑥ℎ𝑓𝑜 𝑜 ≤ 1 1. What is the input size at level 𝑗 ? 𝑈 𝑜 = 2𝑈 𝑜 𝑈 𝑜 2 + 1 𝑝𝑢ℎ𝑓𝑠𝑥𝑗𝑡𝑓 2 + 𝑜 𝑝𝑢ℎ𝑓𝑠𝑥𝑗𝑡𝑓 2. What is the number of nodes at level 𝑗 ? 3. What is the work done at recursive level 𝑗 ? 4. What is the last level of the tree? Level vel Input Size Work/cal /call Work/l /lev evel el 5. What is the work done at the base case? 0 𝑜 1 1 6. Sum over all levels (using 3,5). 1 𝑜/2 1 1 7. Simplify 𝑜/2 2 2 1 1 𝑜/2 𝑗 𝑗 1 1 log 2 𝑜 1 1 1 log 2 𝑜−1 1 + 1 = log 2 𝑜 σ 𝑗=0 CSE 373 SU 18 - ROBBIE WEBER 12
Solving Recurrences II: 1 𝑥ℎ𝑓𝑜 𝑜 ≤ 1 n 𝑈 𝑜 = 2𝑈 𝑜 2 + 𝑜 𝑝𝑢ℎ𝑓𝑠𝑥𝑗𝑡𝑓 n n 2 2 n n n n 4 4 4 4 n n n n n n n n 8 8 8 8 8 8 8 8 … … … … … … … … … … … … … … … … 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 CSE 373 SU 18 - ROBBIE WEBER 13
Tree Method Formulas 1 𝑥ℎ𝑓𝑜 𝑜 ≤ 1 𝑈 𝑜 = 2𝑈 𝑜 2 + 𝑜 𝑝𝑢ℎ𝑓𝑠𝑥𝑗𝑡𝑓 How much wo work is done by recur ursiv ive e levels els (branch ch nodes es)? )? 1. What is the input size at level 𝑗 ? (𝑜/2 𝑗 ) - 𝑗 = 0 is overall root level. 2. At each level 𝑗 , how many calls are there? 2 𝑗 3. At each level 𝑗, how much work is done?? 2 𝑗 (𝑜/2 𝑗 ) = 𝑜 𝑚𝑏𝑡𝑢𝑆𝑓𝑑𝑣𝑠𝑡𝑗𝑤𝑓𝑀𝑓𝑤𝑓𝑚 log 2 𝑜−1 𝑜 𝑆𝑓𝑑𝑣𝑠𝑡𝑗𝑤𝑓 𝑥𝑝𝑠𝑙 = 𝑐𝑠𝑏𝑜𝑑ℎ𝑂𝑣𝑛 𝑗 𝑐𝑠𝑏𝑜𝑑ℎ𝑋𝑝𝑠𝑙(𝑗) 2 𝑗 2 𝑗 𝑗=0 𝑗=0 How much wo work is done by the base case e level vel (leaf af nodes es)? )? 4. What is the last level of the tree? (𝑜/2 𝑗 ) = 1 → 2 𝑗 = 𝑜 → 𝑗 = log 2 𝑜 5. What is the work done at the last level? 1 ⋅ 2 log 2 𝑜 = 𝑜 𝑂𝑝𝑜𝑆𝑓𝑑𝑣𝑠𝑡𝑗𝑤𝑓 𝑥𝑝𝑠𝑙 = 𝑋𝑝𝑠𝑙𝑄𝑓𝑠𝐶𝑏𝑡𝑓𝐷𝑏𝑡𝑓 × 𝑜𝑣𝑛𝑐𝑓𝑠𝐷𝑏𝑚𝑚𝑡 log 2 𝑜−1 𝑜 6. Combine and Simplify 2 𝑗 𝑈 𝑜 = 2 𝑗 + 𝑜 = 𝑜 log 2 𝑜 + 𝑜 𝑗=0 CSE 373 SU 18 - ROBBIE WEBER 14
Solving Recurrences III 5 𝑥ℎ𝑓𝑜 𝑜 ≤ 4 𝑈 𝑜 = 3𝑈 𝑜 4 + 𝑑𝑜 2 𝑝𝑢ℎ𝑓𝑠𝑥𝑗𝑡𝑓 𝑑n 2 𝑑𝑜 2 Answer the following questions: 2 2 c 𝑜 𝑑 n 1. What is input size on 2 2 2 c 𝑜 𝑑 n 𝑑 n 2 𝑑 𝑜 level 𝑗 ? 4 4 4 4 4 4 2. Number of nodes at level 𝑗 ? 3. Work done at 2 2 recursive level 𝑗 ? 2 2 n n 2 n 2 n 2 2 n 2 n n n n 𝑑 𝑑 𝑑 𝑑 𝑑 𝑑 𝑑 𝑑 𝑑 4. Last level of tree? 16 16 16 16 16 16 16 16 16 5. Work done at base case? … … … … … … … … … … … … … … … … … … … … … … … … … … … 6. What is sum over all levels? 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 CSE 373 SU 18 - ROBBIE WEBER 15
Solving Recurrences III 5 𝑥ℎ𝑓𝑜 𝑜 ≤ 4 3𝑈 𝑜 𝑈 𝑜 = 4 + 𝑑𝑜 2 𝑝𝑢ℎ𝑓𝑠𝑥𝑗𝑡𝑓 Number er of Work rk per Work rk per 𝑜 Level el (i) 1. Input size on level 𝑗 ? Nodes es Node Level el 4 𝑗 0 1 𝑑𝑜 2 𝑑𝑜 2 2 3 𝑑 𝑜 1 3 2. How many calls on level 𝑗 ? 16 𝑑𝑜 2 3 𝑗 4 2 𝑜 2 3 2 3 2 𝑑𝑜 2 𝑑 𝑗 4 2 2 16 𝑜 𝑜 2 3 3. How much work on level 𝑗 ? 3 𝑗 𝑑 𝑑𝑜 2 𝑑 = 𝑗 4 𝑗 4 𝑗 2 16 𝑜 3 3 𝑗 𝑗 𝑑𝑜 2 𝑑 4 𝑗 16 Base = 4. What is the last level? 5 When 𝑜 4 𝑗 = 4 → log 4 𝑜 − 1 3 log 4 𝑜−1 3 𝑜 log 4 3 5 log 4 𝑜 − 1 6. Combining it all together… 5. A. How much work for each leaf node? 5 log 4 𝑜 −2 𝑗 3 5 𝑑𝑜 2 + 3 𝑜 log 4 3 𝑈 𝑜 = B. How many base case calls? 3 log 4 𝑜−1 = 3 log 4 𝑜 = 𝑜 log 4 3 16 𝑗=0 power of a log 3 3 𝑦 log 𝑐 𝑧 = 𝑧 log 𝑐 𝑦 CSE 373 SU 18 - ROBBIE WEBER 16
Solving Recurrences III 7. Simplify… factoring out a log 4 𝑜 −2 log 4 𝑜 −2 𝑗 constant 𝑗 3 5 3 5 𝑑𝑜 2 + 3 𝑜 log 4 3 𝑈 𝑜 = 𝑑𝑜 2 3 𝑜 log 4 3 𝑈 𝑜 = + 𝑐 𝑐 16 16 𝑗=0 𝑗=0 𝑑𝑔(𝑗) = 𝑑 𝑔(𝑗) 𝑗=𝑏 𝑗=𝑏 Closed form: finite geometric series log 4 𝑜−1 3 − 1 𝑜−1 5 𝑦 𝑗 = 𝑦 𝑜 − 1 16 𝑈 𝑜 = 𝑑𝑜 2 3 𝑜 log 4 3 + 3 16 − 1 𝑦 − 1 𝑗=0 If we’re trying to prove upper bound… infinite geometric 1 + 5 series 𝑈 𝑜 ≤ 𝑑𝑜 2 3 𝑜 log 4 3 ∞ 𝑗 1 − 3 ∞ 3 5 𝑈 𝑜 ≤ 𝑑𝑜 2 1 3 𝑜 log 4 3 + 𝑦 𝑗 = 16 16 1 − 𝑦 𝑗=0 𝑗=0 𝑈 𝑜 ∈ 𝑃(𝑜 2 ) when -1 < x < 1 CSE 373 SU 18 - ROBBIE WEBER 17
Another Example 1 if 𝑜 = 1 𝑈 𝑜 = ቐ 2 if 𝑜 = 2 𝑈 𝑜 − 2 + 4 otherwise CSE 373 SU 18 - ROBBIE WEBER 18
Is there an easier way? We do all that effort to get an exact formula for the number of operations, But we usually only care about the Θ bound. There must be an easier way Sometimes, there is! CSE 373 SU 18 - ROBBIE WEBER 19
Recommend
More recommend