# Difference between revisions of "Chapter 2"

Jump to navigation
Jump to search

m (Protected "Chapter 2" ([Edit=Allow only administrators] (indefinite) [Move=Allow only administrators] (indefinite))) |
|||

(9 intermediate revisions by the same user not shown) | |||

Line 54: | Line 54: | ||

''print'' '*' | ''print'' '*' | ||

<math>x = 2x</math> | <math>x = 2x</math> | ||

− | :Let <math>f(n)</math> be the complexity of this algorithm (or equivalently the number of times * is printed). Proivde correct bounds for <math> O(f(n))</math>, and <math> | + | :Let <math>f(n)</math> be the complexity of this algorithm (or equivalently the number of times * is printed). Proivde correct bounds for <math> O(f(n))</math>, and <math>\Theta(f(n))</math>, ideally converging on <math>\Theta(f(n))</math>. |

[[2.5|Solution]] | [[2.5|Solution]] | ||

Line 111: | Line 111: | ||

:[[2.11]]. For each of the following functions, which of the following asymptotic bounds hold for <math>f(n) = O(g(n)),\Theta(g(n)),\Omega(g(n))</math>? | :[[2.11]]. For each of the following functions, which of the following asymptotic bounds hold for <math>f(n) = O(g(n)),\Theta(g(n)),\Omega(g(n))</math>? | ||

+ | #<math>f(n) = 3n^2, g(n) = n^2</math> | ||

+ | #<math>f(n) = 2n^4 - 3n^2 + 7, g(n) = n^5</math> | ||

+ | #<math>f(n) = log n, g(n) = log n + 1/n</math> | ||

+ | #<math>f(n) = 2^{klog n}, g(n) = n^k</math> | ||

+ | #<math>f(n) = 2^n, g(n) = 2^{2n}</math> | ||

[[2.11|Solution]] | [[2.11|Solution]] | ||

Line 178: | Line 183: | ||

− | :2.24 | + | :2.24. List the functions below from lowest to highest order. If any two or more are of the same order, indicate which. |

+ | <center> | ||

+ | <math>\begin{array}{llll} | ||

+ | n^{\pi} & \pi^n & \binom{n}{5} & \sqrt{2\sqrt{n}} \\ | ||

+ | \binom{n}{n-4} & 2^{log^4n} & n^{5(logn)^2} & n^4\binom{n}{n-4} | ||

+ | \\ | ||

+ | \end{array}</math> | ||

+ | </center> | ||

− | :[[2.25]] | + | :[[2.25]]. List the functions below from lowest to highest order. If any two or more are of the same order, indicate which. |

+ | <center> | ||

+ | <math>\begin{array}{llll} | ||

+ | \sum_{i=1}^n i^i & n^n & (log n)^{log n} & 2^{(log n^2)}\\ | ||

+ | n! & 2^{log^4n} & n^{(log n)^2} & n^4 \binom{n}{n-4}\\ | ||

+ | \end{array}</math> | ||

+ | </center> | ||

+ | |||

[[2.25|Solution]] | [[2.25|Solution]] | ||

Line 227: | Line 246: | ||

:2.30. For each of these questions, briefly explain your answer. | :2.30. For each of these questions, briefly explain your answer. | ||

− | |||

::(a) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on some inputs? | ::(a) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on some inputs? | ||

− | |||

