Divide-and-conquer approach

Slides:



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

A simple example finding the maximum of a set S of n numbers.
What is divide and conquer? Divide and conquer is a problem solving technique. It does not imply any specific computing problems. The idea is to divide.
Comp 122, Spring 2004 Divide and Conquer (Merge Sort)
1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions.
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. Divide and Conquer is a technique for designing the algorithms that consists of decomposing the instance to be solved into a number.
Recurrences. What is a Recurrence Relation? A system of equations giving the value of a function from numbers to numbers in terms of the value of the.
CS 253: Algorithms Chapter 7 Mergesort Quicksort Credit: Dr. George Bebis.
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.
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.
ALGORITHM ANALYSIS AND DESIGN INTRODUCTION TO ALGORITHMS CS 413 Divide and Conquer Algortihms: Binary search, merge sort.
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],
File Organization and Processing Week 13 Divide and Conquer.
CMPT 438 Algorithms. Why Study Algorithms? Necessary in any computer programming problem ▫Improve algorithm efficiency: run faster, process more data,
 Design and Analysis of Algorithms تصميم وتحليل الخوارزميات (311 عال) Chapter 2 Sorting (insertion Sort, Merge Sort)
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Algorithms Sorting – Part 3.
Advanced Algorithms Analysis and Design
Lecture 2: Divide and Conquer
CMPT 438 Algorithms.
Analysis of Algorithms CS 477/677
Introduction to Algorithms: Divide-n-Conquer Algorithms
Introduction to Algorithms
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
分治法.
UNIT- I Problem solving and Algorithmic Analysis
CS 3343: Analysis of Algorithms
Algorithm Design & Analysis
Introduction to Algorithms (2nd edition)
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4 Divide-and-Conquer
Divide-And-Conquer-And-Combine
Divide and Conquer.
Intro to Recursion.
Insertion Sort
Divide-and-Conquer The most-well known algorithm design strategy:
Order Statistics(Selection Problem)
Merge Sort Merge sort is a recursive algorithm for sorting that decomposes the large problem.
Growth Functions Algorithms Lecture 8
CS 3343: Analysis of Algorithms
Chapter 4: Divide and Conquer
Data Structures Review Session
Divide-and-Conquer 7 2  9 4   2   4   7
Lecture No 6 Advance Analysis of Institute of Southern Punjab Multan
Divide-And-Conquer-And-Combine
Searching: linear & binary
CSE 373 Data Structures and Algorithms
Divide-and-Conquer The most-well known algorithm design strategy:
CS200: Algorithms Analysis
CSE 2010: Algorithms and Data Structures
Introduction to Algorithms
Divide and Conquer Algorithms Part I
Chapter 4.
Divide and Conquer (Merge Sort)
Divide-and-Conquer 7 2  9 4   2   4   7
Divide & Conquer Algorithms
Lecture 2: Divide and Conquer
CSC 380: Design and Analysis of Algorithms
Solving recurrence equations
The Selection Problem.
Algorithms CSCI 235, Spring 2019 Lecture 6 Recurrences
nalysis of lgorithms A A Universidad Nacional de Colombia
ITEC324 Principle of CS III
Divide and Conquer Merge sort and quick sort Binary search
CMPT 225 Lecture 10 – Merge Sort.
Presentation transcript:

Divide-and-conquer approach Text Chapters 2

Divide and Conquer What’s divide-and-conquer How to analyze a divide-and-conquer algorithm Examples: merge sort, binary search

What is divide and conquer A technique for designing algorithms that decompose instance into smaller sub-instance of the same problem Solving the sub-instances independently Combining the sub-solutions to obtain the solution of the original instance

Divide-and-conquer basic steps: divide the problem into sub-problems similar to original problem but smaller in size conquer the sub-problems recursively combine solutions to create solution to original problem

Merge Sort Algorithm Divide: divide n-element sequence into two sub-sequences of n/2 elements each Conquer: sort two sub-sequences recursively using merge sort Combine: Merge the two sorted sub-sequences to produce the sorted answer

Merge Sort Algorithm Merge-Sort A[1..n] 1. if n =1, done. 2. Recursively sort A[1..n/2 ] and A[n/2+1.. n ] 3. Merge the two sorted lists

Merging Two Sorted Lists choose the smaller element of the two lists remove it from list and put it into a list repeat previous steps

Merge sort: top down 3 1 4 5 9 2 6 8 3 1 4 5 9 2 6 8 3 1 4 5 9 2 6 8 divide 3 1 4 5 9 2 6 8 1 3 4 5 9 2 6 8 Ad-hoc sort for each small instance

Merge sort: top down 1 3 4 5 9 2 6 8 merge 1 3 4 5 9 2 6 8 1 3 4 5 9 2 6 8 1 2 3 4 5 6 8 9

Merge two arrays 2 3 5 7  1 4 5 6 9 

Merge two arrays 2 3 5 7  1 4 5 6 9  1

Merge two arrays 2 3 5 7  1 4 5 6 9  1 2

Merge two arrays 2 3 5 7  1 4 5 6 9  1 2 3

Merge two arrays 2 3 5 7  1 4 5 6 9  1 2 3 4

Merge two arrays 2 3 5 7  1 4 5 6 9  1 2 3 4 5

Merge two arrays 2 3 5 7  1 4 5 6 9  1 2 3 4 5

Merge two arrays 2 3 5 7  1 4 5 6 9  1 2 3 4 5 6

Merge two arrays 2 3 5 7  1 4 5 6 9  1 2 3 4 5 6 7

Merge two arrays 2 3 5 7  1 4 5 6 9  1 2 3 4 5 6 7 9

Merge Sort

Analyzing Merge Sort Merge-Sort A[1..n] 1. if n =1, done. T(n) Merge-Sort A[1..n] 1. if n =1, done. 2. Recursively sort A[1..n/2 ] and A[n/2+1.. n ] 3. Merge the two sorted lists  (1)  (n/2)+  (n/2) ~ 2 (n/2)  (n) Recurrence:

Recursion Tree T(n) = cn lg n + cn =  (n lg n)

Recursion Tree

A general template Three conditions to be considered DC(x) { if (x is sufficiently small or simple) adhoc(x); // use a basic sub-algorithm decompose x into small instances x[0],..,x[l-1]; // divide for (i=0; i<l; i++) //conquer s[i] = DC(x[i]); combine s[0],.., s[l-1] to obtain solution s for x; // combine return s; } Three conditions to be considered When to use the basic sub-algorithm Efficient decomposition and recombination The sub-instances must be roughly the same size

Sequential Search from a sorted sequence T[] is a sequence in nondecreasing order Find an element in T[] sequentialSearch(T[], x) { for (i=0; i<n; i++) { if (T[i] >= x) // T[i-1]<x<=T[i] return i; } Cost: best, worst, average?

Binary Search Divide: check middle element Conquer: recursively search 1 subarray Combine: trivial

Binary Search Cost? binaryRecursive(T[], i, j, x) binarySearch(T[], x) { // we know T[i-1] < x <= T[j] if (i==j) return i; k = (i+j)/2; if (x <= T[k]) return binaryRecursive(T, i, k, x); else return binaryRecursive(T, k+1, j, x); } binarySearch(T[], x) { if (n==0 || x>T[n]) return n; else return binaryRecursive(T, 1, n, x); } Cost?

Binary Search: Example 3 5 7 8 9 12 15 3 5 7 8 9 12 15 3 5 7 8 9 12 15 12 3 5 7 8 9 12 15

Cost of binary search T(n) = 1 T(n/2) + Θ(1) work dividing and combining number of sub-problem size of sub-problem T(n) = Θ(lg n)