Divide and Conquer Sanghyun Park Fall 2002 CSE, POSTECH.

Slides:



Advertisements
Similar presentations
Algorithms and Data Structures Lecture III
Advertisements

Back to Sorting – More efficient sorting algorithms.
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
A simple example finding the maximum of a set S of n numbers.
Divide And Conquer Distinguish between small and large instances. Small instances solved differently from large ones.
1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions.
September 12, Algorithms and Data Structures Lecture III Simonas Šaltenis Nykredit Center for Database Research Aalborg University
1 Merge Sort Review of Sorting Merge Sort. 2 Sorting Algorithms Selection Sort uses a priority queue P implemented with an unsorted sequence: –Phase 1:
Divide And Conquer Distinguish between small and large instances. Small instances solved differently from large ones.
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Quicksort Quicksort     29  9.
CS 2210 (22C:19) Discrete Structures Advanced Counting
Recursion & Merge Sort Introduction to Algorithms Recursion & Merge Sort CSE 680 Prof. Roger Crawfis.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
1 Merge Sort Merge Sort Reading p A Sorting Pattern The most efficient sorting algorithms all seem to follow a divide-and-conquer strategy.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Merge sort, Insertion sort
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
Merge sort, Insertion sort. Sorting I / Slide 2 Sorting * Selection sort or bubble sort 1. Find the minimum value in the list 2. Swap it with the value.
CS Main Questions Given that the computer is the Great Symbol Manipulator, there are three main questions in the field of computer science: What kinds.
Sorting Rearrange n elements into ascending order. 7, 3, 6, 2, 1  1, 2, 3, 6, 7.
Recurrences The expression: is a recurrence. –Recurrence: an equation that describes a function in terms of its value on smaller functions Analysis of.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
Unit 1. Sorting and Divide and Conquer. Lecture 1 Introduction to Algorithm and Sorting.
ALGORITHM ANALYSIS AND DESIGN INTRODUCTION TO ALGORITHMS CS 413 Divide and Conquer Algortihms: Binary search, merge sort.
Divide-And-Conquer Sorting Small instance.  n
Sorting Sanghyun Park Fall 2002 CSE, POSTECH. Sorts To Consider Selection sort Bubble sort Insertion sort Merge sort Quick sort Why do we care about sorting?
Project 2 due … Project 2 due … Project 2 Project 2.
4.4 Recursive Algorithms A recursive algorithm is one which calls itself to solve “smaller” versions of an input problem. How it works: – The current status.
Dynamic Programming Sequence of decisions. Problem state. Principle of optimality. Dynamic Programming Recurrence Equations. Solution of recurrence equations.
Lecture 2 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
File Organization and Processing Week 13 Divide and Conquer.
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.
Merge sort, Insertion sort. Sorting I / Slide 2 Sorting * Selection sort (iterative, recursive?) * Bubble sort.
Divide and Conquer Applications Sanghyun Park Fall 2002 CSE, POSTECH.
Algorithm Design Methods (II) Fall 2003 CSE, POSTECH.
Divide And Conquer A large instance is solved as follows:  Divide the large instance into smaller instances.  Solve the smaller instances somehow. 
Midterm Review 1. Midterm Exam Thursday, October 15 in classroom 75 minutes Exam structure: –TRUE/FALSE questions –short questions on the topics discussed.
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.
Lecture 2 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Sorting. Typically, most organizations spend large percentage of computing time (25% - 50%) on sorting Internal sorting the list is small enough to sort.
CSC317 1 So far so good, but can we do better? Yes, cheaper by halves... orkbook/cheaperbyhalf.html.
 Design and Analysis of Algorithms تصميم وتحليل الخوارزميات (311 عال) Chapter 2 Sorting (insertion Sort, Merge Sort)
A Introduction to Computing II Lecture 7: Sorting 1 Fall Session 2000.
Divide and Conquer Sorting
Advanced Algorithms Analysis and Design
Lecture 2 Algorithm Analysis
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
分治法.
COMP108 Algorithmic Foundations Divide and Conquer
Unit 1. Sorting and Divide and Conquer
Lecture 4 Divide-and-Conquer
Chapter 4 Divide-and-Conquer
Divide-And-Conquer-And-Combine
Lecture 3: Divide-and-conquer
Divide and Conquer.
Divide And Conquer Distinguish between small and large instances.
Divide-And-Conquer-And-Combine
CSE 2010: Algorithms and Data Structures
Divide and Conquer Algorithms Part I
CS 2210 Discrete Structures Advanced Counting
Trevor Brown CS 341: Algorithms Trevor Brown
Divide & Conquer Algorithms
Divide And Conquer Distinguish between small and large instances.
The Selection Problem.
Divide-and-conquer approach
Chapter 7 : Sorting 교수 : 이상환 강의실 : 113,118호, 324호 연구실 : 과학관 204호
Presentation transcript:

Divide and Conquer Sanghyun Park Fall 2002 CSE, POSTECH

Divide and Conquer A large instance is solved as follows: – Divide the large instance into smaller instances. – Solve the smaller instances somehow. – Combine the results of the smaller instances to obtain the result for the original large instance. A small instance is solved in some other way.

