DIVIDE AND CONQUER. We know that some of the problems can be straight-away solved by Brute- Force technique.

Similar documents
UNIT-2. Problem of size n. Sub-problem 1 size n/2. Sub-problem 2 size n/2. Solution to the original problem

g(n) time to computer answer directly from small inputs. f(n) time for dividing P and combining solution to sub problems

Small And Large Instance. Divide And Conquer. Solving A Large Instance. Solving A Small Instance. Find The Min Of A Large List.

CS6402 Design and Analysis of Algorithm. 2 Marks Question & Answer UNIT I INTRODUCTION

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

Divide and Conquer 4-0

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

Divide-and-Conquer. Divide-and conquer is a general algorithm design paradigm:

Divide & Conquer. 2. Conquer the sub-problems by solving them recursively. 1. Divide the problem into number of sub-problems

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

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

Lecture 9: Sorting Algorithms

COSC242 Lecture 7 Mergesort and Quicksort

The divide-and-conquer paradigm involves three steps at each level of the recursion: Divide the problem into a number of subproblems.

Algorithm Analysis. Spring Semester 2007 Programming and Data Structure 1

Lecture 2: Getting Started

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

1. Answer the following questions about each code fragment below. [8 points]

CS 360 Exam 1 Fall 2014 Solution. 1. Answer the following questions about each code fragment below. [8 points]

1. Answer the following questions about each code fragment below. [8 points]

Analyzing Complexity of Lists

Divide and Conquer. Design and Analysis of Algorithms Andrei Bulatov

Divide-and-Conquer. Dr. Yingwu Zhu

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

Parallel Algorithms for (PRAM) Computers & Some Parallel Algorithms. Reference : Horowitz, Sahni and Rajasekaran, Computer Algorithms

Advanced Algorithms and Data Structures

CS6402 Design and Analysis of Algorithm. 16 marks Question & Answer

Computer Programming

Divide & Conquer Design Technique

The divide and conquer strategy has three basic parts. For a given problem of size n,

CSCI-1200 Data Structures Fall 2017 Lecture 13 Problem Solving Techniques

Lecture 1. Introduction / Insertion Sort / Merge Sort

Sorting. Bubble Sort. Pseudo Code for Bubble Sorting: Sorting is ordering a list of elements.

Data Structures and Algorithms Week 4

Divide and Conquer. Algorithm Fall Semester

CS473 - Algorithms I

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

CSC Design and Analysis of Algorithms

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.

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

CSE 21 Spring 2016 Homework 4 Answer Key

Sorting. Bringing Order to the World

EECS 2011M: Fundamentals of Data Structures

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

Lecture 6 Sorting and Searching

Chapter 2 Divid d e-an -Conquer 1

4. Sorting and Order-Statistics

Data Structures and Algorithms Chapter 4

Divide-and-Conquer CSE 680

Attendance (2) Performance (3) Oral (5) Total (10) Dated Sign of Subject Teacher

CS Divide and Conquer

CS Divide and Conquer

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Internal Sort by Comparison II

QuickSort and Others. Data Structures and Algorithms Andrei Bulatov

Algorithms - Ch2 Sorting

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

Internal Sort by Comparison II

Lecture 19 Sorting Goodrich, Tamassia

A 0 A 1... A i 1 A i,..., A min,..., A n 1 in their final positions the last n i elements After n 1 passes, the list is sorted.

Internal Sort by Comparison II

Data Structures and Algorithms CSE 465

Randomized Algorithms, Quicksort and Randomized Selection

Chapter 1 Divide and Conquer Algorithm Theory WS 2014/15 Fabian Kuhn

Lecture 5: Sorting Part A

MergeSort. Algorithm : Design & Analysis [5]

Design and Analysis of Algorithms

CHAPTER 7 Iris Hui-Ru Jiang Fall 2008

O(n): printing a list of n items to the screen, looking at each item once.

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

We can use a max-heap to sort data.

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

Searching in General

Chapter 1 Divide and Conquer Algorithm Theory WS 2013/14 Fabian Kuhn

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

Test 1 Review Questions with Solutions

