Divide and Conquer.

Slides:



Advertisements
Similar presentations
Back to Sorting – More efficient sorting algorithms.
Advertisements

Garfield AP Computer Science
A simple example finding the maximum of a set S of n numbers.
September 12, Algorithms and Data Structures Lecture III Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
Recursion & Merge Sort Introduction to Algorithms Recursion & Merge Sort CSE 680 Prof. Roger Crawfis.
ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
Divide and Conquer Chapter 6. Divide and Conquer Paradigm Divide the problem into sub-problems of smaller sizes Conquer by recursively doing the same.
Lecture 8 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
Cmpt-225 Sorting. Fundamental problem in computing science  putting a collection of items in order Often used as part of another algorithm  e.g. sort.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
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.
Mergesort and Quicksort Chapter 8 Kruse and Ryba.
MergeSort Source: Gibbs & Tamassia. 2 MergeSort MergeSort is a divide and conquer method of sorting.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
1 Divide and Conquer Binary Search Mergesort Recurrence Relations CSE Lecture 4 – Algorithms II.
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],
Searching and Sorting Recursion, Merge-sort, Divide & Conquer, Bucket sort, Radix sort Lecture 5.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
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.
CSE 340: Review (at last!) Measuring The Complexity Complexity is a function of the size of the input O() Ω() Θ() Complexity Analysis “same order” Order.
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.
 Design and Analysis of Algorithms تصميم وتحليل الخوارزميات (311 عال) Chapter 2 Sorting (insertion Sort, Merge Sort)
Lecture 6 Sorting II Divide-and-Conquer Algorithms.
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
Introduction to Algorithms
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
CS 162 Intro to Programming II
Algorithm Design & Analysis
Lecture 4 Divide-and-Conquer
Sorting by Tammy Bailey
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4 Divide-and-Conquer
Divide and Conquer divide and conquer algorithms typically recursively divide a problem into several smaller sub-problems until the sub-problems are.
Divide-And-Conquer-And-Combine
MergeSort Source: Gibbs & Tamassia.
Algorithm Design Methods
Divide-and-Conquer The most-well known algorithm design strategy:
Merge Sort Merge sort is a recursive algorithm for sorting that decomposes the large problem.
CS 3343: Analysis of Algorithms
Chapter 4: Divide and Conquer
CSC212 Data Structure - Section RS
Sorting Algorithms Ellysa N. Kosinaya.
8/04/2009 Many thanks to David Sun for some of the included slides!
Topic: Divide and Conquer
Lecture No 6 Advance Analysis of Institute of Southern Punjab Multan
Divide-And-Conquer-And-Combine
Divide-and-Conquer The most-well known algorithm design strategy:
CS200: Algorithms Analysis
CSE 2010: Algorithms and Data Structures
Divide and Conquer Algorithms Part I
CSE 332: Data Abstractions Sorting I
ITEC 2620M Introduction to Data Structures
Sorting.
Divide-and-Conquer 7 2  9 4   2   4   7
Sorting And Searching CSE116A,B 4/7/2019 B.Ramamurthy.
Trevor Brown CS 341: Algorithms Trevor Brown
Divide & Conquer Algorithms
Topic: Divide and Conquer
CSC 380: Design and Analysis of Algorithms
Solving recurrence equations
CS200: Algorithm Analysis
Divide and Conquer Merge sort and quick sort Binary search
Divide-and-Conquer Divide: the problem into subproblems
Presentation transcript:

Divide and Conquer

Sorting algorithms Insertion, selection and bubble sort have quadratic worst-case performance The faster comparison based algorithm ? O(nlogn) Mergesort and Quicksort

Sorting Algorithms Courtesy of https://www.youtube.com/watch?v=kPRA0W1kECg

Divide and Conquer Divide: a big problem into smaller problems Conquer: smaller problems recursively Combine: the solutions of smaller problems “Big problems are hard to solve, but smaller ones are much easier”

Example a problem of size n subproblem 1 subproblem 2 of smaller size of size smaller size subproblem 1 of smaller size a solution to the original problem a problem of size n

Sub Problem 1 a subproblem of size n’ sub_subproblem 1 of size smaller size sub_subproblem 1 of smaller size a solution to Sub_subproblem 1 the subproblem a subproblem of size n’

Example: Binary Search Divide: a list into two smaller lists Conquer: search one smaller list recursively Combine: trivial

Sorting Sorting takes an unordered collection and makes it an ordered one. 1 2 3 4 5 6 77 42 35 12 101 5 1 2 3 4 5 6 5 12 35 42 77 101

