Apex Institute of Technology Department of Computer Science & Engineering
Apex Institute of Technology Department of Computer Science & Engineering
2
Max-Min Problem
Max-Min problem is to find a maximum and minimum element from the given
array.
In the traditional approach, the maximum and minimum elements can be found
by comparing each element and updating Max and Min values as and when
required.
This approach is simple but it does (n – 1) comparisons for finding the max and
the same number of comparisons for finding the min.
3
Divide and conquer approach for Max. Min problem
Divide and conquer approach for Max. Min problem works in three stages.
After solving two subproblems, their minimum and maximum numbers are compared
to build the solution to the large problem. This process continues in a bottom-up
fashion to build the solution to a parent problem.
4
Algorithm for Max-Min Problem
5
Algorithm Analysis
The conventional algorithm takes 2(n – 1) comparisons in worst, best, and average cases.
Find_maxmin does two comparisons to determine the minimum and maximum element and creates two
problems of size n/2, so the recurrence can be formulated as
T(n) = 0, if n = 1
T(n) = 1, if n = 2
T(n/2) = 2T(n/4) + 2
⇒ T(n) = 2(2T(n/4) + 2) + 2
= 4T(n/4) + 4 + 2 … (2) 6
Algorithm Analysis
T(n/4) = 2T(n/8) + 2
T(n) = 4[2T(n/8) + 2] + 4 + 2
= 8T(n/8) + 8 + 4 + 2
= 23 T(n/23) + 23 + 22 + 21
• Compared to the Naïve method, in the divide and conquer approach, the
8
TASKS END OF LECTURE LEARNING (TELL):
Task 1:
Find max and min from the sequence <33, 11, 44, 55, 66, 22> using the divide and
conquer approach.
Task 2:
How do we modify the above solutions when input values are repeated?
Task 3:
Why is space complexity O(logn) in the divide and conquer approach? Why are
there two base cases? What would be the time complexity if we remove the base
cases with array size 2? 9
References
• Fundamentals of Computer Algorithms 2nd Edition (2008) by Horowitz, Sahni
and Rajasekaran
• Introduction to Algorithms 3rd Edition (2012) by Thomas H Cormen, Charles E
Lieserson, Ronald
10
THANK YOU
For queries
Email: [email protected]
07/21/2024 11