Algorithms Analysis & Design - Lecture 4

1mohamedgamal54 25 views 31 slides Aug 19, 2024
Slide 1
Slide 1 of 31
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31

About This Presentation

Introduction to the Design and Analysis of Algorithms


Slide Content

Algorithms
By Mohamed Gamal
© Mohamed Gamal 2024

a)
Let ??????(??????) be the number of multiplications made by the algorithm.
Recurrence relation equation: ??????(??????)=??????(??????−1)+1 and ??????0=0
•Using forward substitution:
??????0 = 0
??????1 = ??????0+1 = 1
??????2 = ??????1+1 = 2
??????3 = ??????2+1 = 3
???????????? = ??????
Basic Operation

a) Another approach
Let ??????(??????) be the number of multiplications made by the algorithm.
Recurrence relation equation: ??????(??????)=??????(??????−1)+1 and ??????0=0
•Using backward substitution:
????????????=????????????−1 +1
=????????????−2+1+1 = ????????????−2+2
=????????????−3+1+2 = ????????????−3+3
=????????????−4+1+3 = ????????????−4+4
????????????=????????????−?????? + ??????
= ??????0 + ??????
= ??????
??????−??????=0
??????=??????

Example: The Tower of Hanoi Puzzle
Problem: move disks from source peg A to destination peg C using an auxiliary (temp) peg B.
Constraints:
–Only one disk can be moved at a time.
– A larger disk cannot be placed on top of a smaller disk.
Source DestinationAuxiliary

Try it online!

7 moves in total !

ALGORITHM: Hanoi(n, src, dest, temp)
// Input: no. disks, source, destination, temp
// Output: Moves to solve the Tower of Hanoi problem
if n = 1 then
MOVE disk from src to dest directly
else
Hanoi(n-1, src, temp, dest) // Step 1: from src to temp
MOVE disk n from src to dest // Step 2: nth disk from src to dest
Hanoi(n-1, temp, dest, src) // Step 3: from temp to dest

Let ??????(??????) be the number of moves made by the algorithm.
Recurrence relation equation: ???????????? = ????????????−1+1+??????(??????−1) and ??????1=1
=2????????????−1+1
•Using forward substitution:
??????(1)=1
??????(2)=2??????(1)+1=3
??????(3)=2??????(2)+1=7
??????(??????)=2
??????
−1
Basic Operation

Let ??????(??????) be the number of moves made by the algorithm.
Recurrence relation equation: ????????????=2????????????−1+1 and ??????1=1
•Using backward substitution:
??????(??????)=2????????????−1+1
=22????????????−2+1+1=4??????(??????−2)+3
= 42????????????−3+1+3=8??????(??????−3)+7
??????(??????)=2
??????
??????(??????−??????)+(2
??????
−1)
=2
??????−1
??????(1)+(2
??????−1
−1)
=2
??????
−1
??????−??????=1
??????=??????−1
Basic Operation

Determine what this algorithm computes?
??????(1) = 1
??????(2) = ??????(1) + 2∗2−1 = 1 + 3 = 4
??????(3) = ??????(2) + 2∗3−1 = 4 + 5 = 9
??????(4) = ??????(3) + 2∗4−1 = 9 + 7 = 16
??????(??????) = ??????
2
Then this algorithm computes ??????
2
for each positive ??????.
Recurrence relation equation: ??????(??????−1)+2??????−1 for ??????>1
a)

b)
Let ??????(??????) be the number of multiplications 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)
=(??????−1)
??????−??????=1
??????=??????−1

c)
Let ??????(??????) be the number of additions and subtractions 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(??????−1)
??????−??????=1
??????=??????−1

a)
Let ??????(??????) be the number of multiplications made by the algorithm.
Recurrence relation equation: ??????(??????)=??????(??????−1)+2 and ??????1=0
•Using forward substitution:

??????(1)=0
??????(2)=??????(1)+2=2
??????(3)=??????(2)+2=4
??????(4)=??????(3)+2=6
??????(5)=??????(4)+2=8

????????????=2??????−1
Basic Operation

a) Another approach
Let ??????(??????) 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?????? −1
Basic Operation
??????−??????=1
??????=??????−1

ALGORITHM: S(n)
// Input: A positive integer n
// Output: The sum of the first n cubes
S ← 1
for i ← 2 to n do
S ← S + i * i * i
return S
The number of multiplications made by this algorithm will be

??????=2
??????
2=
??????−2+1
2
×(2+2) = 2(??????−1)
This is exactly the same number as in the recursive version, but the no recursive version
doesn't carry the time and space overhead associated with the recursion's stack.
b) Comparing with the non-recursive algorithm

Question 1:
Assignments

Question 2:

Thank You!

Question 1

Answers

b)
Let ??????(??????) be the number of additions made by the algorithm.
Recurrence relation equation: ????????????=????????????−1+??????(??????−1)+1 and ??????0=0
=2????????????−1+1
•Using forward substitution:

??????(0)=0
??????(1)=2??????(0)+1=1
??????(2)=2??????(1)+1=3
??????(3)=2??????(2)+1=7
??????(4)=2??????(3)+1=15

????????????=2
??????
−1
Basic Operation

b) Another approach
Let ??????(??????) 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
??????
−1
Basic Operation
??????−??????=0
??????=??????

d.It’s a very bad algorithm because it is vastly inferior to the algorithm that simply
multiplies an accumulator by 2 ?????? times. O(2
??????
)

Question 2

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.

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)
=??????−1
Basic Operation
??????−??????=1
??????=??????−1