chapter 7- recursion.pptkabulcs.weebly.com/uploads/5/0/3/5/5035021/chapter_7-_recursion.pdf · 8...

22
16 April, 2010 1 Recursion gorithms ures & Al Recursion ata Struct Da By: S. Hassan Adelyar

Upload: dinhbao

Post on 27-Mar-2018

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Chapter 7- Recursion.pptkabulcs.weebly.com/uploads/5/0/3/5/5035021/chapter_7-_recursion.pdf · 8 Write each digit vertically Recursion ... ("The Fibonacci number at position" + nth

16 April, 20101

Recursion

gorit

hms

ures

& A

l

Recursion

ata

Stru

ctD

a

By: S. Hassan Adelyar

Page 2: Chapter 7- Recursion.pptkabulcs.weebly.com/uploads/5/0/3/5/5035021/chapter_7-_recursion.pdf · 8 Write each digit vertically Recursion ... ("The Fibonacci number at position" + nth

16 April, 20102 Recursion

RecursionThere are two kinds of repetitive technique:

i

gorit

hms Iterative

Recursive

ures

& A

l IterationIteration use loops (for, while, or do).

ata

Stru

ct Iteration often provide a straightforward and efficientway to implement a repetitive process.

Da Iterative solution is complex. Discovering or verifying

such solution is not simple task. In these cases recursionis an elegant alternativeis an elegant alternative.

By: S. Hassan Adelyar

Page 3: Chapter 7- Recursion.pptkabulcs.weebly.com/uploads/5/0/3/5/5035021/chapter_7-_recursion.pdf · 8 Write each digit vertically Recursion ... ("The Fibonacci number at position" + nth

16 April, 20103

RecursionRecursion

i i i l i

gorit

hms Recursion is an important problem solving strategy.

Simple solution to difficult problems.

ures

& A

l Programming technique in which a method call itself. In each call the argument become smaller so the

b

ata

Stru

ct problem become simpler.Recursive method calls itself. When it calls itself, it d t l ll blD

a does so to solve a smaller problem.

By: S. Hassan Adelyar

Page 4: Chapter 7- Recursion.pptkabulcs.weebly.com/uploads/5/0/3/5/5035021/chapter_7-_recursion.pdf · 8 Write each digit vertically Recursion ... ("The Fibonacci number at position" + nth

16 April, 20104 Decimal To Binary

RecursionClass dectobin{static int a = 2;

gorit

hms ;

static int b =65;public static void main(String [] args){performop(b a);

ures

& A

l performop(b,a); }public static void performop(int n, int s) {

ata

Stru

ct if (n > 0 ) {performop (n/s, s);System.out.print(n % s);

Da y p ( );

}}}}

By: S. Hassan Adelyar

Page 5: Chapter 7- Recursion.pptkabulcs.weebly.com/uploads/5/0/3/5/5035021/chapter_7-_recursion.pdf · 8 Write each digit vertically Recursion ... ("The Fibonacci number at position" + nth

16 April, 20105 Factorial

RecursionN! = n*(n-1)*(n-2)*……3*2*1Public static int recursivefactorial(int n)

gorit

hms Public static int recursivefactorial(int n)

{if (n == 0) return 1;

ures

& A

l if (n == 0) return 1;else return n*recursivefactorial(n-1);

}

ata

Stru

ct }

Da

By: S. Hassan Adelyar

Page 6: Chapter 7- Recursion.pptkabulcs.weebly.com/uploads/5/0/3/5/5035021/chapter_7-_recursion.pdf · 8 Write each digit vertically Recursion ... ("The Fibonacci number at position" + nth

16 April, 20106

Recursionclass factofN {static int a;

gorit

hms static int a;

static int b =7;public static void main(String [] args){

ures

& A

l

a= recursivefactorial(b);System.out.println(a);

}

ata

Stru

ct } public static int recursivefactorial(int n) {

if (n == 0) return 1;

Da

else return n*recursivefactorial(n-1);}}}

By: S. Hassan Adelyar

Page 7: Chapter 7- Recursion.pptkabulcs.weebly.com/uploads/5/0/3/5/5035021/chapter_7-_recursion.pdf · 8 Write each digit vertically Recursion ... ("The Fibonacci number at position" + nth

16 April, 20107 Counting the sum of 1+2+3………..+n:

Recursionclass sumofnum {static int a;

gorit

hms static int b =10;

public static void main(String [] args){a= sumof(b);System out println(a);

ures

& A

l System.out.println(a);} public static int sumof(int n) {int sum;

ata

Stru

ct if (n == 1)sum = 1;else

Da sum = sumof(n-1) + n;

return sum;}}}

By: S. Hassan Adelyar

Page 8: Chapter 7- Recursion.pptkabulcs.weebly.com/uploads/5/0/3/5/5035021/chapter_7-_recursion.pdf · 8 Write each digit vertically Recursion ... ("The Fibonacci number at position" + nth

16 April, 20108 Write each digit vertically

Recursionclass vertnum {static int n =3652;

gorit

hms public static void main(String [] args){

writevertical(n); }

ures

& A

l

public static void writevertical(int n) {if (n<10)System.out.println(n);

ata

Stru

ct else{writevertical(n/10);

Da ( )

System.out.println(n % 10);}}}}

By: S. Hassan Adelyar

Page 9: Chapter 7- Recursion.pptkabulcs.weebly.com/uploads/5/0/3/5/5035021/chapter_7-_recursion.pdf · 8 Write each digit vertically Recursion ... ("The Fibonacci number at position" + nth

16 April, 20109 Fibonacci Sequence

RecursionThe fibonacci sequence is: 1,1,2,3,5,8,13,21 …..Long fib( int n) {

gorit

hms g ( ) {

{If (n<2) return n;Return fib(n 1) + fib(n 2);

ures

& A

l Return fib(n-1) + fib(n-2);}

ata

Stru

ctD

a

By: S. Hassan Adelyar

Page 10: Chapter 7- Recursion.pptkabulcs.weebly.com/uploads/5/0/3/5/5035021/chapter_7-_recursion.pdf · 8 Write each digit vertically Recursion ... ("The Fibonacci number at position" + nth

16 April, 201010

Recursionpublic class FibonacciNumber {public static void main (String [] args) {

gorit

hms public static void main (String [] args) {

int firstFibNum =3;int secondFibNum =5;

ures

& A

l int secondFibNum 5;int nth =6;System.out.println("The Fibonacci number at position" + nth + "is:

ata

Stru

ct "+ rFibNum(firstFibNum, secondFibNum, nth));

}Da }

By: S. Hassan Adelyar

Page 11: Chapter 7- Recursion.pptkabulcs.weebly.com/uploads/5/0/3/5/5035021/chapter_7-_recursion.pdf · 8 Write each digit vertically Recursion ... ("The Fibonacci number at position" + nth

16 April, 201011

Recursionpublic static int rFibNum(int a, int b, int n){

if(n == 1)

gorit

hms if(n == 1)

return a;else if(n==2)

ures

& A

l else if(n 2)return b;

else

ata

Stru

ct return rFibNum(a, b, n-1) + rFibNum(a, b, n-2);}

Da }

By: S. Hassan Adelyar

Page 12: Chapter 7- Recursion.pptkabulcs.weebly.com/uploads/5/0/3/5/5035021/chapter_7-_recursion.pdf · 8 Write each digit vertically Recursion ... ("The Fibonacci number at position" + nth

16 April, 201012

Recursionimport java.io.*;public class FibonacciNumber {

gorit

hms p {

static BufferedReader keyboard = newBufferedReader (new InputStreamReader (System.in));

public static void main (String [] args) throws IOException {

ures

& A

l public static void main (String [] args) throws IOException {int firstFibNum;int secondFibNum;

ata

Stru

ct int nth;System.out.print("Enter the first Fibonacci Number: ");firstFibNum = Integer.parseInt(keyboard.readLine());

Da g p ( y ());

System.out.println();

System out print("Enter the second Fibonacci Number: ");System.out.print("Enter the second Fibonacci Number: ");secondFibNum = Integer.parseInt(keyboard.readLine());System.out.println(); By: S. Hassan Adelyar

Page 13: Chapter 7- Recursion.pptkabulcs.weebly.com/uploads/5/0/3/5/5035021/chapter_7-_recursion.pdf · 8 Write each digit vertically Recursion ... ("The Fibonacci number at position" + nth

16 April, 201013

RecursionSystem.out.print("Enter the position of the desired" + "Fibonacci number: ");

nth = Integer.parseInt(keyboard.readLine());

gorit

hms g p ( y ());

System.out.println();System.out.println("The Fibonacci number at position" + nth + "is: "

+ rFibNum(firstFibNum secondFibNum nth));

ures

& A

l + rFibNum(firstFibNum, secondFibNum, nth));}

ata

Stru

ctD

a

By: S. Hassan Adelyar

Page 14: Chapter 7- Recursion.pptkabulcs.weebly.com/uploads/5/0/3/5/5035021/chapter_7-_recursion.pdf · 8 Write each digit vertically Recursion ... ("The Fibonacci number at position" + nth

16 April, 201014

Recursionpublic static int rFibNum(int a, int b, int n){

if(n == 1)

gorit

hms ( )

return a;else if(n==2)

return b;

ures

& A

l return b;else

return rFibNum(a, b, n-1) + rFibNum(a, b, n-2);

ata

Stru

ct }}

Da

By: S. Hassan Adelyar

Page 15: Chapter 7- Recursion.pptkabulcs.weebly.com/uploads/5/0/3/5/5035021/chapter_7-_recursion.pdf · 8 Write each digit vertically Recursion ... ("The Fibonacci number at position" + nth

16 April, 201015 Fractals

Recursionimport java.applet.Applet;i j *

gorit

hms import java.awt.*;

public class Fractal extends Applet {

ures

& A

l private Image display;private Graphics drawingArea;

ata

Stru

ctD

a

By: S. Hassan Adelyar

Page 16: Chapter 7- Recursion.pptkabulcs.weebly.com/uploads/5/0/3/5/5035021/chapter_7-_recursion.pdf · 8 Write each digit vertically Recursion ... ("The Fibonacci number at position" + nth

16 April, 201016

Recursionpublic void init() {

int height = getSize().height;

gorit

hms g g () g ;

int width = getSize().width;display = createImage(width, height);drawingArea display getGraphics();

ures

& A

l drawingArea = display.getGraphics();randomFractal(0, height/2, width, height/2, drawingArea);

}

ata

Stru

ct public void paint(Graphics g) {g.drawImage(display, 0, 0, null);

}

Da }

By: S. Hassan Adelyar

Page 17: Chapter 7- Recursion.pptkabulcs.weebly.com/uploads/5/0/3/5/5035021/chapter_7-_recursion.pdf · 8 Write each digit vertically Recursion ... ("The Fibonacci number at position" + nth

16 April, 201017

Recursionpublic static void randomFractal

(

gorit

hms (

int leftX,

ures

& A

l int leftY,int rightX,

ata

Stru

ct int rightY,Graphics drawingArea

Da

)

By: S. Hassan Adelyar

Page 18: Chapter 7- Recursion.pptkabulcs.weebly.com/uploads/5/0/3/5/5035021/chapter_7-_recursion.pdf · 8 Write each digit vertically Recursion ... ("The Fibonacci number at position" + nth

16 April, 201018

Recursion{

final int STOP =4;

gorit

hms int midX, midY;

int delta;if(( rightX - leftX) <= STOP)

drawingArea drawLine(leftX leftY rightX rightY);

ures

& A

l drawingArea.drawLine(leftX, leftY, rightX, rightY);else {

midX = (leftX + rightX) /2;midY = (leftY + rightY) / 2;

ata

Stru

ct

( g )delta = (int) (( Math.random() - 0.5) * (rightX - leftX));midY += delta;randomFractal(leftX, leftY, midX, midY, drawingArea);

Da randomFractal(midX, midY, rightX, rightY, drawingArea);

}}

}}

By: S. Hassan Adelyar

Page 19: Chapter 7- Recursion.pptkabulcs.weebly.com/uploads/5/0/3/5/5035021/chapter_7-_recursion.pdf · 8 Write each digit vertically Recursion ... ("The Fibonacci number at position" + nth

16 April, 201019 The Towers of Hanoi

RecursionAn ancient puzzle. There are 3 towers and there are 7 disks on tower A The disks have different diameter

gorit

hms A. The disks have different diameter.

The goal is to transfer all the disks from tower A to tower Caccording to the following rules:

ures

& A

l

Carry only one disk at a time.Never put a big disk on the small disk.

ata

Stru

ct You can use tower B temporary.If the sub-tree has an odd number of disks, start by moving the topmost disk directly to the tower where you want the sub-tree to go

Da topmost disk directly to the tower where you want the sub tree to go.

Tower of HanoiBy: S. Hassan Adelyar

Page 20: Chapter 7- Recursion.pptkabulcs.weebly.com/uploads/5/0/3/5/5035021/chapter_7-_recursion.pdf · 8 Write each digit vertically Recursion ... ("The Fibonacci number at position" + nth

16 April, 201020

Recursionclass towerapp {

static int ndisks =3;

gorit

hms static int ndisks =3;

public static void main (String[] args){

dotowers(ndisks, 'A', 'B', 'C');

ures

& A

l

}public static void dotowers(int topn, char from, char inter, char to){

if(topn == 1)

ata

Stru

ct if(topn == 1)System.out.println("Disk 1 from " + from + " to " + to);

else{

Da dotowers(topn-1, from, to, inter);

System.out.println("Disk " + topn + "from " + from + "to " + to);dotowers(topn-1, inter, from, to);

}}}

}By: S. Hassan Adelyar

Page 21: Chapter 7- Recursion.pptkabulcs.weebly.com/uploads/5/0/3/5/5035021/chapter_7-_recursion.pdf · 8 Write each digit vertically Recursion ... ("The Fibonacci number at position" + nth

16 April, 201021

Recursion

gorit

hms

ures

& A

lat

a St

ruct

Da

By: S. Hassan Adelyar

Page 22: Chapter 7- Recursion.pptkabulcs.weebly.com/uploads/5/0/3/5/5035021/chapter_7-_recursion.pdf · 8 Write each digit vertically Recursion ... ("The Fibonacci number at position" + nth

16 April, 201022 Traversing a Maze

RecursionSuppose the following two facts about a maze:Somewhere is a panel which contain the secret of the universe

gorit

hms Somewhere is a panel which contain the secret of the universe.

You can keep this panel if you can enter the maze, find the panel, and return to the maze’s entrance.

ures

& A

l

The maze is built on a rectangular grid. At each point of the grid, there are four directions to move: north, east, south, or west. Some directions may be blocked by an impenetrable wall

ata

Stru

ct directions may be blocked by an impenetrable wall. You accept to enter the maze but only with the help of your portablecomputer and a method that we will write to guide you into the maze

Da p g y

and back out.

By: S. Hassan Adelyar