0% found this document useful (0 votes)
79 views59 pages

Lec03-Nonrec Analysis PDF

This document discusses analyzing the time efficiency of non-recursive algorithms. It explains that the time efficiency is determined by identifying the basic operation, which is the operation that contributes most to the running time. The number of times the basic operation is executed depends on the input size and can be determined for worst, average, and best cases. The document provides an example of analyzing an algorithm to find the largest element in an array, where the basic operation is comparison using >.

Uploaded by

Al Basyir
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)
79 views59 pages

Lec03-Nonrec Analysis PDF

This document discusses analyzing the time efficiency of non-recursive algorithms. It explains that the time efficiency is determined by identifying the basic operation, which is the operation that contributes most to the running time. The number of times the basic operation is executed depends on the input size and can be determined for worst, average, and best cases. The document provides an example of analyzing an algorithm to find the largest element in an array, where the basic operation is comparison using >.

Uploaded by

Al Basyir
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/ 59

CS 350 Algorithms and Complexity

Winter 2019

Lecture 3: Analyzing Non-Recursive Algorithms

Andrew P. Black

Department of Computer Science


Portland State University
Analysis of time efficiency
! Time efficiency is analyzed by determining
the number of repetitions of the “basic
operation”
! Almost always depends on the size of the
input
! “Basicoperation”: the operation that
contributes most towards the running time
of the algorithm

T(n) ≈ cop ⨉ C(n)


"2
Analysis of time efficiency
! Time efficiency is analyzed by determining
the number of repetitions of the “basic
operation”
! Almost always depends on the size of the
input
! “Basicoperation”: the operation that
contributes most towards the running time
of the algorithm

run time
T(n) ≈ cop ⨉ C(n)
"2
Analysis of time efficiency
! Time efficiency is analyzed by determining
the number of repetitions of the “basic
operation”
! Almost always depends on the size of the
input
! “Basicoperation”: the operation that
contributes most towards the running time
of the algorithm cost of basic
op: constant

run time
T(n) ≈ cop ⨉ C(n)
"2
Analysis of time efficiency
! Time efficiency is analyzed by determining
the number of repetitions of the “basic
operation”
! Almost always depends on the size of the
input
! “Basicoperation”: the operation that
contributes most towards the running time
of the algorithm cost of basic
op: constant number
of times basic op
run time is executed
T(n) ≈ cop ⨉ C(n)
"2
Problem Input size measure Basic operation

Searching for key in a


list of n items

Multiplication of two
matrices

Checking primality of
a given integer n

Shortest path through


a graph

"3
Complete the table
Problem Input size measure Basic operation

Searching for key in a A: Number of list’s


A: Key comparison
list of n items items, i.e. n

B: Matrix dimension, or
Multiplication of two B: Multiplication of
total number of
matrices two numbers
elements

Checking primality of C: size of n = number of


C: Division
a given integer n digits

Shortest path through D: #vertices and/or D: Visiting a vertex or


a graph edges traversing an edge

"4
Problem Input size measure Basic operation

Searching for key in a A: Number of list’s


list of n items items, i.e. n

B: Matrix dimension, or
total number of
elements

C: size of n = number of
digits

D: #vertices and/or
edges

"5
Problem Input size measure Basic operation

Searching for key in a


A: Key comparison
list of n items

B: Multiplication of
two numbers

C: Division

D: Visiting a vertex or
traversing an edge

"6
Problem Input size measure Basic operation

A: Number of list’s
items, i.e. n

B: Matrix dimension, or
Multiplication of two
total number of
matrices
elements

C: size of n = number of
digits

D: #vertices and/or
edges

"7
Problem Input size measure Basic operation

A: Key comparison

Multiplication of two B: Multiplication of


matrices two numbers

C: Division

D: Visiting a vertex or
traversing an edge

"8
Problem Input size measure Basic operation

A: Number of list’s
items, i.e. n

B: Matrix dimension, or
total number of
elements

Checking primality of C: size of n = number of


a given integer n digits

D: #vertices and/or
edges

"9
Problem Input size measure Basic operation

A: Key comparison

B: Multiplication of
two numbers

Checking primality of
C: Division
a given integer n

D: Visiting a vertex or
traversing an edge

"10
Problem Input size measure Basic operation

A: Number of list’s
items, i.e. n

B: Matrix dimension, or
total number of
elements

C: size of n = number of
digits

Shortest path through D: #vertices and/or