::(b) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on all inputs? | ::(b) If I prove that an algorithm takes <math>O(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on all inputs? | ||

− | |||

::(c) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on some inputs? | ::(c) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on some inputs? | ||

− | |||

::(d) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on all inputs? | ::(d) If I prove that an algorithm takes <math>\Theta(n^2)</math> worst-case time, is it possible that it takes <math>O(n)</math> on all inputs? | ||

− | |||

::(e) Is the function <math>f(n) = \Theta(n^2)</math>, where <math>f(n) = 100 n^2</math> for even <math>n</math> and <math>f(n) = 20 n^2 - n \log_2 n</math> for odd <math>n</math>? | ::(e) Is the function <math>f(n) = \Theta(n^2)</math>, where <math>f(n) = 100 n^2</math> for even <math>n</math> and <math>f(n) = 20 n^2 - n \log_2 n</math> for odd <math>n</math>? | ||

:[[2.31]]. For each of the following, answer ''yes'', ''no'', or ''can't tell''. Explain your reasoning. | :[[2.31]]. For each of the following, answer ''yes'', ''no'', or ''can't tell''. Explain your reasoning. | ||

− | |||

::(a) Is <math>3^n = O(2^n)</math>? | ::(a) Is <math>3^n = O(2^n)</math>? | ||

− | |||

::(b) Is <math>\log 3^n = O( \log 2^n )</math>? | ::(b) Is <math>\log 3^n = O( \log 2^n )</math>? | ||

− | |||

::(c) Is <math>3^n = \Omega(2^n)</math>? | ::(c) Is <math>3^n = \Omega(2^n)</math>? | ||

− | |||

::(d) Is <math>\log 3^n = \Omega( \log 2^n )</math>? | ::(d) Is <math>\log 3^n = \Omega( \log 2^n )</math>? | ||

Line 376: | Line 386: | ||

− | :2.48 | + | :2.48. You are given a set <math>S</math> of <math>n</math> numbers. You must pick a subset <math>S'</math> of <math>k</math> numbers from <math>S</math> such that the probability of each element of <math>S</math> occurring in <math>S'</math> is equal (i.e., each is selected with probability <math>k/n</math>). You may make only one pass over the numbers. What if <math>n</math> is unknown? |

+ | |||

+ | |||

+ | :[[2.49]]. We have 1,000 data items to store on 1,000 nodes. Each node can store copies of exactly three different items. Propose a replication scheme to minimize data loss as nodes fail. What is the expected number of data entries that get lost when three random nodes fail? | ||

+ | |||

+ | [[2.49|Solution]] | ||

+ | |||

+ | |||

+ | :2.50. Consider the following algorithm to find the minimum element in an array of numbers <math>A[0, \ldots, n]</math>. One extra variable <math>tmp</math> is allocated to hold the current minimum value. Start from A[0]; "tmp" is compared against <math>A[1]</math>, | ||

+ | <math>A[2]</math>, <math>\ldots</math>, <math>A[N]</math> in order. When <math>A[i]<tmp</math>, <math>tmp = A[i]</math>. What is the expected number of times that the assignment operation <math>tmp = A[i]</math> is performed? | ||

+ | |||

+ | |||

+ | :[[2.51]]. You are given ten bags of gold coins. Nine bags contain coins that each weigh 10 grams. One bag contains all false coins that weigh 1 gram less. You must identify this bag in just one weighing. You have a digital balance that reports the weight of what is placed on it. | ||

+ | |||

+ | [[2.51|Solution]] | ||

+ | |||

+ | |||

+ | :2.52. You have eight balls all of the same size. Seven of them weigh the same, and one of them weighs slightly more. How can you find the ball that is heavier by using a balance and only two weightings? | ||

+ | |||

− | :2. | + | :[[2.53]]. Suppose we start with <math>n</math> companies that eventually merge into one big company. How many different ways are there for them to merge? |

− | + | [[2.53|Solution]] | |

− | |||

− | :2. | + | :2.54. Six pirates must divide $300 among themselves. The division is to proceed as follows. The senior pirate proposes a way to divide the money. Then the pirates vote. If the senior pirate gets at least half the votes he wins, and that division remains. If he doesn’t, he is killed and then the next senior-most pirate gets a chance to propose the division. Now tell what will happen and why (i.e. how many pirates survive and how the division is done)? All the pirates are intelligent and the first priority is to stay alive and the next priority is to get as much money as possible. |

− | |||

− | :2. | + | :[[2.55]]. Reconsider the pirate problem above, where we start with only one indivisible dollar. Who gets the dollar, and how many are killed? |

− | + | [[2.55|Solution]] | |

Back to [[Chapter List]] | Back to [[Chapter List]] |

## Latest revision as of 18:11, 1 October 2020

## Contents

# Algorithm Analysis

### Program Analysis

- 2.1. What value is returned by the following function? Express your answer as a function of . Give the worst-case running time using the Big Oh notation.

mystery(n) r:=0fori:=1ton-1doforj:=i+1tondofork:=1tojdor:=r+1return(r)

- 2.2. What value is returned by the following function? Express your answer as a function of . Give the worst-case running time using Big Oh notation.

pesky(n) r:=0fori:=1tondoforj:=1toidofork:=jtoi+jdor:=r+1return(r)

- 2.3. What value is returned by the following function? Express your answer as a function of . Give the worst-case running time using Big Oh notation.

prestiferous(n) r:=0fori:=1tondoforj:=1toidofork:=jtoi+jdoforl:=1toi+j-kdor:=r+1return(r)

- 2.4. What value is returned by the following function? Express your answer as a function of . Give the worst-case running time using Big Oh notation.

conundrum()fortodofortodofortodoreturn(r)

- 2.5. Consider the following algorithm: (the print operation prints a single asterisk; the operation doubles the value of the variable ).

fortowhile():

- Let be the complexity of this algorithm (or equivalently the number of times * is printed). Proivde correct bounds for , and , ideally converging on .

- 2.6. Suppose the following algorithm is used to evaluate the polynomial

for to do

- How many multiplications are done in the worst-case? How many additions?
- How many multiplications are done on the average?
- Can you improve this algorithm?

- 2.7. Prove that the following algorithm for computing the maximum value in an array is correct.

max(A)fortondoifthenreturn(m)

### Big Oh

- 2.8. True or False?

- Is ?
- Is ?

- 2.9. For each of the following pairs of functions, either is in , is in , or . Determine which relationship is correct and briefly explain why.

- ; +
- ;
- ;
- ;
- ;
- ;
- ;
- ;

- 2.10. For each of the following pairs of functions and , determine whether , , or both.

- ,
- ,
- ,
- ,
- ,
- ,

- 2.11. For each of the following functions, which of the following asymptotic bounds hold for ?

- 2.12. Prove that .

- 2.13. Prove that .

- 2.14. Prove or disprove: .

- 2.15. Suppose you have algorithms with the five running times listed below. (Assume these are the exact running times.) How much slower do each of these inputs get when you (a) double the input size, or (b) increase the input size by one?
- (a) (b) (c) (d) (e)

- 2.16. Suppose you have algorithms with the six running times listed below. (Assume these are the exact number of operations performed as a function of input size .)Suppose you have a computer that can perform operations per second. For each algorithm, what is the largest input size n that you can complete within an hour?
- (a) (b) (c) (d) (e) (f)

- 2.17. For each of the following pairs of functions and , give an appropriate positive constant such that for all .

- ,
- ,
- ,

- 2.18. Prove that if and , then .

- 2.19. Prove that if and , then .

- 2.20. Prove that if and , then

- 2.21. Prove for all and all sets of constants ,

- 2.22. Show that for any real constants and ,

- 2.23. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which.

- 2.24. List the functions below from lowest to highest order. If any two or more are of the same order, indicate which.

- 2.25. List the functions below from lowest to highest order. If any two or more are of the same order, indicate which.

- 2.26. List the functions below from the lowest to the highest order. If any two or more are of the same order, indicate which.

- 2.27. Find two functions and that satisfy the following relationship. If no such and exist, write
*None.*

- and
- and
- and
- and

- 2.28. True or False?

- 2.29. For each of the following pairs of functions and , state whether , , , or none of the above.

- ,
- ,
- ,

- 2.30. For each of these questions, briefly explain your answer.
- (a) If I prove that an algorithm takes worst-case time, is it possible that it takes on some inputs?
- (b) If I prove that an algorithm takes worst-case time, is it possible that it takes on all inputs?
- (c) If I prove that an algorithm takes worst-case time, is it possible that it takes on some inputs?
- (d) If I prove that an algorithm takes worst-case time, is it possible that it takes on all inputs?
- (e) Is the function , where for even and for odd ?

- 2.31. For each of the following, answer
*yes*,*no*, or*can't tell*. Explain your reasoning.- (a) Is ?
- (b) Is ?
- (c) Is ?
- (d) Is ?

- 2.32. For each of the following expressions find a simple such that .

- .
- .
- .
- .

- 2.33. Place the following functions into increasing asymptotic order.
- , , , .

- 2.34. Which of the following are true?

- .
- .
- .

- 2.35. For each of the following functions find a simple function such that .

- .
- .
- .

- 2.36. For each pair of expressions below, indicate whether is , , , , or of . Note that zero, one or more of these relations may hold for a given pair; list all correct ones.

### Summations

- 2.37. Find an expression for the sum of the th row of the following triangle, and prove its correctness. Each entry is the sum of the three entries directly above it. All non existing entries are considered 0.

- 2.38. Assume that Christmas has days. Exactly how many presents did my
*true love*send me? (Do some research if you do not understand this question.)

- 2.40. Consider the following code fragment.

`
`

for i=1 to n do for j=i to 2*i do outputfoobar

`
`

- Let denote the number of times `foobar' is printed as a function of .

- Express as a summation (actually two nested summations).
- Simplify the summation. Show your work.

- 2.41.Consider the following code fragment.

`
`

for i=1 to n/2 do for j=i to n-i do for k=1 to j do outputfoobar

`
`

- Assume is even. Let denote the number of times `foobar' is printed as a function of .

- Express as three nested summations.
- Simplify the summation. Show your work.

- 2.42. When you first learned to multiply numbers, you were told that means adding a total of times, so . What is the time complexity of multiplying two -digit numbers in base (people work in base 10, of course, while computers work in base 2) using the repeated addition method, as a function of and . Assume that single-digit by single-digit addition or multiplication takes time. (Hint: how big can be as a function of and ?)

- 2.43. In grade school, you learned to multiply long numbers on a digit-by-digit basis, so that . Analyze the time complexity of multiplying two -digit numbers with this method as a function of (assume constant base size). Assume that single-digit by single-digit addition or multiplication takes time.

### Logartihms

- 2.44. Prove the following identities on logarithms:

- Prove that
- Prove that
- Prove that
- Prove that

- 2.45. Show that

- 2.46. Prove that that the binary representation of has + bits.

- 2.47. In one of my research papers I give a comparison-based sorting algorithm that runs in . Given the existence of an lower bound for sorting, how can this be possible?

### Interview Problems

- 2.48. You are given a set of numbers. You must pick a subset of numbers from such that the probability of each element of occurring in is equal (i.e., each is selected with probability ). You may make only one pass over the numbers. What if is unknown?

- 2.49. We have 1,000 data items to store on 1,000 nodes. Each node can store copies of exactly three different items. Propose a replication scheme to minimize data loss as nodes fail. What is the expected number of data entries that get lost when three random nodes fail?

- 2.50. Consider the following algorithm to find the minimum element in an array of numbers . One extra variable is allocated to hold the current minimum value. Start from A[0]; "tmp" is compared against ,

, , in order. When , . What is the expected number of times that the assignment operation is performed?

- 2.51. You are given ten bags of gold coins. Nine bags contain coins that each weigh 10 grams. One bag contains all false coins that weigh 1 gram less. You must identify this bag in just one weighing. You have a digital balance that reports the weight of what is placed on it.

- 2.52. You have eight balls all of the same size. Seven of them weigh the same, and one of them weighs slightly more. How can you find the ball that is heavier by using a balance and only two weightings?

- 2.53. Suppose we start with companies that eventually merge into one big company. How many different ways are there for them to merge?

- 2.54. Six pirates must divide $300 among themselves. The division is to proceed as follows. The senior pirate proposes a way to divide the money. Then the pirates vote. If the senior pirate gets at least half the votes he wins, and that division remains. If he doesn’t, he is killed and then the next senior-most pirate gets a chance to propose the division. Now tell what will happen and why (i.e. how many pirates survive and how the division is done)? All the pirates are intelligent and the first priority is to stay alive and the next priority is to get as much money as possible.

- 2.55. Reconsider the pirate problem above, where we start with only one indivisible dollar. Who gets the dollar, and how many are killed?

Back to Chapter List