Design and Analysis of Algorithms - - PDF document

design and analysis of algorithms
SMART_READER_LITE
LIVE PREVIEW

Design and Analysis of Algorithms - - PDF document

Design and Analysis of Algorithms


slide-1
SLIDE 1

Design and Analysis of Algorithms

. . ก ก ก 2542

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

ก ก กกก

Analysis of Algorithms

Introduction

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

  • Complexity Measures

Size of Problem Instance Worst-, Best-, and Average-Case Analysis Exact Analysis Analysis Simplification

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

Complexity Measures

กก

ก memory ก

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

Analysis of Algorithms

predict the behavior of an algorithm without implementing it on a specific computer impossible to predict exact behavior analysis = approximate the main characteristics use for comparison

slide-2
SLIDE 2

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

Instances and Sizes

กกก

problem instance ก problem instance

instances ก instances

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

Size of Problem Instance

  • bits, bytes, words

vertices, edges, triangles, literals, ...

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

Sorting

Input : a list of n numbers Assumption : each number can be stored in a single computer word. Input size : n

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

Minimum Spanning Tree

Input : a weighted graph G=(V, E) Assumption : edge weights are real numbers each can be stored in a computer word Input size : V , E

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

Primality Testing

Input : a positive integer n Input size : log n e.g., 1,000,000 -> 1 + log 2 106 = 20 bits

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

Primality Testing

Input : an positive integer n, n < 2237 Input size : constant

slide-3
SLIDE 3

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

Cost vs. Instance Size

Running time sizes of instances n

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

Cost vs. Problem Instances

Running time Instances of size n

worst case best case

  • avg. case

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

Analysis of Algorithms

Running time sizes of instances n

worst case best case

  • avg. case

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

Average-Case Analysis

Expected cost Depend on frequencies of instances of size n

  • ccur in practice
  • n

I I avg

I t I p n t ) ( ) ( ) (

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

Insertion Sort

Demo

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

Insertion Sort : Analysis

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

n (n-1) (n-1) (n-1)

n j j

t

2

) (

  • n

j j

t

2

) 1 (

  • n

j j

t

2

) 1 (

tj tj -1 tj -1 c1 c2 c3 c4 c5 c6 c7

slide-4
SLIDE 4

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

) 1 ( ) 1 ( ) 1 ( ) ( ) 1 ( ) 1 ( ) (

7 2 6 2 5 2 4 3 2 1

  • Г
  • Г
  • Г

Г

  • Г
  • Г
  • n

c t c t c t c n c n c n c n t

n j j n j j n j j

Insertion Sort : Best-Case

function linear c c c c n c c c c c n c c c c n c n c n c n t

n j n j n j best

  • Г

Г Г

  • Г

Г Г Г

  • Г
  • Г
  • Г

Г

  • Г
  • Г
  • )

( ) ( ) 1 ( ) 1 1 ( ) 1 1 ( ) 1 ( ) 1 ( ) 1 ( ) (

7 4 3 2 7 4 3 2 1 7 2 6 2 5 2 4 3 2 1

Best-case : tj =1

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

Insertion Sort : Worst-Case

) 1 ( ) 1 ( ) 1 ( ) ( ) 1 ( ) 1 ( ) (

7 2 6 2 5 2 4 3 2 1

  • Г
  • Г
  • Г

Г

  • Г
  • Г
  • n

c t c t c t c n c n c n c n t

n j j n j j n j j

  • function

quadratic c c c c n c c c c c c c n c c c n c j c j c j c n c n c n c n t

n j n j n j worst

  • Г

Г Г

  • Г
  • Г

Г Г Г

  • Г

Г

  • Г
  • Г
  • Г

Г

  • Г
  • Г
  • 7

4 3 2 7 6 5 4 3 2 1 2 6 5 4 7 2 6 2 5 2 4 3 2 1

2 2 ) 1 ( ) 1 ( ) 1 ( ) ( ) 1 ( ) 1 ( ) (

Worst-case : tj = j

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

Insertion Sort : Average-Case

) 1 ( ) 1 ( ) 1 ( ) ( ) 1 ( ) 1 ( ) (

7 2 6 2 5 2 4 3 2 1

  • Г
  • Г
  • Г

Г

  • Г
  • Г
  • n

c t c t c t c n c n c n c n t

n j j n j j n j j

  • function

quadratic c c c c n c c c c c c c n c c c n c j c j c j c n c n c n c n t

n j n j n j avg

  • Г

Г Г

  • Г
  • Г

Г Г Г

  • Г

Г

  • Г
  • Г
  • Г

Г

  • Г
  • Г
  • 7

4 3 2 7 6 5 4 3 2 1 2 6 5 4 7 2 6 2 5 2 4 3 2 1

4 4 ) 1 ( ) 1 2 / ( ) 1 2 / ( ) 2 / ( ) 1 ( ) 1 ( ) (

Average-case : tj = j/2

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

Insertion Sort

time sizes of instances n

worst case best case average case

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

Best, Average, Worst

Best-case is usually ruled out Average-case is good,

but hard to measure effectively not clear what an average input is

Worst-case is usually fairly easy to analyze and

  • ften close to the average

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

Analysis : Simplification

Time the number of elementary instructions get executed

slide-5
SLIDE 5

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

) 1 ( ) 1 ( ) 1 ( ) 1 ( ) 1 ( ) (

7 2 6 2 5 2 4 3 2 1

  • Г
  • Г
  • Г

Г

  • Г
  • Г
  • n

c t c t c t c n c n c n c n t

n j j n j j n j j

Time vs. # Instructions

  • Г
  • n

j j

t n c

2

3 1 2

  • Г
  • Г
  • Г

Г

  • Г
  • Г
  • )

1 ( ) 1 ( ) 1 ( ) 1 ( ) 1 ( ) , , , , , , max(

2 2 2 7 6 5 4 3 2 1

n t t t n n n c c c c c c c

n j j n j j n j j

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

Elementary Operation

Operation whose execution time can be bounded above by a constant Examples

128-bit multiplication n-bit multiplication

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

Selection Sort

SelectionSort( A[1..n], n ) { for j = n downto 2 { k = MaxIndex( A[1..j], j ) Swap( A, k, j ) } }

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

Wilsons Algorithm

Wilson( n ) m = (n-1)! + 1 if m mod n = 0 then return TRUE else return FALSE

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

Analysis : More Simplification

To simplify running-time analysis

count only Barometer instructions use asymptotic analysis

Sufficient for obtaining growth rate of running time

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

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

Barometer

n j j

t

2

) (

  • n

j j

t c n t

2

) (

slide-6
SLIDE 6

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

Asymptotic Analysis

  • n

j j

t c n t

2

) (

2

n O

  • 2

2

2

  • Г
  • n

n c

  • n

j worst

j c n t

2

) (

  • Г
  • 1

2 ) 1 (n n c

  • n

j j

t c n t

2

) (

  • n

j worst

j c n t

2

) (

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

Conclusion

ก instance

instances

worst-case, best-case, average-case analysis

Barometer asymptotic notation กก