You are on page 1of 2

University of M’Sila Level: Master 1 (IA and SIGL)

Department of Computer Science Module: Advanced Algorithms


Instructor: Noureddine Amraoui Academic Year: 2022/2023

Assignment 4: Recursion, and Divide and Conquer


Exercise 1: Run findMax Algorithm for A = [3, 11, 6, 10, 7]. Then, calculate its complexity.
Algorithm findMax (A, n) {
If n == 1
Return A [1]
Return max (A[n], findMax (A, n-1))
}

Exercise 2: Run Reverse Algorithm for A = 1, 2, 3, 4, 5 (with first and last indexes initialized to
1 and 5, respectively). Then, calculate its complexity.
Algorithm Reverse (A, first, last) {
If (first < last)
Swap (A[first], A[last])
Reverse (A, first + 1, last – 1)
}

Exercise 3: Run SelectionSort for A = [7, 3, 10, 2, 6]. Then, calculate its time complexity.
Algorithm SelectionSort (A, n) {
If n == 1 Return
min = minIndex (A, n) /* For loop */
Swap (A [1], A [min])]
SelectionSort ([2…n], n - 1)
}
Exercise 4: Run Fibo Algorithm for n = 5. Then, calculate its complexity.
Algorithm Fibo (n)
If n <= 1
Return n
Else
Return Fibo (n – 1) + Fibo (n – 2)
}
Exercise 5: Solve the following recurrence relation using substitution method:
1, 𝑖𝑓 𝑛 = 2
𝑇(𝑛) =
𝑇(𝑛) = 𝑇 √𝑛 + 1, 𝑖𝑓 𝑛 > 2

1/2
Exercise 6: Let A and B be two matrices to be multiplied. Strassen's algorithm takes 𝑂(𝑛 )
time. Suppose someone discovers a way to obtain the product of two matrices of order 𝑛 ∗ 𝑛 by
performing 24 multiplications of two matrices of order 𝑛/5 and combining the results into one
𝑂(𝑛 ) time.
1. Calculate the complexity of this new algorithm.
2. Is this running time better than the running time of Strassen's algorithm
Exercise 7: You decide to use divide and conquer paradigm to solve some type of problem. For
𝑛 ≥ 4, you know that you can get the solution of an instance of size 𝑛 by solving 𝑎 sub-instances
of size 𝑛/4. The time required for the decomposition of the original instance into sub-instances is
in 𝑂 and the time required for the recombination of the sub-solutions is in 𝑂(𝑛 ).

1. Give the asymptotic recurrence relation expressing the running time 𝑇(𝑛) of this algorithm
as a function of the size n of the example.
2. 2. Give the exact order of T(n), in the simplest possible form when: a = 8; a = 16; a = 32.
Exercise 8: We consider a problem whose data is an array of size 𝑛 and the solution is an array
of the same size. To solve this problem on large arrays, we propose the following two
algorithms:
 Algo A processes an array of size 𝑛 by dividing it into 2 sub-problems of size 𝑛/2 and
combining the solutions in time 𝑛 .
 Algo B processes an array of size 𝑛 by dividing it into 4 subproblems of size 𝑛/4 and
combining the solutions in time √𝑛.
1. Give the recurrence relations of the two algorithms.
2. By justifying your answer, indicate which of the two should be chosen.
Annex
Master Theorem
For Decreasing Functions For Dividing Functions
𝑇(𝑛) = 𝑎𝑇(𝑛 − 𝑏) + 𝑓(𝑛) 𝑇(𝑛) = 𝑎𝑇(𝑛/𝑏) + 𝑓(𝑛)
𝑤𝑖𝑡ℎ 𝑎 > 0, 𝑏 > 0, 𝑤𝑖𝑡ℎ 𝑎 > 0, 𝑏 > 0,
𝑎𝑛𝑑 𝑓(𝑛) = 𝑂(𝑛 ) 𝑤ℎ𝑒𝑟𝑒 𝑘 ≥ 0 𝑎𝑛𝑑 𝑓(𝑛) = 𝑂(𝑛 ) 𝑤ℎ𝑒𝑟𝑒 𝑘 ≥ 0

𝑖𝑓 𝑎 < 1: 𝑇(𝑛) = 𝑂 𝑓(𝑛) = 𝑂(𝑛 ) 𝑖𝑓 𝑎 < 𝑏 : 𝑇(𝑛) = 𝑂 𝑓(𝑛) = 𝑂(𝑛 )

𝑖𝑓𝑎 = 1: 𝑇(𝑛) = 𝑂 𝑛 ∗ 𝑓(𝑛) = 𝑂(𝑛 ) 𝑖𝑓𝑎 = 𝑏 : 𝑇(𝑛) = 𝑂(𝑛 log 𝑛)

/
𝑖𝑓 𝑎 > 1: 𝑇(𝑛) = 𝑂(𝑛 𝑎 ) 𝑖𝑓 𝑎 > 𝑏 : 𝑇(𝑛) = 𝑂(𝑛 )

2/2

You might also like