Foundations of Computer Science Last Time Lecture 9 Sums And Asymptotics Computing Sums Asymptotics: big-Θ( · ), big- O ( · ), big-Ω( · ) The Integration Method 1 Structural induction: proofs about recursively defined sets. ◮ Matched parentheses. ◮ N ( − 1) k +1 k 2 ∞ ◮ Palindromes. � = ?? k 3 + 1 ◮ Arithmetic expressions. k =1 ◮ Rooted Binary Trees (RBT). Creator: Malik Magdon-Ismail Sums And Asymptotics: 2 / 16 Today → Today: Sums And Asymptotics Maximum Substring Sum 1 − 1 − 1 2 3 4 − 1 − 1 2 3 − 4 1 2 − 1 − 2 1 max. substring sum= 12 More generally, compute the maximum substring sum for Maximum Substring Sum 1 a 1 a 2 a 3 a 4 · · · a n − 1 a n Different algorithms have different running times ( n measures the “size” of the input), Computing Sums 2 j n n T 1 ( n ) = 2 + � 2 + � 5 + k = i 2 � . (3 for loops) i =1 j = i n n Asymptotics: Big-Theta, Big-Oh and Big-Omega T 2 ( n ) = 2 + 3 + j = i 6 . 3 � � (2 for loops) i =1 3 n = 1; 2 T 3 ( 1 T 3 ( n ) = 2 n ) + 6 n + 9 n > 1 and even ; (recursive) Integration Method 4 T ( 1 2 ( n + 1)) + T ( 1 2 ( n − 1)) + 6 n + 9 n > 1 and odd . n T 4 ( n ) = 5 + i =1 10 . � (1 for loops) n (What does i =1 mean: Pop Quiz 9.1) � Which algorithm is best? Creator: Malik Magdon-Ismail Sums And Asymptotics: 3 / 16 Maximum Substring Sum → Creator: Malik Magdon-Ismail Sums And Asymptotics: 4 / 16 Evaluate the Runtimes →
Evaluate the Runtimes Computing Sums: Tool 1: Constant Rule 10 S 1 = i =1 3 = 3 + 3 + 3 + 3 + 3 + 3 + 3 + 3 + 3 + 3 � 3 × 10 n 1 2 3 4 5 6 7 8 9 10 10 � S 2 = i =1 j = j + j + j + j + j + j + j + j + j + j j × 10 T 1 ( n ) 11 29 58 100 157 231 324 438 575 737 11 26 47 74 107 146 191 242 299 362 T 2 ( n ) 10 1 S 3 = i =1 i = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 � 2 × 10 × (10 + 1) T 3 ( n ) 3 27 57 87 123 159 195 231 273 315 T 4 ( n ) 15 25 35 45 55 65 75 85 95 105 The index of summation is i in these examples. T 2 is better than T 1 ; T 2 versus T 3 ??? What about T 4 ? Constants (independent of summation index) can be taken outside the sum. We need: 10 10 10 10 S 1 = i =1 3 = 3 � i =1 1 = 3 × 10 � S 2 = i =1 j = j � i =1 1 = j × 10 . � 1 Simple formulas for T 1 ( n ) , . . . , T 4 ( n ) : we need to compute sums and solve recurrences. 2 A way to compare runtime- functions that captures the essence of the algorithm. n Pop Quiz 9.2 Compute T 4 ( n ) = 5 + i =1 10 . � Creator: Malik Magdon-Ismail Sums And Asymptotics: 5 / 16 Constant Rule → Creator: Malik Magdon-Ismail Sums And Asymptotics: 6 / 16 Addition Rule → Computing Sums: Tool 2: Addition Rule Computing Sums: Tool 3: Common Sums n i =0 2 i = 2 n +1 − 1 n n i = k 1 = n + 1 − k � 1 � i =1 i = � 2 n ( n + 1) n 2 i = 2 − 1 1 i =1 i 2 = n n 5 i =1 f ( x ) = nf ( x ) � 1 � 6 n ( n + 1)(2 n + 1) � i =1 ( i + i 2 ) � S = 2 n i =0 = (1 + 1 2 ) + (2 + 2 2 ) + (3 + 3 2 ) + (4 + 4 2 ) + (5 + 5 2 ) i =0 r i = 1 − r n +1 n i =1 i 3 = n n 4 n 2 ( n + 1) 2 � 1 � � ( r � =1) i =1 log i = log n ! = (1 + 2 + 3 + 4 + 5) + (1 2 + 2 2 + 3 2 + 4 2 + 5 2 ) 1 − r (rearrange terms) 5 5 i =1 i 2 . = i =1 i + � � n i =1 (1 + 2 i + 2 i +2 ) � Example: n n n n i =1 (1 + 2 i + 2 i +2 ) = i =1 2 i +2 � i =1 1 + � i =1 2 i + � � (addition rule) The sum of terms added together is the addition of the individual sums. n n n � i ( a ( i ) + b ( i ) + c ( i ) + · · · ) = � i a ( i ) + � i b ( i ) + � i c ( i ) + · · · i =1 2 i = i =1 1 + 2 � i =1 i + 4 � � (constant rule) 2 n ( n + 1) + 4 · (2 n +1 − 1 − 1 ) = n + 2 × 1 (common sums) = n + n ( n + 1) + 2 n +3 − 8 (common sums) Creator: Malik Magdon-Ismail Sums And Asymptotics: 7 / 16 Common Sums → Creator: Malik Magdon-Ismail Sums And Asymptotics: 8 / 16 Nested Sums →
n n Computing a Formula for T 2 ( n ) = 2 + 3 + j = i 6 Computing Sums: Tool 3: Nested Sum Rule � � i =1 n n n n n = 2 + � � � � � T 2 ( n ) = 2 + 3 + j = i 6 i =1 3 + j = i 6 (sum rule) 3 3 3 i S 1 = � j =1 1; � S 2 = � j =1 1 . � i =1 i =1 i =1 i =1 n n n = 2 + 3 i =1 1 + � � j = i 6 � (constant rule) i =1 n n = 2 + 3 n + � j = i 6 � (common sum) To compute a nested sum, start with the innermost sum and proceed outward. i =1 n n = 2 + 3 n + � j = i 6 � (innermost sum) 3 3 3 i =1 S 1 = j =1 1 + � j =1 1 + � j =1 1 � = 3 + 3 + 3 = 9 . n n = 2 + 3 n + 6 � j = i 1 � ( i =1) ( i =2) ( i =3) (constant rule) i =1 1 2 3 S 2 = j =1 1 + � j =1 1 + � j =1 1 � = 1 + 2 + 3 = 6 . n = 2 + 3 n + 6 i =1 ( n + 1 − i ) � (common sum) ( i =1) ( i =2) ( i =3) = 2 + 3 n + 6( n + ( n − 1) + · · · + 1) More generally: n i n i n = 2 + 3 n + 6 × 1 2 n ( n + 1) i =1 i = 1 S ( n ) = � j =1 1 = � � j =1 1 � = � 2 n ( n + 1) . (common sum) i =1 i =1 � �� � f ( i )= i = 2 + 6 n + 3 n 2 (algebra) Creator: Malik Magdon-Ismail Sums And Asymptotics: 9 / 16 Computing T 2 ( n ) → Creator: Malik Magdon-Ismail Sums And Asymptotics: 10 / 16 Practice → n i Practice: Compute a Formula for the Sum j =1 ij � � Summary of Maximum Substring Sum Algorithms i =1 100 Runtimes T 1 ( n ) 2 n 2 + 1 (Running Time)/n 80 T 2 ( n ) n i n i T 1 ( n ) = 2 + 31 6 n + 7 3 n 3 � � � � j =1 ij = j =1 ij (innermost sum) 60 i =1 i =1 T 2 ( n ) = 2 + 6 n + 3 n 2 T 3 ( n ) n i 40 = i =1 i � j =1 j � (constant rule) 3 n (log 2 n + 1) − 9 ≤ T 3 ( n ) ≤ 12 n (log 2 n + 3) − 9 20 T 4 ( n ) T 4 ( n ) = 5 + 10 n n i =1 i × 1 = � 2 i ( i + 1) 0 (common sum) 10 20 30 40 50 n (“simple” formulas for T 1 ( n ) , . . . , T 4 ( n )) i =1 ( i 3 + i 2 ) n 1 = � (algebra, constant rule) 2 n n i =1 i 3 + 1 i =1 i 2 = 1 � � (sum rule) 2 2 So, which algorithm is best? Computers solve problems with big inputs. We care about large n . 8 n 2 ( n + 1) 2 + 1 1 = 12 n ( n + 1)(2 n + 1) (common sums) Compare runtimes asymptotically in the input size n . That is n → ∞ . 8 n 2 + 5 12 n 3 + 1 12 n + 3 1 8 n 4 = (algebra) Ignore additive and multiplicative constants (minutia). We care about growth rate . Algorithm 4 is linear in n , T 4 ( n ) → constant. n Creator: Malik Magdon-Ismail Sums And Asymptotics: 11 / 16 Summary of Max. Substring Sum → Creator: Malik Magdon-Ismail Sums And Asymptotics: 12 / 16 Linear Functions Θ( n ) →
Recommend
More recommend