1 / 20

Advanced Java Programming

Learn the powerful concept of recursion in Java programming, understand its relationship with iteration, analyze problems easily solvable with recursion, and explore mutual recursion. Dive into topics like Fibonacci sequences, palindrome testing, permutations, mutual recursion, evaluating expressions, and backtracking techniques.

strader
Download Presentation

Advanced Java Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Advanced Java Programming CSS446 Spring 2014 Nan Wang

  2. Chapter Goals • To learn to “think recursively” • To be able to use recursive helper methods • To understand the relationship between recursion and iteration • To understand when the use of recursion affects the efficiency of an algorithm • To analyze problems that are much easier to solve by recursion than by iteration • To process data with recursive structures using mutual recursion

  3. Recursion • The recursion method is to break up complex computational problem into simpler, smaller ones. • Recursion refers to that face that the same computation recurs, or occurs repeatedly as the problem is solved. • E.g. Fibonacci sequence:

  4. Triangle Numbers • To compute the area of a triangle of width n, assuming that each [] square has area 1. • [] • [][] • [][][] • [][][][] • Area(1) = 1 • Area(4) = Area(3) + 1 • =>Area(n) = Area(n-1) + n

  5. Palindrome test • Test whether a sentence is a palindrome – a string that is equal to itself when you reverse all characters. • Madam, I’m Adam • A man, a plan, a canal – Panama!

  6. Fibonacci Sequence 1, 1, 2, 3, 5, 8, 13, 21, 34, 55

  7. Loop version of Fib

  8. Recursion Vs. Loop

  9. Permutations • The string ‘eat’ has six permutations: • eat eta aet ate tea tae • Index=0 e—at ta • Index=1 a—et te • Index=2 t—ea ae

  10. Mutual Recursion • Recursive solution 1: a method call itself to solve a simpler problem. • In a mutual recursion, a set of cooperating methods calls each other repeatedly.

  11. Evaluating an Expression • Compute the values of arithmetic expressions • 3+4*5, (3+4)/5, 3-(4-5) etc

  12. Backtracking • Backtracking is a problem solving technique that builds up partial solutions that get increasingly closer to the goal. • If a partial solution cannot be completed, one abandons it and returns to examining the other candidates.

  13. Homework assignment 5 • DO NOT implement • Submit a softcopy to usm.java@gmail.com

More Related