0% found this document useful (0 votes)
50 views3 pages

Design and Analysis of Algorithms: Divide and Conquer Divide - and - Conquer Technique

The document discusses divide-and-conquer algorithms. It explains that divide-and-conquer involves dividing a problem into smaller subproblems, solving the subproblems recursively, and combining the solutions to solve the original problem. Examples of divide-and-conquer algorithms covered include mergesort, quicksort, binary search, and matrix multiplication using Strassen's algorithm. The efficiency of these algorithms is analyzed, with most having time complexities of Θ(n log n).

Uploaded by

Salih_Bak_r_8581
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views3 pages

Design and Analysis of Algorithms: Divide and Conquer Divide - and - Conquer Technique

The document discusses divide-and-conquer algorithms. It explains that divide-and-conquer involves dividing a problem into smaller subproblems, solving the subproblems recursively, and combining the solutions to solve the original problem. Examples of divide-and-conquer algorithms covered include mergesort, quicksort, binary search, and matrix multiplication using Strassen's algorithm. The efficiency of these algorithms is analyzed, with most having time complexities of Θ(n log n).

Uploaded by

Salih_Bak_r_8581
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Design and Analysis of Algorithms

Divide and Conquer


The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances
2.

Chapter 4
DivideDivide-andand-conquer technique
a problem of size n

Solve smaller instances recursively Obtain solution to original (larger) instance by combining these solutions

subproblem 1 of size n/2

subproblem 2 of size n/2

3.

a solution to subproblem 1

a solution to subproblem 2

a solution to the original problem


Design and Analysis of Algorithms - Chapter 4 1 Design and Analysis of Algorithms - Chapter 4 2

Divide and Conquer Examples


I

General Divide and Conquer recurrence:


T(n) = aT(n/b) + f (n)
1. 2.

Sorting: mergesort and quicksort Tree traversals Binary search Matrix multiplicationmultiplication-Strassens algorithm Convex hullhull-QuickHull algorithm
Design and Analysis of Algorithms - Chapter 4 3

where f (n) (nk)

3.

a < bk a = bk a > bk

T(n) (nk) T(n) (nk lg n ) T(n) (nlog b a)

Note: the same results hold with O instead of .

Design and Analysis of Algorithms - Chapter 4

Mergesort
Algorithm: I Split array A[1..n] in two and make copies of each half in arrays B[1.. n/2 ] and C[1.. n/2 ] I Sort arrays B and C I Merge sorted arrays B and C into array A as follows:
Repeat the following until no elements remain in one of the arrays: 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

Mergesort Example
7 2 1 6 4

Once all elements in one of the arrays are processed, copy the remaining unprocessed elements from the other array into A.
Design and Analysis of Algorithms - Chapter 4 5 Design and Analysis of Algorithms - Chapter 4 6

Design and Analysis of Algorithms


Efficiency of mergesort
I

Chapter 4
Quicksort
I I

All cases have same efficiency: ( n log n) Number of comparisons is close to theoretical minimum for comparisoncomparison-based sorting: log n ! n lg n - 1.44 n

I I

Space requirement: ( n ) (NOT (NOT inin-place) Can be implemented without recursion (bottom(bottom-up)

Select a pivot (partitioning element) Rearrange the list so that all the elements in the positions before the pivot are smaller than or equal to the pivot and those after the pivot are larger than the pivot (See algorithm Partition in section 4.2) Exchange the pivot with the last element in the first (i.e., sublist) sublist) the pivot is now in its final position Sort the two sublists p A[i]p A[i]>p
8

Design and Analysis of Algorithms - Chapter 4

Design and Analysis of Algorithms - Chapter 4

The partition algorithm

Quicksort Example
15 22 13 27 12 10 20 25

Design and Analysis of Algorithms - Chapter 4

Design and Analysis of Algorithms - Chapter 4

10

Efficiency of quicksort
I I I I

QuickHull Algorithm
Inspired by Quicksort compute Convex Hull: Assume points are sorted by x-coordinate values I Identify extreme points P1 and P2 (part of hull) I Compute upper hull:
I

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 avoids worst worst case in sorted files switch to insertion sort on small subfiles elimination of recursion these combine to 2020-25% improvement

find point Pmax that is farthest away from line P1P2 compute the hull of the points to the left of line P1Pmax compute the hull of the points to the left of line PmaxP2
I

Compute lower hull in a similar manner Pmax P2 P1

Considered the method of choice for internal sorting for large files (n 10000)
Design and Analysis of Algorithms - Chapter 4 11

Design and Analysis of Algorithms - Chapter 4

12

Design and Analysis of Algorithms


Efficiency of QuickHull algorithm
I I

Chapter 4
Strassens matrix multiplication
I

Finding point farthest away from line P1P2 can be done in linear time This gives same efficiency as quicksort: quicksort:
Worst case: ( n2) Average case: ( n log n)

Strassen observed [1969] that the product of two matrices can be computed as follows:
C00 C01 = C10 C11 A10 A11 A00 A01 * B10 B11 M3 + M5 M1 + M3 - M2 + M6 B00 B01

If points are not initially sorted by xx-coordinate value, this can be accomplished in ( n log n) no increase in asymptotic efficiency class Other algorithms for convex hull:
Grahams scan DCHull also in ( n log n)
Design and Analysis of Algorithms - Chapter 4 13

M1 + M4 - M5 + M7 = M2 + M4

Design and Analysis of Algorithms - Chapter 4

14

Submatrices: Submatrices:
I

Efficiency of Strassens algorithm


I

M1 = (A00 + A11) * (B00 + B11) M2 = (A10 + A11) * B00 M3 = A00 * (B01 - B11) M4 = A11 * (B10 - B00) M5 = (A00 + A01) * B11 M6 = (A10 - A00) * (B00 + B01) M7 = (A01 - A11) * (B10 + B11)
Design and Analysis of Algorithms - Chapter 4 15

If n is not a power of 2, matrices can be padded with zeros Number of multiplications:

Number of additions:

Other algorithms have improved this result, but are even more complex
Design and Analysis of Algorithms - Chapter 4 16

You might also like