L_14_Divide&Conquer

23
National University Of Computer & Emerging Sciences Divide & Conquer Design and Analysis of Algorithms (Lecture 14) Divide And Conquer Fall-29

Transcript of L_14_Divide&Conquer

Page 1: L_14_Divide&Conquer

National University Of Computer & Emerging Sciences

Divide & Conquer

Design and Analysis of Algorithms (Lecture 14)

Divide And Conquer

Fall-29

Page 2: L_14_Divide&Conquer

National University Of Computer & Emerging Sciences

Divide & Conquer

• General techniques that often yield effective algorithms for solving large classes of problems.

• Some of the more important techniques are (i) Greedy (ii) Dynamic Programming and (iii) Divide & Conquer

• Before using any technique, better ask about the quality of the solution obtained.

• Remember that for NP-Complete problems, these or any other known techniques will NOT yield efficient solutions.

Introduction

Page 3: L_14_Divide&Conquer

National University Of Computer & Emerging Sciences

Divide & Conquer

• Breaking up large problems into smaller units that are easier to handle

• Example: Cleaning the house, implementing a word processor

• Top-down methodology and object-oriented methodology are based on the principle of divide and conquer

Divide and Conquer

Page 4: L_14_Divide&Conquer

National University Of Computer & Emerging Sciences

Divide & Conquer

Divide and Conquer

Easy ProblemEasy Problem Easy ProblemEasy Problem

Hard ProblemHard ProblemEasy ProblemEasy Problem Easy ProblemEasy Problem

Hard ProblemHard Problem

Page 5: L_14_Divide&Conquer

National University Of Computer & Emerging Sciences

Divide & Conquer

Divide & Conquer• Divide & conquer paradigm involves 3

steps:– Divide the problem into sub-problems– Conquer the sub-problems– Combine the solutions to the sub-problems.

• This nature of divide & conquer algorithms automatically lends to recursion.

• Classic examples: – Merge sort : T(n) = 2 T(n/2) + O(n)– Binary search: T(n) = T(n/2) + 1

• Proof: usually by induction method.

P

P1 P2

S1 S2

S

Page 6: L_14_Divide&Conquer

National University Of Computer & Emerging Sciences

Divide & Conquer

Divide & Conquer (Constructing Tennis Tournament)

• Round robin tournament of n = 2k players.

• Each player must play every other player.

• Each player must play one match per day for n – 1 days.

• The tournament schedule is thus an n row by n-1 column table, whose entry is row i and column j is that player i must play with which player on jth day.

• Work by finding schedule for one half of players, keep on doing it till we get down to two players.

Page 7: L_14_Divide&Conquer

National University Of Computer & Emerging Sciences

Divide & Conquer

Divide & Conquer (Constructing Tennis Tournament)

• Divide the matrix into four parts

• The upper left corner comes from the smallest schedule, recursively.

• The bottom left corner is obtained by adding n/2 to each player code of the top left corner

• The top right corner is obtained by pitting the high order players against low order players, by cyclically permuting the orders.

• The bottom right corner is similarly obtained by pitting the low order players against high order players, by cyclically permuting the orders.

Page 8: L_14_Divide&Conquer

National University Of Computer & Emerging Sciences

Divide & Conquer

Divide & Conquer (Constructing Tennis Tournament)

Player

Will play player

Page 9: L_14_Divide&Conquer

National University Of Computer & Emerging Sciences

Divide & Conquer

Divide & Conquer (Constructing Tennis Tournament)

Page 10: L_14_Divide&Conquer

National University Of Computer & Emerging Sciences

Divide & Conquer

Example: Min-Max Problems

• Problem: Find the largest (heaviest) and smallest (lightest) elements in a set of n elements (e.g., gold nuggets, counterfeit coins)

• Simple Algorithm: Make n-1 comparisons to find largest, n-2 more comparisons to find smallest for a total of 2n-3 comparisons.

Page 11: L_14_Divide&Conquer

National University Of Computer & Emerging Sciences

Divide & Conquer

Min-Max Cont’d

• Divide-and-Conquer:– For n 2, make 1 comparison– For large n, divide set into two smaller sets and determine

largest/smallest element for each set– Compare largest/smallest from two subsets to determine

smallest/largest of combined sets– Do recursively

• Let n =8• How many comparisons?

Page 12: L_14_Divide&Conquer

National University Of Computer & Emerging Sciences

Divide & Conquer