a graph edges

"11
Problem Input size measure Basic operation

A: Key comparison

B: Multiplication of
two numbers

C: Division

Shortest path through D: Visiting a vertex or


a graph traversing an edge

"12
Problem Input size measure Basic operation

Searching for key in a


list of n items

Multiplication of two
matrices

Checking primality of
a given integer n

Shortest path through


a graph

"13
Best-case, average-case, worst-case
! For some algorithms, efficiency depends on the input:
! Worst case: Cworst(n) – maximum over inputs of size n
! Best case: Cbest(n) – minimum over inputs of size n
! Average case: Cavg(n) – “average” over inputs of size n
" Number of times the basic operation will be executed on
typical input
# Not the average of worst and best case
" Expected number of basic operations under some
assumption about the probability distribution of all
possible inputs

"14
Discuss:

! What’s the best case, and its running time?


A. constant — O(1)

B. linear — O(n)

C. quadratic — O(n2)

"15
Discuss:

! What’s the worst case, and its running time?


A. constant — O(1)

B. linear — O(n)

C. quadratic — O(n2)

"16
Discuss:

! What’s the average case, and its running time?


A. constant — O(1)

B. linear — O(n)

C. quadratic — O(n2)

"17
General Plan for Analysis of
non-recursive algorithms
1. Decide on parameter n indicating input size

2. Identify algorithm’s basic operation

3. Determine worst, average, and best cases


for input of size n

4. Set up a sum for the number of times the


basic operation is executed

5. Simplify the sum using standard formulae


and rules (see Levitin Appendix A)

"18
analyzing such algorithms.

EXAMPLE 1 Consider the problem of finding the value of the largest element

“Basic Operation”
in a list of n numbers. For simplicity, we assume that the list is implemented as
an array. The following is pseudocode of a standard algorithm for solving the
problem.

ALGORITHM MaxElement(A[0..n − 1])


//Determines the value of the largest element in a given array
//Input: An array A[0..n − 1] of real numbers
//Output: The value of the largest element in A
maxval ← A[0]
for i ← 1 to n − 1 do
if A[i] > maxval
maxval ← A[i]
return maxval

The obvious measure of an input’s size here is the number of elements in the
array,!
Why choose > as the basic operation?
i.e., n. The operations that are going to be executed most often are in the
algorithm’s for loop. There are two operations in the loop’s body: the comparison
A[i] > maxval and the assignment maxval ← A[i]. Which of these two operations
should we " Why not i
consider basic? Since the← i+1?
comparison is executed on each repetition
of the loop and the assignment is not, we should consider the comparison to be
Or [ ] ?
" basic operation. Note that the number of comparisons will be the
the algorithm’s
same for all arrays of size n; therefore, in terms of this metric, there is no need to
distinguish among the worst, average, and best cases here.

"19
Same Algorithm:
ALGORITHM MaxElement (A: List)
// Determines the value of the largest element in the list A
// Input: a list A of real numbers
// Output: the value of the largest element of A
maxval ← A.first
for each in A do
if each > maxval
maxval ← each
return maxval

! Why choose > as the basic operation?


" Why not i ← i + 1 ?
" Or [ ] ?

"20
From Algorithm to Formula
! We want a formula for the # of basic ops
! Basic op will normally be in inner loop

! Bounds of for loop become bounds of


summation
! e.g. for i ← l .. h do:


3 basic operations
h
X
!
3
<latexit sha1_base64="PgrGfqzWsuLxxVb2ld/iMhb1CnM=">AAAB+XicdZDLSgMxFIYz9VbrbdSlm2ARXJVMbel0IRTduKxgL9COQyZN29BkZkgyhTLMm7hxoYhb38Sdb2N6EVT0h8DHf87hnPxBzJnSCH1YubX1jc2t/HZhZ3dv/8A+PGqrKJGEtkjEI9kNsKKchbSlmea0G0uKRcBpJ5hcz+udKZWKReGdnsXUE3gUsiEjWBvLt+2+SoSfskue3afjDF74dhGVylVUdxE0UENV1zVQrdRRGUGnhBYqgpWavv3eH0QkETTUhGOleg6KtZdiqRnhNCv0E0VjTCZ4RHsGQyyo8tLF5Rk8M84ADiNpXqjhwv0+kWKh1EwEplNgPVa/a3Pzr1ov0UPXS1kYJ5qGZLlomHCoIziPAQ6YpETzmQFMJDO3QjLGEhNtwiqYEL5+Cv+HdrnkGL6tFBtXqzjy4AScgnPggBpogBvQBC1AwBQ8gCfwbKXWo/VivS5bc9Zq5hj8kPX2Ccikk8A=</latexit>
i=l
"21
Works for nested loops too

