0% found this document useful (0 votes)
29 views11 pages

Apex Institute of Technology Department of Computer Science & Engineering

Uploaded by

Kartik Garg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views11 pages

Apex Institute of Technology Department of Computer Science & Engineering

Uploaded by

Kartik Garg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Apex Institute of Technology

Department of Computer Science & Engineering


Bachelor of Engineering (Computer Science & Engineering)
Design and Analysis of Algorithms– (21CSH-282)
Prepared By: Mr. Vikas Kumar (E13657)

07/21/2024 DISCOVER . LEARN . EMPOWER


1
Content

 Algorithms for Find Min and Max


 Example

2
Max-Min Problem

 Max-Min problem is to find a maximum and minimum element from the given
array.

 We can effectively solve it using the divide-and-conquer approach.

 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.

 It results in a total of 2(n – 1) comparisons. Using a divide-and-conquer


approach, we can reduce the number of comparisons.

3
Divide and conquer approach for Max. Min problem

Divide and conquer approach for Max. Min problem works in three stages.

 If a1 is the only element in the array, a1 is the maximum and minimum.


 If the array contains only two elements a1 and a2, then the single comparison between
the two elements can decide the minimum and maximum of them.
 If there are more than two elements, the algorithm divides the array from the middle
and creates two subproblems. Both subproblems are treated as an independent
problems and the same recursive process is applied to them.
 This division continues until the subproblem size becomes one or two.

 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) = 2T(n/2) + 2, if n > 2

Let us solve this equation using an interactive approach.

T(n) = 2T(n/2) + 2 … (1)


By substituting n by (n / 2) in Equation (1)

T(n/2) = 2T(n/4) + 2

⇒ T(n) = 2(2T(n/4) + 2) + 2

= 4T(n/4) + 4 + 2 … (2) 6
Algorithm Analysis

By substituting n by n/4 in Equation (1),

T(n/4) = 2T(n/8) + 2

Substitute it in Equation (1),

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

number of comparisons is less. However, using the asymptotic notation

both of the approaches are represented by O(n). 7


Algorithm Analysis

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

You might also like