Download presentation
Presentation is loading. Please wait.
1
Advanced Java Programming
Session #, Speaker Name Advanced Java Programming CSS446 Spring 2014 Nan Wang 08/24/11
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
6
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!
9
Fibonacci Sequence 1, 1, 2, 3, 5, 8, 13, 21, 34, 55
12
Loop version of Fib
13
Recursion Vs. Loop
14
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
16
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.
17
Evaluating an Expression
Compute the values of arithmetic expressions 3+4*5, (3+4)/5, 3-(4-5) etc
19
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.
20
Homework assignment 5 DO NOT implement
Submit a softcopy to
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.