0 1
n
X2 n
X1
@ 1A
i=0 j=i+1
<latexit sha1_base64="Vg9ZBRgSeUB979vv8vT2hoziRi8=">AAACH3icdZDLSgMxFIYzXmu9VV26CRZBEUumWG0XgujGpYJVoVNLJs20sZnMkJwRyjBv4sZXceNCEXHn25heBBU9EPj4/3M4Ob8fS2GAkA9nYnJqemY2N5efX1hcWi6srF6aKNGM11kkI33tU8OlULwOAiS/jjWnoS/5ld87GfhXd1wbEakL6Me8GdKOEoFgFKzUKux7JglbqTgk2U2qdssZ9iQPAG/hkXF7KHbcoeVm2MWeFp0u4O1WoUhK5QqpVQm2cEAq1aqFyl6NlAl2S2RYRTSus1bh3WtHLAm5AiapMQ2XxNBMqQbBJM/yXmJ4TFmPdnjDoqIhN810eF+GN63SxkGk7VOAh+r3iZSGxvRD33aGFLrmtzcQ//IaCQTVZipUnABXbLQoSCSGCA/Cwm2hOQPZt0CZFvavmHWppgxspHkbwtel+H+4LJdcy+d7xaPjcRw5tI420BZy0QE6QqfoDNURQ/foET2jF+fBeXJenbdR64QznllDP8r5+ASTcaFr</latexit>

"22
Works for nested loops too

0 1
n
X2 n
X1
@ 1A
i=0 j=i+1
<latexit sha1_base64="Vg9ZBRgSeUB979vv8vT2hoziRi8=">AAACH3icdZDLSgMxFIYzXmu9VV26CRZBEUumWG0XgujGpYJVoVNLJs20sZnMkJwRyjBv4sZXceNCEXHn25heBBU9EPj4/3M4Ob8fS2GAkA9nYnJqemY2N5efX1hcWi6srF6aKNGM11kkI33tU8OlULwOAiS/jjWnoS/5ld87GfhXd1wbEakL6Me8GdKOEoFgFKzUKux7JglbqTgk2U2qdssZ9iQPAG/hkXF7KHbcoeVm2MWeFp0u4O1WoUhK5QqpVQm2cEAq1aqFyl6NlAl2S2RYRTSus1bh3WtHLAm5AiapMQ2XxNBMqQbBJM/yXmJ4TFmPdnjDoqIhN810eF+GN63SxkGk7VOAh+r3iZSGxvRD33aGFLrmtzcQ//IaCQTVZipUnABXbLQoSCSGCA/Cwm2hOQPZt0CZFvavmHWppgxspHkbwtel+H+4LJdcy+d7xaPjcRw5tI420BZy0QE6QqfoDNURQ/foET2jF+fBeXJenbdR64QznllDP8r5+ASTcaFr</latexit>

"22
Works for nested loops too

0 1
n
X2 n
X1
@ 1A
i=0 j=i+1
<latexit sha1_base64="Vg9ZBRgSeUB979vv8vT2hoziRi8=">AAACH3icdZDLSgMxFIYzXmu9VV26CRZBEUumWG0XgujGpYJVoVNLJs20sZnMkJwRyjBv4sZXceNCEXHn25heBBU9EPj4/3M4Ob8fS2GAkA9nYnJqemY2N5efX1hcWi6srF6aKNGM11kkI33tU8OlULwOAiS/jjWnoS/5ld87GfhXd1wbEakL6Me8GdKOEoFgFKzUKux7JglbqTgk2U2qdssZ9iQPAG/hkXF7KHbcoeVm2MWeFp0u4O1WoUhK5QqpVQm2cEAq1aqFyl6NlAl2S2RYRTSus1bh3WtHLAm5AiapMQ2XxNBMqQbBJM/yXmJ4TFmPdnjDoqIhN810eF+GN63SxkGk7VOAh+r3iZSGxvRD33aGFLrmtzcQ//IaCQTVZipUnABXbLQoSCSGCA/Cwm2hOQPZt0CZFvavmHWppgxspHkbwtel+H+4LJdcy+d7xaPjcRw5tI420BZy0QE6QqfoDNURQ/foET2jF+fBeXJenbdR64QznllDP8r5+ASTcaFr</latexit>

