Unit-2 Divide and conquer 2016

Similar documents
Divide and Conquer. Algorithm Fall Semester

Chapter 4. Divide-and-Conquer. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Classic Data Structures Introduction UNIT I

Divide-and-Conquer. The most-well known algorithm design strategy: smaller instances. combining these solutions

CSC Design and Analysis of Algorithms

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer

Faster Sorting Methods

Quicksort. Repeat the process recursively for the left- and rightsub-blocks.

Divide-and-Conquer. Dr. Yingwu Zhu

DIVIDE & CONQUER. Problem of size n. Solution to sub problem 1

CS 171: Introduction to Computer Science II. Quicksort

17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer

Lecture 8: Mergesort / Quicksort Steven Skiena

COMP Data Structures

Better sorting algorithms (Weiss chapter )

S O R T I N G Sorting a list of elements implemented as an array. In all algorithms of this handout the sorting of elements is in ascending order

Sorting Algorithms. + Analysis of the Sorting Algorithms

BM267 - Introduction to Data Structures

QuickSort

We can use a max-heap to sort data.

Problem. Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all. 1 i j n.

Sorting and Searching

ITEC2620 Introduction to Data Structures

Cpt S 122 Data Structures. Sorting

Sorting. Task Description. Selection Sort. Should we worry about speed?

Sorting. Order in the court! sorting 1

Quick Sort. Mithila Bongu. December 13, 2013

Sorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place?

Sorting. CSE 143 Java. Insert for a Sorted List. Insertion Sort. Insertion Sort As A Card Game Operation. CSE143 Au

SORTING, SETS, AND SELECTION

SAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6. Sorting Algorithms

Sorting. CSC 143 Java. Insert for a Sorted List. Insertion Sort. Insertion Sort As A Card Game Operation CSC Picture

Lecture #2. 1 Overview. 2 Worst-Case Analysis vs. Average Case Analysis. 3 Divide-and-Conquer Design Paradigm. 4 Quicksort. 4.

Sorting. Order in the court! sorting 1

Divide and Conquer CISC4080, Computer Algorithms CIS, Fordham Univ. Instructor: X. Zhang

Divide and Conquer 4-0

Divide and Conquer CISC4080, Computer Algorithms CIS, Fordham Univ. Acknowledgement. Outline

Arrays, Vectors Searching, Sorting

Mergesort again. 1. Split the list into two equal parts

DATA STRUCTURES AND ALGORITHMS

Key question: how do we pick a good pivot (and what makes a good pivot in the first place)?

Lecture 19 Sorting Goodrich, Tamassia

Sorting. Lecture10: Sorting II. Sorting Algorithms. Performance of Sorting Algorithms

4. Sorting and Order-Statistics

Bubble sort starts with very first two elements, comparing them to check which one is greater.

Sorting and Selection

Lecture Notes on Quicksort

Lecture Notes on Quicksort

Sorting Goodrich, Tamassia Sorting 1

Back to Sorting More efficient sorting algorithms

Recursive Algorithms II

Component 02. Algorithms and programming. Sorting Algorithms and Searching Algorithms. Matthew Robinson

Sorting. Two types of sort internal - all done in memory external - secondary storage may be used

DIVIDE AND CONQUER ALGORITHMS ANALYSIS WITH RECURRENCE EQUATIONS

Overview of Sorting Algorithms

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Divide and Conquer

Data structures. More sorting. Dr. Alex Gerdes DIT961 - VT 2018

Homework Assignment #3. 1 (5 pts) Demonstrate how mergesort works when sorting the following list of numbers:

IS 709/809: Computational Methods in IS Research. Algorithm Analysis (Sorting)

Quicksort (Weiss chapter 8.6)

Programming II (CS300)

Recursion. Chapter 7. Copyright 2012 by Pearson Education, Inc. All rights reserved

Data Structure Lecture#17: Internal Sorting 2 (Chapter 7) U Kang Seoul National University

CIS 121 Data Structures and Algorithms with Java Spring Code Snippets and Recurrences Monday, January 29/Tuesday, January 30

Randomized Algorithms: Selection

Lecture 7 Quicksort : Principles of Imperative Computation (Spring 2018) Frank Pfenning

L14 Quicksort and Performance Optimization

2/26/2016. Divide and Conquer. Chapter 6. The Divide and Conquer Paradigm

Divide and Conquer Algorithms: Advanced Sorting. Prichard Ch. 10.2: Advanced Sorting Algorithms

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Chapter 3:- Divide and Conquer. Compiled By:- Sanjay Patel Assistant Professor, SVBIT.