Complexity and Advanced Algorithms Monsoon Parallel Algorithms Lecture 2

Introduction to Algorithms

Module 2: Classical Algorithm Design Techniques

Algorithms. Algorithms 1.4 ANALYSIS OF ALGORITHMS

Jana Kosecka. Linear Time Sorting, Median, Order Statistics. Many slides here are based on E. Demaine, D. Luebke slides

Chapter 1 Divide and Conquer Algorithm Theory WS 2015/16 Fabian Kuhn

7. Sorting I. 7.1 Simple Sorting. Problem. Algorithm: IsSorted(A) 1 i j n. Simple Sorting

Searching Elements in an Array: Linear and Binary Search. Spring Semester 2007 Programming and Data Structure 1

Quick Sort. CSE Data Structures May 15, 2002

Introduction to the Analysis of Algorithms. Algorithm

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

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

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

DATA STRUCTURES AND ALGORITHMS

Better sorting algorithms (Weiss chapter )

Lecture 6: Divide-and-Conquer

CPSC 311 Lecture Notes. Sorting and Order Statistics (Chapters 6-9)

CS 360 Exam 1 Fall 2014 Name. 1. Answer the following questions about each code fragment below. [8 points]

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

Sorting & Growth of Functions

Merge Sort. Algorithm Analysis. November 15, 2017 Hassan Khosravi / Geoffrey Tien 1

LECTURE NOTES OF ALGORITHMS: DESIGN TECHNIQUES AND ANALYSIS

Merge Sort. Run time typically depends on: Insertion sort can be faster than merge sort. Fast, able to handle any data

Data Structures and Algorithms. Roberto Sebastiani

Transcription:

DIVIDE AND CONQUER We know that some of the problems can be straight-away solved by Brute- Force technique. But, in many cases, Brute-force fails. That is, some of the problems can not be solved using Brute force method. Here we will study one more algorithm design technique, Divide-and- Conquer. The divide and conquer strategy suggests to divide the given problem of size n into k distinct subproblems (1<k<=n). These subproblems must be solved and then a method must be found to combine subsolutions to get the solution of whole problem. If the subproblem size is relatively large, then, divide and conquer strategy may be re-applied on that. Usually, in divide and conquer strategy, we write a control abstraction that mirrors the way an algorithm will look. Control Abstraction is a procedure whose flow of control is clear but whose primary operations are specified by other procedures whose precise meanings are left undefined. Consider the following algorithm DAndC, which is invoked for a problem P to be solved. Small(P) is a Boolean-valued function that determines whether the input size is small enough that the answer can be computed without spitting. 1

Algorithm DAndC(P) if Small(P) then return S(P); else divide P into smaller instances P1, P2,, Pk, k>=1; Apply DAndC to each of these subproblems; return Combine(DAndC(P1), DAndC(P2),, DAndC(Pk)); If the size of P is n and the sizes of k subproblems are n1, n2,,nk, then the computing time of DAndC is described by the recurrence relation: g( n), if n is small T ( n) T ( n1) T ( n2)... T ( nk) f ( n), otherwise The complexity of many divide and conquer algorithms is given by the recurrences of the form T(n) = T(1), if n=1 a. T(n/b) + f(n), if n>1 2

Merge Sort: Merge sort is a best example of DAC technique This sorting technique divides a given array A[0 n-1] by dividing it into two parts, viz. A[0 n/2-1] and A[n/2 n-1], Then each of the problems are sorted recursively and finally, they are merged. Merging of two sorted arrays is done as follows Compare first elements of both arrays. Put the smaller element into the resulting array. Now compare remaining element with the 2 nd element of other array. Store the smaller into resulting array. Continue the above procedure till one of the arrays get exhausted. Copy all the elements of other array into resulting array. ALGORITHM MergeSort(low, high) //a[low:high] is an array to be sorted //Small(P) is true if there is only one element to sort. if (low<high) then //if there are more than one element //Divide P into subproblems mid=(low+high)/2; MergeSort(low, mid); //solve subproblem MergeSort(mid+1, high); //solve subproblem Merge(low, mid, high) //combine the solutions 3

