Big O-Aeronautical Engineering And Computer Programming-Lecture Slides, Slides of Aeronautical Engineering

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

2011/2012

Uploaded on 07/20/2012

savitha_48
savitha_48 🇮🇳

3

(1)

109 documents

1 / 16

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
4
Big-O
n)
0 1 so that
4n-2 cn for n n0
true for c = 4 and n0 = 1
3 + 10n2 + 4n +2 is O(n3)
0 1 so that
5n3+10n2+4n +2 cn3 for n n0
true for c = 21 and n0 = 1
2n + 3 is O(log2 n)
0 1 so that
2log2 n + 3 c log2 n for n n0
true for c = 5 and n0 = 2
•4n2 is O(
– Need a c > 0 and n
•5n
– Need a c > 0 and n
•2 log
– Need a c > 0 and n
Big-O
Given function f(n) and g(n), we say
that f(n) is O(g(n)) if there are
positive constants c and n0 so that
f(n) cg(n) for n n0
f(n) is O(g(n)) g(n) is O(f(n))
g(n) grows more Yes No
f(n) grows more No Yes
g(n) and f(n) has
same growth Yes 5
Yes
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Big O-Aeronautical Engineering And Computer Programming-Lecture Slides and more Slides Aeronautical Engineering in PDF only on Docsity!

4

Big-O n) 4n-2cn 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 nn 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

  • 4n – 2 is O(
    • Need a c > 0 and n
  • 5n
    • Need a c > 0 and n
  • 2 log
    • Need a c > 0 and n

Big-O

  • Given function f(n) and g(n), we say that f(n) is O(g(n)) if there are positive constants c and n 0 so that f(n) ≤ cg(n) for n ≥ n 0 f(n) is O(g(n)) g(n) is O(f(n))

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

of times

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

  1. N, N(I+1), NI, N*I
  2. N, N5, N4, N*
  3. N, N*5, 4, 4
  4. I still don’t get it (^11)

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

  1. O(n)
  2. O(n
  3. log n
  4. 42

17

16

Merge

  • Input : Array A and indices p, mid, r such that - p ≤ mid < r A(p .. mid) is sorted and subarray A(mid+1 .. r) is sorted
  • Output : single sorted array A(p .. r)
  • T(n) = O(n), where n=r-p+1 = # of elements being merged

Merge Sort Analysis

  • The base case : when n =1 , T(n) =O(1) n ≥ 2, time for merge sort steps:
  • Divide : Compute mid as the average of p, r ⇒ cost = O(1)
  • Conquer : Solve 2 subproblems, each of size n/ 2 ⇒cost = 2 T( n/2 )
  • Combine : merge to an n element subarray ⇒ cost = O (n)

T(n) = O(1) n = 1

2T(n/2) + O(n) + O(1) n> 1

  • subarray
  • When

18

Solving Recurrences:

Iteration

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)

  • T(n) =

) + cn(a/b + 1)

  • … + a

( ) 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

  • T(n) = aT(n/b) + cn k , where and b > 1

( ) k

k

k

k

b

k

a

a b

a b

a b

O n

O n n

O n b

log

log

T(n) =

  • Given: a
    • An algorithm that divides the problem of
    • Let the cost of each stage (i.e., the work to
    • The master method ” solution

a,c > 0

24

The Towers of Hanoi

  • Goal : Move stack of rings to another peg

ring

For simplicity, suppose there were just 3 disks

the top disk from A to B.

A B C

The Towers of Hanoi

  • May only move 1 ring at a time
  • May never have larger ring on top of smaller

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

  • hanoi(from,to,other,number) number disks from to needle to if number=1 then move the top disk from needle from to needle to else hanoi(from,other,to, number-1) hanoi(from,to,other, 1) hanoi(other,to, from, number-1) end

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

  • a(b+c)^ = ab^ ac
  • abc^ = (ab^ )c
  • ab^ /a c^ = a(b-c) log (^) ab
  • b c^ = aclog^ ab

-- move the top -- from needle

  • log x + log
  • log x – log
  • log xa = alog
  • log a = log
  • b = a