4.1 Divide and Conquer
4.1 Divide and Conquer
UNIVERSITY INSTITUTE OF
ENGINEERING
COMPUTER SCIENCE
ENGINEERING
Bachelor of Engineering
Design and Analysis of
Algorithms(CST-311/ITT-311)
Outcome:
• Student will understand
Divide and Problem
Binary Search
Min max Problem
•Binary Search
•Max-Min Problem
•Merge Sort
search sort
Searching a Dictionary
• To get a general sense of how the divide
and conquer strategy improves search,
consider how people find information in
a phone book or dictionary
– suppose you want to find “janissary” in
a dictionary
– open the book near the middle
– the heading on the top left page is “kiwi”,
so move back a small number of pages
– here you find “hypotenuse”, so move forward
– find “ichthyology”, move forward again
• The number of pages you move gets
smaller (or at least adjusts in response
to the words you find)
Binary Search
• The binary search algorithm uses the divide-and-conquer
strategy to search through an array
• Solution:
In this algorithm, we want to find whether
element x belongs to a set of numbers stored in an
array numbers[ ]. Where l and r represent the left and
right index of a sub-array in which searching operation
should be performed.
Algorithm
Algorithm: Binary-Search(numbers[ ], x, l, r)
if l = r then
return l
else
m := ⌊(l + r) / 2⌋
if x ≤ numbers[m] then
return Binary-Search(numbers[ ], x, l, m)
else
return Binary-Search(numbers[ ], x, m+1, r)
Example
In this example, we are going to search element 63.
Max-Min Problem
• Problem Statement
The Max-Min Problem in algorithm analysis is finding the
maximum and minimum value in an array.
• Solution
To find the maximum and minimum numbers in a given
array numbers[ ] of size n, the following algorithm can
be used. First we are representing the naive method and
then we will present divide and conquer approach.
Naïve Method
REFERENCES
Text books:
•Cormen, Leiserson, Rivest, Stein, “Introduction to Algorithms”, Prentice Hall of
India, 3rd edition 2012. problem, Graph coloring.
Websites:
•https://www.tutorialspoint.com/design_and_analysis_of_algorithms/
design_and_analysis_of_algorithms_max_min_problem.htm
•https://www.tutorialspoint.com/design_and_analysis_of_algorithms/
design_and_analysis_of_algorithms_binary_search.htm
Summary