Recursion Computational Complexity 1 Recursively Move Smallest to































- Slides: 31
Recursion: Computational Complexity 1
Recursively Move Smallest to Front public class Recursion { public static void min. To. Front(List<Integer> t) { if (t. size() < 2) { return; } Recursion. min. To. Front(t. sub. List(1, t. size())); int first = t. get(0); int second = t. get(1); if (second < first) { t. set(0, second); t. set(1, first); } } } 2
Estimating complexity the basic strategy for estimating complexity: 1. 2. 3. 3 for each line of code, estimate its number of elementary instructions for each line of code, determine how often it is executed determine the total number of elementary instructions
Elementary instructions what is an elementary instruction? for our purposes, any expression that can be computed in a constant amount of time examples: 4 declaring a variable assignment (=) arithmetic (+, -, *, /, %) comparison (<, >, ==, !=) Boolean expressions (||, &&, !) if, else return statement
Estimating complexity count the number of elementary operations in each line of min. To. Front assume that the following are all elementary operations: 5 t. size() t. get(0) t. get(1) t. set(0, . . . ) t. set(1, . . . ) t. sub. List(x, y) leave the line with the recursive call blank for now
Recursively Move Smallest to Front public class Recursion { public static void min. To. Front(List<Integer> t) { if (t. size() < 2) { return; } Recursion. min. To. Front(t. sub. List(1, t. size())); int first = t. get(0); int second = t. get(1); if (second < first) { t. set(0, second); t. set(1, first); } } } 6 number of elementary ops 3 1 3 3 2 1 1
Estimating complexity for each line of code, determine how often it is executed 7
Recursively Move Smallest to Front public class Recursion { public static void min. To. Front(List<Integer> t) { if (t. size() < 2) { return; } Recursion. min. To. Front(t. sub. List(1, t. size())); int first = t. get(0); int second = t. get(1); if (second < first) { t. set(0, second); t. set(1, first); } } } 8 1 1 or 0 1 or 0
Total number of operations 9
Total number of operations public class Recursion { public static void min. To. Front(List<Integer> t) { if (t. size() < 2) { return; } Recursion. min. To. Front(t. sub. List(1, t. size())); int first = t. get(0); 1 elementary operation int second = t. get(1); if (second < first) { t. set(0, second); t. set(1, first); } } } 10
Total number of operations public class Recursion { public static void min. To. Front(List<Integer> t) { if (t. size() < 2) { return; } Recursion. min. To. Front(t. sub. List(1, t. size())); int first = t. get(0); 1 elementary operation int second = t. get(1); if (second < first) { t. set(0, second); t. set(1, first); } } } 11
Total number of operations public class Recursion { public static void min. To. Front(List<Integer> t) { if (t. size() < 2) { return; } Recursion. min. To. Front(t. sub. List(1, t. size())); int first = t. get(0); int second = t. get(1); if (second < first) { t. set(0, second); t. set(1, first); } } } 12
Total number of operations public class Recursion { public static void min. To. Front(List<Integer> t) { if (t. size() < 2) { return; } Recursion. min. To. Front(t. sub. List(1, t. size())); int first = t. get(0); int second = t. get(1); 1 elementary operation if (second < first) { t. set(0, second); 1 elementary operation t. set(1, first); } } } 13
Total number of operations public class Recursion { public static void min. To. Front(List<Integer> t) { if (t. size() < 2) { these lines run if the return; base case is true } Recursion. min. To. Front(t. sub. List(1, t. size())); int first = t. get(0); int second = t. get(1); if (second < first) { t. set(0, second); t. set(1, first); } } } 14
Total number of operations public class Recursion { public static void min. To. Front(List<Integer> t) { if (t. size() < 2) { return; } Recursion. min. To. Front(t. sub. List(1, t. size())); int first = t. get(0); int second = t. get(1); if (second < first) { t. set(0, second); t. set(1, first); } } } 15 3*1 1*1 (T(n-1) + 2) * 1 3*1 2*1 1*1
Total number of operations 16
Total number of operations public class Recursion { public static void min. To. Front(List<Integer> t) { this line runs if the base case is not true if (t. size() < 2) { return; } Recursion. min. To. Front(t. sub. List(1, t. size())); these lines run if the int first = t. get(0); base case is not true int second = t. get(1); if (second < first) { these lines might run if the t. set(0, second); base case is not true t. set(1, first); } } } 17
Total number of operations when counting the total number of operations, we often consider the worst case scenario 18 let’s assume that the lines that might run always run
Total number of operations public class Recursion { public static void min. To. Front(List<Integer> t) { if (t. size() < 2) { return; } Recursion. min. To. Front(t. sub. List(1, t. size())); int first = t. get(0); int second = t. get(1); if (second < first) { t. set(0, second); t. set(1, first); } } } 19 3*1 1*1 (T(n-1) + 2) * 1 3*1 2*1 1*1
Total number of operations 20
Selection Sort public class Recursion { // min. To. Front not shown public static void selection. Sort(List<Integer> t) { if (t. size() > 1) { Recursion. min. To. Front(t); Recursion. selection. Sort(t. sub. List(1, t. size())); } } } 21 number of elementary ops?
Total number of operations 22
Solving the recurrence relation 23
Solving the recurrence relation 24
Solving the recurrence relation 25
Solving the recurrence relation there is clearly a pattern 26
Solving the recurrence relation 27
Big-O notation 28
Try to solve the recurrence relation 29
Try to solve the recurrence relation 30
Try to solve the recurrence relation 31