"22
Useful Summation Formulae
Σl≤i≤u1 = 1+1+…+1 = u - l + 1

In particular, Σ1≤i≤n1 = n - 1 + 1 = n ∈ Θ(n)

Σ1≤i≤n i = 1+2+…+n = n(n+1)/2 ≈ n2/2 ∈ Θ(n2)

Σ1≤i≤n i2 = 12+22+…+n2 = n(n+1)(2n+1)/6 ≈ n3/3 ∈ Θ(n3)

Σ0≤i≤n ai = 1 + a +…+ an = (an+1 - 1)/(a - 1) for any a ≠ 1

In particular, Σ0≤i≤n 2i = 20 + 21 + … + 2n = 2n+1 - 1 ∈ Θ(2n ) 


Σ(ai ± bi ) = Σ ai ± Σ bi Σc ai = c Σ ai 

Σl≤i≤u ai = Σ l ≤i≤m ai + Σ m+1≤i≤u ai
"23
Useful Summation Formulae
Σl≤i≤u1 = 1+1+…+1 = u - l + 1

In particular, Σ1≤i≤n1 = n - 1 + 1 = n ∈ Θ(n)

Σ1≤i≤n i = 1+2+…+n = n(n+1)/2 ≈ n2/2 ∈ Θ(n2)

Σ1≤i≤n i2 = 12+22+…+n2 = n(n+1)(2n+1)/6 ≈ n3/3 ∈ Θ(n3)

Σ0≤i≤n ai = 1 + a +…+ an = (an+1 - 1)/(a - 1) for any a ≠ 1

In particular, Σ0≤i≤n 2i = 20 + 21 + … + 2n = 2n+1 - 1 ∈ Θ(2n ) 


Σ(ai ± bi ) = Σ ai ± Σ bi Σc ai = c Σ ai 

Σl≤i≤u ai = Σ l ≤i≤m ai + Σ m+1≤i≤u ai
"23
Useful Summation Formulae
Σl≤i≤u1 = 1+1+…+1 = u - l + 1

In particular, Σ1≤i≤n1 = n - 1 + 1 = n ∈ Θ(n)

Σ1≤i≤n i = 1+2+…+n = n(n+1)/2 ≈ n2/2 ∈ Θ(n2)

Σ1≤i≤n i2 = 12+22+…+n2 = n(n+1)(2n+1)/6 ≈ n3/3 ∈ Θ(n3)

Σ0≤i≤n ai = 1 + a +…+ an = (an+1 - 1)/(a - 1) for any a ≠ 1

In particular, Σ0≤i≤n 2i = 20 + 21 + … + 2n = 2n+1 - 1 ∈ Θ(2n ) 


Σ(ai ± bi ) = Σ ai ± Σ bi Σc ai = c Σ ai 

Σl≤i≤u ai = Σ l ≤i≤m ai + Σ m+1≤i≤u ai
"23
Useful Summation Formulae
Σl≤i≤u1 = 1+1+…+1 = u - l + 1

In particular, Σ1≤i≤n1 = n - 1 + 1 = n ∈ Θ(n)

Σ1≤i≤n i = 1+2+…+n = n(n+1)/2 ≈ n2/2 ∈ Θ(n2)

Σ1≤i≤n i2 = 12+22+…+n2 = n(n+1)(2n+1)/6 ≈ n3/3 ∈ Θ(n3)

Σ0≤i≤n ai = 1 + a +…+ an = (an+1 - 1)/(a - 1) for any a ≠ 1

In particular, Σ0≤i≤n 2i = 20 + 21 + … + 2n = 2n+1 - 1 ∈ Θ(2n ) 


Σ(ai ± bi ) = Σ ai ± Σ bi Σc ai = c Σ ai 

Σl≤i≤u ai = Σ l ≤i≤m ai + Σ m+1≤i≤u ai
"23
Useful Summation Formulae
Σl≤i≤u1 = 1+1+…+1 = u - l + 1

In particular, Σ1≤i≤n1 = n - 1 + 1 = n ∈ Θ(n)

Σ1≤i≤n i = 1+2+…+n = n(n+1)/2 ≈ n2/2 ∈ Θ(n2)

Σ1≤i≤n i2 = 12+22+…+n2 = n(n+1)(2n+1)/6 ≈ n3/3 ∈ Θ(n3)

