Topic: Divide and Conquer

Slides:



Advertisements
Similar presentations
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Advertisements

Divide-and-Conquer CIS 606 Spring 2010.
A simple example finding the maximum of a set S of n numbers.
Divide and Conquer Strategy
Strassen's Matrix Multiplication Sibel KIRMIZIGÜL.
Chapter 6 Divide and Conquer  Introduction  Binary Search  Mergesort  The Divide and Conquer Paradigm  Quicksort  Multiplication of Large Integers.
Lecture 10 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Introduction to Algorithms 6.046J/18.401J L ECTURE 3 Divide and Conquer Binary search Powering a number Fibonacci numbers Matrix multiplication Strassen’s.
CS223 Advanced Data Structures and Algorithms 1 Divide and Conquer Neil Tang 4/15/2010.
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
Divide and Conquer. Recall Complexity Analysis – Comparison of algorithm – Big O Simplification From source code – Recursive.
Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.
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.
Divide and Conquer Reading Material: Chapter 6 (except Section 6.9).
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 5 Instructor: Paul Beame TA: Gidon Shavit.
Unit 1. Sorting and Divide and Conquer. Lecture 1 Introduction to Algorithm and Sorting.
Prof. Swarat Chaudhuri COMP 482: Design and Analysis of Algorithms Spring 2013 Lecture 12.
Divide-and-Conquer 7 2  9 4   2   4   7
Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.
Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 3 Prof. Erik Demaine.
Analysis of Algorithms
Project 2 due … Project 2 due … Project 2 Project 2.
Télécom 2A – Algo Complexity (1) Time Complexity and the divide and conquer strategy Or : how to measure algorithm run-time And : design efficient algorithms.
4.Divide-and-Conquer Hsu, Lih-Hsing. Computer Theory Lab. Chapter 4P.2 Instruction Divide Conquer Combine.
ADA: 4.5. Matrix Mult.1 Objective o an extra divide and conquer example, based on a question in class Algorithm Design and Analysis (ADA) ,
1 How to Multiply Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. integers, matrices, and polynomials.
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.
Divide and Conquer Faculty Name: Ruhi Fatima Topics Covered Divide and Conquer Matrix multiplication Recurrence.
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 16.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Chapter 2. Divide-and-conquer Algorithms
Analysis of Algorithms CS 477/677
Introduction to Algorithms: Divide-n-Conquer Algorithms
Lecture 3: Parallel Algorithm Design
Modeling with Recurrence Relations
Analysis of Algorithms
Unit 1. Sorting and Divide and Conquer
Chapter 4: Divide and Conquer
Introduction to the Design and Analysis of Algorithms
Lecture 4 Divide-and-Conquer
Advanced Design and Analysis Techniques
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4 Divide-and-Conquer
CS 3343: Analysis of Algorithms
CSCE 411 Design and Analysis of Algorithms
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
Unit-2 Divide and Conquer
Dynamic Programming General Idea
Topic: Divide and Conquer
CSE 373 Data Structures and Algorithms
ICS 353: Design and Analysis of Algorithms
Introduction to Algorithms
Divide and Conquer Algorithms Part I
Solving Recurrence Relations
Order Statistics Def: Let A be an ordered set containing n elements. The i-th order statistic is the i-th smallest element. Minimum: 1st order statistic.
Divide-and-Conquer 7 2  9 4   2   4   7
Dynamic Programming General Idea
Divide and Conquer Neil Tang 4/24/2008
Divide & Conquer Algorithms
CSCI 256 Data Structures and Algorithm Analysis Lecture 12
Topic: Divide and Conquer
ICS 353: Design and Analysis of Algorithms
Divide-and-Conquer 7 2  9 4   2   4   7
Lecture 15 Closest Pair, Multiplication
Divide and Conquer Merge sort and quick sort Binary search
Time Complexity and the divide and conquer strategy
Use of Submatrices If the original matrix is n x n, the four submatrices are n/2 x n/2 each; for simplicity we assume powers of two To find the result.
Presentation transcript:

Topic: Divide and Conquer General idea: Divide a problem into subprograms of the same kind; solve subprograms using the same approach, and combine partial solution (if necessary). Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

Topic: Divide and Conquer Find the maximum and minimum The problem: Given a list of unordered n elements, find max and min The straightforward algorithm:   max ← min ← A (1); for i ← 2 to n do if A (i) > max, max ← A (i); if A (i) < min, min ← A (i); Key comparisons: 2(n – 1) Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

