Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.

Slides:



Advertisements
Similar presentations
Comp 122, Spring 2004 Order Statistics. order - 2 Lin / Devi Comp 122 Order Statistic i th order statistic: i th smallest element of a set of n elements.
Advertisements

Order Statistics Sorted
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 6.
Theory of Computing Lecture 3 MAS 714 Hartmut Klauck.
Data Structures Lecture 9 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
Medians and Order Statistics
Introduction to Algorithms
Introduction to Algorithms Jiafen Liu Sept
CS 206 Introduction to Computer Science II 03 / 02 / 2009 Instructor: Michael Eckmann.
Divide And Conquer Distinguish between small and large instances. Small instances solved differently from large ones.
Divide-and-Conquer Recursive in structure –Divide the problem into several smaller sub-problems that are similar to the original but smaller in size –Conquer.
Stephen P. Carl - CS 2421 Recursive Sorting Algorithms Reading: Chapter 5.
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
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.
25 May Quick Sort (11.2) CSE 2011 Winter 2011.
CSC 2300 Data Structures & Algorithms March 23, 2007 Chapter 7. Sorting.
CS38 Introduction to Algorithms Lecture 7 April 22, 2014.
Lecture 8 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
Lecture 6 Divide and Conquer for Nearest Neighbor Problem Shang-Hua Teng.
CSC 2300 Data Structures & Algorithms January 30, 2007 Chapter 2. Algorithm Analysis.
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.
Divide-And-Conquer Sorting Small instance.  n
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.
Order Statistics The ith order statistic in a set of n elements is the ith smallest element The minimum is thus the 1st order statistic The maximum is.
CSCE 3110 Data Structures & Algorithm Analysis Sorting (I) Reading: Chap.7, Weiss.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
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],
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
Sorting: Implementation Fundamental Data Structures and Algorithms Klaus Sutner February 24, 2004.
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.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 7.
Divide And Conquer A large instance is solved as follows:  Divide the large instance into smaller instances.  Solve the smaller instances somehow. 
1 Ch.19 Divide and Conquer. 2 BIRD’S-EYE VIEW Divide and conquer algorithms Decompose a problem instance into several smaller independent instances May.
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)
Lecture # 6 1 Advance Analysis of Algorithms. Divide-and-Conquer Divide the problem into a number of subproblems Similar sub-problems of smaller size.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 8b. Sorting(2): (n log n) Algorithms.
Sorting – Lecture 3 More about Merge Sort, Quick Sort.
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
1 Chapter 7 Quicksort. 2 About this lecture Introduce Quicksort Running time of Quicksort – Worst-Case – Average-Case.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Advanced Sorting.
Analysis of Algorithms CS 477/677
Introduction to Algorithms: Divide-n-Conquer Algorithms
Quick-Sort 9/12/2018 3:26 PM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Divide-And-Conquer-And-Combine
Divide and Conquer.
CS 3343: Analysis of Algorithms
Chapter 4: Divide and Conquer
Quick Sort (11.2) CSE 2011 Winter November 2018.
Topic: Divide and Conquer
Divide-And-Conquer-And-Combine
Trevor Brown CS 341: Algorithms Trevor Brown
Quick-Sort 4/25/2019 8:10 AM Quick-Sort     2
Topic: Divide and Conquer
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
The Selection Problem.
CSCE 3110 Data Structures & Algorithm Analysis
Divide and Conquer Merge sort and quick sort Binary search
Presentation transcript:

Nattee Niparnan

Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive

Today Topic  Divide and Conquer

Algorithm Design Technique Divide and conquer

Divide and Conquer

Induction

Induction Example

The Inductive Step  The inductive step  Assume that it is true for (n-1)  Check the case (n)  … + n = ( … + (n-1) ) + n  = (n-1)(n) / 2 + n  = n((n-1)/2 + 1)  = n(n/2-1/2 + 1)  = n(n/2 + 1/2)  = n(n+1)/2

The Inductive Step  The inductive step  Assume that it is true for (n-1)  Check the case (n)  … + n = ( … + (n-1) ) + n  = (n-1)(n) / 2 + n  = n((n-1)/2 + 1)  = n(n/2-1/2 + 1)  = n(n/2 + 1/2)  = n(n+1)/2 By using (n-1)

The Induction  By using the result of the smaller case  We can proof the larger case

Key of Divide and Conquer

Steps in D&C  Questions  What if we has the solution of the subproblem (smaller problem)?  What is the subproblem? (how to divide?)  What can we do with the result? (how to conquer?)  If we know the answer, the rest comes automatically from the recursion