Σ0≤i≤n ai = 1 + a +…+ an = (an+1 - 1)/(a - 1) for any a ≠ 1

In particular, Σ0≤i≤n 2i = 20 + 21 + … + 2n = 2n+1 - 1 ∈ Θ(2n ) 


Σ(ai ± bi ) = Σ ai ± Σ bi Σc ai = c Σ ai 

Σl≤i≤u ai = Σ l ≤i≤m ai + Σ m+1≤i≤u ai
"23
Useful Summation Formulae
Σl≤i≤u1 = 1+1+…+1 = u - l + 1

In particular, Σ1≤i≤n1 = n - 1 + 1 = n ∈ Θ(n)

Σ1≤i≤n i = 1+2+…+n = n(n+1)/2 ≈ n2/2 ∈ Θ(n2)

Σ1≤i≤n i2 = 12+22+…+n2 = n(n+1)(2n+1)/6 ≈ n3/3 ∈ Θ(n3)

Σ0≤i≤n ai = 1 + a +…+ an = (an+1 - 1)/(a - 1) for any a ≠ 1

In particular, Σ0≤i≤n 2i = 20 + 21 + … + 2n = 2n+1 - 1 ∈ Θ(2n ) 


Σ(ai ± bi ) = Σ ai ± Σ bi Σc ai = c Σ ai 

Σl≤i≤u ai = Σ l ≤i≤m ai + Σ m+1≤i≤u ai
"23
Useful Summation Formulae
Σl≤i≤u1 = 1+1+…+1 = u - l + 1

In particular, Σ1≤i≤n1 = n - 1 + 1 = n ∈ Θ(n)

Σ1≤i≤n i = 1+2+…+n = n(n+1)/2 ≈ n2/2 ∈ Θ(n2)

Σ1≤i≤n i2 = 12+22+…+n2 = n(n+1)(2n+1)/6 ≈ n3/3 ∈ Θ(n3)

Σ0≤i≤n ai = 1 + a +…+ an = (an+1 - 1)/(a - 1) for any a ≠ 1

In particular, Σ0≤i≤n 2i = 20 + 21 + … + 2n = 2n+1 - 1 ∈ Θ(2n ) 


Σ(ai ± bi ) = Σ ai ± Σ bi Σc ai = c Σ ai 

Σl≤i≤u ai = Σ l ≤i≤m ai + Σ m+1≤i≤u ai
"23
Useful Summation Formulae
Σl≤i≤u1 = 1+1+…+1 = u - l + 1

In particular, Σ1≤i≤n1 = n - 1 + 1 = n ∈ Θ(n)

Σ1≤i≤n i = 1+2+…+n = n(n+1)/2 ≈ n2/2 ∈ Θ(n2)

Σ1≤i≤n i2 = 12+22+…+n2 = n(n+1)(2n+1)/6 ≈ n3/3 ∈ Θ(n3)

Σ0≤i≤n ai = 1 + a +…+ an = (an+1 - 1)/(a - 1) for any a ≠ 1

In particular, Σ0≤i≤n 2i = 20 + 21 + … + 2n = 2n+1 - 1 ∈ Θ(2n ) 


Σ(ai ± bi ) = Σ ai ± Σ bi Σc ai = c Σ ai 

Σl≤i≤u ai = Σ l ≤i≤m ai + Σ m+1≤i≤u ai
"23
Useful Summation Formulae
Σl≤i≤u1 = 1+1+…+1 = u - l + 1

In particular, Σ1≤i≤n1 = n - 1 + 1 = n ∈ Θ(n)

Σ1≤i≤n i = 1+2+…+n = n(n+1)/2 ≈ n2/2 ∈ Θ(n2)

Σ1≤i≤n i2 = 12+22+…+n2 = n(n+1)(2n+1)/6 ≈ n3/3 ∈ Θ(n3)

Σ0≤i≤n ai = 1 + a +…+ an = (an+1 - 1)/(a - 1) for any a ≠ 1

In particular, Σ0≤i≤n 2i = 20 + 21 + … + 2n = 2n+1 - 1 ∈ Θ(2n ) 


Σ(ai ± bi ) = Σ ai ± Σ bi Σc ai = c Σ ai 

Σl≤i≤u ai = Σ l ≤i≤m ai + Σ m+1≤i≤u ai
"23
Useful Summation Formulae
Σl≤i≤u1 = 1+1+…+1 = u - l + 1

