Docsity

Divide and Conquer Introduction, Summaries of Data Structures and Algorithms

Partial preview of the text

Download Divide and Conquer Introduction and more Summaries Data Structures and Algorithms in PDF only on Docsity!

Computer Algorithms

2026 Spring

Jhih-Ciang Wu

1

Chapter 4

Divide-and-Conquer

2

Analyzing divide-and-conquer algorithms

  • Use a^ recurrence^ to characterize the running time of a divide-and-conquer algorithm.
  • Solving the recurrence gives us the asymptotic running time.
  • A recurrence is a function is defined in terms of
    • one or more base cases, and
    • itself, with smaller arguments.
  • A recurrence could have 0, 1, or more functions that satisfy it.^ Well defined^ if at least 1 function satisfies; otherwise, ill defined. 4

Algorithmic recurrences

  • A recurrence^ is^ algorithmic^ if for every sufficiently large^ threshold^ constant :

For all ,

For all , every path of recursion terminates in a defined base case within a finite number of recursive invocations. T ( n ) n 0

n < n 0 T ( n ) = Θ( 1 ) n ≥ n 0 5 Can consider the running time constant for small problem sizes. The recursive algorithm terminates.

Methods for solving recurrences

  • Substitution method: Guess the solution, then use induction to prove that it’s correct.
  • Recursion-tree method: Draw out a recursion tree, determine the costs at each level, and sum them up. Useful for coming up with a guess for the substitution method.
  • Master method: A cookbook method for recurrences of the form , where and are constants, subject to certain conditions. Requires memorizing three cases, but applies to many divide-and- conquer algorithms. T ( n ) = aT ( n / b ) + f ( n ) a > 0 b > 1 7

Multiplying square matrices

  • Input: three (square) matrices, , ,.
  • Result: the matrix product^ is added into^ , so that for.
  • If only the product^ is needed, then zero out all entries of^ beforehand. n × n A = ( a ij ) B = ( b ij ) C = ( c ij

A ⋅ B C

c ij = c ij

n ∑ k = 1 a ik ⋅ b kj i , j = 1,2,..., n A ⋅ B C 8

Simple divide-and-conquer algorithm

  • For simplicity, assume that^ is initialized to 0, so computing^.
  • If^ , partition each of^ ,^ ,^ into four^ matrices. , ,

C C = A ⋅ B

n > 1 A B C n / 2 × n / 2 A = (

A

11

A

12 A 21

A

22

B =

B

11

B

12 B 21

B

22

C =

C

11

C

12 C 21

C

22

10

Simple divide-and-conquer algorithm

  • Rewrite^ as giving the four equations Use these equations to get a divide-and-conquer algorithm. C = A ⋅ B ( C 11 C 12 C 21 C 22 ) = ( A 11 A 12 A 21 A 22 ) ⋅ ( B 11 B 12 B 21 B 22 ) C 11 = A 11 ⋅ B 11
  • A 12 ⋅ B 21 C 12 = A 11 ⋅ B 12
  • A 12 ⋅ B 22 C 21 = A 21 ⋅ B 11
  • A 22 ⋅ B 21 C 22 = A 21 ⋅ B 12
  • A 22 ⋅ B 22 11

Analysis

  • Let^ be the time to multiply two^ matrices.
  • Base case:^. Perform one scalar multiplication ->
  • Recursive case:^.
    • Conquering makes 8 recursive calls, each multiplying^ matrices ->
    • No combine step, because^ is updated in place.
  • Recurrence is^.
  • Can use master method to show that it has solution^.
  • Asymptotically, no better than the obvious method. T ( n ) n × n n = 1 Θ( 1 ) n > 1 n / 2 × n / 2 8 T ( n / 2 ) C T ( n ) = 8 T ( n / 2 ) + Θ( 1 ) T ( n ) = Θ( n 3 ) 13

Revisit merge sort

  • Divide : Just compute^ as the average of^ and^ ->
  • Conquer : Recursively solve 2 subproblems, each of size^ ->
  • Combine : MERGE on an^ -element subarray takes^ time.
  • Recurrence for merge sort running time is
  • Can use master method to show that it has solution^. q p r Θ( 1 ) n / 2 2 T ( n / 2 ) n Θ( n ) T ( n ) = 2 T ( n / 2 ) + Θ( n ) T ( n ) = Θ( n lg n ) 14

Strassen’s algorithm

Idea

  • Make the recursion tree less bushy. Perform only 7 recursive multiplications of matrices, rather than
  • Will cost several additions/subtractions of matrices. n / 2 × n / 2 n / 2 × n / 2 16 Volker Strassen

Example of reducing multiplications

  • Compute^ will use 2 multiplications and one subtraction.
  • We know that^.
  • At the expense of one extra addition, can get by with only 1 multiplication.
  • Not a big deal if^ are scalars, but can make a difference if they are matrices. x 2 − y 2 x 2 − y 2 = ( x + y )( x − y ) x , y 17

Strassen’s algorithm

Analysis

  • Recurrence will be^.
  • By the master method, solution is^.
  • Since^ , the running time is^ , beating the^ -time methods. T ( n ) = 7 T ( n / 2 ) + Θ( n 2 ) T ( n ) = Θ( n lg ) lg 7 < 2.81 O ( n

) O ( n 3 ) 19

Strassen’s algorithm

  • Create the 10 matrices S 1 = B 12 − B 22 S 2 = A 11 + A 12 S 3 = A 21 + A 22 S 4 = B 21 − B 11 S 5 = A 11 + A 22 S 6 = B 11 + B 22 S 7 = A 12 − A 22 S 8 = B 21 + B 22 S 9 = A 11 − A 21 S 10 = B 11 + B 12 20
  • Compute the 7 matrices P 1 = A 11 ⋅ S 1 = A 11 ⋅ B 12 − A 11 ⋅ B 22 P 2 = S 2 ⋅ B 22 = A 11 ⋅ B 22
  • A 12 ⋅ B 22 P 3 = S 3 ⋅ B 11 = A 21 ⋅ B 11
  • A 22 ⋅ B 11 P 4 = A 22 ⋅ S 4 = A 22 ⋅ B 21 − A 22 ⋅ B 11 P 5 = S 5 ⋅ S 6 P 6 = S 7 ⋅ S 8 P 7 = S 9 ⋅ S 10
  • Add and subtract the to construct submatrices of P i C C 11 = P 5
  • P 4 − P 2
  • P 6 C 12 = P 1
  • P 2 C 21 = P 3
  • P 4 C 22 = P 5
  • P 1 − P 3 − P 7