19
Introduction to Recursion Manish Sinha

Introduction to Recursion Manish Sinha. Topics Two Parts to Recursion: – Solves easy problem in one step – Divide hard problem in smaller ones, and solve

Embed Size (px)

Citation preview

Page 1: Introduction to Recursion Manish Sinha. Topics Two Parts to Recursion: – Solves easy problem in one step – Divide hard problem in smaller ones, and solve

Introduction to Recursion

Manish Sinha

Page 2: Introduction to Recursion Manish Sinha. Topics Two Parts to Recursion: – Solves easy problem in one step – Divide hard problem in smaller ones, and solve

Topics

Two Parts to Recursion:– Solves easy problem in one step– Divide hard problem in smaller ones, and solve small problems

Examples in Recursion:– Walk a Distance– Smashing a Rock

Recursion in JAVA Quiz

Page 3: Introduction to Recursion Manish Sinha. Topics Two Parts to Recursion: – Solves easy problem in one step – Divide hard problem in smaller ones, and solve

How to cross a Parking Lot

It is Sunday evening and the only parking spot you can find at R-Mall is far from the entrance (horror!). How do you get from your car to the mall

A journey of 1000 yards begins with a single step --- Lao Tse

Apply Lao’s instruction to our problem What do you do next? How do you know when the “crossing the parking lot”

problem has been solved?

Page 4: Introduction to Recursion Manish Sinha. Topics Two Parts to Recursion: – Solves easy problem in one step – Divide hard problem in smaller ones, and solve

Two Parts to Recursion

If the problem is easy, solve it immediately If the problem can not be solved immediately,

divide it into smaller problems:– Solve the smaller problems by applying this

procedure to each of them

Page 5: Introduction to Recursion Manish Sinha. Topics Two Parts to Recursion: – Solves easy problem in one step – Divide hard problem in smaller ones, and solve

Recursion on Parking lot problem

If you are one step from the mall, take that step and you are done

If you are further than one step from the mall, divide the distance into two parts:

– a single step, and – the remaining distance– Now take a step and then cross the remaining distance

Say that you have a small rock and you have to break it into smaller pieces with a hammer. How can you do this?

Page 6: Introduction to Recursion Manish Sinha. Topics Two Parts to Recursion: – Solves easy problem in one step – Divide hard problem in smaller ones, and solve

Pounding a rock to Dust

Page 7: Introduction to Recursion Manish Sinha. Topics Two Parts to Recursion: – Solves easy problem in one step – Divide hard problem in smaller ones, and solve

Recursion on the Rocks

When a piece is small, don’t pound it any further

To destroy a large rock, hit it with a hammer. The rock shatters, leaving smaller and large pieces– Apply this procedure to each of the pieces

Page 8: Introduction to Recursion Manish Sinha. Topics Two Parts to Recursion: – Solves easy problem in one step – Divide hard problem in smaller ones, and solve

String Equality

Lets forget that there is an equal() method that is part of class String.

Here are some equal strings:– “abc” equals “abc”– “abc de” equals “abc de”

Here are not equal strings:– “ab” !equals “abc”– “abC” !equals “abc”– “abc ” !equals “aBc”

Page 9: Introduction to Recursion Manish Sinha. Topics Two Parts to Recursion: – Solves easy problem in one step – Divide hard problem in smaller ones, and solve

Rules for string equality

The symbols x stands for a single character, as does y. The symbol X stands for a string of characters, as does Y. The symbol + stands for concatenation.

Rule 1: equals(“” ,“”) = true Rule 2: equals(“”, X) = false if X is not empty string Rule 3: equals(X, “”) = false if X is not empty string Rule 4: equals(x+X,y+Y) = false if x != y Rule 5: equals(x+X,y+Y) = true if x == y and

equals(X,Y)

Page 10: Introduction to Recursion Manish Sinha. Topics Two Parts to Recursion: – Solves easy problem in one step – Divide hard problem in smaller ones, and solve

String Equality Examples

equals(“bat”, “radio”) = false // rule 4 equals(“rat”, “rat”) = equals( "at", "at") // rule 5

– equals( "at", "at" ) = equals( "t", "t") // rule 5 – equals( "t", "t" ) = equals( "", "") // rule 5 – equals( "", "" ) = true // rule 1

equals( "rat", "ra" ) = equals( "at", "a") // rule 5– equals( "at", "a" ) = equals( "t", "") // rule 5 – equals( "t", "" ) = false // rule 3

Page 11: Introduction to Recursion Manish Sinha. Topics Two Parts to Recursion: – Solves easy problem in one step – Divide hard problem in smaller ones, and solve

String Equality Examples …

