23
Recursion (part 1) Recursion (part 1) HKUCS Provinci Training 2010-01-28 Chan Ho Leung

Recursion (part 1) HKUCS Provinci Training 2010-01-28 Chan Ho Leung

Embed Size (px)

Citation preview

Page 1: Recursion (part 1) HKUCS Provinci Training 2010-01-28 Chan Ho Leung

Recursion (part 1)Recursion (part 1)HKUCS Provinci Training 2010-01-28

Chan Ho Leung

Page 2: Recursion (part 1) HKUCS Provinci Training 2010-01-28 Chan Ho Leung

A problemA problem

Given a 2n x 2n board with the top right corner removed. Show how to cover it using L-shape tiles.

Page 3: Recursion (part 1) HKUCS Provinci Training 2010-01-28 Chan Ho Leung

A problemA problem

Given a 2n x 2n board with the top right corner removed. Show how to cover it using L-shape tiles.

Page 4: Recursion (part 1) HKUCS Provinci Training 2010-01-28 Chan Ho Leung

A problemA problem

Given a 2n x 2n board with the top right corner removed. Show how to cover it using L-shape tiles.

Page 5: Recursion (part 1) HKUCS Provinci Training 2010-01-28 Chan Ho Leung

A problemA problem

Given a 2n x 2n board with the top right corner removed. Show how to cover it using L-shape tiles.

Page 6: Recursion (part 1) HKUCS Provinci Training 2010-01-28 Chan Ho Leung

A problemA problem

Given a 2n x 2n board with the top right corner removed. Show how to cover it using L-shape tiles.

Page 7: Recursion (part 1) HKUCS Provinci Training 2010-01-28 Chan Ho Leung

A solutionA solution

Given a 2n x 2n board with the top right corner removed. Show how to cover it using L-shape tiles.

2n-1

2n-1

Page 8: Recursion (part 1) HKUCS Provinci Training 2010-01-28 Chan Ho Leung

A solutionA solution

Given a 2n x 2n board with the top right corner removed. Show how to cover it using L-shape tiles.

2n-1

2n-1 2

2

If a problem can be reduced to a smaller problem, I should be able to solve it easily.

If a problem can be reduced to a smaller problem, I should be able to solve it easily.

Page 9: Recursion (part 1) HKUCS Provinci Training 2010-01-28 Chan Ho Leung

Tower of HanoiTower of Hanoi

Legend: Inside a Vietnamese temple there are three rods (labeled as r1, r2, and r3), surrounded by 64 golden disks of different sizes. The monks of Hanoi, by the command of an ancient prophecy, have been moving these disks according to the following rules •at each step, take the upper disk of one rod and place it to the top of another rod•no bigger disk can be placed on top of a smaller disk

Page 10: Recursion (part 1) HKUCS Provinci Training 2010-01-28 Chan Ho Leung

Game timeGame time

http://wipos.p.lodz.pl/zylla/games/hanoi5e.html

Page 11: Recursion (part 1) HKUCS Provinci Training 2010-01-28 Chan Ho Leung

Smaller problem?Smaller problem?

A B C

move disks from A to C

A B C

1. Move top n-1 disks from A to B2. Move a disk from A to C3. Move the top n-1 disks from B to C

Page 12: Recursion (part 1) HKUCS Provinci Training 2010-01-28 Chan Ho Leung

ProgramProgram#include <iostream>using namespace std;

void hanoi( char A, char B, char C, int n ){if( n==1 )

cout << A << “ to “ << C << endl;}else{

hanoi( A, C, B, n-1 );cout << A << “ to “ << C << endl;hanoi( B, A, C, n-1 );

}}

int main(){hanoi( ‘L’, ‘M’, ‘R’, 5);

}

If a problem can be reduced to a smaller problem, I should be able to solve it easily.

If a problem can be reduced to a smaller problem, I should be able to solve it easily.

Page 13: Recursion (part 1) HKUCS Provinci Training 2010-01-28 Chan Ho Leung

RecursionRecursion Solving a problem by reducing it into a smaller problem is

called recursion.

Solve it by solving same problem with smaller size ◦ [The problem can be reduced to a smaller problem.]◦ [The problem is recursive.]

Small enough => solve it directly◦ [The problem has base case]

Page 14: Recursion (part 1) HKUCS Provinci Training 2010-01-28 Chan Ho Leung

UVA 374UVA 374 Given B, P, and M, find

BP mod M

Idea 1.

BP = B * BP-1

B1 = B

0 ≤ B, P ≤ 2,147,483,647 1 ≤ M ≤ 46340 Too slow to find BP

Idea 2.

BP = BP/2 * BP/2 * (P%2?B:1)B1 = B

