290210 data structures and algorithms

30
290210 Data Structures and Algorithms กกกกกกกกกกกก (Recursion) อ.อออออออออ อออออออออ อออออออออออออออออออออออออออ ออออออออออออออออ ออออออออออออออออ ออออออออ [email protected]

Upload: eliana-morin

Post on 01-Jan-2016

34 views

Category:

Documents


2 download

DESCRIPTION

290210 Data Structures and Algorithms. การเรียกซ้ำ (Recursion) อ.ธารารัตน์ พวงสุวรรณ คณะวิทยาศาสตร์และศิลปศาสตร์ มหาวิทยาลัยบูรพา วิทยาเขตสารสนเทศจันทบุรี [email protected]. การเรียกซ้ำ (Recursion). - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 290210 Data Structures and Algorithms

290210 Data Structures and Algorithms

การเร�ยกซ้ำ�า (Recursion)

อ.ธาราร�ตน์� พวงสุ�วรรณคณะว�ทยาศาสุตร�และศ�ลปศาสุตร�

มหาว�ทยาล�ยบู�รพา ว�ทยาเขตสุารสุน์เทศจั�น์ทบู�ร [email protected]

Page 2: 290210 Data Structures and Algorithms

การเร�ยกซ้ำ�า (Recursion)

เป!น์การน์�ยามการแก#ป$ญหาด้#วยข�'น์ตอน์ว�ธ แบูบูวน์ซ้ำ)'า (Repetition Algorithm) โด้ยม การเร ยกใช้#งาน์ต�วม�น์เอง (Call itself)

เร ยกการเข ยน์โปรแกรมใน์ล�กษณะเร ยกใช้#งาน์ต�วเองว.า Recursive Programming

เร ยกฟั$งก�ช้� 0น์ท 0ม การเร ยกใช้#งาน์ต�วม�น์เองว.า Recursive Function

Page 3: 290210 Data Structures and Algorithms

Approach to writing Repetition Algorithms

ม 2 Approach ค1อ Use Iteration Use Recursion

Recursion is a repetitive process in which an algorithm calls itself

Some older languages do not support recursion

Page 4: 290210 Data Structures and Algorithms

Recursive Function

ฟั$งก�ช้�น์เร ยกตน์เอง จัะท)างาน์ได้#อย.างสุมบู�รณ� จัะต#องม องค�ประกอบู ด้�งต.อไปน์ '

ม การเร ยกใช้#ฟั$งก�ช้�น์ท 0ม ช้10อเป!น์ช้10อฟั$งก�ช้�น์ของตน์เอง เช้.น์ ช้10อของตน์ค1อ factorial ก3จัะม การเร ยกใช้#ฟั$งก�ช้�น์ factorial()ภายใน์ซ้ำ50งอาจัจัะอย�.ใน์สุ.วน์ค)าสุ�0งหร1อเป!น์องค�ประกอบูใน์น์�พจัน์�

ม การร�บูค.าเข#าทางพาราม�เตอร�ของฟั$งก�ช้�น์  โด้ยค.าด้�งกล.าวจัะถู�กน์)าไปใช้#ภายใน์ฟั$งก�ช้�น์เอง ซ้ำ50งผลของการค)าน์วณบูางสุ.วน์จัะถู�กน์)าไปใช้#เวลาท 0ม การเร ยกใช้# ช้10อ ฟั$งก�ช้�น์ตน์เองภายใน์ต�ว” ”ฟั$งก�ช้�น์

Page 5: 290210 Data Structures and Algorithms

Recursive Function

ม การสุ.งผลการค)าน์วณกล�บู   ซ้ำ50งผลด้�งกล.าวจัะน์)าไปใช้#ใน์การค)าน์วณใน์ฟั$งก�ช้�น์เร ยกตน์เองหร1ออาจัจัะสุ.งกล�บูให#ก�บูโปรแกรม

ม จั�ด้สุ�'น์สุ�ด้ของการเร ยกตน์เอง   ฟั$งก�ช้�น์ เร ยกตน์เองจัะท)างาน์ไม.ได้#หากไม.ม ช้.องทางให#โปรแกรมหย�ด้การเร ยกตน์เองใน์ข�'น์ ใด้ข�'น์หน์50ง เพราะจัะเก�ด้การเร ยกตน์เองแบูบูไม.ม สุ�'น์สุ�ด้

Page 6: 290210 Data Structures and Algorithms

Recursive Programming

บูางป$ญหาสุามารถูท 0จัะเข ยน์ใน์ร�ปแบูบูของ recursion จัะง.ายกว.า หล�กการแก#ป$ญหาใน์ร�ปแบูบู recursion

1 . น์�ยามป$ญหาใน์ร�ปการเร ยกต�วเอง 2. ม เง10อน์ไขสุ)าหร�บูจับูการท)างาน์

“One important requirement for a recursive algorithm to be correct is that it not generate an infinite sequence of call on itself”

Page 7: 290210 Data Structures and Algorithms

ลั�กษณะของ Recursive function

call itself directly call itself indirectly หร1อเร ยกว.า