equals( "rAt", "rat" ) = equals( "At", "at") // rule5– equals( "At", "at" ) = false // rule 4

Definition of Base case: A base case is a problem that can solved immediately.

The base cases are:– equals( "", "" ) = true – equals( "", X ) = false if X is not the empty string – equals( X, "" ) = false if X is not the empty string – equals( x+X, y+Y ) = false if x != y

Page 12: Introduction to Recursion Manish Sinha. Topics Two Parts to Recursion: – Solves easy problem in one step – Divide hard problem in smaller ones, and solve

Translation into JAVA

boolean equals( String strA, String strB ) { // 1. equals( "", "" ) = true

if ( strA.length() ________ 0 && strB.length() ________ 0 ) return true; // 2. equals( "", X ) = false if X is not the empty string

else if ( strA.length() ________ 0 && strB.length() ________ 0 ) return false; // 3. equals( X, "" ) = false if X is not the empty string else if ( strA.length() ________ 0 && strB.length() ________ 0 ) return false;// 4. equals( x+X, y+Y ) = false if x != y else if ( strA.charAt(0) ________ strB.charAt(0) ) return false;

// 5. equals( x+X, y+Y ) = true if x == y and equals( X, Y ) else return ________( strA.substring(1), strB.substring(1) ); }

Page 13: Introduction to Recursion Manish Sinha. Topics Two Parts to Recursion: – Solves easy problem in one step – Divide hard problem in smaller ones, and solve

A good answer might be:

boolean equals( String strA, String strB ) { if ( strA.length() == 0 && strB.length() == 0 )

return true; else if ( strA.length() == 0 && strB.length() != 0 )

return false; else if ( strA.length() != 0 && strB.length() == 0 )

return false; else if ( strA.charAt(0) != strB.charAt(0) )

return false; else return equals( strA.substring(1), strB.substring(1) );

}

Page 14: Introduction to Recursion Manish Sinha. Topics Two Parts to Recursion: – Solves easy problem in one step – Divide hard problem in smaller ones, and solve

Quiz (Choose the single best answer)

What are the two parts in recursion?A. (1) If the problem is easy, solve it immediately, and

(2) If the problem can't be solved immediately, divide it into smaller problems.

B. 1) Divide the problem into smaller problems, and (2) give immediate solutions for the hard problems.

C. (1) Discard the hard cases , and (2) solve the easy easy cases.

D. (1) Solve the problem by asking it to solve itself, (2) Solve the easy cases in one step.

Page 15: Introduction to Recursion Manish Sinha. Topics Two Parts to Recursion: – Solves easy problem in one step – Divide hard problem in smaller ones, and solve

Quiz…

How can you drink an entire keg of root beer?A. (1) take one swallow, then (2) take another

swallow.B. (1) If the keg is empty do nothing, otherwise (2)

take one swallow, then drink the rest of the keg.C. (1) take one enormous gulp, and (2) wish you

hadn't. D. (1) drink one keg, and (2) drink another keg.

Page 16: Introduction to Recursion Manish Sinha. Topics Two Parts to Recursion: – Solves easy problem in one step – Divide hard problem in smaller ones, and solve

Quiz…

How do you study a text book?A. (1) Read the book on day 1, and (2) read it again

each day of the semester.

B. (1) If you have reached the end of the book you are done, else (2) study one page, then study the rest of the book.

C. (1) Divide the book in two, and (2) study each half.

D. (1) Cram all the pages in one horrible session, and (2) forget everything the next night.

Page 17: Introduction to Recursion Manish Sinha. Topics Two Parts to Recursion: – Solves easy problem in one step – Divide hard problem in smaller ones, and solve

Quiz…

How does detective solve a mystery?A. (1) Examine one clue, and (2) examine the remaining

clues.

B. (1) Question one witness, and (2) question the victim.

C. (1) Eliminate one witness, and (2) eliminate the remaining witnesses.

D. (1) When one suspect remains, that is who did it. (2) Examine the evidence to eliminate one suspect, then eliminate the remaining suspects.

Page 18: Introduction to Recursion Manish Sinha. Topics Two Parts to Recursion: – Solves easy problem in one step – Divide hard problem in smaller ones, and solve

Quiz…

How does a Web crawler visit every Web page at a Web site?A. (1) Visit one page, and (2) follow one link.B. (1) If a page has one link follow that link, and (2) If

a page has several links follow each one. C. (1) If a page links to itself, quit. (2) If a page links to

another page, follow the link. D. (1) If a page has no links, look no further. (2) If the

page has links to other pages, visit each link.

Page 19: Introduction to Recursion Manish Sinha. Topics Two Parts to Recursion: – Solves easy problem in one step – Divide hard problem in smaller ones, and solve

Thank You!Questions??