Divide and Conquer Strategy

Slides:



Advertisements
Similar presentations
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Advertisements

Divide-and-Conquer The most-well known algorithm design strategy:
Divide and Conquer Strategy
Algorithm Design Paradigms
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Divide-and-Conquer Dr. Yingwu Zhu P65-74, p83-88, p93-96, p
1 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.
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.
CS4413 Divide-and-Conquer
CSC 331: Algorithm Analysis Divide-and-Conquer Algorithms.
Divide and Conquer Technique General Method:  The Divide and Conquer Technique splits n inputs into k subsets, 1< k ≤ n, yielding k subproblems.  These.
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Chapter 3: Divide and Conquer
Theory of Algorithms: Divide and Conquer
Lecture 2: Divide and Conquer algorithms Phan Thị Hà Dương
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
Foundations of Algorithms, Fourth Edition
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 5.
Chapter 4 Divide-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Introduction to Algorithms Chapter 7: Quick Sort.
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 4 Some of the sides are exported from different sources.
Lecture 8 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Algorithm Design Strategy Divide and Conquer. More examples of Divide and Conquer  Review of Divide & Conquer Concept  More examples  Finding closest.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
CS 253: Algorithms Chapter 7 Mergesort Quicksort Credit: Dr. George Bebis.
Chapter 4 Divide-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
5 - 1 § 5 The Divide-and-Conquer Strategy e.g. find the maximum of a set S of n numbers.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
Chapter 2: Fundamentals of the Analysis of Algorithm Efficiency
Design and Analysis of Algorithms – Chapter 51 Divide and Conquer (I) Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
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.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
MA/CSSE 473 Day 17 Divide-and-conquer Convex Hull Strassen's Algorithm: Matrix Multiplication.
Quick Sort By: HMA. RECAP: Divide and Conquer Algorithms This term refers to recursive problem-solving strategies in which 2 cases are identified: A case.
Theory of Algorithms: Divide and Conquer James Gain and Edwin Blake {jgain | Department of Computer Science University of Cape Town.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Chapter.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Complexity of algorithms Algorithms can be classified by the amount of time they need to complete compared to their input size. There is a wide variety:
Project 2 due … Project 2 due … Project 2 Project 2.
1 Designing algorithms There are many ways to design an algorithm. Insertion sort uses an incremental approach: having sorted the sub-array A[1…j - 1],
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.
Divide and Conquer Andreas Klappenecker [based on slides by Prof. Welch]
Decision Problems Optimization problems : minimum, maximum, smallest, largest Satisfaction (SAT) problems : Traveling salesman, Clique, Vertex-Cover,
2IS80 Fundamentals of Informatics Fall 2015 Lecture 6: Sorting and Searching.
Young CS 331 D&A of Algo. Topic: Divide and Conquer1 Divide-and-Conquer General idea: Divide a problem into subprograms of the same kind; solve subprograms.
 Design and Analysis of Algorithms تصميم وتحليل الخوارزميات (311 عال) Chapter 2 Sorting (insertion Sort, Merge Sort)
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Quick Sort Modifications By Mr. Dave Clausen Updated for Python.
Lecture 6 Sorting II Divide-and-Conquer Algorithms.
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
UNIT- I Problem solving and Algorithmic Analysis
Chapter 4 Divide-and-Conquer
Divide and Conquer Strategy
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4 Divide-and-Conquer
Algorithm Design Methods
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4: Divide and Conquer
Topic: Divide and Conquer
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4 Divide-and-Conquer
Divide and Conquer Merge sort and quick sort Binary search
Chapter 4.
Divide-and-Conquer The most-well known algorithm design strategy:
Algorithms Dr. Youn-Hee Han April-May 2013
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
Divide and Conquer Merge sort and quick sort Binary search
Presentation transcript:

Divide and Conquer Strategy

General Concept of Divide & Conquer Given a function to compute on n inputs, the divide-and-conquer strategy consists of: splitting the inputs into k distinct subsets, 1kn, yielding k subproblems. solving these subproblems combining the subsolutions into solution of the whole. if the subproblems are relatively large, then divide_Conquer is applied again. if the subproblems are small, the are solved without splitting.

The Divide and Conquer Algorithm Divide_Conquer(problem P) { if Small(P) return S(P); else { divide P into smaller instances P1, P2, …, Pk, k1; Apply Divide_Conquer to each of these subproblems; return Combine(Divide_Conque(P1), Divide_Conque(P2),…, Divide_Conque(Pk)); }

Divde_Conquer recurrence relation The computing time of Divde_Conquer is T(n) is the time for Divde_Conquer on any input size n. g(n) is the time to compute the answer directly (for small inputs) f(n) is the time for dividing P and combining the solutions. n small otherwise

Three Steps of The Divide and Conquer Approach The most well known algorithm design strategy: Divide the problem into two or more smaller subproblems. Conquer the subproblems by solving them recursively. Combine the solutions to the subproblems into the solutions for the original problem.

A Typical Divide and Conquer Case a problem of size n subproblem 1 of size n/2 subproblem 2 of size n/2 a solution to subproblem 1 a solution to subproblem 2 a solution to the original problem

An Example: Calculating a0 + a1 + … + an-1 ALGORITHM RecursiveSum(A[0..n-1]) //Input: An array A[0..n-1] of orderable elements //Output: the summation of the array elements if n > 1 return ( RecursiveSum(A[0.. n/2 – 1]) + RecursiveSum(A[n/2 .. n– 1]) ) Efficiency: (for n = 2k) A(n) = 2A(n/2) + 1, n >1 A(1) = 0;

General Divide and Conquer recurrence The Master Theorem T(n) = aT(n/b) + f (n), where f (n) ∈ Θ(nk) a < bk T(n) ∈ Θ(nk) a = bk T(n) ∈ Θ(nk lg n ) a > bk T(n) ∈ Θ(nlog b a) the time spent on solving a subproblem of size n/b. the time spent on dividing the problem into smaller ones and combining their solutions. This is the same slide from chapter 2 (analysis of recursive algorithms) It is a good idea to remind students at this point and do some examples especially if you went over this quickly and did not do examples the previous time. Here are examples from before: T(n) = T(n/2) + 1 T(n) = 2T(n/2) + n T(n) = 3 T(n/2) + n => this last example is important to mention at some point: Θ(nlog23) NB: cannot “eliminate” or simplify this constant

Divide and Conquer Other Examples Sorting: mergesort and quicksort Binary search Matrix multiplication-Strassen’s algorithm

The Divide, Conquer and Combine Steps in Quicksort Divide: Partition array A[l..r] into 2 subarrays, A[l..s-1] and A[s+1..r] such that each element of the first array is ≤A[s] and each element of the second array is ≥ A[s]. (computing the index of s is part of partition.) Implication: A[s] will be in its final position in the sorted array. Conquer: Sort the two subarrays A[l..s-1] and A[s+1..r] by recursive calls to quicksort Combine: No work is needed, because A[s] is already in its correct place after the partition is done, and the two subarrays have been sorted.

Quicksort p A[i]≤p A[i] ≥ p Select a pivot whose value we are going to divide the sublist. (e.g., p = A[l]) Rearrange the list so that it starts with the pivot followed by a ≤ sublist (a sublist whose elements are all smaller than or equal to the pivot) and a ≥ sublist (a sublist whose elements are all greater than or equal to the pivot ) (See algorithm Partition in section 4.2) Exchange the pivot with the last element in the first sublist(i.e., ≤ sublist) – the pivot is now in its final position Sort the two sublists recursively using quicksort. p A[i]≤p A[i] ≥ p

The Quicksort Algorithm ALGORITHM Quicksort(A[l..r]) //Sorts a subarray by quicksort //Input: A subarray A[l..r] of A[0..n-1],defined by its left and right indices l and r //Output: The subarray A[l..r] sorted in nondecreasing order if l < r s  Partition (A[l..r]) // s is a split position Quicksort(A[l..s-1]) Quicksort(A[s+1..r]

The Partition Algorithm ALGORITHM Partition (A[l ..r]) //Partitions a subarray by using its first element as a pivot //Input: A subarray A[l..r] of A[0..n-1], defined by its left and right indices l and r (l < r) //Output: A partition of A[l..r], with the split position returned as this function’s value P A[l] i l; j  r + 1; Repeat repeat i  i + 1 until A[i]>=p //left-right scan repeat j j – 1 until A[j] <= p//right-left scan if (i < j) //need to continue with the scan swap(A[i], a[j]) until i >= j //no need to scan swap(A[l], A[j]) return j

Quicksort Example 15 22 13 27 12 10 20 25

Efficiency of Quicksort Based on whether the partitioning is balanced. Best case: split in the middle — Θ( n log n) T(n) = 2T(n/2) + Θ(n) //2 subproblems of size n/2 each Worst case: sorted array! — Θ( n2) T(n) = T(n-1) + n+1 //2 subproblems of size 0 and n-1 respectively Average case: random arrays — Θ( n log n)

Binary Search – an Iterative Algorithm ALGORITHM BinarySearch(A[0..n-1], K) //Implements nonrecursive binary search //Input: An array A[0..n-1] sorted in ascending order and // a search key K //Output: An index of the array’s element that is equal to K // or –1 if there is no such element l  0, r  n – 1 while l  r do //l and r crosses over can’t find K. m (l + r) / 2 if K = A[m] return m //the key is found else if K < A[m] r m – 1 //the key is on the left half of the array else l  m + 1 // the key is on the right half of the array return -1

Strassen’s Matrix Multiplication Brute-force algorithm c00 c01 a00 a01 b00 b01 = * c10 c11 a10 a11 b10 b11 a00 * b00 + a01 * b10 a00 * b01 + a01 * b11 = a10 * b00 + a11 * b10 a10 * b01 + a11 * b11 8 multiplications Efficiency class:  (n3) 4 additions

Strassen’s Matrix Multiplication Strassen’s Algorithm (two 2x2 matrices) c00 c01 a00 a01 b00 b01 = * c10 c11 a10 a11 b10 b11 m1 + m4 - m5 + m7 m3 + m5 = m2 + m4 m1 + m3 - m2 + m6 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) 7 multiplications 18 additions

Strassen’s Matrix Multiplication Two nxn matrices, where n is a power of two (What about the situations where n is not a power of two?) C00 C01 A00 A01 B00 B01 = * C10 C11 A10 A11 B10 B11 M1 + M4 - M5 + M7 M3 + M5 = M2 + M4 M1 + M3 - M2 + M6

Submatrices: 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)

Efficiency of Strassen’s Algorithm If n is not a power of 2, matrices can be padded with rows and columns with zeros Number of multiplications Number of additions Other algorithms have improved this result, but are even more complex