0% found this document useful (0 votes)
43 views21 pages

Daa Unit II

This document discusses the divide-and-conquer algorithm design strategy. It provides examples of divide-and-conquer algorithms like mergesort, quicksort, and binary search. It analyzes the time complexity of these algorithms and provides pseudocode. It also includes discussion of a defective chess board problem and assignment questions related to sorting and searching algorithms.

Uploaded by

Nikhil Yasalapu
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)
43 views21 pages

Daa Unit II

This document discusses the divide-and-conquer algorithm design strategy. It provides examples of divide-and-conquer algorithms like mergesort, quicksort, and binary search. It analyzes the time complexity of these algorithms and provides pseudocode. It also includes discussion of a defective chess board problem and assignment questions related to sorting and searching algorithms.

Uploaded by

Nikhil Yasalapu
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

Subject Name:

Design and Analysis of Algorithm


Subject Code:
10CS43
Prepared By:
Shruthi N
Department:
CSE
Date: 11/08/23
Divide-and-Conquer
The most-well known algorithm design strategy:

1. Divide instance of problem into two or more smaller


instances.

2. Solve smaller instances recursively.

3. Obtain solution to original (larger) instance by combining


these solutions.
Divide-and-Conquer Technique
a(cont.)
problem of size n

subproblem 1 subproblem 2
of size n/2 of size n/2

a solution to a solution to
subproblem 1 subproblem 2

a solution to It general leads to a


the original problem recursive algorithm!
Divide-and-Conquer Examples
Sorting: mergesort and quicksort

Binary tree traversals

Binary search (?)

Defective chess board


Divide-and-Conquer Recurrence
Let T(n) be a monotonically increasing function that satisfies
T(n) = a T(n/b) + f(n)
T(1) = c
where a  1, b  2, c>0. If f(n) is (nd) where d  0 then

if a < bd
T(n) = If a = bd

if a > bd
Mergesort
• Split array A[0..n-1] into about equal halves and make copies of each half
in arrays B and C

• Sort arrays B and C recursively

• Merge sorted arrays B and C into array A as follows:

 Repeat the following until no elements remain in one of the arrays:

• compare the first elements in the remaining unprocessed portions


of the arrays
• copy the smaller of the two into A, while incrementing the index
indicating the unprocessed portion of that array

 Once all elements in one of the arrays are processed, copy the
remaining unprocessed elements from the other array into A.
Pseudocode of Mergesort
Pseudocode of Merge

Time complexity: Θ(p+q) = Θ(n) comparisons


Mergesort Example
8 3 2 9 7 1 5 4

8 3 2 9 7 1 5 4

8 3 2 9 7 1 5 4

8 3 2 9 7 1 5 4

3 8 2 9 1 7 4 5

2 3 8 9 1 4 5 7

1 2 3 4 5 7 8 9

12/8/13
Analysis of Mergesort
• All cases have same efficiency: Θ(n log n)
T(n) = 2T(n/2) + Θ(n), T(1) = 0
• Number of comparisons in the worst case is close to
theoretical minimum for comparison-based sorting:
. Space requirement: Θ(n) (not in-place)

• Can be implemented without recursion (bottom-up)


Quicksort
• Select a pivot (partitioning element) – here, the first element.

• Rearrange the list so that all the elements in the first ‘s’
positions are smaller than or equal to the pivot and all the
elements in the remaining n-s positions are larger than or
equal to the pivot.

Exchange the pivot with the last element in the first subarray
— the pivot is now in its final position.

• Sort the two subarrays recursively.


Partitioning Algorithm

or i > r
or j = l
<

Time complexity: Θ(r-l) comparisons


Quicksort Example
5 3 1 9 8 2 4 7
2 3 1 4 5 8 9 7
1 2 3 4 5 7 8 9
1 2 3 4 5 7 8 9
1 2 3 4 5 7 8 9
1 2 3 4 5 7 8 9
Analysis of Quicksort
• Best case: split in the middle — Θ(n log n)

• Worst case: sorted array! — Θ(n2)

• Average case: random arrays — Θ(n log n)

Improvements:
• better pivot selection: median of three partitioning

• switch to insertion sort on small subfiles

• elimination of recursion
These combine to 20-25% improvement
Considered the method of choice for internal sorting of large
files (n ≥ 10000)
Binary Search
public static int binarySearch(int [ ] list, int listLength, int key) {
int first = 0, last = listLength - 1;
int mid;
boolean found = false;

while (first <= last && !found) {


mid = (first + last) / 2;
if (list[mid] == key)
found = true;
else
if(list[mid] > key)
last = mid - 1;
else
first = mid + 1;
}
if (found)
return mid;
else
return –1;
} //end binarySearch
Analysis of Binary Search
• Time efficiency:

 This is VERY fast.

 Optimal for searching a sorted array


Limitations:

• Must be a sorted array (not linked list)


Bad (degenerate) example of divide-and-conquer
because only one of the sub-instances is solved

• Has a continuous counterpart called bisection method for


solving equations in one unknown f(x) = 0
Defective chess board
• The total idea of chessboard coverage
algorithm based on the divide-and-conquer
strategy in the teaching materials is to divide
the chessboard into four sub-chessboards first,
and then respectively cover these four sub-
chessboards.

12/8/13
Contd..

• For each sub-chessboard, the program will


first judge whether the special pane is in this
sub-chessboard. If it is in the sub-chessboard,
the program will recursively transfer the
program to cover this sub-chessboard, or else,
cover the neighboring panes with other three
sub-chessboards, and then recursively transfer
the program to cover this sub-chessboard.

12/8/13
Assignment questions
1. What is brute-force method? Write a brute-force string
matching algorithm. Analyze its complexity.
2. Write the quick sort algorithm. Analyze its efficiency.
Apply the algorithm to sort the list 4, 1, 6, 3, 9, 2, 7, 5 .
3. Using quick sort algorithm. Arrange the letters of the
word a” EXAMPLE” in alphabetical order .
4. Using quick sort algorithm. Arrange the letters of the
word a” QUESTION” in alphabetical order.
5. Write the algorithm for binary search and find the
average case efficiency.
1
-
2
0

Contd..
6. Discuss the merge sort algorithm with recursive tree
and its efficiency. Apply the same algorithm to sort the
list {4,6,1,3,9,5}
7. Using bubble sort algorithm. Arrange the letters of the
word a” QUESTION” in alphabetical order
8. Using bubble sort algorithm. Arrange the letters of the
word a” EXAMPLE” in alphabetical order
9. Give the general divide and conquer recurrence and
explain the same. Give the master‟s theorem.
10. What is divide-and conquer technique? Explain the
concept of divide and conquer methodology indicating
three major variations.
Thank You..

You might also like