Things to know:1.Can int store 2,147,483,647?2.Can int store BP?

Page 15: Recursion (part 1) HKUCS Provinci Training 2010-01-28 Chan Ho Leung

Modular arithmeticModular arithmetic Modular arithmetic is the arithmetic of congruence,

sometimes known informally as "clock arithmetic.“ – From mathworld.wolfram.com

Arithmetic where we only consider the remainder E.g., 4 = 9 (mod 5) (5 is called “modulus”) 10 = 5 (mod 5) = 0 (mod 5) 14 = 27 (mod 13) -1 = 12 (mod 13)

14 + 5 = 19 (mod 13) = 6 (mod 13) 5 – 7 = -2 (mod 13) = 11 (mod 13) 5*7 = 35 (mod 13) = 9 (mod 13)

Page 16: Recursion (part 1) HKUCS Provinci Training 2010-01-28 Chan Ho Leung

Properties of modular Properties of modular arithmeticarithmetic Note that 4 = 9 = 14 (mod 5) Define the residue of N (mod D) as the smallest integer r in

{0,1,…, D-1} such that N = r (mod D). Denote the residue of N (mod D) by N%D. (“N mod D”)

For any integers A, B and modulus D,A + B = A%D + B%D (mod D)A - B = A%D - B%D (mod D)

A * B = A%D * B%D (mod D)e.g., let D = 521 + 33 = 21%5 + 33%5 = 4 (mod 5)21 * 33 = 1 * 3 (mod 5)

Proof. Note that A can be uniquely written as kD + A%D for some integer k. Similarly, B = k’D + B%D. Hence, A + B = (k+k’)D + A%D + B%D

Page 17: Recursion (part 1) HKUCS Provinci Training 2010-01-28 Chan Ho Leung

UVA 374UVA 374 Given B, P, and M, find

BP mod M

Idea 1.

BP = B * BP-1

B1 = B

0 ≤ B, P ≤ 2,147,483,647 1 ≤ M ≤ 46340 Too slow to find BP

Idea 2.

BP = BP/2 * BP/2 * (P%2?B:1)B1 = B

Things to know:1.Can int store 2,147,483,647?2.Can int store BP?

3.BP % M = BP/2 % M * BP/2 %M * ( )%M4.Can int store the right hand side?5.BP % M = [BP/2 % M * BP/2 %M]%M * ( )%M

6.B1 % M = B % M

Page 18: Recursion (part 1) HKUCS Provinci Training 2010-01-28 Chan Ho Leung

UVA 374UVA 374#include <iostream>using namespace std;

int mod( int B, int P, int M ){ if( P==0 ){ return 1%M; }else if( P==1 ){ return B%M; }else{ int half = mod( B, P/2, M ); return (half*half)%M*(P%2?B:1)%M; }}

int main(){ int B, P, M; while( cin >> B ){ cin >> P >> M; cout << mod( B, P, M ) << endl; }}

This program has 1 bug.

Page 19: Recursion (part 1) HKUCS Provinci Training 2010-01-28 Chan Ho Leung

UVA 10940UVA 10940 Simulation probably does not work Hint 1. 31 lines

What is the rank of the card that remains? Assume n is even

S = x1 x2 x3 x4 x5 x6 x7 x8 x9 … x2k After removing all odd cards

S’ = x2 x4 x6 x8 … x2k If someone can tell you the rank of the remaining card in S’…

Assume n is odd

S = x1 x2 x3 x4 x5 x6 x7 x8 x9 … x2k x2k+1 After removing all odd cards

S’ = x4 x6 x8 … x2k 2 If someone can tell you the rank of the remaining card in S’ …

Page 20: Recursion (part 1) HKUCS Provinci Training 2010-01-28 Chan Ho Leung

What is the rank that What is the rank that remains?remains?

S(n): which rank survive?

S(n/2): which rank survive?

S(2): which rank survive?

S(1): which rank survive?

There are at most log n recursive calls

Page 21: Recursion (part 1) HKUCS Provinci Training 2010-01-28 Chan Ho Leung

What is recursion good What is recursion good for?for?When you see the recursive

relationship.◦ Very easy to write (usually)

When you want to do brute force testing.

Page 22: Recursion (part 1) HKUCS Provinci Training 2010-01-28 Chan Ho Leung

UVA ExercisesUVA Exercises110 – Sorting297 – Quadtree

◦The problems are long, but are not difficult

10994 – Simple addition◦A bit hard to think

10689◦need to use matrix

10549, 195, 536◦not sure

Page 23: Recursion (part 1) HKUCS Provinci Training 2010-01-28 Chan Ho Leung

Job opportunitiesJob opportunities$200 per week as web master

◦Duties: update the homepage, build and organize the content