Chapter 2 Divid d e-an -Conquer 1

CSC Design and Analysis of Algorithms. Lecture 6. Divide and Conquer Algorithm Design Technique. Divide-and-Conquer

Analysis of Algorithms. Unit 4 - Analysis of well known Algorithms

Algorithmic Analysis and Sorting, Part Two

Divide and Conquer Sorting Algorithms and Noncomparison-based

CSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting. Aaron Bauer Winter 2014

CSE 373: Data Structures and Algorithms

10/21/ Linear Search The linearsearch Algorithm Binary Search The binarysearch Algorithm

! Search: find a given item in a list, return the. ! Sort: rearrange the items in a list into some. ! list could be: array, linked list, string, etc.

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, Merge Sort & Quick Sort

COMP2012H Spring 2014 Dekai Wu. Sorting. (more on sorting algorithms: mergesort, quicksort, heapsort)

e-pg PATHSHALA- Computer Science Design and Analysis of Algorithms Module 10

COMP 352 FALL Tutorial 10

Sorting Algorithms. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

SORTING AND SELECTION

CPS 616 TRANSFORM-AND-CONQUER 7-1

Quicksort. The divide-and-conquer strategy is used in quicksort. Below the recursion step is described:

Algorithms and Data Structures (INF1) Lecture 7/15 Hua Lu

University of the Western Cape Department of Computer Science

Divide and Conquer. Algorithm D-and-C(n: input size)

Quick Sort. CSE Data Structures May 15, 2002

Searching and Sorting

Divide and Conquer Algorithms: Advanced Sorting

Quick-Sort. Quick-Sort 1

Algorithms in Systems Engineering ISE 172. Lecture 12. Dr. Ted Ralphs

CSC 273 Data Structures

SORTING. Comparison of Quadratic Sorts

Quick-Sort. Quick-sort is a randomized sorting algorithm based on the divide-and-conquer paradigm:

9/10/12. Outline. Part 5. Computational Complexity (2) Examples. (revisit) Properties of Growth-rate functions(1/3)

Transcription:

2 Divide and conquer Overview, Structure of divide-and-conquer algorithms, binary search, quick sort, Strassen multiplication. 13% 05 Divide-and- conquer The Divide and Conquer Paradigm, is a method of designing algorithms, a general approach to construct an efficient solution to a problem. The Divide and Conquer Paradigm is an algorithm design paradigm which uses this simple process: It Divides the problem into smaller sub-parts until these sub-parts become simple enough to be solved, and then the sub parts are solved recursively, and then the solutions to these sub-parts can be combined to give a solution to the original problem. In other words, this paradigm follows three basic steps; Step 1: Break down the problem into smaller sub-problems, Step 2: Solve these separate sub-problems, and Step 3: Combine the solutions of the sub-problems. Advantages: Solving difficult problems : Divide and conquer is a powerful tool for solving conceptually difficult problems: all it requires is a way of breaking the problem into sub-problems, of solving the trivial cases and of combining sub-problems to the original problem. Similarly, decrease and conquer only requires reducing the problem to a single smaller problem, such as the classic Tower of Hanoi puzzle, which reduces moving a tower of height n to moving a tower of height n 1. Algorithm efficiency : The divide-and-conquer paradigm often helps in the discovery of efficient algorithms. It was the key, for example, to Karatsuba's fast multiplication method, the quicksort and mergesort algorithms, the Strassen algorithm for matrix multiplication, and fast Fourier transforms. Parallelism : Divide and conquer algorithms are naturally adapted for execution in multiprocessor machines, especially shared-memory systems where the communication of data between processors does not need to be planned in advance, because distinct sub-problems can be executed on different processors. D & C mainly have three parts Divide : divide the problem into smaller sub problems. Conquer : the sub problem solving them recursively. If they are small enough solve the sub problem as base case. Combine : merge the solution of sub problem to find solution of actual problem. Analysis and Design of Algorithm Page 1

If we expand out two more recursive steps, it looks like this: Because divide-and-conquer creates at least two subproblems, a divide-and-conquer algorithm makes multiple recursive calls. Analysis and Design of Algorithm Page 2

