• Tidak ada hasil yang ditemukan

PDF Three Main Topics Design and Analysis of Algorithms

N/A
N/A
Protected

Academic year: 2025

Membagikan "PDF Three Main Topics Design and Analysis of Algorithms"

Copied!
13
0
0

Teks penuh

(1)

Design and Analysis of Algorithms

ความนํา

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

เนื้อหา

• Introduction

• Computational problems

• Algorithms

• Algorithm Design Techniques

• Analysis of Algorithms

• Examples

Three Main Topics

• Algorithm Design

• Algorithm Analysis

• Computation Complexity

Design and Analysis Steps

มีปญหา หาวิธีแกไข

ยอมรับได ?

หมดหวัง ?

พิสูจนวายาก ?

หาวิธีแกไขแบบ ไมทะเยอทะยาน จบ

N

Y

Y

N

Y

N

(2)

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

Design and Analysis Steps

มีปญหา หาวิธีแกไข

ยอมรับได ?

หมดหวัง ?

พิสูจนวายาก ?

หาวิธีแกไขแบบ ไมทะเยอทะยาน จบ

N

Y

Y

N

Y

N

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

Computational Problems

• A computational (or algorithmic) problem is specified by a precise definition of

– the legal inputs

– the required outputs as a function of those inputs

Sorting Problem

• Sorting problem instances : < 31, 41, 59, 26, 41 >

<1, 2, 5, 7, 6, 9, 2>

<2, 2, 2>

• Input : a sequence of n numbers < a 1 , a 2 , ..., a n >

• Output : a reordering < a’ 1 , a’ 2 , ..., a’ n > of the input such that a’ 1 ≤ a’ 2 ≤ ... ≤ a’ n

Problem Instance Input Instance Instance

Sorting

Input Output

(3)

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

Median and Selection

Input Output

k = 10

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

Minimum Spanning Tree

Input Output

Steiner Tree

Input Output

Bandwidth Minimization

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

Input Output

(4)

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

Traveling Salesman Problem

Input Output

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

Convex Hull

Input Output

Bin Packing

Input Output

Primality Testing

Input Output

8338169264555846052842102071 NO

(5)

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

Factoring

Input Output

8338169264555846052842102071

179424673 x 2038074743 x 22801763489

= 8338169264555846052842102071

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

String Matching

Input Output

You are the fairest of your sex, Let me be your hero;

I love you as one over x, As x approaches zero.

Positively.

you

You are the fairest of your sex, Let me be your hero;

I love you as one over x, As x approaches zero.

Positively.

Approximate String Matching

Input Output

You are the fairest of your sex, Let me be your hero;

I love you as one over x, As x approaches zero.

Positively.

heero

You are the fairest of your sex, Let me be your hero;

I love you as one over x, As x approaches zero.

Positively.

Data Compression

Input Output

You are the fairest of your sex, Let me be your hero;

I love you as one over x, As x approaches zero.

Positively.

You are the fairest of your sex, Let me be your hero;

I love you as one over x, As x approaches zero.

Positively.

(6)

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

Cryptography

Input Output

You are the fairest of your sex, Let me be your hero;

I love you as one over x, As x approaches zero.

Positively.

)*#$(*KJSNpsld09LKDF0osk POS)s8sd,??<CNZisd(&sD(6%