ALGORITHM Merge(low, mid, high) //a[low : high] is an array, and two sorted subsets are a[low : mid] and // a[mid+1 : high]. The goal is to combine these two sets into a single // set. b[ ] is an auxiliary array. h:=low, i:=low, j:=mid+1; while(h<=mid) and (j<=high) do if(a[h]<=a[j]) then b[i] :=a[h]; h :=h+1; else b[i]:=a[j]; j := j+1; if (h>mid) then for k := j to high do b[i] :=a[k]; i :=i+1; else for k := h to mid do b[i] :=a[k]; i :=i+1; for k:= low to high do a[k] :=b[k]; Quick Sort Here, the given array is divided into two sub-arrays such that the elements at the left-side of some key element are less than the key element and the elements at the right-side of the key element are greater than the key element. The dividing procedure is done with the help of two index variables and one key element as explained below 1. Usually the first element of the array is treated as key. The position of the second element is taken as the first index variable left and the position of the last element will be the index variable right. 2. Now the index variable left is incremented by one till the value stored at the position left is greater than the key. 3. Similarly right is decremented by one till the value stored at the position right is smaller than the key. 4

4. Now, these two elements are interchanged. Again from the current position, left and right are incremented and decremented respectively and exchanges are made appropriately, if required. 5. This process is continued till the index variables either meet or crossover. Now, exchange key with the element at the position right. 6. Now, the whole array is divided into two parts such that one part is containing the elements less than the key element and the other part is containing the elements greater than the key. And, the position of key is fixed now. 7. The above procedure (from step i to step vi) is applied on both the sub-arrays. After some iteration we will end-up with subarrays containing single element. By that time, the array will be stored. ALGORITHM QuickSort (p, q) //Sorts the elements a[p],, a[q] which is inside the array a[1: n]. if(p<q) then j :=Partition(a, p, q+1); QuickSort(p, j-1); QuickSort(j+1, q); 5

ALGORITHM Partition(a, m, p) v :=a[m]; i:=m; j:=p; repeat repeat i :=i+1; until (a[i]>=v); repeat j :=j-1; until (a[ j]<=v); Algorithm Interchange(a, i, j) p:=a[i]; a[i] := a[ j]; a[ j] :=p; if (i<j) then until (i>=j); a[m] := a[j]; a[j] :=v; return j; Interchange(a, i, j); BINARY SEARCH ALGORITHM BinSrch(a, i, l, x) //Given an array a[i:l] of elements in nondecreasing order, // 1<=i<=l, determine whether x is present. If so, return //such that x=a[j], else return 0. if(l==i) then //If Small(P) if (x==a[i]) then return i; else return 0; else mid:= (i+l)/2; if(x==a[mid] then return mid; else if (x<a[mid]) then return BinSrch(a, i, mid-1, x); else return BinSrch(a, mid+1, l x); 6

Defective Chess Board Problem A chessboard is an n x n grid, where n =2 k 1x1 2x2 4x4 8x8 A defective chessboard is a chessboard that has one unavailable (defective) position. 1x1 2x2 4x4 8x8 7

The problem is to tile (cover) all nondefective cells using 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. 1x1 2x2 4x4 8x8 8

Divide into four smaller chessboards. 4 x 4 One of these is a defective 4 x 4 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. 9

In general, a 2 k x 2 k defective chessboard can be divided as 2 k-1 x 2 k-1 2 k-1 x 2 k-1 2 k-1 x 2 k-1 2 k-1 x 2 k-1 Let n = 2 k. Analysis Let t(k) be the time taken to tile a 2 k x 2 k defective chessboard. Then, t( k) 0, 4t( k 1) c, if n 0 otherwise Here, c is constant representing time spent on finding the appropriate position for a triomino and to rotate the triomino for a required shape. 10

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) + 4 2 c + 4c + c = = 4 k t(0) + 4 k-1 c + 4 k-2 c +... + 4 2 c + 4c + c = 4 k-1 c + 4 k-2 c +... + 4 2 c + 4c + c = Θ(4 k ) = Θ(number of triominoes placed) 11