In particular, Σ1≤i≤n1 = n - 1 + 1 = n ∈ Θ(n)

Σ1≤i≤n i = 1+2+…+n = n(n+1)/2 ≈ n2/2 ∈ Θ(n2)

Σ1≤i≤n i2 = 12+22+…+n2 = n(n+1)(2n+1)/6 ≈ n3/3 ∈ Θ(n3)

Σ0≤i≤n ai = 1 + a +…+ an = (an+1 - 1)/(a - 1) for any a ≠ 1

In particular, Σ0≤i≤n 2i = 20 + 21 + … + 2n = 2n+1 - 1 ∈ Θ(2n ) 


Σ(ai ± bi ) = Σ ai ± Σ bi Σc ai = c Σ ai 

Σl≤i≤u ai = Σ l ≤i≤m ai + Σ m+1≤i≤u ai
"23
Where do the Summation formulae come
from?
! Answer:mathematics.
! Example:

The Euler–Mascheroni constant 𝛾 is defined as:

n
!
X 1
= lim ln n
n!1
i=1
i

"24
Where do the Summation formulae come
from?
! Answer:mathematics.
! Example:

The Euler–Mascheroni constant 𝛾 is defined as:

n
!
X 1
Harmonic = lim ln n
series n!1
i=1
i

"24
Where do the Summation formulae come
from?
! Answer:mathematics.
! Example:

The Euler–Mascheroni constant 𝛾 is defined as:

n
!
X 1
Harmonic = lim ln n
series n!1
i=1
i

y = 1/x

"24
Where do the Summation formulae come
from?
! Answer:mathematics.
! Example:

The Euler–Mascheroni constant 𝛾 is defined as:

n
!
X 1
Harmonic = lim ln n
series n!1
i=1
i

y = 1/x

ln x

"24
What does Levitin’s ⇡ mean?
! “becomes almost equal to as n → ∞”
! So formula 8
nX
lg i ⇡ n lg n
i=1
" means
n
!
X
lim lg i n lg n =0
n!1
i=1

"25
Example: Counting Binary Digits

"26
Example: Counting Binary Digits

! How many times is the basic operation


executed?

"26
Example: Counting Binary Digits

! How many times is the basic operation


executed?
! Why is this algorithm harder to analyze
than the earlier examples?
"26
Ex 2.3, Problem 1
2.3 with a partner:
! Working
Exercises
1. Compute the following sums.

a. 1 + 3 + 5 + 7 + ... + 999

b. 2 + 4 + 8 + 16 + ... + 1024
!n+1 !n+1 !n−1
c. i=3 1 d. i=3 i e. i=0 i(i + 1)
!n j+1
!n !n P n
!n−1
f. j=1 3 g. i=1 j=1 ij h. i=1
i=0 1/i(i + 1)
2. Find the order of growth of the following sums.
!n−1 2 2
!n−1
a. i=0 (i +1) b. i=2 lgi2
"27
Ex!2.3, Problem 2
b. 2 + 4 + 8 + 16 + ... + 1024

n+1 ! n+1 !n−1


c. i=3 1 d. i=3 i e. i=0 i(i + 1)
!n j+1
!n !n !n−1
f. j=1 3 g. i=1 j=1 ij h. i=0 1/i(i + 1)
2. Find the order of growth of the following sums.
!n−1 2 2
!n−1
a. i=0 (i +1) b. i=2 lgi2
!n i−1
!n−1 !i−1
c. i=1 (i + 1)2 d. i=0 j=0 (i + j)

Use the Θ(g(n)) notation with the simplest function g(n) possible.
3. The sample variance of n measurements x1 , x2 , ..., xn can be compute
!n 2
!n
i=1 (xi − x̄) i=1 xi
where x̄ =
n−1 n
or !n !n "28
2 2
i=1 xi − ( i=1 xi ) /n
a. i=0 (i2 +1)2 b. i=2 lgi2

Ex 2.3, Problem 3
!n i−1
!n−1 !i−1
c. i=1 (i + 1)2 d. i=0 j=0 (i + j)

Use the Θ(g(n)) notation with the simplest function g(n) possible.
3. The sample variance of n measurements x1 , x2 , ..., xn can be computed as
!n 2
!n
i=1 (xi − x̄) i=1 xi
where x̄ =
n−1 n
or !n !n
2 2
i=1 xi − ( i=1 xi ) /n
.
n−1
Find and compare the number of divisions, multiplications, and addi-
tions/subtractions (additions and subtractions are usually bunched to-
gether) that are required for computing the variance according to each of
these formulas.
4. Consider the following algorithm.

