Loop analysis - example 1
- What is the running time of
the following
program fragment:
int sum = 0;
for ( int i = 1; i <= n; i = 2*i )
sum++;
|
Analysis:
|
Loop analysis - example 1
- What is the running time of
the following
program fragment:
int sum = 0;
for ( int i = 1; i <= n; i = 2*i )
sum++;
|
Analysis:
The variable i takes on these values through the loop:
i = 1 2 4 8 16 32 ???
|
|
Loop analysis - example 1
- What is the running time of
the following
program fragment:
int sum = 0;
for ( int i = 1; i <= n; i = 2*i )
sum++;
|
Analysis:
The variable i takes on these values through the loop:
i = 1 2 4 8 16 32 ???
The loop will exit when i <= n:
i = 1 2 4 8 16 32 n ???
<------------------------------------->
How many values did i take on ?
|
|
Loop analysis - example 1
- What is the running time of
the following
program fragment:
int sum = 0;
for ( int i = 1; i <= n; i = 2*i )
sum++;
|
Analysis:
The variable i takes on these values through the loop:
i = 1 2 4 8 16 32 ???
The loop will exit when i <= n:
i = 1 2 4 8 16 32 n ???
<------------------------------------->
How many values did i take on ?
Suppose 2k-1 ≤ n < 2k for some k:
i = 1 2 4 8 16 32 2k-1 n 2k
|
|
Loop analysis - example 1
- What is the running time of
the following
program fragment:
int sum = 0;
for ( int i = 1; i <= n; i = 2*i )
sum++;
|
Analysis:
The variable i takes on these values through the loop:
i = 1 2 4 8 16 32 ???
The loop will exit when i <= n:
i = 1 2 4 8 16 32 n ???
<------------------------------------->
How many values did i take on ?
Suppose 2k-1 ≤ n < 2k for some k:
i = 1 2 4 8 16 32 2k-1 n 2k
#Iterations = k ≈ log(n) = O(log(n)) (n < 2k <=> log(n) < k)
|
|
DEMO:
demo/13-analysis/Demo4.java
Variant of the loop analysis in example 1
- A variant of the
same loop analysis is
as follows:
int sum = 0;
for ( int i = n; i >= 1; i = i/2 )
sum++;
|
Analysis:
The variable i takes on these values through the loop:
i = n n/2 n/4 ... ???
The loop will exit when i >= 1:
i = n n/2 n/4 ... 1
<------------------------------------>
How many values did i take on ?
Suppose 2k-1 ≤ n < 2k for some k:
i = n n/2 n/4 ... n/2k-1 1 n/2k=0
#Iterations = k ≈ log(n) = O(log(n)) (n < 2k <=> log(n) < k)
|
|
DEMO:
demo/13-analysis/Demo5.java
Inner loop
depends on the variable in the
outer loop
- Example 1
- What is the running time of
the following
program fragment:
int sum = 0;
for ( int i = 0; i < n; i++ )
for ( int j = 0; j <= i; j++ )
sum++;
|
Analysis:
Notice:
The inner loop depends on the value of the variable i
in the outer loop
In this case, we cannot multiply...
We must count very carefully
|
|
Inner loop
depends on the variable in the
outer loop
- Example 1
- What is the running time of
the following
program fragment:
int sum = 0;
for ( int i = 0; i < n; i++ )
for ( int j = 0; j <= i; j++ )
sum++;
|
Analysis:
The variable i takes on these values:
i = 0 1 2 3 .... n-1
|
|
Inner loop
depends on the variable in the
outer loop
- Example 1
- What is the running time of
the following
program fragment:
int sum = 0;
for ( int i = 0; i < n; i++ )
for ( int j = 0; j <= i; j++ )
sum++;
|
Analysis:
The variable j takes on these values for each i:
i = 0 1 2 3 .... n-1
---------------------------------------
j = 0 0 0 0 .... 0
1 1 1 .... 1
2 2 .... 2
3 .... ...
.... ...
n-1
|
|
Inner loop
depends on the variable in the
outer loop
- Example 1
- What is the running time of
the following
program fragment:
int sum = 0;
for ( int i = 0; i < n; i++ )
for ( int j = 0; j <= i; j++ )
sum++;
|
Analysis:
The variable j takes on these values for each i:
i = 0 1 2 3 .... n-1
---------------------------------------
j = * * * * *
* * * *
* * *
* .... ...
.... ...
*
#Iteration = # stars (*) in the above diagraim
|
|
Inner loop
depends on the variable in the
outer loop
- Example 1
- What is the running time of
the following
program fragment:
int sum = 0;
for ( int i = 0; i < n; i++ )
for ( int j = 0; j <= i; j++ )
sum++;
|
Analysis:
The variable j takes on these values for each i:
i = 0 1 2 3 .... n-1
---------------------------------------
j = * * * * *
* * * *
* * *
* .... ...
.... ...
*
#Iteration = 1 + 2 + 3 + ... + n = n(n+1)/2
Runtime complexity = O(n2)
|
|
DEMO:
demo/13-analysis/Demo6.java
Inner loop
depends on the variable in the
outer loop
- Example 2
- What is the running time of
the following
program fragment:
for ( int i = n; i > 0; i = i/2 )
for ( int j = 0; j < i; j++ )
sum++;
|
Analysis:
|
Inner loop
depends on the variable in the
outer loop
- Example 2
- What is the running time of
the following
program fragment:
for ( int i = n; i > 0; i = i/2 )
for ( int j = 0; j < i; j++ )
sum++;
|
Analysis:
The outer index i takes on these values:
<-------- log(n) values -------->
i = n n/2 n/4 n/8 .... 1
|
|
Inner loop
depends on the variable in the
outer loop
- Example 2
- What is the running time of
the following
program fragment:
for ( int i = n; i > 0; i = i/2 )
for ( int j = 0; j < i; j++ )
sum++;
|
Analysis:
The inner index j takes on these values (depending on i):
<-------- log(n) values -------->
i = n n/2 n/4 n/8 .... 1
-------------------------------------------------
j = 0 0 0 0 .... 0
1 1 1 1 ....
2 2 2 2
... ... ... ...
... n/2-1
n-1
|
|
Inner loop
depends on the variable in the
outer loop
- Example 2
- What is the running time of
the following
program fragment:
for ( int i = n; i > 0; i = i/2 )
for ( int j = 0; j < i; j++ )
sum++;
|
Analysis:
The inner index j takes on these values (depending on i):
<-------- log(n) values -------->
i = n n/2 n/4 n/8 .... 1
-------------------------------------------------
j = 0 0 0 0 .... 0
1 1 1 1 ....
2 2 2 2
... ... ... ...
... n/2-1
n-1
#Iterations = n + n/2 + n/4 + n/8 + .... + 1
|
|
Inner loop
depends on the variable in the
outer loop
- Example 2
- What is the running time of
the following
program fragment:
for ( int i = n; i > 0; i = i/2 )
for ( int j = 0; j < i; j++ )
sum++;
|
Analysis:
The inner index j takes on these values (depending on i):
<-------- log(n) values -------->
i = n n/2 n/4 n/8 .... 1
-------------------------------------------------
j = 0 0 0 0 .... 0
1 1 1 1 ....
2 2 2 2
... ... ... ...
... n/2-1
n-1
#Iterations = n + n/2 + n/4 + n/8 + .... + 1 (take n out)
= n*(1 + 1/2 + 1/4 + ... + 1/n) (geometric sum)
≈ n*2
Runtime complexity = O(n)
|
|
DEMO:
demo/13-analysis/Demo7.java
Inner loop
depends on the variable in the
outer loop
- Example 3
- What is the running time of
the following
program fragment:
for ( int i = 1; i <= n; i++ )
for ( int j = 0; j < n; j = j + i )
sum++;
|
Analysis:
|
Inner loop
depends on the variable in the
outer loop
- Example 3
- What is the running time of
the following
program fragment:
for ( int i = 1; i <= n; i++ )
for ( int j = 0; j < n; j = j + i )
sum++;
|
Analysis:
The variable i takes on these values:
i = 1 2 3 4 .... n
|
|
Inner loop
depends on the variable in the
outer loop
- Example 3
- What is the running time of
the following
program fragment:
for ( int i = 1; i <= n; i++ )
for ( int j = 0; j < n; j = j + i )
sum++;
|
Analysis:
The variable j takes on these values for each i:
i = 1 2 3 4 .... n
-----------------------------------
j = 0 0 0 0 .... 0
1 2 3 4
2 4 6 8
3 ... ... ...
4 n-1 n-1 n-1
...
n-1
|
|
Inner loop
depends on the variable in the
outer loop
- Example 3
- What is the running time of
the following
program fragment:
for ( int i = 1; i <= n; i++ )
for ( int j = 0; j < n; j = j + i )
sum++;
|
Analysis:
The variable j takes on these values for each i:
i = 1 2 3 4 .... n
-----------------------------------
j = 0 0 0 0 .... 0
1 2 3 4
2 4 6 8
3 ... ... ...
4 n-1 n-1 n-1
... (n/2)(n/3)(n/4)
n-1
#Iterations = n + n/2 + n/3 + n/4 + .... + n/n
|
|
Inner loop
depends on the variable in the
outer loop
- Example 3
- What is the running time of
the following
program fragment:
for ( int i = 1; i <= n; i++ )
for ( int j = 0; j < n; j = j + i )
sum++;
|
Analysis:
The variable j takes on these values for each i:
i = 1 2 3 4 .... n
-----------------------------------
j = 0 0 0 0 .... 0
1 2 3 4
2 4 6 8
3 ... ... ...
4 n-1 n-1 n-1
... (n/2)(n/3)(n/4)
n-1
#Iterations = n + n/2 + n/3 + n/4 + .... + n/n (take n out)
= n*(1 + 1/2 + 1/3 + ... + 1/n) (= Harmonic sum)
≈ n*log(n)
Runtime complexity = O(nlog(n))
|
|
DEMO:
demo/13-analysis/Demo8.java
❮
❯