Applications: Merge Sort Quick Sort Binary Search Strassen's Matrix Multiplication Closest pair (points) Binary Search: Binary search is a fast search algorithm with run-time complexity of Ο(log n). This search algorithm works on the principle of divide and conquer. For this algorithm to work properly the data collection should be in sorted form. Binary search search a particular item by comparing the middle most item of the collection. If match occurs then index of item is returned. If middle item is greater than item then item is searched in sub-array to the right of the middle item other wise item is search in sub-array to the left of the middle item. This process continues on sub-array as well until the size of subarray reduces to zero. How binary search works? For a binary search to work, it is mandatory for the target array to be sorted. Here is the process of binary search with an pictorial example. The below given is our sorted array and assume that we need to search location of value 31 using binary search. First, we shall determine the half of the array by using this formula mid = low + (high - low) / 2 Here it is, 0 + (9-0 ) / 2 = 4 (integer value of 4.5). So 4 is the mid of array. Now we compare the value stored at location 4, with the value being searched i.e. 31. We find that value at location 4 is 27, which is not a match. Because value is greater than 27 and Analysis and Design of Algorithm Page 3

we have a sorted array so we also know that target value must be in upper portion of the array. We change our low to mid + 1 and find the new mid value again. low = mid + 1 mid = low + (high - low) / 2 Our new mid is 7 now. We compare the value stored at location 7 with our target value 31. The value stored at location 7 is not a match, rather it is less that what we are looking for. So the value must be in lower part from this location. So we calculate the mid again. This time it is 5. We compare the value stored ad location 5 with our target value. We find that it is a match. We conclude that the target value 31 is stored at location 5. Binary search halves the searchable items and thus reduces the count of comparisons to be made to very less numbers. Pseudo code The pseudo code of binary search algorithm should look like this Analysis and Design of Algorithm Page 4

Procedure binary_search A sorted array n size of array x value ot be searched Set lowerbound = 1 Set upperbound = n while x not found if upperbound < lowerbound EXIT: x does not exists. set midpoint = lowerbound + ( upperbound - lowerbound ) / 2 if A[midPoint] < x set lowerbound = midpoint + 1 if A[midPoint] > x set upperbound = midpoint - 1 if A[midPoint] = x EXIT: x found at location midpoint end while end procedure Time Complexity is Ο(log n). Analysis and Design of Algorithm Page 5

Quick Sort: Quick sort is a highly efficient sorting algorithm and is based on partitioning of array of data into smaller arrays. A large array is partitioned into two arrays one of which holds values smaller than specified value say pivot based on which the partition is made and another array holds values greater than pivot value. The quick sort partitions an array and then calls itself recursively twice to sort the resulting two subarray. This algorithm is quite efficient for large sized data sets as its average and worst case complexity are of O(nlogn) where n are no. of items. The three steps of Quicksort are as follows: Divide: Rearrange the elements and split the array into two subarrays and an element in between such that so that each element in the left subarray is less than or equal the middle element and each element in the right subarray is greater than the middle element. Conquer: Recursively sort the two subarrays. Combine: None 1. Features o Divide-and-conquer recursive algorithm o One of the fastest sorting algorithms o Average running time O(NlogN) o Worst-case running time O(N 2 ) 2. Basic idea 1. Pick one element in the array, which will be the pivot. 2. Make one pass through the array, called a partition step, re-arranging the entries so that: the pivot is in its proper place. entries smaller than the pivot are to the left of the pivot. entries larger than the pivot are to its right. 3. Recursively apply quicksort to the part of the array that is to the left of the pivot, and to the right part of the array. Here we don't have the merge step, at the end all the elements are in the proper order. Algorithm STEP 1. Choosing the pivot Analysis and Design of Algorithm Page 6

