Embed presentation
Download as PDF, PPTX

















![a) Another approachLet 𝑀(𝑛) be the number of multiplications made by the algorithm.Recurrence relation equation: 𝑀(𝑛) = 𝑀(𝑛 − 1) + 2 and 𝑀 1 = 0• Using backward substitution:𝑀(𝑛) = 𝑀(𝑛 − 1) + 2= [ 𝑀(𝑛 − 2) + 2 ] + 2 = 𝑀(𝑛 − 2) + 4= [ 𝑀(𝑛 − 3) + 2 ] + 4 = 𝑀(𝑛 − 3) + 6= [ 𝑀(𝑛 − 4) + 2 ] + 6 = 𝑀(𝑛 − 4) + 8𝑀(𝑛) = 𝑀 𝑛 − 𝑖 + 2𝑖= 𝑀 1 + 2(𝑛 − 1)= 2 𝑛 − 1Basic Operation𝑛 − 𝑖 = 1𝑖 = 𝑛 − 1](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fsection4-240819185317-60831acb%2f75%2fAlgorithms-Analysis-Design-Lecture-4-18-2048.jpg&f=jpg&w=240)








![b) Another approachLet 𝐴(𝑛) be the number of additions made by the algorithm.Recurrence relation equation: 𝐴(𝑛) = 2𝐴(𝑛 − 1) + 1 and 𝐴 0 = 0• Using backward substitution:𝐴(𝑛) = 2𝐴(𝑛 − 1) + 1= 2[ 2𝐴(𝑛 − 2) + 1 ] + 1 = 4𝐴(𝑛 − 2) + 3= 4[ 2𝐴(𝑛 − 3) + 1 ] + 3 = 8𝐴(𝑛 − 3) + 7= 8[ 2𝐴(𝑛 − 4) + 1 ] + 7 = 16𝐴(𝑛 − 4) + 15𝐴 𝑛 = 2𝑖𝐴 𝑛 − 𝑖 + (2𝑖 − 1)= 2𝑛 𝑀 0 + (2𝑛 − 1)= 2𝑛 − 1Basic Operation𝑛 − 𝑖 = 0𝑖 = 𝑛](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fsection4-240819185317-60831acb%2f75%2fAlgorithms-Analysis-Design-Lecture-4-27-2048.jpg&f=jpg&w=240)


![a) The algorithm computes the value of the smallest element in a given array.Let A = [9, 5, 8, 2]▪ n = 4 ≠ 1▪ Call Riddle([9, 5, 8]) (subarray A[0..2]):▪ For Riddle([9, 5, 8]):▪ n = 3 ≠ 1▪ Call Riddle([9, 5]) (subarray A[0..1]):▪ For Riddle([9, 5]):▪ n = 2 ≠ 1▪ Call Riddle([9]) (subarray A[0..0]):▪ n = 1, so it returns 9.▪ Compare temp = 9 with A[1] = 5:▪ 9 > 5, so return 5.▪ Riddle([9, 5]) returns 5.▪ Compare temp = 5 with A[2] = 8:▪ 5 <= 8, so return 5.▪ Riddle([9, 5, 8]) returns 5.▪ Compare temp = 5 with A[3] = 2:▪ 5 > 2, so return 2.](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fsection4-240819185317-60831acb%2f75%2fAlgorithms-Analysis-Design-Lecture-4-30-2048.jpg&f=jpg&w=240)
![b)Let 𝐶(𝑛) be the number of the number of key comparisons made by the algorithm.Recurrence relation equation: 𝐶(𝑛) = 𝐶(𝑛 − 1) + 1 and 𝐶 1 = 0• Using backward substitution:𝐶 𝑛 = 𝐶(𝑛 − 1) + 1= [ 𝐶(𝑛 − 2) + 1 ] + 1 = 𝐶(𝑛 − 2) + 2= [ 𝐶(𝑛 − 3) + 1 ] + 2 = 𝐶(𝑛 − 3) + 3= [ 𝐶(𝑛 − 4) + 1 ] + 3 = 𝐶(𝑛 − 4) + 4𝐶 𝑛 = 𝐶 𝑛 − 𝑖 + 𝑖= 𝐶 1 + (𝑛 − 1)= 𝑛 − 1Basic Operation𝑛 − 𝑖 = 1𝑖 = 𝑛 − 1](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fsection4-240819185317-60831acb%2f75%2fAlgorithms-Analysis-Design-Lecture-4-31-2048.jpg&f=jpg&w=240)
The document discusses various algorithms, particularly focusing on their recurrence relations and the number of operations they perform, such as multiplications, additions, and comparisons. It includes specific examples like the Tower of Hanoi problem and algorithms for computing powers and finding the smallest element in an array, analyzing their time complexity and operational breakdown. The document emphasizes the efficiency of different approaches and highlights the advantages of non-recursive methods.

