Code Example ResultType DandC(Problem p) { if (p is trivial) { solve p directly return the result } else { divide p into p 1,p 2,...,p n for (i = 1 to n) r i = DandC(p i ) combine r 1,r 2,...,r n into r return r }

Code Example ResultType DandC(Problem p) { if (p is trivial) { solve p directly t s return the result } else { divide p into p 1,p 2,...,p n t d for (i = 1 to n) t r r i = DandC(p i ) combine r 1,r 2,...,r n into r t c return r }

Examples  Let’s see some examples

Merge Sort

The Sorting Problem  Given a sequence of numbers  A = [a 1,a 2,a 3,…,a n ]  Sort the sequence from min to max

The question  What if we know the solution of the smaller problem  What is the smaller problem?  Try the same problem  sorting  What if we know the result of the sorting of some elements?

The Question  How to divide?  Let’s try sorting of the smaller array  Divide exactly at the half of the array  How to conquer?  Merge the result directly

Idea  Simplest dividing  Divide array into two array of half size  Laborious conquer  Merge the result

Divide

Merge

Analysis  T(n) = 2T(n/2) + O(n)  Master method  T(n) = O(n lg n)

Quick Sort

The Sorting Problem (again)  Given a sequence of numbers  A = [a 1,a 2,a 3,…,a n ]  Sort the sequence from min to max

Problem of Merge Sort  Need the use of external memory for merging  Are there any other way such that conquering is not that hard?

The Question  How to divide?  Try doing the same thing as the merge sort  Add that every element in the first half is less than the second half  Can we do that?  How to conquer?  The sorted result should be easier to merge?

Idea  Laborious dividing  Divide array into two arrays  Add the requirement of value of the array  Simplest conquer  Simply connect the result

Is dividing scheme possible?  Can we manage to have two subproblem of equal size  That satisfy our need?  Any trade off?

The Median

Simplified Division

partitioning First k element Other n- k element

Solve by Recursion sorting

Do nothing!

Analysis  T(n) = T(k) + T(n – k) + Θ (n)  There can be several cases  Up to which K that we chosen

Analysis : Worst Case  K always is 1 T(n) = T(1) + T(n – 1) + Θ (n) = T(n – 1) + Θ (n) = Σ Θ (i) = Θ (n 2 )

Analysis : Best Case  K always is the median T(n) = 2T(n/2) + Θ (n) = Θ (n log n) The same as the merge sort (without the need of external memory)

Fixing the worst case  Worst case happens when we select wrong pivot  If we select the first element as a pivot  What if the data is sorted?

Fixing the worst case  Select wrong pivot leads to worst case.  There will exist some input such that for any strategy of “deterministic” pivot selection leads to worst case.

Fixing the worst case  Use “non-deterministic” pivot selection  i.e., randomized selection  Pick a random element as a pivot  It is unlikely that every selection leads to worst case  We can hope that, on average,  it is O(n log n)

Exponential

The Problem  Computing x n mod k  Given x, n and k

Naïve method res = x mod n; for (i = 2 to n) do { res = res * x; res = res mod n; } Θ (n)

The Question  What if we knows the solution to the smaller problem?  Smaller problem  smaller N  x n ?? from x (n-e) ?  Let’s try x (n/2)

The Question  How to divide?  x n = x n/2 * x n/2 (n is even)  x n = x n/2 * x n/2 * x (n is odd)  How to conquer?  Compute x n/2 mod k  Square and times with x, if needed,  mod k afterward

Analysis  T(n) = T(n/2) + Θ (1)  = O(log n)

Example 2 92 mod 10  2 92 = 2 46 × 2 46  2 46 = 2 23 × 2 23  2 23 = 2 11 × 2 11 × 2  2 11 = 2 5 × 2 5 × 2  2 5 = 2 2 × 2 2 × 2  2 2 = 2 1 × 2 1 = 4×4 mod 10 = 6 = 8×8 mod 10 = 4 = 8×8×2 mod 10 = 8 = 2×2×2 mod 10 = 8 = 4×4 × 2 mod 10 = 2 = 2×2 mod 10 = 4

Maximum Sum of Subsequence

The MSS Problem  Given a sequence of numbers  A = [a 1,a 2,a 3,…,a n ]  Find a subsequence s = [a i,a i+1,a i+2,…,a k ]  Such that the sum of the element in s is maximum

Naïve approach  Try all possible sequences  How many sequence?  How much time does it use to compute the sum?

Naïve approach

The DC Approach  What if we know the solution of the smaller problem  What if we know MSS of the first half and the second half?

The Question  How to divide?  By half of the member  How to conquer?  Does the result of the subproblem can be used to compute the solution?  Let’s see

Combining the result from sub MSS

Combining the result from sub MSS

Combining the result from sub MSS Sum = 6Sum = 8 Shall this be our answer?

Combining the result from sub MSS Do we consider all possibilities?

Combining the result from sub MSS Considered all pairs in each half But not the pair including member from both half There are (n/2) 2 additiona l pairs

Compute max of cross over  Consider this  The max sequence from the green part is always the same, regardless of the pink part  The sequence always start at the left border

Compute max of cross over  Similarly  The max sequence from the pink part is always the same, regardless of the green part  The sequence always start at the right border

Compute max of cross over  Just find the marginal max from each part

Compute max of cross over  Just find the marginal max from each part

Compute max of cross over  Just find the marginal max from each part

Compute max of cross over  Just find the marginal max from each part Less than previous

Compute max of cross over  Just find the marginal max from each part

Compute max of cross over  Just find the marginal max from each part

Compute max of cross over  Just find the marginal max from each part

Compute max of cross over  Just find the marginal max from each part This is max

Compute max of cross over  Just find the marginal max from each part Total = 13 takes Θ (n/2)

Combine  Max from the three problem  The left half  The right half  The cross over half

Analysis  T(n) = 2 T(n/2) + Θ (n)  = Θ (n log n) Left and right parts Cross over part