Choosing the pivot is an essential step. Depending on the pivot the algorithm may run very fast, or in quadric time.: Example: 1. Some fixed element: e.g. the first, the last, the one in the middle This is a bad choice - the pivot may turn to be the smallest or the largest element, then one of the partitions will be empty. 2. Randomly chosen (by random generator ) - still a bad choice. 3. The median of the array (if the array has N numbers, the median is the [N/2] largest number. This is difficult to compute - increases the complexity. 4. The median-of-three choice: take the first, the last and the middle element. Choose the median of these three elements. 8, 3, 25, 6, 10, 17, 1, 2, 18, 5 The first element is 8, the middle - 10, the last - 5. The median of [8, 10, 5] is 8 STEP 2. Partitioning Partitioning is illustrated on the above example. 1. The first action is to get the pivot out of the way - swap it with the last element 5, 3, 25, 6, 10, 17, 1, 2, 18, 8 2. We want larger elements to go to the right and smaller elements to go to the left. Two "fingers" are used to scan the elements from left to right and from right to left: [5, 3, 25, 6, 10, 17, 1, 2, 18, 8] ^ ^ i j o While i is to the left of j, we move i right, skipping all the elements less than the pivot. If an element is found greater then the pivot, i stops Analysis and Design of Algorithm Page 7

o o o While j is to the right of i, we move j left, skipping all the elements greater than the pivot. If an element is found less then the pivot, j stops When both i and j have stopped, the elements are swapped. When i and j have crossed, no swap is performed, scanning stops, and the element pointed to by i is swapped with the pivot. In the example the first swapping will be between 25 and 2, the second between 10 and 1. 3. Restore the pivot. After restoring the pivot we obtain the following partitioning into three groups: [5, 3, 2, 6, 1] [ 8 ] [10, 25, 18, 17] STEP 3. Recursively quicksort the left and the right parts Partition in Quicksort The pivot value divides the list in to two parts. And recursively we find pivot for each sublists until all lists contains only one element. QuickSort Pivot Algorithm Based on our understanding of partitioning in quicksort, we should now try to write an algorithm for it here. Step 1 Choose the highest index value as pivot Step 2 Take two variables to point left and right of the list excluding pivot Step 3 left points to the low index Step 4 right points to the high Step 5 while value at left is less than pivot move right Step 6 while value at right is greater than pivot move left Step 7 if both step 5 and step 6 does not match swap left and right Step 8 if left right, the point where they met is new pivot QuickSort Pivot Pseudocode The pseudocode for the above algorithm can be derived as function partitionfunc(left, right, pivot) leftpointer = left -1 Analysis and Design of Algorithm Page 8

rightpointer = right while True do while A[++leftPointer] < pivot do //do-nothing end while while rightpointer > 0 && A[--rightPointer] > pivot do //do-nothing end while if leftpointer >= rightpointer break else swap leftpointer,rightpointer end if end while swap leftpointer,right return leftpointer end function QuickSort Algorithm Using pivot algorithm recursively we end-up with smaller possible partitions. Each partition then processed for quick sort. We define recursive algorithm for quicksort as below Step 1 Make the right-most index value pivot Step 2 partition the array using pivot value Step 3 quicksort left partition recursively Step 4 quicksort right partition recursively QuickSort Pseudocode To get more into it, let see the pseudocode for quick sort algorithm procedure quicksort(left, right) Analysis and Design of Algorithm Page 9

if right-left <= 0 return else pivot = A[right] partition = partitionfunc(left, right, pivot) quicksort(left,partition-1) quicksort(partition+1,right) end if end procedure Example: Analysis and Design of Algorithm Page 10

Analysis and Design of Algorithm Page 11

Analysis and Design of Algorithm Page 12

Analysis and Design of Algorithm Page 13

Analysis and Design of Algorithm Page 14

Analysis and Design of Algorithm Page 15

Analysis and Design of Algorithm Page 16

Analysis and Design of Algorithm Page 17

Analysis and Design of Algorithm Page 18

Strassen multiplication: Naive Method Following is a simple way to multiply two matrices. void multiply(int A[][N], int B[][N], int C[][N]) { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { C[i][j] = 0; for (int k = 0; k < N; k++) { C[i][j] += A[i][k]*B[k][j]; } } } } Time Complexity of above method is O(N 3 ). Let A, B be two square matrices. Calculate the matrix product C as Analysis and Design of Algorithm Page 19

If the matrices A, B are not of type 2 n 2 n we fill the missing rows and columns with zeros. We partition A, B and C into equally sized block matrices with then C i, j N k 1 a i, k b ThusT ( N) k, j N N N i1 j1 k 1 c cn 3 O( N 3 ) Strassen showed that 2x2 matrix multiplication can be accomplished in 7 multiplication and 18 additions or subtractions..(2 log 2 7 =2 2.807 ) This reduction can be done by Divide and Conquer Approach. A B = R Divide matrices into sub-matrices: A 0, A 1, A 2 etc Use blocked matrix multiply equations Recursively multiply sub-matrices Analysis and Design of Algorithm Page 20

Strassan Algorithm: C 11 = P 1 + P 4 - P 5 + P 7 = (A 11 + A 22 )(B 11 +B 22 ) + A 22 * (B 21 - B 11 ) - (A 11 + A 12 ) * B 22 + (A 12 - A 22 ) * (B 21 + B 22 ) = A 11 B 11 + A 11 B 22 + A 22 B 11 + A 22 B 22 + A 22 B 21 A 22 B 11 - A 11 B 22 -A 12 B 22 + A 12 B 21 + A 12 B 22 A 22 B 21 A 22 B 22 = A 11 B 11 + A 12 B 21 Complexity: Where N is the size of matrix. Analysis and Design of Algorithm Page 21