Algorithm Control Structures Analysis of Algorithms Sequencing - - PowerPoint PPT Presentation

algorithm control structures analysis of algorithms
SMART_READER_LITE
LIVE PREVIEW

Algorithm Control Structures Analysis of Algorithms Sequencing - - PowerPoint PPT Presentation

Algorithm Control Structures Analysis of Algorithms Sequencing If-Then-Else For loop Algorithm Control Structures While loop Recursive calls http://www.cp.eng.chula.ac.th/faculty/spj Sequencing Sequencing


slide-1
SLIDE 1

Analysis of Algorithms

Algorithm Control Structures

http://www.cp.eng.chula.ac.th/faculty/spj

Algorithm Control Structures

Sequencing If-Then-Else For loop While loop Recursive calls

http://www.cp.eng.chula.ac.th/faculty/spj

Sequencing

block #1 block #2 t1 t2 t1 + t2 = max(t1, t2)

http://www.cp.eng.chula.ac.th/faculty/spj

Sequencing

block #1 block #2

Θ(n)

Θ( n2 )

Θ(n2)

slide-2
SLIDE 2

http://www.cp.eng.chula.ac.th/faculty/spj

Sequencing

block #1 block #2

Θ(n)

Ο( n2 )

Ο(n2)

http://www.cp.eng.chula.ac.th/faculty/spj

Sequencing

block #1 block #2

Ο(n)

Θ( n2 )

Θ(n2)

http://www.cp.eng.chula.ac.th/faculty/spj

Sequencing

block #1 block #2

Ο(n2)

Θ( n2 )

Θ(n2)

http://www.cp.eng.chula.ac.th/faculty/spj

If-Then-Else

block #2 block #1 t1 t2 max( t1 , t2 )

slide-3
SLIDE 3

http://www.cp.eng.chula.ac.th/faculty/spj

For Loop

P(i) ti

m i i

t

1

for ( i = 1 to m ) { P(i) }

http://www.cp.eng.chula.ac.th/faculty/spj

For Loop

=

Θ