Algorithm Mystery( n) "29


//Input: A nonnegative integer n
Find and compare the number of divisions, multiplications, and addi-
tions/subtractions (additions and subtractions are usually bunched to-

Ex 2.3, Problem 4
gether) that are required for computing the variance according to each of
these formulas.
4. Consider the following algorithm.

Algorithm Mystery( n)
//Input: A nonnegative integer n
S←0
for i ← 1 to n do
S ←S+i∗i
returnS

a. What does this algorithm compute?

b. What is its basic operation?

c. How many times is the basic operation executed?

d. What is the efficiency class of this algorithm?


16
e. Suggest an improvement or a better algorithm altogether and indi- "30
cate its efficiency class. If you cannot do it, try to prove that, in fact, it
Find and compare the number of divisions, multiplications, and addi-
tions/subtractions (additions and subtractions are usually bunched to-

Ex 2.3, Problem 4
gether) that are required for computing the variance according to each of
these formulas.
4. Consider the following algorithm.

Algorithm Mystery( n) What does this


//Input: A nonnegative integer n
S←0
algorithm compute?
for i ← 1 to n do
S ←S+i∗i A. n2
returnS
Pn
a. What does this algorithm compute?
B. i=1 i
Pn 2
b. What is its basic operation? C. i=1 i
Pn
D. i=1 2i
c. How many times is the basic operation executed?

d. What is the efficiency class of this algorithm?


16
e. Suggest an improvement or a better algorithm altogether and indi- "30
cate its efficiency class. If you cannot do it, try to prove that, in fact, it
Find and compare the number of divisions, multiplications, and addi-
tions/subtractions (additions and subtractions are usually bunched to-

Ex 2.3, Problem 4
gether) that are required for computing the variance according to each of
these formulas.
4. Consider the following algorithm.

Algorithm Mystery( n)
//Input: A nonnegative integer n
S←0
for i ← 1 to n do
S ←S+i∗i
returnS

a. What does this algorithm compute?

b. What is its basic operation?

c. How many times is the basic operation executed?

d. What is the efficiency class of this algorithm?


16
e. Suggest an improvement or a better algorithm altogether and indi- "30
cate its efficiency class. If you cannot do it, try to prove that, in fact, it
Find and compare the number of divisions, multiplications, and addi-
tions/subtractions (additions and subtractions are usually bunched to-

Ex 2.3, Problem 4
gether) that are required for computing the variance according to each of
these formulas.
4. Consider the following algorithm.

Algorithm Mystery( n) What is the


//Input: A nonnegative integer n
S←0
basic operation?
for i ← 1 to n do
S ←S+i∗i A. multiplication
returnS

a. What does this algorithm compute? B. addition


b. What is its basic operation?
C. assignment
c. How many times is the basic operation executed?
D. squaring
d. What is the efficiency class of this algorithm?
16
e. Suggest an improvement or a better algorithm altogether and indi- "31
cate its efficiency class. If you cannot do it, try to prove that, in fact, it
Find and compare the number of divisions, multiplications, and addi-
tions/subtractions (additions and subtractions are usually bunched to-

Ex 2.3, Problem 4
gether) that are required for computing the variance according to each of
these formulas.
4. Consider the following algorithm.
How many times is the
Algorithm Mystery( n) basic operation executed?
//Input: A nonnegative integer n
S←0
for i ← 1 to n do
A. once
S ←S+i∗i
returnS B. n times
a. What does this algorithm compute?
C. lg n times
b. What is its basic operation?
D. none of the above
c. How many times is the basic operation executed?

d. What is the efficiency class of this algorithm?


16
e. Suggest an improvement or a better algorithm altogether and indi- "32
cate its efficiency class. If you cannot do it, try to prove that, in fact, it
Find and compare the number of divisions, multiplications, and addi-
tions/subtractions (additions and subtractions are usually bunched to-

Ex 2.3, Problem 4
gether) that are required for computing the variance according to each of
these formulas.
4. Consider the following algorithm.
What is the efficiency
class of this algorithm?
Algorithm Mystery( n)
//Input: A nonnegative integer n [b is # of bits needed to
S←0 represent n]
for i ← 1 to n do
S ←S+i∗i
returnS A. ⇥(1)

