ICS%20241 - PowerPoint PPT Presentation

About This Presentation
Title:

ICS%20241

Description:

Each new sequence is one-half original (so b=2) Use two comparisons to find max & min of two sequences (so g(n) = 2) So T(n) = 2T( n/2 ) 2 ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 16
Provided by: williama3
Learn more at: http://www2.hawaii.edu
Category:
Tags: ics

less

Transcript and Presenter's Notes

Title: ICS%20241


1
ICS 241
  • Discrete Mathematics II
  • William Albritton, Information and Computer
    Sciences Department at University of Hawaii at
    Manoa
  • For use with Kenneth H. Rosens Discrete
    Mathematics Its Applications (5th Edition)
  • Based on slides originally created by
  • Dr. Michael P. Frank, Department of Computer
    Information Science Engineering at University
    of Florida

2
6.3 Divide Conquer R.R.s
  • Many types of problems are solvable by reducing a
    problem of size n into some number a of
    independent subproblems, each of size ??n/b?,
    where a?1 and bgt1.
  • The time complexity to solve such problems is
    given by a recurrence relation
  • T(n) aT(?n/b?) g(n)

3
Divide Conquer Example
  • Binary search
  • Reduce problem to 1 sub-problem (namely,
    searching half of the list) (thus, a1) of size
    ??n/2? (thus, b2).
  • Use two comparisons to implement reduction (g(n)
    2)
  • Which half of list
  • Whether any terms remain
  • So T(n) T(?n/2?)2

4
Divide Conquer Example
  • Find min max of a sequence
  • If n1, then a1 is max min
  • If ngt1, then split into two sequences (so a2)
  • Each new sequence is one-half original (so b2)
  • Use two comparisons to find max min of two
    sequences (so g(n) 2)
  • So T(n) 2T(?n/2?)2

5
Divide Conquer Example
  • Merge sort
  • Break list of length n into 2 sublists (a2),
  • Each of size ??n/2? (so b2),
  • Sort them, then merge, in g(n) T(n) time (fewer
    than n comparisons)
  • So T(n) 2T(?n/2?) n

6
Class Exercise
  • Section 6.3, Exercise 7.a.b.c. (p. 433)
  • Each pair of students should use only one sheet
    of paper while solving the class exercises

7
Fast Multiplication Example
  • The ordinary grade-school algorithm takes T(n2)
    steps to multiply two n-digit numbers.
  • Seems like too much work!
  • Lets find an asymptotically faster algorithm!
  • To find the product cd of two 2n-digit base-b
    numbers, c(c2n-1c2n-2c0)b and
    d(d2n-1d2n-2d0)b first, we break c and d in
    half cbnC1C0, dbnD1D0, and then...
    (see next slide)

8
Derivation of Fast Multiplication

(Multiply out polynomials)
(Factor last term)
9
Recurrence Rel. for Fast Mult.
  • Notice that the time complexity T(n) of the fast
    multiplication algorithm obeys the recurrence
  • T(2n)3T(n)?(n)
  • T(n)3T(n/2)Cn
  • So a3, b2.

Time to do the needed adds subtracts of
n-digit and 2n-digit numbers
10
Theorem 1
  • Consider increasing function f(n) that satisfies
    the recurrence relation
  • f(n) af(n/b) c, with a1, integer bgt1, real
    cgt0. Then
  • f(n) is ?(nlog b a) if a gt 1
  • f(n) is ?(log n) if a 1
  • When n bk, with k positive integer
  • f(n) C1 n log b a C2
  • C1 f(1) c/(a-1) C2 -c/(a-1)

11
Theorem 1 Example
  • Let f(n) 5f(n/2)3, f(1) 7, n2k. Find f(n)
    estimate f(n) if f is an increasing function
  • a5, b2, c3, nbk 2k
  • f(n) C1 n log b a C2
  • n log b a nlog 2 5
  • C1 f(1) c/(a-1) C2 -c/(a-1)
  • C1 7 3/4 31/4 C2 -3/4
  • f(n) (31/4) nlog 2 5 3/4
  • f(n) is ?(nlog b a) if a gt 1, so ?(nlog25)

12
Class Exercise
  • Section 6.3, Exercise 12 13 (p. 434)
  • Work together in pairs using one sheet of paper
    to solve the class exercises
  • Use Theorem 1 to solve these

13
The Master Theorem
  • Consider a function f(n) that, for all nbk for
    all k?Z,,satisfies the recurrence relation
  • f(n) af(n/b) cnd
  • with a1, integer bgt1, real cgt0, d0. Then

14
Master Theorem Example
  • Recall that complexity of fast multiply was
  • T(n) 3T(n/2) Cn
  • Thus, a3, b2, d1. So a gt bd, so case 3 of the
    master theorem applies, so
  • which is O(n1.58), so the new algorithm is
    strictly faster than ordinary T(n2) multiply!

15
Class Exercise
  • Exercise 21.a.b. (p. 434)
  • Work together in pairs using one sheet of paper
    to solve the class exercises
Write a Comment
User Comments (0)
About PowerShow.com