(^%#&(dls28s8&AK)8dksF_8 OSD7slx.zz846(&%}{l’ps ska@

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

Satisfiability

Input Output

( x + y ) ( x + y ) x NO

Halting

Input Output

while x ≠ 1 do ?

if x is even then x = x/2 else x = 3x+1

x = 7

Design and Analysis Steps

มีปญหา หาวิธีแกไข

ยอมรับได ?

หมดหวัง ?

พิสูจนวายาก ?

หาวิธีแกไขแบบ ไมทะเยอทะยาน จบ

N

Y

Y

N

Y

N

หาวิธีแกไข

หาวิธีแกไขแบบ

ไมทะเยอทะยาน

(7)

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

Algorithms

• A sequence of computational steps that

transform any legal input into the desired output

• A tool for solving a well-specified computational problem

• Idea behinds a computer program

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

Sequential Search

SeqSearch( D[1..n], x ) {

i = 1

while ( i <= n && D[i] != x ) i = i + 1

if ( i > n ) return 0 return i

}

Sequential Search

SeqSearch( D[1..n], x ) {

i = 1

while ( i <= n && D[i] != x ) i = i + 1

if ( i > n ) return 0 return i

}

Search forward

Sequential Search

SeqSearch( D[1..n], x ) {

i = n

while ( i >= 1 && D[i] != x ) i = i - 1

if ( i < 1 ) return 0 return i

}

Search backward

(8)

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

Sequential Search

SeqSearch( D[1..n], x ) {

i = n; D[0] = x while ( D[i] != x ) i = i - 1

return i }

Search backward with sentinel

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

Algorithms

• An algorithms is correct if, for every input instance, it halts with the correct output.

• We seek algorithms which are

– correct – efficient

Algorithm Design Techniques

• Brute Force

• Incremental

• Divide and Conquer

• Dynamic Programming

• Greedy Algorithm

• Search and Traversal

• Probabilistic Algorithm

• Approximation Algorithm

Algorithm Design

มีปญหา หาวิธีแกไข

ยอมรับได ?

หมดหวัง ?

พิสูจนวายาก ?

หาวิธีแกไขแบบ ไมทะเยอทะยาน จบ

N

Y

Y

N

Y

N

•Brute Force

•Incremental

•Divide & Conquer

•Dynamic Prog.

•Greedy

•Search & Traversal

•Approximation

•Probabilistic

หาวิธีแกไข

หาวิธีแกไขแบบ

ไมทะเยอทะยาน

(9)

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

Abu Abd-Allah ibn Musa al'Khwarizmi

Al'Khwarizmi (lived from 790 to 840) wrote on Hindu-Arabic numerals and was the first to use zero as a place holder in

positional base notation. The word algorithm derives from his name.

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

Design and Analysis Steps

มีปญหา หาวิธีแกไข

ยอมรับได ?

หมดหวัง ?

พิสูจนวายาก ?

หาวิธีแกไขแบบ ไมทะเยอทะยาน จบ

N

Y

Y

N

Y

ยอมรับได ? N

Analysis of Algorithms

• วิเคราะหประสิทธิภาพของอัลกอริทึม

– เวลาการทํางาน

– จํานวน memory ที่ใชในการทํางาน

• Worst case analysis

• Average case analysis

• Amortized analysis

Algorithm Complexity

time

size of instance

best

worst avg.

(10)

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

Algorithm Complexity

time

size of instance best worst avg.

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

Sorting Algorithms

• Bubble sort t(n) ∝ n 2

• Insertion sort t(n) ∝ n 2

• Shell sort t(n) ∝ n 1.xx

• Heap sort t(n) ∝ n log n

• Merge sort t(n) ∝ n log n

• Quick sort t(n) ∝ n log n (average case)

Design and Analysis Steps

มีปญหา หาวิธีแกไข

ยอมรับได ?

หมดหวัง ?

พิสูจนวายาก ?

หาวิธีแกไขแบบ ไมทะเยอทะยาน จบ

N

Y

Y

N

Y

พิสูจนวายาก ? N

Computational Complexity

• Problem reduction 0

0 A B

A B

B A 0 0

2

=

การยกกําลังสองเมตริกซไมงายกวาการคูณเมตริกซ

(11)

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

A Small Problem

• Input : a number n, k

• Output : (the n th Fibonacci number ) mod k

• Input instance : 10, 21

• Output : f 10 mod 21 = 55 mod 21 = 13

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

Fibonacci Number

n: 0 1 2 3 4 5 6 7 8 9 10 fn: 0 1 1 2 3 5 8 13 21 34 55

f n = f n-1 + f n-2 for n > 1 f 0 = 0, f 1 = 1

sss 

 

 

  −

 +

 

 +

=

n n

f n

2 5 1 2

5 1 5 1

Fib1

Fib1( n, k ) {

t1 = (1 + sqrt(5))/2 t2 = (1 - sqrt(5))/2

f = int( (t1^n + t2^n)/sqrt(5) ) return f mod k

}

sss 

 

 

  −

 +

 

 +

=

n n

f n

2 5 1 2

5 1 5 1

Fib1

Fib1( n, k ) {

t1 = (1 + sqrt(5))/2 t2 = (1 - sqrt(5))/2

f = int( (t1^n + t2^n)/sqrt(5) ) return f mod k

}

n เล็ก : t(n) ∝ n n ใหญ : t(n) ∝ ?

คําตอบอาจผิด

(12)

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

Fib2

Fib2( n, k ) {

if ( n < 2 ) return n mod k return ( Fib2(n-1,k) +

Fib2(n-2,k) ) mod k }

f n = f n-1 + f n-2 for n > 1 f 0 = 0, f 1 = 1

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

Fib2

Fib2( n, k ) {

if ( n < 2 ) return n mod k return ( Fib2(n-1,k) +

Fib2(n-2,k) ) mod k }

t(n) = t(n-1) + t(n-2) + 1 t 0 = t 1 = 1

t(n) ∝ 1.618 n

Fib2 on 1-MIPS Machine

n 10 20 30 50 100

Fib2 8ms 1s 2min 21days 10 9 yrs.

t(n) ∝ φ n , φ ≈ 1.618

Moore’s Law

• Moore’s Law :

อีก 80 ป จะมี Pentium XXX 10 14 GHz.

(เร็วกวาเครื่องที่ทดลอง 10 17 เทา)

• ใชเวลา 10 9 yrs. เพื่อหาคา Fn mod k เมื่อ n = 181 ( t(n) ∝ φ n ∴ log 1.618 10 17 ≈ 81 )

• รอ Pentium XXXX 10 1000 GHz !!!!!

(13)

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

Fib3

Fib3( n, k ) {

fn2 = 0 fn1 = 1

for i = 2 to n

fn = ( fn1 + fn2 ) mod k fn2 = fn1

fn1 = fn return fn }

f n = f n-1 + f n-2 for n > 1 f 0 = 0, f 1 = 1

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

Fib3

Fib3( n, k ) {

fn2 = 0 fn1 = 1

for i = 2 to n

fn = ( fn1 + fn2 ) mod k fn2 = fn1

fn1 = fn return fn

} t(n) ∝ n

Fib2 vs. Fib3

n 10 20 30 50 100

Fib2 8ms 1s 2min 21days 10 9 yrs.

Fib3 0.16ms 0.33ms 0.5ms 0.75ms 1.5ms มี algorithm ที่เร็วกวา Fib3 อีก

Conclusion

• รูจักปญหา

• รูจักวิธีออกแบบ

• รูจักวิธีวิเคราะห

• “กึ๋น” อยูที่อัลกอริทึม

Referensi

Dokumen terkait