m i 1

) 1 (       Θ =

= m i 1

1 ) (m Θ =

for ( i = 1 to m ) s += A[i][j]

http://www.cp.eng.chula.ac.th/faculty/spj

For Loop

∑∑

= =

Θ

m i m j 1 1

) 1 (

=

Θ =

m i

m

1

) ( ) (

2

m Θ =       Θ =

= m i

m

1

for ( i = 1 to m ) for ( j = 1 to m ) s += A[i][j]

http://www.cp.eng.chula.ac.th/faculty/spj

For Loop

∑∑

= =

Θ

m i i j 1 1

) 1 (

=

Θ =

m i

i

1

) ( ) (

2

m O =       =

= m i

m O

1

=

=

m i

m O

1

) (

for ( i = 1 to m ) for ( j = 1 to i ) s += A[i][j]

slide-4
SLIDE 4

http://www.cp.eng.chula.ac.th/faculty/spj

For Loop

∑∑

= =

Θ

m i i j 1 1

) 1 (

=

Θ =

m i

i

1

) ( ) (

2

m Θ =       + Θ = 2 ) 1 (m m       Θ =

= m i

i

1

for ( i = 1 to m ) for ( j = 1 to i ) s += A[i][j]

http://www.cp.eng.chula.ac.th/faculty/spj

For Loop

for ( i = 2 to m-1 ) for ( j = 3 to i ) s += A[i][j]

∑∑

− = =

Θ

1 2 3

) 1 (

m i i j

− =

Θ =

1 2

) (

m i

i ) (

2

m Θ =         Θ + Θ = ) ( 2

2

m m       Θ =

− = 1 2 m i

i

http://www.cp.eng.chula.ac.th/faculty/spj

While Loop

while ( n > 0 ) { … n = n - 1 } while ( n > 0 ) { … n = n / 2 } i = 0; j = n while ( i < j ) { … i++; j--; }

Θ(n) Θ(log n) Θ(n)

http://www.cp.eng.chula.ac.th/faculty/spj

Θ( i )

While Loop: Insertion Sort

Insertion_Sort( A[1..n] ) for j = 2 to n { key = A[j] i = j-1 while i>0 and A[i]>key { A[i+1] = A[i] i = i-1 } A[i+1] = key }

Ο( i )

slide-5
SLIDE 5

http://www.cp.eng.chula.ac.th/faculty/spj

While Loop: Insertion Sort

Insertion_Sort( A[1..n] ) for j = 2 to n { key = A[j] i = j-1 while i>0 and A[i]>key { A[i+1] = A[i] i = i-1 } A[i+1] = key }

Ο( i ) Ο( j )

http://www.cp.eng.chula.ac.th/faculty/spj

Ο( j )

While Loop: Insertion Sort

Insertion_Sort( A[1..n] ) for j = 2 to n { key = A[j] i = j-1 while i>0 and A[i]>key { A[i+1] = A[i] i = i-1 } A[i+1] = key }

Σ Ο( j )

j=2 n

Ο( n2 )

http://www.cp.eng.chula.ac.th/faculty/spj

While Loop: Euclids GCD

GCD( k, n ) { while k > 0 { t = k k = n mod k n = t } return n } t k n 88 128 88 40 88 40 8 40 8 8

http://www.cp.eng.chula.ac.th/faculty/spj

While Loop: Euclids GCD

GCD( k, n ) { while k > 0 { t = a k = n mod k n = t } return n }

n ≥ k n mod k < n/2 1: k ≤ n/2 2: k > n/2

Ο( log n )

n mod k < k n/k < 2 ,

≤ n/2

int( n/k ) = 1 n mod k = n - k * int( n/k ) = n - k < n - n/2 = n/2

slide-6
SLIDE 6

http://www.cp.eng.chula.ac.th/faculty/spj

While Loop: FindMin_BST

Position FindMin_BST( TREE T ) { Position p; p = T; if ( p != NULL ) { while ( p->left != NULL ) { p = p->left; } } return p; }

Ο( n ) Ο( h )

http://www.cp.eng.chula.ac.th/faculty/spj

Recursive Calls

waste( n ) { if ( n = 0 ) return 0 for ( i = 1 to n ) for (j = 1 to i ) print i,j,n for( i = 1 to 3 ) waste( n/2 ) }

Θ(n2 )

3T( n/2 ) T(n) = 3T( n/2 ) + Θ(n2 )

Θ(1 )

http://www.cp.eng.chula.ac.th/faculty/spj

= 0.9cn + O(n) ≤ 0.9cn + dn = cn ( choose d = 0.1c )

Guessing+ Induction

T(n) = T(0.7n) + T(0.2n) + O(n) Guess: T(n) = O(n), T(n) ≤ cn Proof: Basis: obvious Induction: T(n) ≤ 0.7cn + 0.2cn + O(n)

http://www.cp.eng.chula.ac.th/faculty/spj

If then the solution to the recurrence is T(n) = O(n)

Theorem

1

1

<

= k i i

α ) ( ) ( ) (

1

n O n T n T

k i i

+ = ∑

=

α

T(n) = T(0.3n)+T(0.2n)+T(0.09n)+T(0.4n) + O(n) = O(n)

slide-7
SLIDE 7

http://www.cp.eng.chula.ac.th/faculty/spj

Recursion Trees

T(n) = 3T( n/2 ) + Θ(n2 )

(n/2)2 (n/2)2 (n/2)2 (n/4)2 (n/4)2 (n/4)2 (n/4)2 (n/4)2 (n/4)2 (n/4)2 (n/4)2 (n/4)2 n2 n2 (3/4)n2 (9/16)n2

Θ(n2 )

... lgn

T(n/2) = 3T( n/4 ) + Θ((n/2)2 )

http://www.cp.eng.chula.ac.th/faculty/spj

Recursion Trees

T(n) = T( n/3 ) + T(2n/3) + n

n/9 2n/9 2n/9 4n/9 n n

Θ(n log n )

... log1.5 n n n n/3 2n/3

http://www.cp.eng.chula.ac.th/faculty/spj

Master Method

T(n) = aT(n/b) + f (n) a ≥ 1, b > 1 Let c = logba If f (n) = Ο( nc - ε ) for some constant ε > 0, then

  • T(n) = Θ( nc )

If f (n) = Θ( nc ) for some constant ε > 0, then

  • T(n) = Θ( nc log n )

http://www.cp.eng.chula.ac.th/faculty/spj

Master Method

T(n) = aT(n/b) + f (n) a ≥ 1, b ≥ 1 Let c = logba If f (n) = Ω( nc + ε ) for some constant ε > 0 and if a f (n/b) ≤ d f (n) for some constant d < 1 and all sufficiently large n, then

  • T(n) = Θ( f (n) )
slide-8
SLIDE 8

http://www.cp.eng.chula.ac.th/faculty/spj

Master Method

T(n) = aT(n/b) + f (n) a ≥ 1, b > 1 Let c = logba f (n) = Ο( nc - ε )

T(n) = Θ( nc ) f (n) = Ω( nc + ε )

T(n) = Θ( f (n) ) f (n) = Θ( nc )

T(n) = Θ( nc log n )

http://www.cp.eng.chula.ac.th/faculty/spj

Master Method : Example

T(n) = 9T(n/3) + n a = 9, b = 3, c = log b a = 2, nc = n2 f (n) = n = Ο( n2 - 0.1 ) T(n) = Θ(nc) = Θ(n2)

http://www.cp.eng.chula.ac.th/faculty/spj

Master Method : Example

T(n) = T(n/3) + 1 a = 1, b = 3, c = log b a = 0, nc = 1 f (n) = 1 = Θ( nc ) = Θ( 1 ) T(n) = Θ(nc log n) = Θ( log n)

http://www.cp.eng.chula.ac.th/faculty/spj

Master Method : Example

T(n) = 3T(n/4) + n log n a = 3, b = 4, c = log b a < 0.793, nc < n0.793 f (n) = n log n = Ω( n0.793 ) a f (n/b) = 3 ((n/4) log (n/4) ) ≤ (3/4) n log n = d f (n) T(n) = Θ( f (n) ) = Θ( n log n)

slide-9
SLIDE 9

http://www.cp.eng.chula.ac.th/faculty/spj

Conclusion

กก blocks control structures block blocks control structure asymptotic