0% found this document useful (0 votes)
4 views3 pages

A0A - Lecture 2

The document provides an analysis of various algorithms, focusing on the time complexity of different loops. It explains how to determine the number of iterations for loops that multiply or divide variables, as well as nested loops. The overall time complexities discussed include Θ(log2 n), Θ(Γn), and Θ(n log2 n).
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

A0A - Lecture 2

The document provides an analysis of various algorithms, focusing on the time complexity of different loops. It explains how to determine the number of iterations for loops that multiply or divide variables, as well as nested loops. The overall time complexities discussed include Θ(log2 n), Θ(Γn), and Θ(n log2 n).
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

How to Analyze an Algorithm

Analysis Piece of Code: I is initially 1


For ( I= 1; I < n; I = I x 2) 1x2 =2
{ 2x2= 22
Stmt; 22 x 2=23
} :::::::::::::::::::
How much
Blindly, we cannot say that the loop is repeating n times as each time I is
times???
multiply by 2. Assume that the loop is executing 2K times, loop condition
2K
will terminate when I > = n since I = 2K
2K >= n => log2 2K >= log2n  K= log2n  f(n)= Θ(log2n)
Loop body statements will be executed Θ(log2n) times.

Analysis Piece of Code:


For ( I= 1; I < n; I=Ix2 For ( I= 0; I < n; I ++)
{ {
Smt; Stmt;
} }
I =2x2x2……….=n I=1+1+1……………..= n
1 is added up until I >=n. 1 is added up to k
2 is being multiplied until i.>=n
times.
How much times 2 is being multiplied, let it
K=n
be up to 2k times
2k = n  K=log2n

For ( I= 1; I < n; I = I x 2) n=8 n=10


{ I I
Stmt; 1 1
} 2 2
4 4
log2 n = log2 8= log2 23 = 3 times loop body will
8* 8
be executed. condition
false
log2 n = log2 10= 3.2 = 4 timesIn this case, 16* condition false take

ceil value of log2 n i.e log2 n


Analysis Piece of Code: I =n initial value
For ( I= n; I .>= 1; I = I /2) n/2
n/22
{
n/23
Stmt;
--------
}
How many times
Loop will stop when I < 1 loop will execute
Let it be n/2K
Since I = n/2K
n/2K < 1  n/2K = 1
n = 2K  K= log2n or f(n)= Θ(log2n)

Analysis Piece of Code:


For ( I= 0; I x I < n; I ++)
{
Stmt;
}
Loop will be terminated as I x I > n  I2 > n  I2 = n
I= Γn f(n)= Θ (Γn)
Loop will be executed Γn times.

Analysis Piece of Code:


For ( I= 0; I < n; I ++)
{
Stmt; ____________________ n times
}
For ( J= 0; J < n; J ++)
{
Stmt; ____________________ n times
}

F(n) = 2n  F(n) = Θ(n)


Analysis Piece of Code:
P=0
For ( I= 1; I < n; I = I x 2)
{
P++; P evaluated here ___________________________log2 n times
}
Analysis Piece of Code:
For ( J= 1; J < P; J = J x 2) P used here
{
Stmt; ______________________________ log2 P times
}
What is P? Since P is incrementing in each iteration, so P will take the value that number of
times the loop is repeating i.e P = log2 n
F(n) = Θ(log2 P) = Θ(log2 log2 n)
Analysis Piece of Code:
For ( I= 0; I < n; I ++) ____________________ n+1 times
{
For ( J= 1; J < n; J = J x 2) ______________________ n x Log2 n
{
Stmt; ___________________________ n x (Log2 n-1)
}
}

F(n)= 2 n Log2 n + n
F(n)= Θ( n Log2 n)

For ( i= 0; i < n; i ++) Θ( n)


For ( i= 0; i < n; i =i+2) n/2 or Θ( n)
For ( i= n; i > 1; i-- ) Θ( n)
For ( i= n; i > 1; i=i-2 ) n/2 or Θ( n)
For ( i= 1; i < n; i =i*2) Θ( Log2 n)
For ( i= 1; i < n; i =i*3) Θ( Log3 n)
For ( i= n; i > 1; i =i/2) Θ( Log2 n)

You might also like