Small and Large Instance Small instance – Sort a list that has n <= 10 elements. – Find the minimum of n <= 2 elements. Large instance – Sort a list that has n > 10 elements. – Find the minimum of n > 2 elements.

Solving A Small Instance A small instance is solved using some direct/simple strategy. – Sort a list that has n <= 10 elements. Use insertion, bubble, or selection sort. – Find the minimum of n <= 2 elements. When n = 0, there is no minimum element. When n = 1, the single element is the minimum. When n = 2, compare the two elements and determine which is smaller.

Sort A Large List Sort a list that has n > 10 elements. – Sort 15 elements by dividing them into 2 smaller lists. One list has 7 elements and the other has 8 elements. – Sort these two lists using the method for small lists. – Merge the two sorted lists into a single sorted list.

Find The Min Of A Large List Find the minimum of 20 elements. – Divide into two groups of 10 elements each. – Find the minimum element in each group somehow. – Compare the minimums of each group to determine the overall minimum.

Recursion In Divide and Conquer Often the smaller instances that result from the divide step are instances of the original problem (true for our sort and min problems). In this case, – If the new instance is a smaller instance, it is solved using the method for small instances. – If the new instance is a large instance, it is solved using the divide-and-conquer method recursively. Generally, performance is best when the smaller instances that result from the divide step are of approximately the same size.

Recursive Find Min Find the minimum of 20 elements. – Divide into two groups of 10 elements each. – Find the minimum element in each group recursively. The recursion terminates when the number of elements is <= 2. At this time the minimum is found using the method for small instances. – Compare the minimums of each group to determine the overall minimum.

Tiling A Defective Chessboard

Our Definition Of A Chessboard A chessboard is an n x n grid, where n is a power of 2.

A Defective Chessboard A defective chessboard is chessboard that has one unavailable (defective) position.

A Triomino A triomino is an L shaped object that can cover three squares of a chessboard. A triomino has four orientations.

Tiling A Defective Chessboard Place (n 2 -1)/3 triominoes on an n x n defective chessboard so that all n 2 -1 nondefective positions are covered.

Tiling A Defective Chessboard Divide into four smaller chessboards. 4 x 4 One of these is a defective 4 x 4 chessboard.

Tiling A Defective Chessboard Make the other three 4 x 4 chessboards defective by placing a triomino at their common corner. Recursively tile the four defective 4 x 4 chessboards.

Complexity Let n = 2 k. Let t(k) be the time taken to tile a 2 k x 2 k defective chessboard. t(0) = d, where d is a constant. t(k) = 4t(k-1) + c, when k > 0. Here c is a constant. Recurrence equation for t().

Substitution Method t(k)= 4t(k-1) + c = 4[4t(k-2)+c] + c = 4 2 t(k-2) + 4c + c = 4 2 [4t(k-3)+c] + 4c + c = 4 3 t(k-3) c + 4c + c = … = 4 k t(0) + 4 k-1 c + 4 k-2 c + … c + 4c + c = 4 k d + 4 k-1 c + 4 k-2 c + … c + 4c + c = Theta(4 k ) = Theta(number of triominoes placed)

Tiling A Defective Chessboard C++ program is given on p. 675 of the text book. The complete program with ‘main’ is also available at

Min And Max Find the lightest and heaviest of n elements using a balance that allows you to compare the weight of 2 elements. Minimize the number of comparisons.

Max Element Find element with max weight from w[0:n-1]. maxElement = 0; for (int i = 1; i < n; i++) if (w[maxElement] < w[i]) maxElement = i; Number of comparisons of w values is n-1.

Min And Max Find the max of n elements making n-1 comparisons. Find the min of the remaining n-1 elements making n-2 comparisons. Total number of comparisons is 2n-3.

Divide and Conquer Small instance: n <= 2. Find the min and max element making at most one comparison.

Large Instance Min And Max n > 2. Divide the n elements into 2 groups A and B with floor(n/2) and ceil(n/2) elements, respectively. Find the min and max of each group recursively. Overall min is min{min(A),min(B)}. Overall max is max{max(A),max(B)}.

Min And Max Example Find the min and max of {3,5,6,2,4,9,3,1}. Large instance. A = {3,5,6,2} and B = {4,9,3,1}. min(A) = 2, min(B) = 1. max(A) = 6, max(B) = 9. min{min(A),min(B)} = 1. max{max(A),max(B)} = 9.

Time Complexity Let c(n) be the number of comparisons made when finding the min and max of n elements. c(0) = c(1) = 0. c(2) = 1. c(n) = c(floor(n/2)) + c(ceil(n/2)) + 2 when c > 2. To solve the recurrence, assume n is a power of 2 and use repeated substitution. c(n) = ceil(3n/2) – 2.

Interpretation Of Recursive Version The working of recursive divide-and-conquer algorithm can be described by a tree: recursion tree. The algorithm moves down the recursion tree dividing the large instances into smaller ones. Leaves represent small instances. The recursive algorithm moves back up the tree combining the results from the subtrees. The combining finds the min of the mins computed at leaves and the max of the leaf maxs.

Downward Pass Divides Into Smaller Instances

Upward Pass Combines Results From Subtrees {2,8}