Скачать презентацию Introduction to Recursion Manish Sinha Topics l Скачать презентацию Introduction to Recursion Manish Sinha Topics l

14406fa45770afcfef2983f21e1c785e.ppt

  • Количество слайдов: 19

Introduction to Recursion Manish Sinha Introduction to Recursion Manish Sinha

Topics l Two Parts to Recursion: – – l Examples in Recursion: – – Topics l Two Parts to Recursion: – – l Examples in Recursion: – – l l Solves easy problem in one step Divide hard problem in smaller ones, and solve small problems Walk a Distance Smashing a Rock Recursion in JAVA Quiz

How to cross a Parking Lot l l l It is Sunday evening and How to cross a Parking Lot l l l 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?

Two Parts to Recursion l l If the problem is easy, solve it immediately Two Parts to Recursion l l 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

Recursion on Parking lot problem l l If you are one step from the Recursion on Parking lot problem l l 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: – – – l 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?

Pounding a rock to Dust Pounding a rock to Dust

Recursion on the Rocks l l When a piece is small, don’t pound it Recursion on the Rocks l l 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

String Equality l l Lets forget that there is an equal() method that is String Equality l l Lets forget that there is an equal() method that is part of class String. Here are some equal strings: – – l “abc” equals “abc” “abc de” equals “abc de” Here are not equal strings: – – – “ab” !equals “abc” “ab. C” !equals “abc” “abc ” !equals “a. Bc”

Rules for string equality l l l The symbols x stands for a single Rules for string equality l l l 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)

String Equality Examples l l equals(“bat”, “radio”) = false // rule 4 equals(“rat”, “rat”) String Equality Examples l l equals(“bat”, “radio”) = false // rule 4 equals(“rat”, “rat”) = equals( "at", "at") // rule 5 – – – l 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

String Equality Examples … l equals( String Equality Examples … l equals( "r. At", "rat" ) = equals( "At", "at") // rule 5 – l l 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

Translation into JAVA boolean equals( String str. A, String str. B ) { // Translation into JAVA boolean equals( String str. A, String str. B ) { // 1. equals( "", "" ) = true if ( str. A. length() ____ 0 && str. B. length() ____ 0 ) return true; // 2. equals( "", X ) = false if X is not the empty string else if ( str. A. length() ____ 0 && str. B. length() ____ 0 ) return false; // 3. equals( X, "" ) = false if X is not the empty string else if ( str. A. length() ____ 0 && str. B. length() ____ 0 ) return false; // 4. equals( x+X, y+Y ) = false if x != y else if ( str. A. char. At(0) ____ str. B. char. At(0) ) return false; // 5. equals( x+X, y+Y ) = true if x == y and equals( X, Y ) else return ____( str. A. substring(1), str. B. substring(1) ); }

A good answer might be: boolean equals( String str. A, String str. B ) A good answer might be: boolean equals( String str. A, String str. B ) { if ( str. A. length() == 0 && str. B. length() == 0 ) return true; else if ( str. A. length() == 0 && str. B. length() != 0 ) return false; else if ( str. A. length() != 0 && str. B. length() == 0 ) return false; else if ( str. A. char. At(0) != str. B. char. At(0) ) return false; else return equals( str. A. substring(1), str. B. substring(1) ); }

Quiz (Choose the single best answer) l What are the two parts in recursion? Quiz (Choose the single best answer) l 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 cases. D. (1) Solve the problem by asking it to solve itself, (2) Solve the easy cases in one step.

Quiz… l How can you drink an entire keg of root beer? A. (1) Quiz… l 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.

Quiz… l How do you study a text book? A. (1) Read the book Quiz… l 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.

Quiz… l How does detective solve a mystery? A. (1) Examine one clue, and Quiz… l 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.

Quiz… l How does a Web crawler visit every Web page at a Web Quiz… l 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.

Thank You! Questions? ? Thank You! Questions? ?