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

Slides:



Advertisements
Similar presentations
The Towers of Hanoi or Apocalypse When?.
Advertisements

More Recursion: Permutations and Towers of Hanoi COP 3502.
Recursion Lecture 18: Nov 18.
Number Theory and Cryptography
Recursion, Divide and Conquer Lam Chi Kit (George) HKOI2007.
Factorial Recursion stack Binary Search Towers of Hanoi
Recursion. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
Computer Science 1620 Other Data Types. Quick Review: checklist for performing user input: 1) Be sure variable is declared 2) Prompt the user for input.
Programming Switch command. COMP102 Prog. Fundamentals: Switch command / Slide 2 Multiple Selection: The switch Statement value1 action 1 value2 action.
Recursion.
Recursion!. Question Stephen King’s books J. R. R. Tolkien’s books Recursion Do they have something in common?
Review of Recursion. Definition: Recursion - The process of a function calling itself.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 5. Recursive Algorithms.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 16: Recursion.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Introduction to Programming (in C++) Recursion Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Data Structures Mohamed Mustaq Ahmed Chapter 2- Algorithms.
Recursion Chapter Nature of Recursion t Problems that lend themselves to a recursive solution have the following characteristics: –One or more.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
Chapter 1 Introduction. Goals Why the choice of algorithms is so critical when dealing with large inputs Basic mathematical background Review of Recursion.
13.2 Recursive Definitions Objective 1) Provide the recursive definition for sequences; 2) Identify the type of a sequence from a recursive definition.
2-1 2 Algorithms Principles. Efficiency. Complexity. O-notation. Recursion. © 2001, D.A. Watt and D.F. Brown.
CompSci 102 Discrete Math for Computer Science February 16, 2012 Prof. Rodger.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Recursion Jordi Cortadella Department of Computer Science.
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data Depends.
Recursion. Circular Definition Circular definition Circular definition Dialectic materialism is materialism that is dialectic. Dialectic materialism is.
Recursion. Hanoi Tower Legend: Inside a Vietnamese temple there are three rods (labeled as r 1, r 2, and r 3 ), surrounded by n golden disks of different.
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
Informal Analysis of Merge Sort  suppose the running time (the number of operations) of merge sort is a function of the number of elements to sort  let.
ACM programming contest Introduction + Recursion.
CHAPTER 4 RECURSION. BASICALLY, A FUNCTION IS RECURSIVE IF IT INCLUDES A CALL TO ITSELF.
Upon completion, the world will end…
Chapter 6.1: Recurrence Relations Discrete Mathematical Structures: Theory and Applications.
Data Structure and Algorithms. Algorithms: efficiency and complexity Recursion Reading Algorithms.
Chinese Remainder Theorem. How many people What is x? Divided into 4s: remainder 3 x ≡ 3 (mod 4) Divided into 5s: remainder 4 x ≡ 4 (mod 5) Chinese Remainder.
Recursion. Circular Definition (not useful) E.g., E.g., Projenitor: one who produces an offspring Projenitor: one who produces an offspring Offspring:
1 Chapter 8 Recursion. 2 Objectives  To know what is a recursive function and the benefits of using recursive functions (§8.1).  To determine the base.
UNIT 17 Recursion: Towers of Hanoi.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
A Sample Program #include using namespace std; int main(void) { cout
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
Chapter 4 With Question/Answer Animations 1. Chapter Motivation Number theory is the part of mathematics devoted to the study of the integers and their.
1 7.Algorithm Efficiency These factors vary from one machine/compiler (platform) to another  Count the number of times instructions are executed So, measure.
Mathematical Induction And Recursion Discrete Math Team KS MATEMATIKA DISKRIT (DISCRETE MATHEMATICS ) 1.
1 Data Structures CSCI 132, Spring 2016 Notes 16 Tail Recursion.
Induction (chapter of the book and chapter of the notes)
Hubert Chan (Chapters 1.6, 1.7, 4.1)
Algorithm Analysis 1.
Chapter 4 Stacks
Chapter 15 Recursion.
Analysis of Algorithms
Recursion Chapter 12.
Chapter 15 Recursion.
Analysis of Algorithms
Hubert Chan (Chapters 1.6, 1.7, 4.1)
Number Theory (Chapter 7)
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Jordi Cortadella Department of Computer Science
Recursion.
ECE 103 Engineering Programming
Material in the textbook on
The Hanoi Tower Problem
Chapter 19: Recursion.
Cryptography Lecture 16.
Recursion Chapter 12.
Analysis of Algorithms
Presentation transcript:

Recursion (part 1) HKUCS Provinci Training Chan Ho Leung

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

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

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

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

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

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

A solution Given a 2 n x 2 n board with the top right corner removed. Show how to cover it using L-shape tiles. 2 n If a problem can be reduced to a smaller problem, I should be able to solve it easily.

Tower 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

Game time

Smaller problem? ABC move disks from A to C A B C 1.Move top n-1 disks from A to B 2.Move a disk from A to C 3.Move the top n-1 disks from B to C

Program #include 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.

Recursion 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]

UVA 374 Given B, P, and M, find B P mod M Idea 1. B P = B * B P-1 B 1 = B 0 ≤ B, P ≤ 2,147,483,647 1 ≤ M ≤ Too slow to find B P Idea 2. B P = B P/2 * B P/2 * (P%2?B:1) B 1 = B Things to know: 1.Can int store 2,147,483,647? 2.Can int store B P ?

Modular 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) = 19 (mod 13) = 6 (mod 13) 5 – 7 = -2 (mod 13) = 11 (mod 13) 5*7 = 35 (mod 13) = 9 (mod 13)

Properties of modular arithmetic 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 = = 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

UVA 374 Given B, P, and M, find B P mod M Idea 1. B P = B * B P-1 B 1 = B 0 ≤ B, P ≤ 2,147,483,647 1 ≤ M ≤ Too slow to find B P Idea 2. B P = B P/2 * B P/2 * (P%2?B:1) B 1 = B Things to know: 1.Can int store 2,147,483,647? 2.Can int store B P ? 3.B P % M = B P/2 % M * B P/2 %M * ( )%M 4.Can int store the right hand side? 5.B P % M = [B P/2 % M * B P/2 %M]%M * ( )%M 6.B 1 % M = B % M

UVA 374 #include 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.

UVA Simulation probably does not work Hint 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’ …

What is the rank that 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

What is recursion good for? When you see the recursive relationship. ◦ Very easy to write (usually) When you want to do brute force testing.

UVA Exercises 110 – Sorting 297 – Quadtree ◦ The problems are long, but are not difficult – Simple addition ◦ A bit hard to think ◦ need to use matrix 10549, 195, 536 ◦ not sure

Job opportunities $200 per week as web master ◦ Duties: update the homepage, build and organize the content