0% found this document useful (0 votes)
498 views

4.1 Divide and Conquer

The document discusses the divide and conquer algorithm design technique. It covers binary search, which uses divide and conquer to search a sorted array in logarithmic time rather than linear time. Max-min problem finding the maximum and minimum values in an array is also covered, comparing a naive linear solution to a faster divide and conquer approach. Examples of both binary search and the max-min problem are provided.

Uploaded by

madhu jha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
498 views

4.1 Divide and Conquer

The document discusses the divide and conquer algorithm design technique. It covers binary search, which uses divide and conquer to search a sorted array in logarithmic time rather than linear time. Max-min problem finding the maximum and minimum values in an array is also covered, comparing a naive linear solution to a faster divide and conquer approach. Examples of both binary search and the max-min problem are provided.

Uploaded by

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

Department of Computer Science and Engineering (CSE)

UNIVERSITY INSTITUTE OF
ENGINEERING
COMPUTER SCIENCE
ENGINEERING
Bachelor of Engineering
Design and Analysis of
Algorithms(CST-311/ITT-311)

Topic: Divide and Conquer DISCOVER . LEARN . EMPOWER

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Learning Objectives & Outcomes


Objective:
• To understand the concept of Divide and conquer and
how to solve binary search and min max problem using
divide and conquer problem.

Outcome:
• Student will understand
 Divide and Problem
 Binary Search
 Min max Problem

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Divide and Conquer

Breaking large problems into smaller subproblems.

•Binary Search
•Max-Min Problem
•Merge Sort

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Divide and Conquer


• The common theme for the previous slides: iterate over
every location in the list
• The common theme for this chapter’s slides: divide and
conquer
– break a problem into smaller pieces and solve the smaller sub-
problems
• It may not seem like that big a deal, but the improvement
can be dramatic
– approximate number of comparisons (worst case):

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

search sort

n = 100 n = 1,000 n = 100 n = 1,000

search 100 1,000 isort 5,000 500,000

bsearch 7 10 msort 700 10,000

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

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)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)
Searching a Dictionary

A detailed specification of this process:


1.the goal is to search for a word w in
region of the book
2.the initial region is the entire book
3.at each step pick a word x in the middle
of the current region
4.there are now two smaller regions: the
part before x and the part after x
5.if w comes before x, repeat the search on
the region before x, otherwise search the
region following x (go back to step 3)
Note: at first a “region” is of a group of
pages,
but eventually a region is a set of words on
a single page

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Binary Search
• The binary search algorithm uses the divide-and-conquer
strategy to search through an array

• The array must be sorted

– the “zeroing in” strategy for looking up a


word in the dictionary won’t work it the
words are not in alphabetical order

– binary search will not work unless the


array is sorted

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Binary Search (Contd.)


• Problem:
Binary search can be performed on a sorted array. In this
approach, the index of an element x is determined if the
element belongs to the list of elements.

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

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

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)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Analysis of Binary Search

Linear search runs in O(n) time. Whereas binary search


produces the result in O(log n) time
Let T(n) be the number of comparisons in worst-case in an
array of n elements.
Hence,

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Example
In this example, we are going to search element 63.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

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.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Naïve Method

Naïve method is a basic method to solve any problem. In


this method, the maximum and minimum number can be
found separately. To find the maximum and minimum
numbers, the following straightforward algorithm can be
used.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Analysis of Naïve Method


• The number of comparison in Naive method is 2n - 2.
• The number of comparisons can be reduced using the
divide and conquer approach. Following is the technique.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Divide and Conquer Approach


• In this approach, the array is divided into two halves.
Then using recursive approach maximum and minimum
numbers in each halves are found. Later, return the
maximum of two maxima of each half and the minimum
of two minima of each half.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

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

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Summary

Divide and Conquer:

• Concept of Divide and conquer


• Binary Search
• Min Max Problem

University Institute of Engineering (UIE)


THANK YOU

University Institute of Engineering (UIE)

You might also like