fundamental algorithms · remark: the value of fn grows very fast as a function of n. it can be...
TRANSCRIPT
![Page 1: Fundamental Algorithms · Remark: The value of fn grows very fast as a function of n. It can be shown that fn ≥ 2⌊ n−1 2 ⌋. For example, f100 = 3.54·1020 f1000 = 4.53·10208](https://reader034.vdocuments.net/reader034/viewer/2022042302/5eccf84f7f03df4e9b7be2be/html5/thumbnails/1.jpg)
WS 2007/2008
Fundamental Algorithms
Dmytro Chibisov, Jens Ernst
Fakultat fur Informatik
TU Munchen
http://www14.in.tum.de/lehre/2007WS/fa-cse/
Fall Semester 2007
![Page 2: Fundamental Algorithms · Remark: The value of fn grows very fast as a function of n. It can be shown that fn ≥ 2⌊ n−1 2 ⌋. For example, f100 = 3.54·1020 f1000 = 4.53·10208](https://reader034.vdocuments.net/reader034/viewer/2022042302/5eccf84f7f03df4e9b7be2be/html5/thumbnails/2.jpg)
Remark: The value of fn grows very fast as a function of n. It canbe shown that
fn ≥ 2⌊n−1
2⌋.
For example,
f100 = 3.54 · 1020
f1000 = 4.53 · 10208
· · ·If one arithmetic operation costs 1µs, computing f1000 by thisalgorithm takes 4.3 · 10195 years.
![Page 3: Fundamental Algorithms · Remark: The value of fn grows very fast as a function of n. It can be shown that fn ≥ 2⌊ n−1 2 ⌋. For example, f100 = 3.54·1020 f1000 = 4.53·10208](https://reader034.vdocuments.net/reader034/viewer/2022042302/5eccf84f7f03df4e9b7be2be/html5/thumbnails/3.jpg)
Remark: The value of fn grows very fast as a function of n. It canbe shown that
fn ≥ 2⌊n−1
2⌋.
For example,
f100 = 3.54 · 1020
f1000 = 4.53 · 10208
· · ·If one arithmetic operation costs 1µs, computing f1000 by thisalgorithm takes 4.3 · 10195 years.
![Page 4: Fundamental Algorithms · Remark: The value of fn grows very fast as a function of n. It can be shown that fn ≥ 2⌊ n−1 2 ⌋. For example, f100 = 3.54·1020 f1000 = 4.53·10208](https://reader034.vdocuments.net/reader034/viewer/2022042302/5eccf84f7f03df4e9b7be2be/html5/thumbnails/4.jpg)
Remark: The value of fn grows very fast as a function of n. It canbe shown that
fn ≥ 2⌊n−1
2⌋.
For example,
f100 = 3.54 · 1020
f1000 = 4.53 · 10208
· · ·If one arithmetic operation costs 1µs, computing f1000 by thisalgorithm takes 4.3 · 10195 years.
![Page 5: Fundamental Algorithms · Remark: The value of fn grows very fast as a function of n. It can be shown that fn ≥ 2⌊ n−1 2 ⌋. For example, f100 = 3.54·1020 f1000 = 4.53·10208](https://reader034.vdocuments.net/reader034/viewer/2022042302/5eccf84f7f03df4e9b7be2be/html5/thumbnails/5.jpg)
Remark: The value of fn grows very fast as a function of n. It canbe shown that
fn ≥ 2⌊n−1
2⌋.
For example,
f100 = 3.54 · 1020
f1000 = 4.53 · 10208
· · ·If one arithmetic operation costs 1µs, computing f1000 by thisalgorithm takes 4.3 · 10195 years.
![Page 6: Fundamental Algorithms · Remark: The value of fn grows very fast as a function of n. It can be shown that fn ≥ 2⌊ n−1 2 ⌋. For example, f100 = 3.54·1020 f1000 = 4.53·10208](https://reader034.vdocuments.net/reader034/viewer/2022042302/5eccf84f7f03df4e9b7be2be/html5/thumbnails/6.jpg)
Remark: The value of fn grows very fast as a function of n. It canbe shown that
fn ≥ 2⌊n−1
2⌋.
For example,
f100 = 3.54 · 1020
f1000 = 4.53 · 10208
· · ·If one arithmetic operation costs 1µs, computing f1000 by thisalgorithm takes 4.3 · 10195 years.
![Page 7: Fundamental Algorithms · Remark: The value of fn grows very fast as a function of n. It can be shown that fn ≥ 2⌊ n−1 2 ⌋. For example, f100 = 3.54·1020 f1000 = 4.53·10208](https://reader034.vdocuments.net/reader034/viewer/2022042302/5eccf84f7f03df4e9b7be2be/html5/thumbnails/7.jpg)
Remark: The value of fn grows very fast as a function of n. It canbe shown that
fn ≥ 2⌊n−1
2⌋.
For example,
f100 = 3.54 · 1020
f1000 = 4.53 · 10208
· · ·If one arithmetic operation costs 1µs, computing f1000 by thisalgorithm takes 4.3 · 10195 years.
![Page 8: Fundamental Algorithms · Remark: The value of fn grows very fast as a function of n. It can be shown that fn ≥ 2⌊ n−1 2 ⌋. For example, f100 = 3.54·1020 f1000 = 4.53·10208](https://reader034.vdocuments.net/reader034/viewer/2022042302/5eccf84f7f03df4e9b7be2be/html5/thumbnails/8.jpg)
Wy is this simple “top-down” algorithm so inefficient?
The reason lies in the repeated identical calls of the function f .
Example 1
f(6) calls f(5) and f(4).
In f(5), the call f(2) occurs 3 times
In f(4), the call f(2) occurs twice
Ideally, identical calls should not be repeated. This suggests that a“bottom-up” approach which memorizes previously computedresults might be more efficient.
![Page 9: Fundamental Algorithms · Remark: The value of fn grows very fast as a function of n. It can be shown that fn ≥ 2⌊ n−1 2 ⌋. For example, f100 = 3.54·1020 f1000 = 4.53·10208](https://reader034.vdocuments.net/reader034/viewer/2022042302/5eccf84f7f03df4e9b7be2be/html5/thumbnails/9.jpg)
Wy is this simple “top-down” algorithm so inefficient?
The reason lies in the repeated identical calls of the function f .
Example 1
f(6) calls f(5) and f(4).
In f(5), the call f(2) occurs 3 times
In f(4), the call f(2) occurs twice
Ideally, identical calls should not be repeated. This suggests that a“bottom-up” approach which memorizes previously computedresults might be more efficient.
![Page 10: Fundamental Algorithms · Remark: The value of fn grows very fast as a function of n. It can be shown that fn ≥ 2⌊ n−1 2 ⌋. For example, f100 = 3.54·1020 f1000 = 4.53·10208](https://reader034.vdocuments.net/reader034/viewer/2022042302/5eccf84f7f03df4e9b7be2be/html5/thumbnails/10.jpg)
Wy is this simple “top-down” algorithm so inefficient?
The reason lies in the repeated identical calls of the function f .
Example 1
f(6) calls f(5) and f(4).
In f(5), the call f(2) occurs 3 times
In f(4), the call f(2) occurs twice
Ideally, identical calls should not be repeated. This suggests that a“bottom-up” approach which memorizes previously computedresults might be more efficient.
![Page 11: Fundamental Algorithms · Remark: The value of fn grows very fast as a function of n. It can be shown that fn ≥ 2⌊ n−1 2 ⌋. For example, f100 = 3.54·1020 f1000 = 4.53·10208](https://reader034.vdocuments.net/reader034/viewer/2022042302/5eccf84f7f03df4e9b7be2be/html5/thumbnails/11.jpg)
Wy is this simple “top-down” algorithm so inefficient?
The reason lies in the repeated identical calls of the function f .
Example 1
f(6) calls f(5) and f(4).
In f(5), the call f(2) occurs 3 times
In f(4), the call f(2) occurs twice
Ideally, identical calls should not be repeated. This suggests that a“bottom-up” approach which memorizes previously computedresults might be more efficient.
![Page 12: Fundamental Algorithms · Remark: The value of fn grows very fast as a function of n. It can be shown that fn ≥ 2⌊ n−1 2 ⌋. For example, f100 = 3.54·1020 f1000 = 4.53·10208](https://reader034.vdocuments.net/reader034/viewer/2022042302/5eccf84f7f03df4e9b7be2be/html5/thumbnails/12.jpg)
Wy is this simple “top-down” algorithm so inefficient?
The reason lies in the repeated identical calls of the function f .
Example 1
f(6) calls f(5) and f(4).
In f(5), the call f(2) occurs 3 times
In f(4), the call f(2) occurs twice
Ideally, identical calls should not be repeated. This suggests that a“bottom-up” approach which memorizes previously computedresults might be more efficient.
![Page 13: Fundamental Algorithms · Remark: The value of fn grows very fast as a function of n. It can be shown that fn ≥ 2⌊ n−1 2 ⌋. For example, f100 = 3.54·1020 f1000 = 4.53·10208](https://reader034.vdocuments.net/reader034/viewer/2022042302/5eccf84f7f03df4e9b7be2be/html5/thumbnails/13.jpg)
Wy is this simple “top-down” algorithm so inefficient?
The reason lies in the repeated identical calls of the function f .
Example 1
f(6) calls f(5) and f(4).
In f(5), the call f(2) occurs 3 times
In f(4), the call f(2) occurs twice
Ideally, identical calls should not be repeated. This suggests that a“bottom-up” approach which memorizes previously computedresults might be more efficient.
![Page 14: Fundamental Algorithms · Remark: The value of fn grows very fast as a function of n. It can be shown that fn ≥ 2⌊ n−1 2 ⌋. For example, f100 = 3.54·1020 f1000 = 4.53·10208](https://reader034.vdocuments.net/reader034/viewer/2022042302/5eccf84f7f03df4e9b7be2be/html5/thumbnails/14.jpg)
Wy is this simple “top-down” algorithm so inefficient?
The reason lies in the repeated identical calls of the function f .
Example 1
f(6) calls f(5) and f(4).
In f(5), the call f(2) occurs 3 times
In f(4), the call f(2) occurs twice
Ideally, identical calls should not be repeated. This suggests that a“bottom-up” approach which memorizes previously computedresults might be more efficient.
![Page 15: Fundamental Algorithms · Remark: The value of fn grows very fast as a function of n. It can be shown that fn ≥ 2⌊ n−1 2 ⌋. For example, f100 = 3.54·1020 f1000 = 4.53·10208](https://reader034.vdocuments.net/reader034/viewer/2022042302/5eccf84f7f03df4e9b7be2be/html5/thumbnails/15.jpg)
0.1 2nd Algorithm for Computing Fibonacci Numbers
In this algorithm we compute the sequence f1, f2, f3, . . . , fn−1, fn
in this ascending order.
At each point in time, we store those previous results that arestill needed to compute the next element of the sequence.
For step k (where f1, f2, . . . , fk−1 are already known), weshould have the values fk−2 and fk−1 in memory. Let us callthese values x and y, respectively.
To compute z := fk, all we need to do is z := x + y.
For the next step, we set x := y and y := z and start over.
![Page 16: Fundamental Algorithms · Remark: The value of fn grows very fast as a function of n. It can be shown that fn ≥ 2⌊ n−1 2 ⌋. For example, f100 = 3.54·1020 f1000 = 4.53·10208](https://reader034.vdocuments.net/reader034/viewer/2022042302/5eccf84f7f03df4e9b7be2be/html5/thumbnails/16.jpg)
0.1 2nd Algorithm for Computing Fibonacci Numbers
In this algorithm we compute the sequence f1, f2, f3, . . . , fn−1, fn
in this ascending order.
At each point in time, we store those previous results that arestill needed to compute the next element of the sequence.
For step k (where f1, f2, . . . , fk−1 are already known), weshould have the values fk−2 and fk−1 in memory. Let us callthese values x and y, respectively.
To compute z := fk, all we need to do is z := x + y.
For the next step, we set x := y and y := z and start over.
![Page 17: Fundamental Algorithms · Remark: The value of fn grows very fast as a function of n. It can be shown that fn ≥ 2⌊ n−1 2 ⌋. For example, f100 = 3.54·1020 f1000 = 4.53·10208](https://reader034.vdocuments.net/reader034/viewer/2022042302/5eccf84f7f03df4e9b7be2be/html5/thumbnails/17.jpg)
0.1 2nd Algorithm for Computing Fibonacci Numbers
In this algorithm we compute the sequence f1, f2, f3, . . . , fn−1, fn
in this ascending order.
At each point in time, we store those previous results that arestill needed to compute the next element of the sequence.
For step k (where f1, f2, . . . , fk−1 are already known), weshould have the values fk−2 and fk−1 in memory. Let us callthese values x and y, respectively.
To compute z := fk, all we need to do is z := x + y.
For the next step, we set x := y and y := z and start over.
![Page 18: Fundamental Algorithms · Remark: The value of fn grows very fast as a function of n. It can be shown that fn ≥ 2⌊ n−1 2 ⌋. For example, f100 = 3.54·1020 f1000 = 4.53·10208](https://reader034.vdocuments.net/reader034/viewer/2022042302/5eccf84f7f03df4e9b7be2be/html5/thumbnails/18.jpg)
0.1 2nd Algorithm for Computing Fibonacci Numbers
In this algorithm we compute the sequence f1, f2, f3, . . . , fn−1, fn
in this ascending order.
At each point in time, we store those previous results that arestill needed to compute the next element of the sequence.
For step k (where f1, f2, . . . , fk−1 are already known), weshould have the values fk−2 and fk−1 in memory. Let us callthese values x and y, respectively.
To compute z := fk, all we need to do is z := x + y.
For the next step, we set x := y and y := z and start over.
![Page 19: Fundamental Algorithms · Remark: The value of fn grows very fast as a function of n. It can be shown that fn ≥ 2⌊ n−1 2 ⌋. For example, f100 = 3.54·1020 f1000 = 4.53·10208](https://reader034.vdocuments.net/reader034/viewer/2022042302/5eccf84f7f03df4e9b7be2be/html5/thumbnails/19.jpg)
0.1 2nd Algorithm for Computing Fibonacci Numbers
In this algorithm we compute the sequence f1, f2, f3, . . . , fn−1, fn
in this ascending order.
At each point in time, we store those previous results that arestill needed to compute the next element of the sequence.
For step k (where f1, f2, . . . , fk−1 are already known), weshould have the values fk−2 and fk−1 in memory. Let us callthese values x and y, respectively.
To compute z := fk, all we need to do is z := x + y.
For the next step, we set x := y and y := z and start over.
![Page 20: Fundamental Algorithms · Remark: The value of fn grows very fast as a function of n. It can be shown that fn ≥ 2⌊ n−1 2 ⌋. For example, f100 = 3.54·1020 f1000 = 4.53·10208](https://reader034.vdocuments.net/reader034/viewer/2022042302/5eccf84f7f03df4e9b7be2be/html5/thumbnails/20.jpg)
Algorithm:
unsigned f(unsigned n){if (n ≤ 2) then return 1else{
x := 1y := 1for i := 3 to n do
z := x + y
x := y
y := z
odreturn z}
fi}
![Page 21: Fundamental Algorithms · Remark: The value of fn grows very fast as a function of n. It can be shown that fn ≥ 2⌊ n−1 2 ⌋. For example, f100 = 3.54·1020 f1000 = 4.53·10208](https://reader034.vdocuments.net/reader034/viewer/2022042302/5eccf84f7f03df4e9b7be2be/html5/thumbnails/21.jpg)
The complexity of this algorithm is easy to see. Let titer(n) be thenumber of arithmetic operations performed as a function of theinput value n. Then it holds that
titer(1) = 0
titer(2) = 0
titer(n) = n − 2 for n ≥ 3.
Assuming again that one operation takes 1µs, it now takes0.001sec. to compute f1000.Remark: This iterative algorithm uses a very restricted form ofdynamic programming. We will see more of this later in this course.
![Page 22: Fundamental Algorithms · Remark: The value of fn grows very fast as a function of n. It can be shown that fn ≥ 2⌊ n−1 2 ⌋. For example, f100 = 3.54·1020 f1000 = 4.53·10208](https://reader034.vdocuments.net/reader034/viewer/2022042302/5eccf84f7f03df4e9b7be2be/html5/thumbnails/22.jpg)
The complexity of this algorithm is easy to see. Let titer(n) be thenumber of arithmetic operations performed as a function of theinput value n. Then it holds that
titer(1) = 0
titer(2) = 0
titer(n) = n − 2 for n ≥ 3.
Assuming again that one operation takes 1µs, it now takes0.001sec. to compute f1000.Remark: This iterative algorithm uses a very restricted form ofdynamic programming. We will see more of this later in this course.
![Page 23: Fundamental Algorithms · Remark: The value of fn grows very fast as a function of n. It can be shown that fn ≥ 2⌊ n−1 2 ⌋. For example, f100 = 3.54·1020 f1000 = 4.53·10208](https://reader034.vdocuments.net/reader034/viewer/2022042302/5eccf84f7f03df4e9b7be2be/html5/thumbnails/23.jpg)
The complexity of this algorithm is easy to see. Let titer(n) be thenumber of arithmetic operations performed as a function of theinput value n. Then it holds that
titer(1) = 0
titer(2) = 0
titer(n) = n − 2 for n ≥ 3.
Assuming again that one operation takes 1µs, it now takes0.001sec. to compute f1000.Remark: This iterative algorithm uses a very restricted form ofdynamic programming. We will see more of this later in this course.
![Page 24: Fundamental Algorithms · Remark: The value of fn grows very fast as a function of n. It can be shown that fn ≥ 2⌊ n−1 2 ⌋. For example, f100 = 3.54·1020 f1000 = 4.53·10208](https://reader034.vdocuments.net/reader034/viewer/2022042302/5eccf84f7f03df4e9b7be2be/html5/thumbnails/24.jpg)
The complexity of this algorithm is easy to see. Let titer(n) be thenumber of arithmetic operations performed as a function of theinput value n. Then it holds that
titer(1) = 0
titer(2) = 0
titer(n) = n − 2 for n ≥ 3.
Assuming again that one operation takes 1µs, it now takes0.001sec. to compute f1000.Remark: This iterative algorithm uses a very restricted form ofdynamic programming. We will see more of this later in this course.
![Page 25: Fundamental Algorithms · Remark: The value of fn grows very fast as a function of n. It can be shown that fn ≥ 2⌊ n−1 2 ⌋. For example, f100 = 3.54·1020 f1000 = 4.53·10208](https://reader034.vdocuments.net/reader034/viewer/2022042302/5eccf84f7f03df4e9b7be2be/html5/thumbnails/25.jpg)
The complexity of this algorithm is easy to see. Let titer(n) be thenumber of arithmetic operations performed as a function of theinput value n. Then it holds that
titer(1) = 0
titer(2) = 0
titer(n) = n − 2 for n ≥ 3.
Assuming again that one operation takes 1µs, it now takes0.001sec. to compute f1000.Remark: This iterative algorithm uses a very restricted form ofdynamic programming. We will see more of this later in this course.
![Page 26: Fundamental Algorithms · Remark: The value of fn grows very fast as a function of n. It can be shown that fn ≥ 2⌊ n−1 2 ⌋. For example, f100 = 3.54·1020 f1000 = 4.53·10208](https://reader034.vdocuments.net/reader034/viewer/2022042302/5eccf84f7f03df4e9b7be2be/html5/thumbnails/26.jpg)
The complexity of this algorithm is easy to see. Let titer(n) be thenumber of arithmetic operations performed as a function of theinput value n. Then it holds that
titer(1) = 0
titer(2) = 0
titer(n) = n − 2 for n ≥ 3.
Assuming again that one operation takes 1µs, it now takes0.001sec. to compute f1000.Remark: This iterative algorithm uses a very restricted form ofdynamic programming. We will see more of this later in this course.
![Page 27: Fundamental Algorithms · Remark: The value of fn grows very fast as a function of n. It can be shown that fn ≥ 2⌊ n−1 2 ⌋. For example, f100 = 3.54·1020 f1000 = 4.53·10208](https://reader034.vdocuments.net/reader034/viewer/2022042302/5eccf84f7f03df4e9b7be2be/html5/thumbnails/27.jpg)
The complexity of this algorithm is easy to see. Let titer(n) be thenumber of arithmetic operations performed as a function of theinput value n. Then it holds that
titer(1) = 0
titer(2) = 0
titer(n) = n − 2 for n ≥ 3.
Assuming again that one operation takes 1µs, it now takes0.001sec. to compute f1000.Remark: This iterative algorithm uses a very restricted form ofdynamic programming. We will see more of this later in this course.
![Page 28: Fundamental Algorithms · Remark: The value of fn grows very fast as a function of n. It can be shown that fn ≥ 2⌊ n−1 2 ⌋. For example, f100 = 3.54·1020 f1000 = 4.53·10208](https://reader034.vdocuments.net/reader034/viewer/2022042302/5eccf84f7f03df4e9b7be2be/html5/thumbnails/28.jpg)
0.2 3rd Algorithm for Computing Fibonacci Numbers
For completeness, let us mention another way of computing fn forn ≥ 1. The recurrence relation defining fn can be solved, and anexplicit representation can be obtained. It holds that
fn =1√5·[(
1 +√
5
2
)n
−(
1 −√
5
2
)n]
However, this would require us to work with fractional values at asufficient level of precision and analyzing the complexityaccordingly.
![Page 29: Fundamental Algorithms · Remark: The value of fn grows very fast as a function of n. It can be shown that fn ≥ 2⌊ n−1 2 ⌋. For example, f100 = 3.54·1020 f1000 = 4.53·10208](https://reader034.vdocuments.net/reader034/viewer/2022042302/5eccf84f7f03df4e9b7be2be/html5/thumbnails/29.jpg)
0.2 3rd Algorithm for Computing Fibonacci Numbers
For completeness, let us mention another way of computing fn forn ≥ 1. The recurrence relation defining fn can be solved, and anexplicit representation can be obtained. It holds that
fn =1√5·[(
1 +√
5
2
)n
−(
1 −√
5
2
)n]
However, this would require us to work with fractional values at asufficient level of precision and analyzing the complexityaccordingly.
![Page 30: Fundamental Algorithms · Remark: The value of fn grows very fast as a function of n. It can be shown that fn ≥ 2⌊ n−1 2 ⌋. For example, f100 = 3.54·1020 f1000 = 4.53·10208](https://reader034.vdocuments.net/reader034/viewer/2022042302/5eccf84f7f03df4e9b7be2be/html5/thumbnails/30.jpg)
0.2 3rd Algorithm for Computing Fibonacci Numbers
For completeness, let us mention another way of computing fn forn ≥ 1. The recurrence relation defining fn can be solved, and anexplicit representation can be obtained. It holds that
fn =1√5·[(
1 +√
5
2
)n
−(
1 −√
5
2
)n]
However, this would require us to work with fractional values at asufficient level of precision and analyzing the complexityaccordingly.
![Page 31: Fundamental Algorithms · Remark: The value of fn grows very fast as a function of n. It can be shown that fn ≥ 2⌊ n−1 2 ⌋. For example, f100 = 3.54·1020 f1000 = 4.53·10208](https://reader034.vdocuments.net/reader034/viewer/2022042302/5eccf84f7f03df4e9b7be2be/html5/thumbnails/31.jpg)
1. Time and Space Complexity
The resource usage of an algorithm is measured as a function of itsinput size (or, as in the previous example, of one of its inputvalues).
Definition 2Let x := (x1, x2, . . . , xm) be some input. The uniform input size||x||u is defined as
||x||u := m.
Definition 3The uniform time complexity tu(x) of an algorithm for input x isthe number of operations performed by the algorithm upon input x.
More realistically, the actual size of input x in bits, rather than itslength as a vector, should be taken into account.
![Page 32: Fundamental Algorithms · Remark: The value of fn grows very fast as a function of n. It can be shown that fn ≥ 2⌊ n−1 2 ⌋. For example, f100 = 3.54·1020 f1000 = 4.53·10208](https://reader034.vdocuments.net/reader034/viewer/2022042302/5eccf84f7f03df4e9b7be2be/html5/thumbnails/32.jpg)
1. Time and Space Complexity
The resource usage of an algorithm is measured as a function of itsinput size (or, as in the previous example, of one of its inputvalues).
Definition 2Let x := (x1, x2, . . . , xm) be some input. The uniform input size||x||u is defined as
||x||u := m.
Definition 3The uniform time complexity tu(x) of an algorithm for input x isthe number of operations performed by the algorithm upon input x.
More realistically, the actual size of input x in bits, rather than itslength as a vector, should be taken into account.
![Page 33: Fundamental Algorithms · Remark: The value of fn grows very fast as a function of n. It can be shown that fn ≥ 2⌊ n−1 2 ⌋. For example, f100 = 3.54·1020 f1000 = 4.53·10208](https://reader034.vdocuments.net/reader034/viewer/2022042302/5eccf84f7f03df4e9b7be2be/html5/thumbnails/33.jpg)
1. Time and Space Complexity
The resource usage of an algorithm is measured as a function of itsinput size (or, as in the previous example, of one of its inputvalues).
Definition 2Let x := (x1, x2, . . . , xm) be some input. The uniform input size||x||u is defined as
||x||u := m.
Definition 3The uniform time complexity tu(x) of an algorithm for input x isthe number of operations performed by the algorithm upon input x.
More realistically, the actual size of input x in bits, rather than itslength as a vector, should be taken into account.
![Page 34: Fundamental Algorithms · Remark: The value of fn grows very fast as a function of n. It can be shown that fn ≥ 2⌊ n−1 2 ⌋. For example, f100 = 3.54·1020 f1000 = 4.53·10208](https://reader034.vdocuments.net/reader034/viewer/2022042302/5eccf84f7f03df4e9b7be2be/html5/thumbnails/34.jpg)
Lemma 2For some value x ∈ N0, the length ℓ(x) of the representation of x
as a binary number is
ℓ(x) = ⌊log x⌋ + 1.
This immediately follows from the fact that 2ℓ(x)−1 ≤ x < 2ℓ(x).
Definition 4The logarithmic size ||x||log of input x = (x1, x2, . . . , xm) is
||x||log =
m∑
i=1
ℓ(xi) = m +
m∑
i=1
⌊log xi⌋.
![Page 35: Fundamental Algorithms · Remark: The value of fn grows very fast as a function of n. It can be shown that fn ≥ 2⌊ n−1 2 ⌋. For example, f100 = 3.54·1020 f1000 = 4.53·10208](https://reader034.vdocuments.net/reader034/viewer/2022042302/5eccf84f7f03df4e9b7be2be/html5/thumbnails/35.jpg)
Lemma 2For some value x ∈ N0, the length ℓ(x) of the representation of x
as a binary number is
ℓ(x) = ⌊log x⌋ + 1.
This immediately follows from the fact that 2ℓ(x)−1 ≤ x < 2ℓ(x).
Definition 4The logarithmic size ||x||log of input x = (x1, x2, . . . , xm) is
||x||log =
m∑
i=1
ℓ(xi) = m +
m∑
i=1
⌊log xi⌋.
![Page 36: Fundamental Algorithms · Remark: The value of fn grows very fast as a function of n. It can be shown that fn ≥ 2⌊ n−1 2 ⌋. For example, f100 = 3.54·1020 f1000 = 4.53·10208](https://reader034.vdocuments.net/reader034/viewer/2022042302/5eccf84f7f03df4e9b7be2be/html5/thumbnails/36.jpg)
Definition 5The logarithmic time complexity tlog(x) of an algorithm for input x
is the total of the logarithmic costs of all operations carried out bythe algorithm, given input x. The logarithmic cost of an operationis the total size of all arguments of this operation (in binaryrepresentation). See example.
Example 6
The logarithmic cost of the operation ”a := a + c[d[i]]” is
ℓ(a) + ℓ(i) + ℓ(c[i]) + ℓ(c[d[i]]).
![Page 37: Fundamental Algorithms · Remark: The value of fn grows very fast as a function of n. It can be shown that fn ≥ 2⌊ n−1 2 ⌋. For example, f100 = 3.54·1020 f1000 = 4.53·10208](https://reader034.vdocuments.net/reader034/viewer/2022042302/5eccf84f7f03df4e9b7be2be/html5/thumbnails/37.jpg)
Definition 5The logarithmic time complexity tlog(x) of an algorithm for input x
is the total of the logarithmic costs of all operations carried out bythe algorithm, given input x. The logarithmic cost of an operationis the total size of all arguments of this operation (in binaryrepresentation). See example.
Example 6
The logarithmic cost of the operation ”a := a + c[d[i]]” is
ℓ(a) + ℓ(i) + ℓ(c[i]) + ℓ(c[d[i]]).
![Page 38: Fundamental Algorithms · Remark: The value of fn grows very fast as a function of n. It can be shown that fn ≥ 2⌊ n−1 2 ⌋. For example, f100 = 3.54·1020 f1000 = 4.53·10208](https://reader034.vdocuments.net/reader034/viewer/2022042302/5eccf84f7f03df4e9b7be2be/html5/thumbnails/38.jpg)
Remark: Statements on the resource usage of a given algorithmupon specific inputs x are hardly useful. Rather, we need some wayto argue over all inputs of a given length. This calls for aworst-case consideration.
Definition 7Let t : N −→ N be some function. An algorithm is said to haveuniform (logarithmic) time complexity t(n) if and only if
tu(n) := max{tu(x) : ||x||u = n} ≤ t(n)(tlog(n) := max{tlog(x) : ||x||log = n} ≤ t(n)),
respectively.
This means that worst-case inputs of size n are considered whenbounding the running time from above by function t(n).
![Page 39: Fundamental Algorithms · Remark: The value of fn grows very fast as a function of n. It can be shown that fn ≥ 2⌊ n−1 2 ⌋. For example, f100 = 3.54·1020 f1000 = 4.53·10208](https://reader034.vdocuments.net/reader034/viewer/2022042302/5eccf84f7f03df4e9b7be2be/html5/thumbnails/39.jpg)
Remark: Statements on the resource usage of a given algorithmupon specific inputs x are hardly useful. Rather, we need some wayto argue over all inputs of a given length. This calls for aworst-case consideration.
Definition 7Let t : N −→ N be some function. An algorithm is said to haveuniform (logarithmic) time complexity t(n) if and only if
tu(n) := max{tu(x) : ||x||u = n} ≤ t(n)(tlog(n) := max{tlog(x) : ||x||log = n} ≤ t(n)),
respectively.
This means that worst-case inputs of size n are considered whenbounding the running time from above by function t(n).
![Page 40: Fundamental Algorithms · Remark: The value of fn grows very fast as a function of n. It can be shown that fn ≥ 2⌊ n−1 2 ⌋. For example, f100 = 3.54·1020 f1000 = 4.53·10208](https://reader034.vdocuments.net/reader034/viewer/2022042302/5eccf84f7f03df4e9b7be2be/html5/thumbnails/40.jpg)
Yet more realism can be achieved by considering average case timecomplexity. This requires that, for each value of n, a probabilitydistribution over all possible inputs x of length n be known. Usingthis information, the uniform or logarithmic time complexitiestu(x) (or tlog(x)) can be considered.
In this lecture we will not cover average case analysis.
Definition 8The uniform space complexity su(x) of an algorithm for input x isthe number of storage locations used by the algorithm, given x.
Definition 9The logarithmic space complexity slog(x) of an algorithm for inptux is the sum of the maximum lengths (in binary form) of the valueswritten to the storage registers by the algorithm upon input x.
![Page 41: Fundamental Algorithms · Remark: The value of fn grows very fast as a function of n. It can be shown that fn ≥ 2⌊ n−1 2 ⌋. For example, f100 = 3.54·1020 f1000 = 4.53·10208](https://reader034.vdocuments.net/reader034/viewer/2022042302/5eccf84f7f03df4e9b7be2be/html5/thumbnails/41.jpg)
Yet more realism can be achieved by considering average case timecomplexity. This requires that, for each value of n, a probabilitydistribution over all possible inputs x of length n be known. Usingthis information, the uniform or logarithmic time complexitiestu(x) (or tlog(x)) can be considered.
In this lecture we will not cover average case analysis.
Definition 8The uniform space complexity su(x) of an algorithm for input x isthe number of storage locations used by the algorithm, given x.
Definition 9The logarithmic space complexity slog(x) of an algorithm for inptux is the sum of the maximum lengths (in binary form) of the valueswritten to the storage registers by the algorithm upon input x.
![Page 42: Fundamental Algorithms · Remark: The value of fn grows very fast as a function of n. It can be shown that fn ≥ 2⌊ n−1 2 ⌋. For example, f100 = 3.54·1020 f1000 = 4.53·10208](https://reader034.vdocuments.net/reader034/viewer/2022042302/5eccf84f7f03df4e9b7be2be/html5/thumbnails/42.jpg)
Yet more realism can be achieved by considering average case timecomplexity. This requires that, for each value of n, a probabilitydistribution over all possible inputs x of length n be known. Usingthis information, the uniform or logarithmic time complexitiestu(x) (or tlog(x)) can be considered.
In this lecture we will not cover average case analysis.
Definition 8The uniform space complexity su(x) of an algorithm for input x isthe number of storage locations used by the algorithm, given x.
Definition 9The logarithmic space complexity slog(x) of an algorithm for inptux is the sum of the maximum lengths (in binary form) of the valueswritten to the storage registers by the algorithm upon input x.
![Page 43: Fundamental Algorithms · Remark: The value of fn grows very fast as a function of n. It can be shown that fn ≥ 2⌊ n−1 2 ⌋. For example, f100 = 3.54·1020 f1000 = 4.53·10208](https://reader034.vdocuments.net/reader034/viewer/2022042302/5eccf84f7f03df4e9b7be2be/html5/thumbnails/43.jpg)
Definition 10Let s : N −→ N be some function. An algorithm is said to haveuniform (logarithmic) space complexity if and only if
su(n) := max{su(x) : ||x||u = n} ≤ s(n)(slog(n) := max{slog(x) : ||x||log = n} ≤ s(n)),
respectively.
Like in the case of time complexity, average case analysis can beapplied to space complexity. This will not be covered in this course.
![Page 44: Fundamental Algorithms · Remark: The value of fn grows very fast as a function of n. It can be shown that fn ≥ 2⌊ n−1 2 ⌋. For example, f100 = 3.54·1020 f1000 = 4.53·10208](https://reader034.vdocuments.net/reader034/viewer/2022042302/5eccf84f7f03df4e9b7be2be/html5/thumbnails/44.jpg)
Definition 10Let s : N −→ N be some function. An algorithm is said to haveuniform (logarithmic) space complexity if and only if
su(n) := max{su(x) : ||x||u = n} ≤ s(n)(slog(n) := max{slog(x) : ||x||log = n} ≤ s(n)),
respectively.
Like in the case of time complexity, average case analysis can beapplied to space complexity. This will not be covered in this course.