









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Prof. Balamohan Pawar delivered this lecture at Allahabad University for Aeronautical Engineering and Computer Programming course. Its main points are: Big, Functio, Constant, Positive, Array, Procdure, Type, Begin, End, Range, Loop, Natural, Factorial
Typology: Slides
1 / 16
This page cannot be seen from the preview
Don't miss anything!










4
Big-O n) 4n-2 ≤ cn for^0 ≥ n^ 1 so that ≥ n true for c = 4 and n 0 0 = 1 (^3) + 10n 2 + 4n +2 is O(n (^3) ) 5n 3 +10n^2 +4n +2^0 ≥^ 1 so that ≤ cn 3 for n ≥ n true for c = 21 and n 0 0 = 1 2 n + 3 is O(log^2 n) 2log^0 ≥^ 1 so that true for c = 5^2 n and^ + 3 n^ ≤^ c log^2 n^ for^ n^ ≥^ n^0 0 = 2
Big-O
g(n) grows more (^) Yes No
f(n) grows more (^) No Yes
g(n) and f(n) has same growth Yes^ Yes 5
7
6
type Int_Array (Integer range <>) of Integer; procedure Measure (A : Int_Array ) is begin^ Sum^ : Integer := 0; for I in A' for (^) Sum := Sum + A(J);J in A' ; ; end Measure;
Inner loop Outer loop
Ex 1
is array
range loop range loop end loop end loop
Statement Runs in X time Executes
Variable Sum is initialized Constant1 1 Array of size n is created Constant2 1 Variable I is created and initialized Constant3 1 I is tested against A’range (n) Constant4 n+ Variable J is created and initialized Constant5 n J is tested against A’range (n) Constant6 n(n+1) Sum is incremented by A(J) Constant7 n 2 J is incremented by 1 Constant8 n 2 I is incremented by 1 Constant9 n BigO.adb
10
Ex 3
type Int_Array (Integer range <>) of Integer; procedure Measure (A : Int_Array ) is begin^ Sum^ : Integer := 0; for I in A' for (^) Sum := Sum + A(J in 1 .. 4 loopI ); (^) ---- only change to Ex 2only change to Ex 2 ; ; end Measure; BigO3.adb
is array
range loop
end loop end loop
CQ – Ex 3 Variable J is created and initialized Constant J is tested against I Constant Sum is incremented by A(J) Constant J is incremented by 1 Constant
12
Ex 4
function Factorial (N : in Natural ) return Positive is beginif N = 0 then return 1; elsereturn N * Factorial (N-1); end Factorial;;
13
How long time does executing the Factorial algorithm take?
(^2) )
end if
CQ – Ex 4
17
16
Merge
Merge Sort Analysis
T(n) = O(1) n = 1
2T(n/2) + O(n) + O(1) n> 1
18
19
aT(n/b) + cn a(aT(n/b/b) + cn/b) + cn a^2 T(n/b^2 ) + cna/b + cn a^2 T(n/b^2 a^2 (aT(n/b^2 /b) + cn/b^2 ) + cn(a/b + 1) a^3 T(n/b^3 ) + cn(a^2 /b^2 ) + cn(a/b + 1) a^3 T(n/b^3 ) + cn(a^2 /b^2 + a/b + 1) … ak^ T(n/b k^ ) + cn(ak-1/b k-1^ + ak-2/b k-2^2 /b^2 + a/b + 1)
) + cn(a/b + 1)
( ) cn n b
n aT
c n T n ⎪⎩
( ) cn n b
n aT
c n T n ⎪⎩
( ) cn n b
n aT
c n T n ⎪⎩
( ) cn n b
n aT
c n T n ⎪⎩
( ) cn n b
n aT
c n T n ⎪⎩
( ) cn n b
n aT
c n T n ⎪⎩
( ) cn n b
n aT
c n T n ⎪⎩
( ) cn n b
n aT
c n T n ⎪⎩
( ) cn n b
n aT
c n T n ⎪⎩
( ) cn n b
n aT
c n T n ⎪⎩
( ) cn n b
n aT
c n T n ⎪⎩
( ) cn n b
n aT
c n T n ⎪⎩
( ) cn n b
n aT
c n T n ⎪⎩
( ) cn n b
n aT
c n T n ⎪⎩
( ) cn n b
n aT
c n T n ⎪⎩
( ) cn n b
n aT
c n T n ⎪⎩
( ) cn n b
n aT
c n T n ⎪⎩
( ) cn n b
n aT
c n T n ⎪⎩
( ) cn n b
n aT
c n T n ⎪⎩
( ) cn n b
n aT
c n T n ⎪⎩
( ) cn n b
n aT
c n T n ⎪⎩
( ) cn n b
n aT
c n T n ⎪⎩
( ) cn n b
n aT
c n T n ⎪⎩
( ) cn n b
n aT
c n T n ⎪⎩
( ) cn n b
n aT
c n T n ⎪⎩
( ) cn n b
n aT
c n T n ⎪⎩
( ) cn n b
n aT
c n T n ⎪⎩
( ) cn n b
n aT
c n T n
22
The Master Method
divide and conquer algorithm size n into a subproblems, each of size n / b
divide the problem + combine solved subproblems) be described by the function f(n) provides a simple “ cookbook
23
Simplified Master Method
k
k
k
b
k
a
a b
a b
a b
O n
O n n
O n b
log
log
T(n) =
a,c > 0
24
The Towers of Hanoi
ring
For simplicity, suppose there were just 3 disks
the top disk from A to B.
A B C
The Towers of Hanoi
Since we can only move one disk at a time, we move
For simplicity, suppose there were just 3 disks
We then move the top disk from A to B.
A B C
The Towers of Hanoi
For simplicity, suppose there were just 3 disks
We then move the top disk from C to A.
A B C
The Towers of Hanoi
For simplicity, suppose there were just 3 disks
We then move the top disk from C to B.
A B C
The Towers of Hanoi
For simplicity, suppose there were just 3 disks
We then move the top disk from A to B.
A B C
The Towers of Hanoi
34
Towers of Hanoi
35
Some math that is good to know
b (xy) = log^ b b y b (x/y) = log^ b b y b b x b x a/log^ x b
-- move the top -- from needle