Recursive chains

Page 8: 290210 Data Structures and Algorithms

Recursive Programming

ตั�วอย�าง S(n) =

1 +2 +3 +4 +5 +6 +7 +…+n S(1 ) = 1 S(2 ) = 1 +2 = S(1 )+2 S(3 ) = 1 +2 +3 =

     S(1 )+2 +3 = S(2 )+3จะได้� -S(n) = S(n 1 )+n

Page 9: 290210 Data Structures and Algorithms

ต�วอย.าง recursive function ท 0ไม.ควรท)าint main(void) {

printf(“The universe is never ending\n”);

main();

return 0;

} //an infinite sequence of call on itself

Recursive Programming

Page 10: 290210 Data Structures and Algorithms

ตั�วอย�างของ Recursive chainsa(formal parameters) b(formal

parameters){ {

. .

. .

. .b(arguments);a(arguments);

} /* end a */ } /* end b */“Both a and b are recursive”

Recursive chains

Page 11: 290210 Data Structures and Algorithms

A Classic Recursive Case

Factorial เป!น์ case study Factorial(n) = 1

if n=0

n x (n-1) x (n-2) x ... x 3 x 2 x 1 if n >0

Page 12: 290210 Data Structures and Algorithms

Iterative Factorial Algorithm

Algorithm iterativeFactorial (n)Calculates the factorial of a number using a loop

Pre n is the number to be raised factorialllyPost n! is returnedReturn factN เป็�นค่�า n!

1 set i to 12 set factN to 1 3 loop( i <= n) 1 set factN to factN * i 2 increment i4 end loop5 return factNend iterativeFactorial

Page 13: 290210 Data Structures and Algorithms

Recursive Factorial Algorithm

Algorithm recursiveFactorial (n)Calculates factorial of a number using recursion

Pre n is the number to be raised factorialllyPost n! is returnedReturn ค่�า n!

1 if (n equal 0)1 return 1

2 else 1 return (n * recursiveFactorial (n-

1))3 end ifend recursiveFactorial

Page 14: 290210 Data Structures and Algorithms

การค)าน์วณหาค.าแฟักทอเร ยลของจั)าน์วน์เต3มบูวก

การน์�ยามแบูบู iterativen! = n * (n-1) * (n-2) * ... 1

อาจัเข ยน์ค)าจั)าก�ด้ความของฟั$งก�ช้�น์ ได้#ว.าn! = 1 if n ==0

n! = n * (n-1) * (n-2) * ... * 1 if n > 0

Page 15: 290210 Data Structures and Algorithms

//Function for calculating factorial of integerlong int fact(int n){

int x;prod = 1;for(x = n; x > 0; x--)

prod = prod * x;return(prod);

}

Iterative programming

Page 16: 290210 Data Structures and Algorithms

การน ยามแบบ Recursion

1 .เข ยน์ป$ญหาใน์ร�ปการเร ยกต�วเองn! = n * (n-1)!

อาจัเข ยน์ค)าจั)าก�ด้ความของฟั$งก�ช้�น์ ได้#ว.าn! = 1 if n ==0n! = n * (n-1)! if n > 0

2. หาเง10อน์ไขสุ)าหร�บูจับูการท)างาน์ค1อ เม10อเร ยกต�วเองจัน์ถู5ง 0! = 1

Page 17: 290210 Data Structures and Algorithms

Recursive programming

//Function for calculating factorial of integerlong int fact(int n){

if(n == 0) //condition for end calling itselfreturn(1)

elsereturn(n * fact(n-1));

}

Recursion

Page 18: 290210 Data Structures and Algorithms

“Recursive function จะทำ�างานในลั�าด้�บทำ�&กลั�บก�นก�บเวลัาเร�ยกใช้�” จัากโปรแกรมหาค.าแฟักทอเร ยลแบูบู recursion การเร ยกใช้#จัะเป!น์ล)าด้�บูด้�งน์ '

n! = n * (n-1)!(n-1)! = (n-1) * (n-2)!(n-2)! = (n-2) * (n-3)!

.........

2! = 2 * 1!1! =1 * 0!0! = 1

Recursive programming

Page 19: 290210 Data Structures and Algorithms

ค.าท 0สุ.งกล�บูมาจัะเป!น์ล)าด้�บูท 0กล�บูก�น์ ด้�งน์ '0 ! = 11! = 1 * 0! = 12! = 2 * 1! = 2 * 1 = 23! = 3 * 2! = 3 * 2 = 64! = 4 * 3! = 4 * 6 = 24

........

n! = n * (n-1)! = ......

Recursive programming

Page 20: 290210 Data Structures and Algorithms

ด้�งน์�'น์ สุ.ง 4 เข#าไปให#ฟั$งก�ช้�น์ พอเร ยกฟั$งก�ช้�น์ จัะได้#return(4 *

