java_lect_20.ppt

Upload: chander-kumar

Post on 14-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

  • Introduction to Recursion

  • TopicsTwo Parts to Recursion:Solves easy problem in one stepDivide hard problem in smaller ones, and solve small problemsExamples in Recursion:Walk a DistanceSmashing a RockRecursion in JAVAQuiz

  • How to cross a Parking LotIt 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 mallA journey of 1000 yards begins with a single step --- Lao TseApply Laos instruction to our problemWhat do you do next?How do you know when the crossing the parking lot problem has been solved?

  • Two Parts to RecursionIf the problem is easy, solve it immediatelyIf 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 problemIf you are one step from the mall, take that step and you are doneIf you are further than one step from the mall, divide the distance into two parts:a single step, and the remaining distanceNow take a step and then cross the remaining distanceSay 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

  • Recursion on the RocksWhen a piece is small, dont pound it any furtherTo destroy a large rock, hit it with a hammer. The rock shatters, leaving smaller and large piecesApply this procedure to each of the pieces

  • String EqualityLets forget that there is an equal() method that is part of class String.Here are some equal strings:abc equals abcabc de equals abc deHere are not equal strings:ab !equals abcabC !equals abcabc !equals aBc

  • Rules for string equalityThe 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( ,) = trueRule 2: equals(, X) = false if X is not empty stringRule 3: equals(X, ) = false if X is not empty stringRule 4: equals(x+X,y+Y) = false if x != yRule 5: equals(x+X,y+Y) = true if x == y and equals(X,Y)

  • String Equality Examplesequals(bat, radio) = false // rule 4equals(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 5equals( "at", "a" ) = equals( "t", "") // rule 5 equals( "t", "" ) = false // rule 3

  • String Equality Examples equals( "rAt", "rat" ) = equals( "At", "at") // rule5equals( "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 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) ); }

  • 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) ); }

  • 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.

  • QuizHow 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.

  • QuizHow 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.

  • QuizHow 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.

  • QuizHow 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??