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

Lecture 13(Decrease and Conquer)

The document discusses the concept of decrease-and-conquer algorithms, which solve a problem by reducing it to smaller instances. It outlines three major variations: decrease by a constant, decrease by a constant factor, and variable size decrease, with examples such as Insertion Sort and Binary Search. The document also provides an analysis of the Insertion Sort algorithm, detailing its process and worst-case complexity of O(n^2).

Uploaded by

sainirushil3
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture 13(Decrease and Conquer)

The document discusses the concept of decrease-and-conquer algorithms, which solve a problem by reducing it to smaller instances. It outlines three major variations: decrease by a constant, decrease by a constant factor, and variable size decrease, with examples such as Insertion Sort and Binary Search. The document also provides an analysis of the Insertion Sort algorithm, detailing its process and worst-case complexity of O(n^2).

Uploaded by

sainirushil3
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 18

CSE408

Decrease and Conquer

Lecture #13
Decrease-and-Conquer

Based on relationship between a solution


to a given instance of a problem and
a solution to a smaller problem.

Three Major Variations:


• Decrease by a constant
• Decrease by a constant factor
• Variable size decrease
Decrease(by constant)-and-Conquer

• Size of an instance is reduced by same


constant on each iteration of the
algorithm.
• Typically , this constant is equal to one .
• Some times reduction by two cases
do happen (in algorithms that have to
act differently for instances of odd
and even sizes).
Decrease(by constant)-and-Conquer

a problem of size n
(instance)

subproblem
of size n-1

solution to the
subproblem

a solution to Here constant is


the original problem
taken as 1.
Decrease(by constant)-and-Conquer Examples

• Insertion Sort
• Breadth First Search(BFS)
• Depth First Search(DFS)
• Topological Sorting
• Connected Components
Decrease(by constant factor)-and-Conquer

• Size of an instance is reduced by same


constant factor on each iteration of the
algorithm.
• In most applications , this constant factor is
equal to two .
• Example :Binary Search, Fake coin
Problems
Decrease(by constant factor)-and-Conquer

a problem of size n
(instance)

subproblem
of size n/2

solution to the
subproblem

a solution to Here constant factor


the original problem
is taken as 2.
Variable Size Decrease

• A size reduction pattern varies from


one iteration of an algorithm to
another.
• Example: Euclid’s algorithm for
computing greatest common divisor
Insertion Sort

Idea: like sorting a hand of playing cards


• Start with an empty left hand and
the cards facing down on the table.
• Remove one card at a time from
the table, and insert it into the
correct position in the left hand
• compare it with each of the cards already
in the hand, from right to left
9
Insertion Sort

• The cards held in the left hand are


sorted
• these cards were originally the top cards of the
pile on the table
Insertion Sort

To insert 12, we need to


make room for it by
moving first 36 and then
6 10 24 36
24.

12

11
Insertion Sort

6 10 24 36

12

12
Insertion Sort

6 10 24 3
6

12

13
Insertion Sort

input array
5 2 4 6 1 3

at each iteration, the array is divided in two sub-arrays:


left sub-array right sub-array

sorted unsorted

14
Insertion Sort

15
Insertion Sort

INSERTION-SORT(A)
Alg.: INSERTION-SORT(A[0..n-1])
for j ← 2 to n
for i do
← key
1 to←n-1A[ j ]
do
v← A[A[ij ]] into the sorted sequence A[1
Insert key. . j -1]
i← j - A[
Insert 1 i] into the sorted sequence A[0 . . i -1]
j ←i i>- 01 and A[i] > key
while
do A[ij +
while >= 1] 0← and
A[i] A[j] > v
i←i–1
do A[j + 1] ← A[j]
A[i + 1] ← key
j←j–1
• Insertion sort – sorts the elements in place
A[j + 1] ← v
• Insertion sort – sorts the elements in place
16
Insertion Sort

Analysis:
•In the worst case ,A[j]>v is executed the
largest number of times i.e. , for every j=i-1,
………..,0.
•Since v=A[i] ,it happens if and only

if A[j]>A[i] for j=i-1,………,0.


Insertion Sort

• Thus for the worst case input , we get


A[0]>A[1](for i=1),A[1]>A[2] (for i=2),
………..A[n-2]>A[n-1](for i=n-1).
• In other words , the worst case input is
an array of strictly decreasing values.
• Thus , there will be i number of
comparisons in each pass for above input.
• So worst case complexity is O(n2).

You might also like