0% found this document useful (0 votes)
17 views16 pages

Ch4 Slides

Uploaded by

chenziyi1202
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views16 pages

Ch4 Slides

Uploaded by

chenziyi1202
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

4.

Logarithmic Loops

Learning objective:
to introduce algorithms that perform logarithmic number
of iterations

Lecture Plan:
4.1 Exponents and Logarithms
4.2 Typical algorithms
4.3 Example
4.1 Exponents and Logarithms

Starting with definitions (next slide)


Exponent: 2x = 2  2  …  2

x 2 x
(x times)
1
-2 4
1 10 x log2x
-1 1
2
-2
0 1 4
8
1
1 2 -1
2
2 4 6 1 0
3 8 2 1
4
4 16 3 4 2
2 8 3
1 16 4
-2 1 2 3 4 6 8 10

Logarithm:
Logarithm to the base 2 of a number x is the power
to which 2 must be raised in order to produce x.

log2x is the unique real number y such that 2y = x


Exponent: 2x = 2  2  …  2

x 2 x
(x times)
1
-2 4
1 10 x log2x
-1 1
2
-2
0 1 4
8
1
1 2 -1
2
2 4 6 1 0
3 8 2 1
4
4 16 3 4 2
y = log2x
2 8 3
1 16 4
-2 0 1 2 3 4 6 8 10
-2
Logarithm:
Logarithm to the base 2 of a number x is the power
to which 2 must be raised in order to produce x.

log2x is the unique real number y such that 2y = x


Summary Table
Further Reading

Textbooks for Fundamental Mathematical Concepts


(available online)
- S. Lipschutz, M.L. Lipson: Schaum's outline of discrete
mathematics. McGraw-Hill, 2009 (Chapter 3, pp. 49-50)
- K.H. Rosen: Discrete Mathematics and its Applications. ISE
EBook Online Access for Discrete Mathematics and Its
Applications, 8th edition, McGraw-Hill, 2018
(Appendix A.2.2, pp. 1805-1807)
- R. Johnsonbaugh: Discrete Mathematics. Prentice-Hall,
2008 (Appendix B, pp. 636-638)
4.2 Typical Algorithms

Loops:
- control variable is doubled
or
- control variable is halved
i  1 i  n
while i < n do while i > 1 do
application code application code
i  i*2 i  i/2

Assume n=2k. This implies k=log2n


Cond. to i at the i at the Cond. to i at the i at the
Iter. continue beginning end of Iter. continue beginning end of
the loop of iteration iteration the loop of iter. iteration
i<n i>1
1 T 1 2 1 T n n/2
Thus the number of iterations is k=log2n.
2 T 2 4 2 T n/2 n/4
3 T 4 8 3 T n/4 n/8
4 T 8 16 4 T n/8 n/16
     
k T 2k-1 2k = n k T n/2k-1 n/2k=1
k+1 F k+1 F

The algorithm stops when condition i<n The algorithm stops when condition i>1
is no longer satisfied. This happens when is no longer satisfied. This happens when
at iteration k variable i reaches n=2k. at iteration k variable i reaches n/2k =1.
i  1 i  n
while i < n do while i > 1 do
application code application code
i  i*2 i  i/2

Assume 2k-1< n ≤ 2k. This implies k=log2n.

Cond. to i at the i at the Cond. to i at the i at the


Iter. continue beginning end of Iter. continue beginning end of
the loop of iteration iteration the loop of iter. iteration
i<n i>1
1 T 1 2 1 T n n/2
Thus the number of iterations is k=log2n.
2 T 2 4 2 T n/2 n/4
3 T 4 8 3 T n/4 n/8
4 T 8 16 4 T n/8 n/16
     
k T 2k-1 2k ≥ n k T n/2k-1 n/2k≤1
k+1 F k+1 F

The algorithm stops when condition i<n The algorithm stops when condition i>1
is no longer satisfied. This happens after is no longer satisfied. This happens after
iteration k when i reaches 2k, 2k ≥ n. iteration k when i reaches n/2k, n/2k ≤1.
4.3 Example

Find the number of binary digits in the binary representation


of a positive decimal integer n

n = ak2k + ak-12k-1+ … + a12+ a0 = (𝑎𝑘 … 𝑎1 𝑎0 )2 where ak=1

Given binary representation


(a positive
decimal integer)

Output: 𝑘 + 1
4.3 Example

Find the number of binary digits in the binary representation


of a positive decimal integer n

(13)10 = 123 + 122+021+ 120 = (1101)2

Given binary representation

Output: 𝑘 + 1 = 4
n = ak2k + ak-12k-1+ … + a12+ a0, where ak=1

ALGORITHM number-of-binary-digits(n)
//Input: A positive decimal integer n
//Output: The number of binary digits in the
// binary representation of n
count  1
while n > 1 do
count  count+1
n  n/2
return count
n n
Iteration (at the start of count (at the end of
iteration) iteration)
1
Tracing for n=13: 1 13 2 6
2 6 3 3
3 3 4 1
4 1
n = ak2k + ak-12k-1+ … + a12+ a0, where ak=1

ALGORITHM number-of-binary-digits(n)
//Input: A positive decimal integer n
//Output: The number of binary digits in the
// binary representation of n
count  1
while n > 1 do
count  count+1
n  n/2
/
return count
As we have seen, if the control variable is halved, then the number of iterations is
k=log2n.
In the current example, the control variable is halved and rounded down: n  n/2 .
This implies that the number of iterations is bounded by k=log2n.

(a) What is the algorithm’s main (most frequent) operation? division


(b) What is an upper bound on the number of times it is repeated? log2n
(c) Determine the class O(?) the algorithm belongs to. O(log2n)
n = ak2k + ak-12k-1+ … + a12+ a0, where ak=1

ALGORITHM number-of-binary-digits(n)
//Input: A positive decimal integer n
//Output: The number of binary digits in the
// binary representation of n
count  1
while n > 1 do
count  count+1
+
n  n/2
/
return count
As we have seen, if the control variable is halved, then the number of iterations is
k=log2n.
In the current example, the control variable is halved and rounded down: n  n/2 .
This implies that the number of iterations is bounded by k=log2n.

(a) What are the algorithm’s basic operations? division, summation


(b) What is an upper bound on the number of times they are repeated? 2log2n
(c) Determine the class O(?) the algorithm belongs to. O(log2n)
n = ak2k + ak-12k-1+ … + a12+ a0, where ak=1

ALGORITHM number-of-binary-digits(n)
count  1
while n > 1 do
count  count+1
n  n/2
return count

Algorithm correctness: The algorithm counts how many times operation


n/2 is performed before reaching n=1.
With the initial value count=1, it returns the number of divisions plus 1.
Consider n = ak2k + ak-12k-1+ … + a222 + a12+ a0
The first division results in n = ak2k-1 + ak-12k-2+ … + a22 + a1
The second division results in n = ak2k-2 + ak-12k-3+ … + a2
Thus after k divisions n=ak=1 (the first digit of the binary representation),
and count=k+1 (the number of binary digits).
Conclusions

Make sure you understand


- the definition of logarithm
- the analysis of the two basic pseudocodes for n=2k
(the material for 2k-1< n ≤ 2k is not compulsory)
- the last example and its analysis

Further examples will be provided in tutorials and in lectures


on searching and sorting

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy