• Tidak ada hasil yang ditemukan

Design and Analysis of Algorithms

N/A
N/A
Protected

Academic year: 2025

Membagikan "Design and Analysis of Algorithms"

Copied!
9
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

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

Three Main Topics

Algorithm Design Algorithm Analysis Computation Complexity

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

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

(2)

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

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

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

Sorting

Input Output

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

(3)

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

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

Bin Packing

Input Output

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

Primality Testing

Input Output

8338169264555846052842102071 NO

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 you r sex, Let me be you r hero;

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

Positively.

(4)

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

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.

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

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.

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(&%}{lps 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

!" !

(5)

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

}

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

}

Search forward

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

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

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

(6)

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

Algorithm Design Techniques

Brute Force Incremental Divide and Conquer Dynamic Programming Greedy Algorithm Search and Traversal Probabilistic Algorithm Approximation Algorithm

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

Algorithm Design

?

?

?

!" !

N

Y

Y

N

Y N Brute Force

Incremental Divide & Conquer Dynamic Prog.

Greedy Search & Traversal

Approximation Probabilistic !" !

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

Analysis of Algorithms

ABCDEFGCDHAIJAKLMNONPQRNCAIS !

"!! memory $%&'(&! ! Worst case analysis

Average case analysis Amortized analysis

Algorithm Complexity

time

size of instance best avg.

worst

Algorithm Complexity

time

size of instance best avg.

worst

(7)

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)

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 Complexity

Problem reduction 0

0 A B

A B B A

0 0 2

=

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

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

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

(8)

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

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

!"#$#!%&'(

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

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

Fib2 on 1-MIPS Machine

n 10 20 30 50 100

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

t(n) n , 1.618

Moores Law

MooreYs Law :

N[R 80 G\ ]D[ Pentium XXX 10 14 GHz.

(BC_R`BCabNOI[bIcQNO 10 17 BI`)

defBQ 10 9 yrs. BLabNE` Fn mod k BabN n = 181 ( t(n) n log 1.618 10 17 81 )

CN Pentium XXXX 10 1000 GHz !!!!!

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

(9)

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

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

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 /+1 Fib 3 +

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

Conclusion

Chf]PRGijE

Chf]PRAJ[NNRkll

Chf]PRAJ[ABCDEF

mRSno Nph`I[bNPQRNCAIS

Referensi

Dokumen terkait

Sometimes only few processors can be used to generate all m-permutations of n items. A surpris- ingly simple parallel algorithm can be developed for this situation. The algorithm

In essence, as long as your index.php page has the right code, you can display every piece of content you enter in the WordPress admin with it.. In the index.php file, you should

Table of dimensions and facts and their attributes in this study include: 1 Work_Unit Table Table 1 Work Unit Field Type Description Unit_Code String Work Unit Code Unit_Name

Greedy Algorithms: Example  Consider items in decreasing order of the cost per unit volume value..  Let G1,…,Gn be the volume of items in the sack chosen by

Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers with solutions etc.. Visit https://www.tutorialsduniya.com for Notes, books, programs, question papers

This laboratory implements sorting techniques using divide and conquer strategy, shortest distance algorithms based on Greedy, Dynamic programming techniques, Minimum spanning tree

Sri Chandrasekharendra Saraswathi Viswa Mahavidyalaya Department of Computer Science and Engineering Page 64 of 162 Syllabus B.E[CSE] Full Time L T P C 3 2 0 3 CS4T4 - DESIGN AND

TheAssertclass provides several functions to help you test the state of your program as it runs: Assert public class Assert { static public void preboolean test, String message //