![a) Another approachLet 𝑀(𝑛) be the number of multiplications made by the algorithm.Recurrence relation equation: 𝑀(𝑛) = 𝑀(𝑛 − 1) + 2 and 𝑀 1 = 0• Using backward substitution:𝑀(𝑛) = 𝑀(𝑛 − 1) + 2= [ 𝑀(𝑛 − 2) + 2 ] + 2 = 𝑀(𝑛 − 2) + 4= [ 𝑀(𝑛 − 3) + 2 ] + 4 = 𝑀(𝑛 − 3) + 6= [ 𝑀(𝑛 − 4) + 2 ] + 6 = 𝑀(𝑛 − 4) + 8𝑀(𝑛) = 𝑀 𝑛 − 𝑖 + 2𝑖= 𝑀 1 + 2(𝑛 − 1)= 2 𝑛 − 1Basic Operation𝑛 − 𝑖 = 1𝑖 = 𝑛 − 1](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fsection4-240819185317-60831acb%2f75%2fAlgorithms-Analysis-Design-Lecture-4-18-2048.jpg&f=jpg&w=240)








![b) Another approachLet 𝐴(𝑛) be the number of additions made by the algorithm.Recurrence relation equation: 𝐴(𝑛) = 2𝐴(𝑛 − 1) + 1 and 𝐴 0 = 0• Using backward substitution:𝐴(𝑛) = 2𝐴(𝑛 − 1) + 1= 2[ 2𝐴(𝑛 − 2) + 1 ] + 1 = 4𝐴(𝑛 − 2) + 3= 4[ 2𝐴(𝑛 − 3) + 1 ] + 3 = 8𝐴(𝑛 − 3) + 7= 8[ 2𝐴(𝑛 − 4) + 1 ] + 7 = 16𝐴(𝑛 − 4) + 15𝐴 𝑛 = 2𝑖𝐴 𝑛 − 𝑖 + (2𝑖 − 1)= 2𝑛 𝑀 0 + (2𝑛 − 1)= 2𝑛 − 1Basic Operation𝑛 − 𝑖 = 0𝑖 = 𝑛](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fsection4-240819185317-60831acb%2f75%2fAlgorithms-Analysis-Design-Lecture-4-27-2048.jpg&f=jpg&w=240)


![a) The algorithm computes the value of the smallest element in a given array.Let A = [9, 5, 8, 2]▪ n = 4 ≠ 1▪ Call Riddle([9, 5, 8]) (subarray A[0..2]):▪ For Riddle([9, 5, 8]):▪ n = 3 ≠ 1▪ Call Riddle([9, 5]) (subarray A[0..1]):▪ For Riddle([9, 5]):▪ n = 2 ≠ 1▪ Call Riddle([9]) (subarray A[0..0]):▪ n = 1, so it returns 9.▪ Compare temp = 9 with A[1] = 5:▪ 9 > 5, so return 5.▪ Riddle([9, 5]) returns 5.▪ Compare temp = 5 with A[2] = 8:▪ 5 <= 8, so return 5.▪ Riddle([9, 5, 8]) returns 5.▪ Compare temp = 5 with A[3] = 2:▪ 5 > 2, so return 2.](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fsection4-240819185317-60831acb%2f75%2fAlgorithms-Analysis-Design-Lecture-4-30-2048.jpg&f=jpg&w=240)
![b)Let 𝐶(𝑛) be the number of the number of key comparisons made by the algorithm.Recurrence relation equation: 𝐶(𝑛) = 𝐶(𝑛 − 1) + 1 and 𝐶 1 = 0• Using backward substitution:𝐶 𝑛 = 𝐶(𝑛 − 1) + 1= [ 𝐶(𝑛 − 2) + 1 ] + 1 = 𝐶(𝑛 − 2) + 2= [ 𝐶(𝑛 − 3) + 1 ] + 2 = 𝐶(𝑛 − 3) + 3= [ 𝐶(𝑛 − 4) + 1 ] + 3 = 𝐶(𝑛 − 4) + 4𝐶 𝑛 = 𝐶 𝑛 − 𝑖 + 𝑖= 𝐶 1 + (𝑛 − 1)= 𝑛 − 1Basic Operation𝑛 − 𝑖 = 1𝑖 = 𝑛 − 1](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fsection4-240819185317-60831acb%2f75%2fAlgorithms-Analysis-Design-Lecture-4-31-2048.jpg&f=jpg&w=240)