Docsity

Recitation 1 solutions, Exercises of Electrical Engineering

Partial preview of the text

Download Recitation 1 solutions and more Exercises Electrical Engineering in PDF only on Docsity!

Recitation 1: Bounds

CS260 Data Structures

Galen Long

Summer 2024-

Problem 1

Prove each of the following bounds: a. 5 n^2 + 11 n^3 − 4 = Θ( n^3 ) b. 12 n + 7 = ω (

n ) c. log 2 n = O ( n ) ▶ (^) L’Hôpital’s rule: lim n →∞ f g^ (( nn )) = lim n →∞ f g^ ′′(( nn )) ▶ (^) Derivative of log 2 n = (^) ln 12 ∗ n

Problem 1 solutions

b. 12 n + 7 = ω (

n )

n lim→∞

12 n + 7 √ n = (^) n lim→∞ 12

n +

n = ∞ + 0 = ∞

(Remember that x^ a x b^ =^ x^

a − b (^) , √ x = x 0_._ (^5) , and x = x (^1) ).

Problem 1 solutions

c. log 2 n = O ( n ) Using L’Hôpital’s rule:

n^ lim→∞

log 2 n n

= (^) n lim→∞

= (^) n lim→∞

1 ln 2 ∗ n 1 = (^) n lim→∞

ln 2 n = 0

Proving a little-o bound also proves a big-O bound.

Example 1 solution

function series() { sum = 0 for (i=0; i<100; i++) { sum += i } return sum } ∑^99

i = 0

c = 100 c = O ( 1 )

Example 2

Model the runtime of the following function using summation(s) (if needed): // A, B are arrays of n integers function sumBoth(A, B, n) { sum = 0 for (i=0; i

Example 3

Model the runtime of the following function using summation(s) (if needed): function min(a, b, c) { // a, b, c are integers if (a < b) { if (a < c) { return a } else if (c < b) { return c } else { return b } } else { if (b < c) { return b } else if (c < a) { return c } else { return a } } } Simplify to a closed form and give a tight upper bound on the runtime.

Example 3 solution

function min(a, b, c) { // a, b, c are integers if (a < b) { if (a < c) { return a } else if (c < b) { return c } else { return b } } else { if (b < c) { return b } else if (c < a) { return c } else { return a } } } This function has no loops and only performs a constant number of operations. It runs in O ( 1 ) time.

Problem 2 solution

// n is an integer function weird(n) { x = 0 for (i=n; i>=1; i--) { for (j=1; j<=i; j++) { x = x - i * j } for (k=0; k