Min-Max Cont’d

• Recursive solution: recurrence equation

1, n=2

• c(n) =

2* c(n/2) + 2, n =2k

• Solve using substitutionc(n) = (3n/2) - 2, n=2k

Page 13: L_14_Divide&Conquer

National University Of Computer & Emerging Sciences

Divide & Conquer

Misc• Divide-and-Conquer methodology naturally leads

to recursive algorithms• In general, implement algorithm as recursive

program– Few instances where non-recursive is better

• Rule of thumb: Balance sub-problems, i.e., try and make sub-problems of equal size– See merge sort

Page 14: L_14_Divide&Conquer

National University Of Computer & Emerging Sciences

Divide & Conquer

MULTIPLICATION OF TWO n-BIT NUMBERS• For this problem, the basic operations should be

bit operations.• Let X and Y be two n-bit numbers, the traditional

method requires O(n2) bit operations, say

Page 15: L_14_Divide&Conquer

National University Of Computer & Emerging Sciences

Divide & Conquer

MULTIPLICATION OF TWO n-BIT NUMBERS (cont.)

• Assume n=2m, we treat X and Y as two n-bit strings and partition them into two halves as X = A * B, Y= C * D, i.e., concatenation of A, B and C, D. As binary numbers, we can express

XY = (A2n/2 + B) (C2n/2 + D)

= AC2n + (AD + BC) 2n/2 + BD ............ (*)• The above computation of XY reduces to four multiplications

of (n/2)-bit numbers plus some additions and shifts (multiplications by powers of 2).

Page 16: L_14_Divide&Conquer

National University Of Computer & Emerging Sciences

Divide & Conquer

Two n-bits Number Analysis

• Let T(n) be the number of bit-operations required to multiply two n-bit numbers.

Page 17: L_14_Divide&Conquer

National University Of Computer & Emerging Sciences

Divide & Conquer

Two n-bits Number Analysis (cont.)

Page 18: L_14_Divide&Conquer

National University Of Computer & Emerging Sciences

Divide & Conquer

Two n-bits Number Analysis (cont.)

• Thus T(n) depends very much the number of subproblems and also the size of the subproblems.

• If the number of subproblems were 1, 3, 4, 8, then the algorithms would be of order n, nlog3, n2, n3 respectively.

Page 19: L_14_Divide&Conquer

National University Of Computer & Emerging Sciences

Divide & Conquer

A better Algorithm

• So, the multiplication of two n-bit numbers remains O(n2) if the number of half-size subproblems remains 4

• In order to reduce the time complexity, we attempt to reduce the number of subproblems (i.e., multiplications ) by a trick which uses more additions and shifts, specifically, by reducing one multiplication with many additions and shifts.

• This trick pays off asymptotically, i.e., when n is large.

Page 20: L_14_Divide&Conquer

National University Of Computer & Emerging Sciences

Divide & Conquer

A better Algorithm (cont.)U = (A + B)(C + D)V = ACW = BDZ = U - V - W = AD + BC

So, XY can be written as V2n+ Z2 n/2 + W (from Eq (*)).

• This approach requires only three multiplications of two (n/2)-bit numbers, plus some additions and shifts, to multiply two n-bit numbers.

XY= AC2n + (AD + BC) 2n/2 + BD ............ (*)

Page 21: L_14_Divide&Conquer

National University Of Computer & Emerging Sciences

Divide & Conquer

Analysis

• One can use the multiplication routine recursively to evaluate the products u, v, and w. The additions and shifts require O(n) time. Thus the time complexity of multiplying two n-bit numbers is bounded from above by

• Where k is a constant reflecting the additions and shifts. From the above discussion, the solution to the recurrence is then O(n log3) = O(n 1.59).

Page 22: L_14_Divide&Conquer

National University Of Computer & Emerging Sciences

Divide & Conquer

Example

This divide and conquer technique for multiplication of two n-bit number can also applied to the multiplication problem of two n-digit integers and n-degree polynomial.

Page 23: L_14_Divide&Conquer

National University Of Computer & Emerging Sciences

Divide & Conquer

Is it really better than O(n2)?

If this technique is so good, why don’t we teach it in school?

1. The technique is quite complex to understand. Most likely the students will never learn multiplication.

2. The constant ignored is very large i.e. 500. Useful for large multiplications.

3. Usually don’t multiply numbers 500 bit numbers by paper pencil technique in schools.