B.
a. What does this algorithm compute? ⇥(n)
b. What is its basic operation?
C. ⇥(b)
c. How many times is the basic operation executed?
D. ⇥(2b )
d. What is the efficiency class of this algorithm?
16
e. Suggest an improvement or a better algorithm altogether and indi- "33
cate its efficiency class. If you cannot do it, try to prove that, in fact, it
Ex 2.3, Problem 4 (cont)
d. What is the efficiency class of this algorithm?

e. Suggest an improvement or a better algorithm altogether and indi-


cate its efficiency class. If you cannot do it, try to prove that, in fact, it
cannot be done.
5. Consider the following algorithm.
Algorithm Secret(A[0..n − 1])
//Input: An array A[0..n − 1] of n real numbers
minval ← A[0]; maxval ← A[0]
for i ← 1 to n − 1 do
if A[i] < minval
minval ← A[i]
if A[i] > maxval
maxval ← A[i]
return maxval − minval
"34
e. Suggest an improvement or a better algorithm altogether a

Problem 5 — Group work


cate its efficiency class. If you cannot do it, try to prove that, i
cannot be done.
5. Consider the following algorithm.
Algorithm Secret(A[0..n − 1])
//Input: An array A[0..n − 1] of n real numbers
minval ← A[0]; maxval ← A[0]
for i ← 1 to n − 1 do
if A[i] < minval
minval ← A[i]
a. What does this algorithm compute?
if A[i] > maxval
b. What is its basic operation?
maxval ← A[i] c. How many times is the basic
return maxval − minval operation executed?
d. What is the efficiency class of this
Answer questions a—e of Problem 4algorithm?
about this algorithm.
e. Suggest an improvement or a better
algorithm altogether and indicate its
6. Consider the following algorithm. "35
efficiency class.
What effect will this change have on the algorithm’s efficiency?

Ex 2.3, Problem 9
8. Determine the asymptotic order of growth for the total number of times all
the doors are toggled in the Locker Doors puzzle (Problem 11 in Exercises
1.1).
9. Prove the formula
n
! n(n + 1)
i = 1 + 2 + ... + n =
i=1
2

either by mathematical induction or by following the insight of a 10-year


old schoolboy named Karl Friedrich Gauss (1777—1855) who grew up to
become one of the greatest mathematicians of all times.

17

"36
Ex 2.3, Problem 11
10. Consider the following version of an important algorithm that we will
study later in the book.
Algorithm GE (A[0..n − 1, 0..n])
//Input: An n-by-n + 1 matrix A[0..n − 1, 0..n] of real numbers
for i ← 0 to n − 2 do
for j ← i + 1 to n − 1 do
for k ← i to n do
A[j, k] ← A[j, k] − A[i, k] ∗ A[j, i] / A[i, i]
a.◃ Find the time efficiency class of this algorithm.
a. Find the time efficiency class of this algorithm
b.◃ What glaring inefficiency does this pseudocode contain and how can
b. beWhat
it glaringtoinefficiency
eliminated does this
speed the algorithm code contain, and how
up?
can it be eliminated?
11. von Neumann’s neighborhood How many one-by-one squares are gener-
c. Estimate
ated the reduction
by the algorithm in runwith
that starts time.
a single square square and on each
of its n iterations adds new squares all round the outside. How many
one-by-one squares are generated on the nth iteration? [Gar99, p.88] (In
"37
the parlance of cellular automata theory, the answer is the number of cells
a.◃ Find the time efficiency class of this algorithm.

Problem 11: von Neumann neighborhood


b.◃ What glaring inefficiency does this pseudocode contain and how can
it be eliminated to speed the algorithm up?

How
11. von many one-by-one
Neumann’s neighborhoodsquares
How manyare one-by-one
generatedsquares
by theare gener-
ofalgorithm that adds
startsnew
with a single square,
the and on each
How of its
ated by the algorithm that starts with a single square square and on each
its n iterations squares all round outside. many
n iterations
one-by-one adds
squares arenew squares
generated around
on the the outside.
nth iteration? How
[Gar99, many
p.88] (In
one-by-one
the squares
parlance of cellular are generated
automata theory, theon the nisththe
answer iteration?
number ofHere
cells
are2 the neighborhoods
below: for n = 0, 1, and 2.
in the von Neumann neighborhood of range n.) The results for n = 0, 1,
and are illustrated

n=0 n=1 n=2


"38

You might also like