return(3 *

return(2 *

return(1 *

return(1);

จัะเห3น์ว.า ค.าสุ.งกล�บูจัะเป!น์ return(4*3*2*1*1)

Recursive programming

Page 21: 290210 Data Structures and Algorithms

Recursive Example

เป!น์ต�วอย.างของการแก#ป$ญหาแบูบู Recursion

Greatest Common Divisor (GCD)

Fibonacci NumbersTowers of Hanoi

Page 22: 290210 Data Structures and Algorithms

Greatest Common Division(GCD) เป!น์ฟั$งก�ช้�น์ทางคณ�ตศาสุตร� ท 0ร� #จั�กก�น์ว.า การหาต�ว

หารร.วมมาก (ห.ร.ม.) gcd ของจั)าน์วน์เต3มซ้ำ50งไม.เป!น์ 0 พร#อมก�น์ ค1อ

จั)าน์วน์เต3มท 0มากท 0สุ�ด้ท 0หารท�'งสุองจั)าน์วน์ลงต�ว เช้.น์ gcd ของ 10 ก�บู 25 ค1อ 5 ม ประโยช้น์�ใน์การท)าเศษสุ.วน์ให#เป!น์เศษสุ.วน์อย.างต)0า ใช้# Euclidean Algorithm ใน์การหา gcd ของ

จั)าน์วน์เต3มท 0ไม.เป!น์ค.าลบู

Page 23: 290210 Data Structures and Algorithms

Greatest Common Division(GCD)

gcd (a,b) = aif b = 0

bif a = 0

gcd (b, a mod b)otherwise

Page 24: 290210 Data Structures and Algorithms

Greatest Common Division(GCD)Algorithm gcd (a, b)Calculates greatest common division using the Euclidean

algorithmPre a and b are positive integers greater than 0Post greatest common divisor returned

..............................................

...............................................

.................................................

.................................................

..................................................

..................................................

.................................................end gcd

Page 25: 290210 Data Structures and Algorithms

การหา Fibonacci numbers

น์�ยาม

Fib(n) = n if n = 0 or n = 1Fib(n) = Fib(n-1) + Fib(n-2) if n > 1

การหาช้�ด้ของ Fabonacci series จัะต#องทราบูต�วเลข 2 ต�วแรก

Page 26: 290210 Data Structures and Algorithms

Fibonacci numbers

เข ยน์เป!น์ recursive function ได้#ว.าint fib(int n) {

int x,y;if(n<= 1)

return(n);x = fib(n-1);y = fib(n-2);return(x+y);

}

Page 27: 290210 Data Structures and Algorithms

Tower of Hanoi ProblemA B C

ข�อก�าหนด้- เคล10อน์ย#ายคร�'งละ 1 disk เท.าน์�'น์โด้ยเคล10อน์ย#ายจัาก disk บูน์สุ�ด้ก.อน์- ไม.อน์�ญาตให# disk ท 0ใหญ.กว.าซ้ำ#อน์บูน์ disk ท 0เล3กกว.า

เป็)าหมายต#องการเคล10อน์ย#าย disk ท�'งหมด้ไปไว#อ ก peg หน์50ง

Page 28: 290210 Data Structures and Algorithms

จัากร�ปด้�งกล.าว หากเราต#องการเคล10อน์ย#าย disk ท�'งหมด้จัาก A ไป CRecursive solution for Towers of Hanoi Problem

1. if n==1, move the single disk from A to C and stop

2. Move the top n-1 disks from A to B, using C as auxiliary

3. Move the remaining disk from A to C

4. Move the n-1 disks from B to C, using A as auxiliary

Page 29: 290210 Data Structures and Algorithms

Efficiency of recursion

โด้ยท�0วไป โปรแกรมท 0ม ล�กษณะ nonrecursive การประมวลผลจัะม ประสุ�ทธ�ภาพด้ กว.าโปรแกรมท 0ม ล�กษณะ recursive ใน์ด้#าน์ของเวลาและพ1'น์ท 0หน์.วยความจั)า (space) แต.ล�กษณะบูางโปรแกรมก3เหมาะท 0จัะเข ยน์แบูบู recursive เพราะจัะท)าให#ง.ายกว.าและเก�ด้ข#อผ�ด้พลาด้ได้#น์#อยกว.าแบูบู nonrecursive

ข�อด้�ของ recursive programming

- เข ยน์ได้#สุ� 'น์ ง.าย- มองเห3น์แน์วทางการแก#ป$ญหาได้#ช้�ด้เจัน์- ใช้#เวลาไม.มากใน์การเข ยน์โปรแกรม

Page 30: 290210 Data Structures and Algorithms

ข�อเสี�ยของ recursive programming

บูางป$ญหาไม.เหมาะท 0จัะแก#ป$ญหาด้#วย recursive programming เน์10องจัากท)าให#ม การท)างาน์ท 0ซ้ำ)'าซ้ำ#อน์

เช้.น์ การหา Fibonacci numberFib(n) = n if n = 0 or n = 1Fib(n) = Fib(n-1) + Fib(n-2) if n > 1

ต�วอย.าง Fib(5) = Fib(4) + Fib(3) Fib(4) = Fib(3) + Fib(2) Fib(3) = Fib(2) + Fib(1) Fib(2) = Fib(1) + Fib(0)