Order of growth, modular exponentiation, hanoi towers - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

Order of growth, modular exponentiation, hanoi towers

Description:

Towers of Hanoi. Three posts, and a set of disks of ... for towers of Hanoi ... Hanoi Towers. Say we want to solve the problem for 400 pegs. Say we can move a ... – PowerPoint PPT presentation

Number of Views:186
Avg rating:3.0/5.0
Slides: 28
Provided by: csBg
Category:

less

Transcript and Presenter's Notes

Title: Order of growth, modular exponentiation, hanoi towers


1
Lecture 4
  • Order of growth, modular exponentiation, hanoi
    towers

2
From last lecture
3
Orders of Growth
  • Suppose n is a parameter that measures the size
    of a problem (the size of its input)
  • R(n)measures the amount of resources needed to
    compute a solution procedure of size n.
  • Two common resources are space, measured by the
    number of deferred operations, and time, measured
    by the number of primitive steps.

4
Orders of Growth
  • Want to estimate the order of growth of R(n)

R1(n)100n2 R2(n)2n210n2
R3(n) n2
Are all the same in the sense that if we multiply
the input by a factor of 2, the resource
consumption increase by a factor of 4
Order of growth is proportional to n2
5
Orders of Growth
  • We say that R(n)? O(f(n)) if there is a
    constant cgt0 such that for all n1
  • R(n) c f(n)
  • We say that R(n)? ?(f(n)) if there is a
    constant cgt0 such that for all n1
  • c f(n) R(n)
  • We say that R(n)? Q(f(n)) if there are
    constants c1,c2gt 0 such that for all n1
  • c1 f(n) R(n) c2 f(n)

6
Orders of Growth
True or False?
f
nlog(n) ? O(n)
t
nlog(n) ? ?(n)
f
nlog(n) ? Q(n)
n2 5nlog(n) ? Q(n2)
t
True or False?
t
log2(n) ? O(n)
f
2100n3 ? O(n2)
t
log(100) ? Q(1)
7
Resources Consumed by EXP-1
  • Space R(b) b which is Q(b)
  • Time R(b) 2b which is Q(b)

Linear Recursive Process
8
Resources Consumed by EXP-2
  • Space Q(1)
  • Time Q(b)

Linear Iterative Process
9
Summary
  • Trying to capture the nature of processes
  • Quantify various properties of processes
  • Number of steps a process takes (Time Complexity)
  • Amount of Space a process uses (Space Complexity)

10
The conditional form
(cond (lttest-1gt ltconsequent-1gt) (lttest-2gt
ltconsequent-2gt) . (lttest-ngt
ltconsequent-ngt) (else
ltconsequent-elsegt))
(define (abs x) (cond ((gt x 0) x) (( x 0)
0)
(else (- x))))
((lt x 0) (- x))))
11
Another algorithm for computing ab
  • If b is even, then ab (a2)(b/2)
  • If b is odd, then ab aa(b-1)
  • Note that here, we reduce the problem size in
    half in one step.

(define (exp-fast1 a b) computes ab (cond
(( b 0) 1) ((even? b) (exp-fast1 ( a
a) (/ b 2))) (else ( a (exp-fast1 a (- b
1)))))))
12
(exp-fast 3 56)
(define (exp-fast1 a b) (cond (( b 0) 1)
((even? b) (exp-fast1 ( a a) (/ b 2)))
(else ( a (exp-fast1 a (- b 1)))))))
(exp-fast1 3 56) compute 356 (exp-fast1 9
28) (exp-fast1 81 14) (exp-fast1 6561 7) 6561
(exp-fast1 6561 6) 6561 (exp-fast1 43046721
3) 6561 43046721 (exp-fast1 43046721 2) 6561
43046721 (exp-fast1 1853020188851841 1) 6561
43046721 1853020188851841 (exp-fast1 ..
0) 6561 43046721 1853020188851841 523347633027
360537213511521
13
How much time does exp-fast take?
If b is even T(b)
T(b/2)2 and if b is odd then T(b)
T((b-1)/2)4
Let b1001012
T(1001012) T(100102)4 T(10012)24
T(1002)424 T(12)22424
222424
Conclusion 2logb ? T(b) ? 4log b
  • The order of growth in time is Q(log b)

