Exercises: Simulations, Algorithmic Efficiency, and Undecidable Problems
Engineers use simulations to test how a bridge responds to strong winds before building it. Give one benefit and one limitation of using simulations instead of…
Engineers use simulations to test how a bridge responds to strong winds before building it. Give one benefit and one limitation of using simulations instead of real-world experiments.
a) What are the possible values of RANDOM(1, 6)? b) How many different values can RANDOM(5, 15) return? c) Which call could simulate flipping a coin?
- a) What are the possible values of
RANDOM(1, 6)?
- b) How many different values can
RANDOM(5, 15)return?
- c) Which call could simulate flipping a coin?
An algorithm runs in reasonable time if the number of steps grows as a polynomial in the input size $n$, such as $n$ or $n^2$. It runs in unreasonable time if the number…
An algorithm runs in reasonable time if the number of steps grows as a polynomial in the input size $n$, such as $n$ or $n^2$. It runs in unreasonable time if the number of steps grows exponentially or faster. Classify each as reasonable or unreasonable:
- a) $n$ steps
- b) $n^2$ steps
- c) $2^n$ steps
- d) $n!$ steps
Compare the number of steps for two algorithms: | $n$ | $n^2$ | $2^n$ | |---|---|---| | 10 | 100 | 1,024 | | 20 | 400 | 1,048,576 | | 30 | 900 | 1,073,741,824 | a) Which…
Compare the number of steps for two algorithms:
| $n$ | $n^2$ | $2^n$ |
|---|---|---|
| 10 | 100 | 1,024 |
| 20 | 400 | 1,048,576 |
| 30 | 900 | 1,073,741,824 |
- a) Which algorithm is reasonable and which is unreasonable? Explain from the table.
- b) A computer performs 1 billion steps per second. About how long does the $2^{30}$ algorithm take?
- c) For $n = 60$, the exponential algorithm needs about $1.15 \times 10^{18}$ steps. About how many years is that at the same speed? Use $1\text{ year} \approx 3.15 \times 10^7$ seconds.
Liam simulates a basketball player who makes 70% of her free throws. a) What is the probability that each simulated shot is made? Explain. b) What range of values can be…
Liam simulates a basketball player who makes 70% of her free throws.
makes ← 0
REPEAT 10 TIMES
{
IF (RANDOM(1, 10) ≤ 7)
{
makes ← makes + 1
}
}
DISPLAY(makes)
- a) What is the probability that each simulated shot is made? Explain.
- b) What range of values can be displayed?
- c) Will the output be the same on every run? Why does running the simulation many times and averaging give a better estimate?
A software company claims it has built a tool that can analyze any program and tell with certainty whether it will eventually stop or run forever. a) Explain the…
A software company claims it has built a tool that can analyze any program and tell with certainty whether it will eventually stop or run forever.
- a) Explain the difference between a decidable and an undecidable problem.
- b) The problem described is known as the halting problem. Is the company's claim possible? Explain.
- c) Describe one practical thing developers can still do to catch programs that get stuck.
A delivery driver must visit $n$ cities and return home. Brute force checks every possible order. Starting at a fixed city, there are $(n-1)!$ routes. a) Compute the…
A delivery driver must visit $n$ cities and return home. Brute force checks every possible order. Starting at a fixed city, there are $(n-1)!$ routes.
- a) Compute the number of routes for $n = 5$ and $n = 10$. Then use $14! = 87{,}178{,}291{,}200$ for $n = 15$.
- b) A computer checks 1,000,000 routes per second. How long does it take for $n = 10$ and for $n = 15$? Give the second answer in hours, rounded to one decimal place.
- c) Is the brute-force algorithm reasonable or unreasonable? Describe a heuristic that gives a good, though not necessarily best, route, and explain the trade-off.
Two algorithms find out whether a list of $n$ values has a duplicate. Algorithm A compares every pair, using about $\dfrac{n(n-1)}{2}$ steps. Algorithm B sorts the list…
Two algorithms find out whether a list of $n$ values has a duplicate.
-
Algorithm A compares every pair, using about $\dfrac{n(n-1)}{2}$ steps.
-
Algorithm B sorts the list first and then checks neighbors, using about $10n$ steps.
- a) Compute the number of steps for each algorithm when $n = 10$ and when $n = 1{,}000$.
- b) For what value of $n$ do the two algorithms use the same number of steps? Which is faster for larger $n$?
- c) Are both algorithms reasonable? Explain. Then explain why the algorithm with fewer steps for small inputs is not always the best choice.