Topic: Divide and Conquer List List 1 List 2 n elements n/2 elements n/2 elements min, max min1, max1 min2, max2 min = MIN ( min1, min2 ) max = MAX ( max1, max2) Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

Topic: Divide and Conquer The Divide-and-Conquer algorithm: procedure Rmaxmin (i, j, fmax, fmin); // i, j are index #, fmax, begin // fmin are output parameters case: i = j: fmax ← fmin ← A (i); i = j –1: if A (i) < A (j) then fmax ← A (j); fmin ← A (i); else fmax ← A (i); fmin ← A (j); else: mid ← ; call Rmaxmin (i, mid, gmax, gmin); call Rmaxmin (mid+1, j, hmax, hmin); fmax ← MAX (gmax, hmax); fmin ← MIN (gmin, hmin); end end; Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

Topic: Divide and Conquer Example: find max and min in the array: 22, 13, -5, -8, 15, 60, 17, 31, 47 ( n = 9 ) Index: 1 2 3 4 5 6 7 8 9 Array: 22 13 -5 -8 15 60 17 31 47 Rmaxmin(1, 9, 60, -8) 1, 5, 22, -8 6, 9, 60, 17 1, 3, 22, -5 4, 5, 15, -8 6,7, 60, 17 8, 9, 47, 31 1,2, 22, 13 3, 3,-5, -5 (1) (6) (7) (2) (5) (8) (3) (4) Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

Topic: Divide and Conquer Analysis: For algorithm containing recursive calls, we can use recurrence relation to find its complexity T(n) - # of comparisons needed for Rmaxmin Recurrence relation: Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

Topic: Divide and Conquer Assume n = 2k for some integer k Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

Topic: Divide and Conquer Theorem: Claim: They don’t have to be the same constant. We make them the same here for simplicity. It will not affect the overall result. where are a, b, c constants Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

Topic: Divide and Conquer Proof: Assume n = ck Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

Topic: Divide and Conquer If a < c, is a constant T(n) = bn · a constant  T(n) = O(n) if a = c, Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

Topic: Divide and Conquer If a > c, Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

Topic: Divide and Conquer 2. Matrix multiplication The problem:   Multiply two matrices A and B, each of size Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

Topic: Divide and Conquer The traditional way:   use three for-loop Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

Topic: Divide and Conquer The Divide-and-Conquer way: transform the problem of multiplying A and B, each of size [n×n] into 8 subproblems, each of size Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

Topic: Divide and Conquer which an2 is for addition so, it is no improvement compared with the traditional way Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

Topic: Divide and Conquer Example: use Divide-and-Conquer way to solve it as following: Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

Topic: Divide and Conquer Strassen’s matrix multiplication: · Discover a way to compute the Cij’s using 7 multiplications and 18 additions or subtractions Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

Topic: Divide and Conquer Algorithm : procedure Strassen (n, A, B, C) // n is size, A,B the input matrices, C output matrix begin if n = 2, else (cont.) Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

Topic: Divide and Conquer else Partition A into 4 submatrices: ; Partition B into 4 submatrices: ; call Strassen ( ; call Strassen ( call Strassen ( ; Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

Topic: Divide and Conquer call Strassen ( ; end; Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

Topic: Divide and Conquer Analysis: Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

Topic: Divide and Conquer Assume n = 2k for some integer k Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

Topic: Divide and Conquer 3. Integer multiplication The problem:   Multiply two large integers (n digits) The traditional way: Use two loops, it takes O(n2) operations Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

Topic: Divide and Conquer The Divide-and-Conquer way:   Suppose x and y are large integers, divide x into two parts (a and b), and divide y into c and d. x: y: a b c d So, transform the problem of multiplying two n-digit integers into four subproblems of multiplying two -digit integers. Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

Topic: Divide and Conquer Worst-Case is:   However, it is same as the traditional way. Instead, we write: Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer

Topic: Divide and Conquer The algorithm by Divide-and-Conquer: function multiplication (x,y) begin n = MAX ( # of digits in x, # of digits in y); if (x = 0) or (y = 0), return 0; else if (n = 1), return x*y in the usual way; else m = ; a = x divide 10m; b = x rem 10m; c = y divide 10m; d = y rem 10m; p1= MULTIPLICATION (a, c); p2= MULTIPLICATION (b, d); p3 = MULTIPLICATION (a+b, c+d); return ; end; Spring-2005 Young CS 331 D&A of Algo. Topic: Divide and Conquer