The order of growth in space is O(log b)

14
Fast exponentiation (Iterative Approach)
product ? 1, base a, exp b
Init
Loop
Even? b base ? base2 exp ?
exp/2 Odd? b product ? producta exp ? exp-1
15
Scheme code (Iterative Approach)
(define (exp-fast2 a b) (define (exp-fast-iter
a b product) (cond (( b 0) product)
((even? b) (exp-fast-iter ( a a) (/ b 2)
product)) (else (exp-fast-iter a (- b 1)
( a product))))) (exp-fast-iter a b 1))
16
Comparing the three exponentiation procedures
exp-fast and exp-fast1 are exponentially faster
than exp-1 and exp-2
17
Towers of Hanoi
  • Three posts, and a set of disks of different
    sizes
  • A disk can be only on a disk of larger size.
  • At the beginning all the disks are on the left
    post.
  • The goal is to move the disks one at a time,
    while preserving these conditions, until the
    entire stack has moved from one post to another

You are allowed to move only the topmost disk
18
Use our paradigm
  • Wishful thinking
  • Smaller problem A problem with one disk less
  • How do we use it ?

Move n-1 disks from peg A to peg B Move the
largest from peg A to peg C Move n-1 disks from
peg B to peg C
We solve 2 smaller problems !
19
Towers of Hanoi
(define (move-tower size from to aux) (cond
(( size 1) (one-move from to)) (else
(move-tower (- size 1) from aux to)
(one-move from to) (move-tower (- size
1) aux to from))))
(define (one-move from to) (display "Move top
disk from ") (display from) (display " To
") (display to) (newline))
20
Towers of Hanoi -- trace
  • (move-tower 3 1 2 3)
  • Move top disk from 1 to 2
  • Move top disk from 1 to 3
  • Move top disk from 2 to 3
  • Move top disk from 1 to 2
  • Move top disk from 3 to 1
  • Move top disk from 3 to 2
  • Move top disk from 1 to 2

21
Tree Recursion
(mt 3 2 1 3)
22
Tree Recursion
(mt 3 1 2 3)
23
Orders of growth for towers of Hanoi
  • Denote by T(n) be the number of steps that we
    need to take to solve the case for n disks.

T(n) 2T(n-1) 1 T(1) 1
This solves to T(n) 2n-1 Q
(2n)
What does that mean ?
24
Hanoi Towers
Say we want to solve the problem for 400 pegs.
Say we can move a peg in a second. We need
about 2400 seconds. Thats about 2373
years. Thats about 2363 millenniums. Might be
longer then the universe age. Might be longer
than the time our plant will last.
Infeasible !!!!
25
Lets buy a fast computerand make it feasible.
Our new computer can move giga billion (260 )
pegs a second. Absolutely the last word in the
field of computing. We need about 2340
seconds. Thats about 2313 years. Thats about
2303 millenniums.
Does not help much. Infeasible !!!!
26
Space complexity of towers of Hanoi ?
27
Towers of Hanoi
(define (move-tower size from to aux) (cond
(( size 1) (one-move from to)) (else
(move-tower (- size 1) from aux to)
(one-move from to) (move-tower (- size
1) aux to from))))
operation pending
(define (one-move from to) (display "Move top
disk from ") (display from) (display " To
") (display to) (newline))
28
Space complexity of towers of Hanoi
For the space complexity we have
S(n) S(n-1) O(1)
S(n) O(n)
29
Tree Recursion
(mt 3 1 2 3)
(move-one 1 2)
(move-one 1 3)
(move-one 3 2)
(move-one 1 2)
(move-one 2 3)
(move-one 3 1)
(move-one 1 2)
Write a Comment
User Comments (0)
About PowerShow.com