Example: Merge Sort Divide: a list of n into two smaller lists (1) a list of n/2 numbers (2) a list of n/2 numbers Conquer: sort each smaller list recursively Sort list (1) recursively Sort list (2) recursively Combine: merge the results of (1) and (2)

Merge Sort

Merge Sort Algorithm MergeSort(A) if A’s size > 1 Divide array A in halves Call MergeSort on first half. Call MergeSort on second half. Merge two results (combine).

Merge Sort Algorithm MergeSort(A, l, r) if l < r: m = (l + r)/2 //Divide MergeSort(A,l,m) //Conquer MergeSort(A,m+1,r)//Conquer Merge(A,l,m,r)// Combine

How to Merge? Merge the sub-problem solutions together: Compare the sub-array’s first elements Remove the smallest element and put it into the result array Continue the process until all elements have been put into the result array 14 23 45 98 6 33 42 67 6 14 23 33 42 45 67 98

98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 98 23

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 98 23 Merge

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 98 23 23 Merge

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 98 23 23 98 Merge

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 98 23 45 14 23 98

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 98 23 45 14 23 98 Merge

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 98 23 45 14 23 98 14 Merge

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 98 23 45 14 23 98 14 45 Merge

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 98 23 45 14 23 98 14 45 Merge

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 98 23 45 14 23 98 14 45 14 Merge

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 98 23 45 14 23 98 14 45 14 23 Merge

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 98 23 45 14 23 98 14 45 14 23 45 Merge

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 98 23 45 14 23 98 14 45 14 23 45 98 Merge

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 23 98 14 45 14 23 45 98

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 23 98 14 45 14 23 45 98

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 23 98 14 45 14 23 45 98 Merge

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 23 98 14 45 6 14 23 45 98 Merge

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 23 98 14 45 6 67 14 23 45 98 Merge

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 14 23 45 98

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 14 23 45 98 Merge

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 14 23 45 98 Merge

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 14 23 45 98 Merge

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 14 23 45 98 Merge

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 14 23 45 98 6 Merge

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 14 23 45 98 6 33 Merge

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 14 23 45 98 6 33 42 Merge

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 14 23 45 98 6 33 42 67 Merge

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 14 23 45 98 6 33 42 67 Merge

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 14 23 45 98 6 33 42 67 6 Merge

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 14 23 45 98 6 33 42 67 6 14 Merge

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 14 23 45 98 6 33 42 67 6 14 23 Merge

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 14 23 45 98 6 33 42 67 6 14 23 33 Merge

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 14 23 45 98 6 33 42 67 6 14 23 33 42 Merge

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 14 23 45 98 6 33 42 67 6 14 23 33 42 45 Merge

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 14 23 45 98 6 33 42 67 6 14 23 33 42 45 67 Merge

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 14 23 45 98 6 33 42 67 6 14 23 33 42 45 67 98 Merge

98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42 23 98 14 45 6 67 33 42 14 23 45 98 6 33 42 67 6 14 23 33 42 45 67 98

98 23 45 14 6 67 33 42 6 14 23 33 42 45 67 98

Summary Divide the unsorted collection into two Until the sub-arrays only contain one element Then merge the sub-problem solutions together

Time Complexity? Recurrence Equation

Merge Sort Algorithm MergeSort(A, l, r) if l < r: m = (l + r)/2 //Divide MergeSort(A,l,m) //Conquer MergeSort(A,m+1,r)//Conquer Merge(A,l,m,r)// Combine

Time Complexity Recurrence equation: Assume n is a power of 2 c1 if n=1 T(n) = 2T(n/2) + c2n if n>1, n=2k

Recursion Tree

Binary Search Example

Merge Sort Apply divide-and-conquer to sorting problem Problem: Given n elements, sort elements into non-decreasing order Divide-and-Conquer: If n=1 terminate (every one-element list is already sorted) If n>1, partition elements into two or more sub-collections; sort each; combine into a single sorted list How do we partition?

Partitioning - Choice 1 First n-1 elements into set A, last element set B Sort A using this partitioning scheme recursively B already sorted Combine A and B using method Insert() (= insertion into sorted array) Leads to recursive version of InsertionSort() Number of comparisons: O(n2) Best case = n-1 Worst case = O(n2)

Partitioning - Choice 2 Put element with largest key in B, remaining elements in A Sort A recursively To combine sorted A and B, append B to sorted A Use Max() to find largest element  recursive SelectionSort() Use bubbling process to find and move largest element to right-most position  recursive BubbleSort() All O(n2)

Partitioning - Choice 3 Let’s try to achieve balanced partitioning A gets n/2 elements, B gets rest half Sort A and B recursively Combine sorted A and B using a process called merge, which combines two sorted lists into one O(n log n)

Master Theorem