2.6 Exercises¶
2.6.1 Check your understanding¶
1. Time and space costs of iteration and recursion¶
Both functions below compute \(1 + 2 + \dots + n\) for \(n \ge 1\). Set n to 4,
answer the questions by tracing the program in execution order, and then compare the efficiency of the two implementations.
- When the iterative function runs with
n = 4, what value does the accumulatorreshold after each iteration? - When the recursive function runs with
n = 4, what values does the parameterntake, in order? How is the result computed as the calls return, starting at the deepest level? - What are the time and space complexities of each implementation? Explain using the execution traces from questions 1 and 2.
Sample answer
-
The loop variable
itakes the successive values1, 2, 3, 4. At the end of each iteration, the running totalresis1, 3, 6, 10, respectively, so the iterative function returns 10. -
The parameter
ntakes the following values in order:4 → 3 → 2 → 1. The deepest call returns 1. As the remaining calls return, they compute2 + 1 = 3,3 + 3 = 6, and4 + 6 = 10in order. At the deepest level, all four function calls are still active. -
The number of iterations or calls is proportional to \(n\) in both implementations, so both have time complexity \(O(n)\). Their space complexities differ. The iterative version uses a constant number of variables, so its space complexity is \(O(1)\). In the recursive version, earlier calls wait for a result as recursion proceeds toward the base case. The call stack holds up to \(n\) calls at once, so the space complexity is \(O(n)\).
When analyzing space complexity, count the space used by recursive calls as well as the variables in the code.
2. Time complexity of three code snippets¶
Each of the following three snippets takes a positive integer \(n\) as input. Rank them by time complexity, from lowest to highest, and give the complexity of each.
def linear_loop(n: int) -> int:
"""Linear-time loop"""
res = 0
for i in range(n):
res += i
return res
def quadratic_loop(n: int) -> int:
"""Quadratic-time loop"""
res = 0
for i in range(n):
for j in range(i, n):
res += j
return res
def logarithmic_loop(n: int) -> int:
"""Logarithmic-time loop"""
while n > 1:
n //= 2
return n
/* Linear-time loop */
int linearLoop(int n) {
int res = 0;
for (int i = 0; i < n; ++i) {
res += i;
}
return res;
}
/* Quadratic-time loop */
int quadraticLoop(int n) {
int res = 0;
for (int i = 0; i < n; ++i) {
for (int j = i; j < n; ++j) {
res += j;
}
}
return res;
}
/* Logarithmic-time loop */
int logarithmicLoop(int n) {
while (n > 1) {
n /= 2;
}
return n;
}
/* Linear-time loop */
int linearLoop(int n) {
int res = 0;
for (int i = 0; i < n; i++) {
res += i;
}
return res;
}
/* Quadratic-time loop */
int quadraticLoop(int n) {
int res = 0;
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
res += j;
}
}
return res;
}
/* Logarithmic-time loop */
int logarithmicLoop(int n) {
while (n > 1) {
n /= 2;
}
return n;
}
/* Linear-time loop */
int LinearLoop(int n) {
int res = 0;
for (int i = 0; i < n; i++) {
res += i;
}
return res;
}
/* Quadratic-time loop */
int QuadraticLoop(int n) {
int res = 0;
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
res += j;
}
}
return res;
}
/* Logarithmic-time loop */
int LogarithmicLoop(int n) {
while (n > 1) {
n /= 2;
}
return n;
}
/* Linear-time loop */
func linearLoop(n int) int {
res := 0
for i := 0; i < n; i++ {
res += i
}
return res
}
/* Quadratic-time loop */
func quadraticLoop(n int) int {
res := 0
for i := 0; i < n; i++ {
for j := i; j < n; j++ {
res += j
}
}
return res
}
/* Logarithmic-time loop */
func logarithmicLoop(n int) int {
for n > 1 {
n /= 2
}
return n
}
/* Linear-time loop */
func linearLoop(n: Int) -> Int {
var res = 0
for i in 0 ..< n {
res += i
}
return res
}
/* Quadratic-time loop */
func quadraticLoop(n: Int) -> Int {
var res = 0
for i in 0 ..< n {
for j in i ..< n {
res += j
}
}
return res
}
/* Logarithmic-time loop */
func logarithmicLoop(n: Int) -> Int {
var n = n
while n > 1 {
n /= 2
}
return n
}
/* Linear-time loop */
function linearLoop(n) {
let res = 0;
for (let i = 0; i < n; i++) {
res += i;
}
return res;
}
/* Quadratic-time loop */
function quadraticLoop(n) {
let res = 0;
for (let i = 0; i < n; i++) {
for (let j = i; j < n; j++) {
res += j;
}
}
return res;
}
/* Logarithmic-time loop */
function logarithmicLoop(n) {
while (n > 1) {
n = Math.floor(n / 2);
}
return n;
}
/* Linear-time loop */
function linearLoop(n: number): number {
let res = 0;
for (let i = 0; i < n; i++) {
res += i;
}
return res;
}
/* Quadratic-time loop */
function quadraticLoop(n: number): number {
let res = 0;
for (let i = 0; i < n; i++) {
for (let j = i; j < n; j++) {
res += j;
}
}
return res;
}
/* Logarithmic-time loop */
function logarithmicLoop(n: number): number {
while (n > 1) {
n = Math.floor(n / 2);
}
return n;
}
/* Linear-time loop */
int linearLoop(int n) {
int res = 0;
for (int i = 0; i < n; i++) {
res += i;
}
return res;
}
/* Quadratic-time loop */
int quadraticLoop(int n) {
int res = 0;
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
res += j;
}
}
return res;
}
/* Logarithmic-time loop */
int logarithmicLoop(int n) {
while (n > 1) {
n ~/= 2;
}
return n;
}
/* Linear-time loop */
fn linear_loop(n: i32) -> i32 {
let mut res = 0;
for i in 0..n {
res += i;
}
res
}
/* Quadratic-time loop */
fn quadratic_loop(n: i32) -> i32 {
let mut res = 0;
for i in 0..n {
for j in i..n {
res += j;
}
}
res
}
/* Logarithmic-time loop */
fn logarithmic_loop(mut n: i32) -> i32 {
while n > 1 {
n /= 2;
}
n
}
/* Linear-time loop */
int linearLoop(int n) {
int res = 0;
for (int i = 0; i < n; i++) {
res += i;
}
return res;
}
/* Quadratic-time loop */
int quadraticLoop(int n) {
int res = 0;
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
res += j;
}
}
return res;
}
/* Logarithmic-time loop */
int logarithmicLoop(int n) {
while (n > 1) {
n /= 2;
}
return n;
}
/* Linear-time loop */
fun linearLoop(n: Int): Int {
var res = 0
for (i in 0 until n) {
res += i
}
return res
}
/* Quadratic-time loop */
fun quadraticLoop(n: Int): Int {
var res = 0
for (i in 0 until n) {
for (j in i until n) {
res += j
}
}
return res
}
/* Logarithmic-time loop */
fun logarithmicLoop(n: Int): Int {
var value = n
while (value > 1) {
value /= 2
}
return value
}
### Linear-time loop ###
def linear_loop(n)
res = 0
for i in 0...n
res += i
end
res
end
### Quadratic-time loop ###
def quadratic_loop(n)
res = 0
for i in 0...n
for j in i...n
res += j
end
end
res
end
### Logarithmic-time loop ###
def logarithmic_loop(n)
n /= 2 while n > 1
n
end
Sample answer
From lowest to highest, they are snippet 3 (\(O(\log n)\)), snippet 1 (\(O(n)\)), and snippet 2 (\(O(n^2)\)). Snippet 3 halves \(n\) on each iteration, so the loop runs about \(\log_2 n\) times. The loop in snippet 1 runs exactly \(n\) times. In snippet 2, the inner loop runs \(n,n-1,\dots,1\) times on successive passes, for a total of \(n(n+1)/2\) iterations. Its time complexity is therefore quadratic.
3. Which reversal uses less space?¶
Consider two ways to reverse all the elements of the array nums:
- Create an array
resof the same length, copy the elements into it in reverse order, and return it. -
Move two indices,
iandj, inward from the beginning and end, respectively, swappingnums[i]andnums[j]at each step.What is the space complexity of each approach? Which one reverses the array in place?
Sample answer
-
The first approach requires an auxiliary array as long as the input, so its space complexity is \(O(n)\).
-
The second approach uses only two index variables, so its space complexity is \(O(1)\) and it operates in place.
Reversing an array in place modifies the input. Prefer this approach only when the input may be modified. If you need to preserve the original array, the cost of copying in the first approach is unavoidable.
2.6.2 Programming exercises¶
1. Fibonacci number¶
The Fibonacci sequence satisfies \(F(0)=0\) and \(F(1)=1\), and for \(n\ge2\), \(F(n)=F(n-1)+F(n-2)\).
Given a nonnegative integer n, use a loop to compute and return \(F(n)\) without recursion.
Hints
- Handle n = 0 and n = 1 separately first.
- You need only the previous two terms to compute the next one. There is no need to store the whole sequence.
- When updating the two variables, keep each old value until you have finished using it.