Unit 1 Algorithm Performance Analysis and Measurement
Unit 1 Algorithm Performance Analysis and Measurement
• Example:
Step1: Input first number as A
Step2: Input second number as B
Step3: SET SUM = A+B
Step4: PRINT SUM
Step5:END
Decision
• Decision statements are used when outcome of a
process depends on some condition.
step 1 − START
step 2 − declare three integers a, b & c
step 3 − define values of a & b
step 4 − add values of a & b
step 5 − store output of step 4 to c
step 6 − print c
step 7 − STOP
Alternate Way
step 1 − START ADD
step 3 − c ← a + b
step 4 − display c
step 5 − STOP
S(P) = C + S(I),
Hence S(P) = 1 + 3.
• If an operation's worst case time is ƒ(n) then this operation will not
take more than ƒ(n) time where ƒ(n) represents function of n.
Average-case performance for algorithm
• Running time of an algorithm is an estimate of the
running time for an ‘average’ input.
• One may require less memory space and one may require
less CPU time to execute.
statement;
Above we have a single statement. Its Time Complexity
will be Constant.
f(n) = n/2
Nested Loop
• Loops that contain inner loop.
No. of No. of
Total no.
iterations iterations
of
in inner in outer
iterations
loop loop
Quadratic Loop
Ο Notation
Ω Notation
θ Notation
O-Notation (big O notation) (Upper Bound)
• The notation Ο(n) is the formal way
to express the upper bound of an
algorithm's running time.
Θ(g(n)) = { f(n) : there exist positive constants c1, c2 and n0 such that
0 ≤ c1g(n) ≤ f (n) ≤ c2g(n) for all n ≥ n0 }
• Because Θ(g(n)) is a set, we could write f(n) ϵ Θ(g(n)) to indicate
that f(n) is a member of Θ(g(n)).
• g(n) is an asymptotically tight bound for f(n).
Common Asymptotic Notations
constant Ο(1)
logarithmic Ο(log n)
linear Ο(n)
n log n Ο(n log n)
quadratic Ο(n2)
cubic Ο(n3)
polynomial nΟ(1)
exponential 2Ο(n)
Expressing Time & Space Complexity
• Time & Space Complexity can be expressed using function f(n),
where n is the input size
• Required when:
1. We want to predict the rate of growth of complexity as size
of the problem increases
Sorting problems
Best case
Average
case
Worst case
Growth rate of function
Some Functions Common Name
Ordered by Growth
Rate
log n logarithmic
log2 n Poly-logarithmic
n Linear
n log n Linearithmic
n2 Quadratic
n3 Cubic
cn Exponential
Growth rate of function
4 2 4 8 16 64 16
16 4 16 64 256 4096 65536
64 6 36 384 4096 262144 1.84 × 1019
256 8 64 2048 65536 16777216 1.15 × 1077
1024 10 100 10240 1048576 1.07 × 109 1.79 × 10308
4096 12 144 49152 16777216 6.87 × 1010 101233
Exercises
• Express the function n3/1000 - 100n2 - 100n + 3 in
terms of Θ notation. Θ(n3)
1 14 1 6 6 F(n)>cg(n)
2 58 8 6 48 F(n)>cg(n)
b. F(n) = 4n3 + 2n + 3
b. F(n) = 5n3 + n2 + 3n + 2
Exercises