java lect 24

Upload: sangeetha-sangu-bc

Post on 27-Feb-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/25/2019 Java Lect 24

    1/19

    Java Lecture

    Recursion29thfeb 2005

    [email protected]

  • 7/25/2019 Java Lect 24

    2/19

    Saturday, January16, 2016

    CFILT java

    Recursion

    solve(problem){

    If problem is simple

    Return the answerElse

    {

    Break the problem as p1,p2,p3 pn

    or ea!h problem "

    solve(")#

    $

    $

  • 7/25/2019 Java Lect 24

    3/19

    Saturday, January16, 2016

    CFILT java

    Properties

    Cas t!e sa"e #unction $it! s"aer i%p&

    'ivide and con(uer strate)y

  • 7/25/2019 Java Lect 24

    4/19

    Saturday, January16, 2016

    CFILT java

    *+a"pe

    Litte tricy

  • 7/25/2019 Java Lect 24

    5/19

    Saturday, January16, 2016

    CFILT java

    -ppication o# recursion

    ?

  • 7/25/2019 Java Lect 24

    6/19

    Saturday, January16, 2016

    CFILT java

    To$ers o# .anoi

    Let/s try t!is

    %anoi

    Rues

    Input

    utputSoution #or 1, 2, , , 3 n

    http://hanoi/Sample.htmlhttp://hanoi/Sample.html
  • 7/25/2019 Java Lect 24

    7/19

    Saturday, January16, 2016

    CFILT java

    'e#ine t!e pro4e"

    5rap!ica inter#ace 7

    8!at is input 9

    :7u"4er o# discs; 8!at is output9

    :Fina state 9

  • 7/25/2019 Java Lect 24

    8/19

    Saturday, January16, 2016

    CFILT java

    -nao)y $it! recursion

    Soution #or discs

    Can t!e soution #or 4e 4roen9

    =roen pro4e" "ust 4e s"a& It s!oud 4e sa"e as t!e "ain pro4e"&

    8!en to stop 9

    Soution #orxdiscs is very si"pe& Sove itdirecty

  • 7/25/2019 Java Lect 24

    9/19

    Saturday, January16, 2016

    CFILT java

    Steps

    Pro4e" de#inition

    Input representation

    utput representation

    -)orit!"

    I"pe"entation

  • 7/25/2019 Java Lect 24

    10/19

    Saturday, January16, 2016

    CFILT java

    'esi)n

    8e no$

    Recursive condition

    Ter"inatin) condition

    Let/s try t!e a)orit!" #or n

  • 7/25/2019 Java Lect 24

    11/19

    Saturday, January16, 2016

    CFILT java

    Input%utput

    7a"e t!e #unction solve_hanoi

    Input ~ argument ~ (integer n)

    Output

    Assume 3 towers three names.. 3 variables t !t"! t3

    t # $le%t&

    t" # $mi''le& t3 # $right&

    Are the t! t"! t3 also arguments

  • 7/25/2019 Java Lect 24

    12/19

    Saturday, January16, 2016

    CFILT java

    Seeton code

    Sove>!anoi?inte)er n, to$ers t1, t2, t@A

    o something.

    o somethingsove>!anoi?so"e s"a n, t1, t2, t@

    'o so"et!in)3&

    'o so"et!in)3

    B

  • 7/25/2019 Java Lect 24

    13/19

    Saturday, January16, 2016

    CFILT java

    Te"ination

    Sove>!anoi?inte)er n, to$ers t1, t2, t@

    AI% (n ## ) then

    move the 'iscn %rom t to t3

    o something

    sove>!anoi?so"e s"a n@

    'o so"et!in)3&'o so"et!in)3

    B

  • 7/25/2019 Java Lect 24

    14/19

    Saturday, January16, 2016

    CFILT java

    Su4divsion

    Start?@ *nd?

  • 7/25/2019 Java Lect 24

    15/19

    Saturday, January16, 2016

    CFILT java

    3Contd&

    Sove>!anoi?inte)er n, to$ers t1, t2, t@

    A1& Dove t!e n:1 discs #ro" t1to t2usin) t

    2& 7o$ "ove ony re"ainin) disc #ro" t1 to t

    & Dove t!e n:1 discs #ro" t2to tusin) t1

    B

    8!ic! steps are recursive 9

  • 7/25/2019 Java Lect 24

    16/19

  • 7/25/2019 Java Lect 24

    17/19

    Saturday, January16, 2016

    CFILT java

    Co"pete java #unction

    Pu4ic static void sove>!anoi?int n, Strin) t1, t2, t@

    A

    i# ?n 1@ t!en

    Syste"&out&printn?G"ove t!e discHnH #ro" Gt1H to G t@

    ese

    A

    sove>!anoi?n:1, t1, t, t2@

    Syste"&out&printn?G"ove t!e discHnH #ro" Gt1H to G t@

    sove>!anoi?n:1, t2, t1, t@B

    B

  • 7/25/2019 Java Lect 24

    18/19

    Saturday, January16, 2016

    CFILT java

    Re#erence

    Pay t!e )a"e or see t!e soutions !ere !ttpK%%$$$&"ae$ors&co"%!anoi%

    RecursionK!ttpK%%$$$&cs&uca&edu%Min)er%dorene%"at!&!t"

    .istory o# t!e )a"e K!ttpK%%$$$&a$rence!ao#science&or)%Java%To$er%to$er!i

    story&!t"

    http://www.mazeworks.com/hanoi/http://www.cs.ucla.edu/~klinger/dorene/math4.htmhttp://www.lawrencehallofscience.org/Java/Tower/towerhistory.htmlhttp://www.lawrencehallofscience.org/Java/Tower/towerhistory.htmlhttp://www.lawrencehallofscience.org/Java/Tower/towerhistory.htmlhttp://www.lawrencehallofscience.org/Java/Tower/towerhistory.htmlhttp://www.cs.ucla.edu/~klinger/dorene/math4.htmhttp://www.mazeworks.com/hanoi/
  • 7/25/2019 Java Lect 24

    19/19

    Saturday, January16 2016

    CFILT java