Chapter 12 - Recursion - &KDSWHU 5HFXUVLRQ&RS\ULJKW...

Info icon This preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: &KDSWHU 5HFXUVLRQ &RS\ULJKW E\ 6WXDUW 5HJHV DQG 0DUW\ 6WHSS 7KLQNLQJ 5HFXUVLYHO\ $ 1RQSURJUDPPLQJ ([DPSOH $Q ,WHUDWLYH 6ROXWLRQ &RQYHUWHG WR 5HFXUVLRQ 6WUXFWXUH RI 5HFXUVLYH 6ROXWLRQV $ %HWWHU ([DPSOH RI 5HFXUVLRQ 0HFKDQLFV RI 5HFXUVLRQ 5HFXUVLYH )XQFWLRQV ,QWHJHU ([SRQHQWLDWLRQ *UHDWHVW &RPPRQ 'LYLVRU 5HFXUVLYH *UDSKLFV RSWLRQDO &DVH 6WXG\ 3UHIL[ (YDOXDWRU ,QIL[ 3UHIL[ DQG 3RVWIL[ 1RWDWLRQ 3UHIL[ (YDOXDWRU &RPSOHWH 3URJUDP ,QWURGXFWLRQ 7KLV FKDSWHU IRFXVHV RQ D SURJUDPPLQJ WHFKQLTXH NQRZQ DV UHFXUVLRQ ,W DOORZV XV WR VROYH FHUWDLQ FRPSOH[ SUREOHPV LQ D KLJKO\ HOHJDQW PDQQHU 7KH FKDSWHU EHJLQV E\ FRPSDULQJ UHFXUVLRQ WR ZKDW ZH DOUHDG\ NQRZ 7KHQ LW GLVFXVVHV WKH ORZ OHYHO PHFKDQLFV WKDW PDNH UHFXUVLRQ ZRUN LQ -DYD )LQDOO\ ZH H[DPLQH D QXPEHU RI SUREOHPV WKDW DUH HDVLO\ H[SUHVVHG XVLQJ WKH WHFKQLTXH 609 7KLQNLQJ 5HFXUVLYHO\ 7KH SUREOHP VROYLQJ WHFKQLTXHV ZH KDYH VHHQ VR IDU DUH SDUW RI FODVVLFDO LWHUDWLRQ DOVR NQRZQ DV WKH LWHUDWLYH DSSURDFK ,WHUDWLRQ ,WHUDWLYH $ SURJUDPPLQJ WHFKQLTXH LQ ZKLFK \RX GHVFULEH DFWLRQV WR EH UHSHDWHG XVLQJ D ORRS ,Q WKLV FKDSWHU ZH ZLOO H[SORUH D QHZ WHFKQLTXH NQRZQ DV UHFXUVLRQ 5HFXUVLRQ 5HFXUVLYH $ SURJUDPPLQJ WHFKQLTXH LQ ZKLFK \RX GHVFULEH DFWLRQV WR EH UHSHDWHG XVLQJ D PHWKRG WKDW FDOOV LWVHOI <RX KDYH VSHQW VR PXFK WLPH ZULWLQJ VROXWLRQV LWHUDWLYHO\ WKDW LW ZLOO WDNH D ZKLOH WR JHW XVHG WR WKLQNLQJ DERXW SUREOHPV UHFXUVLYHO\ /HW V EHJLQ ZLWK D QRQSURJUDPPLQJ H[DPSOH $ 1RQSURJUDPPLQJ ([DPSOH :H RIWHQ ILQG RXUVHOYHV VWDQGLQJ LQ OLQH <RX PLJKW ILQG \RXUVHOI ZRQGHULQJ ZKDW SODFH \RX DUH LQ OLQH $UH \RX QXPEHU LQ OLQH" $UH \RX QXPEHU " +RZ ZRXOG \RX ILQG RXW" 0RVW SHRSOH ZRXOG VROYH WKLV SUREOHP LWHUDWLYHO\ <RX G FRXQW WKH SHRSOH LQ OLQH RQH WZR WKUHH DQG VR RQ 7KLV LV OLNH D ZKLOH ORRS WKDW FRQWLQXHV ZKLOH WKHUH DUH PRUH SHRSOH OHIW WR FRXQW 7KH LWHUDWLYH DSSURDFK LV D QDWXUDO RQH EXW LW KDV VRPH OLPLWDWLRQV )RU H[DPSOH ZKDW LI WKH SHUVRQ LQ IURQW RI \RX LV WDOO DQG \RX UH QRW YHU\ WDOO :LOO \RX EH DEOH WR VHH SDVW WKH SHUVRQ WR VHH H[DFWO\ ZKR LV LQ OLQH" 2U PD\EH WKH OLQH JRHV DURXQG WKH EORFN DQG \RX FDQ W VHH DURXQG WKH FRUQHU WR FRXQW WKH SHRSOH WKHUH &DQ \RX WKLQN RI DQRWKHU ZD\ WR VROYH WKH SUREOHP" 7R WKLQN DERXW WKH SUREOHP UHFXUVLYHO\ \RX KDYH WR WKLQN LQ WHUPV RI KDYLQJ DOO WKH SHRSOH VWDQGLQJ LQ OLQH ZRUNLQJ WRJHWKHU WR VROYH WKH SUREOHP 6R LQVWHDG RI KDYLQJ RQH SHUVRQ GRLQJ DOO RI WKH FRXQWLQJ \RX KDYH HDFK SHUVRQ GR D OLWWOH SLHFH RI WKH FRXQWLQJ 2QH DSSURDFK ZRXOG EH WR DVN WKH SHUVRQ LQ IURQW RI PH ZKDW P\ SODFH LQ OLQH LV 7KDW SHUVRQ PLJKW DVN DQRWKHU SHUVRQ ZKR PLJKW DVN DQRWKHU SHUVRQ %XW WKDW GRHVQ W KHOS PXFK EHFDXVH LW V WKH H[DFW VDPH TXHVWLRQ 7KDW MXVW OHDGV WR D EXQFK RI SHRSOH VD\LQJ 7KLV JX\ ZDQWV WR NQRZ ZKDW SODFH KH LV LQ OLQH 'RHV DQ\RQH NQRZ" 3UREDEO\ WKHQ VRPHRQH ZRXOG VWDUW FRXQWLQJ DQG VROYH WKH SUREOHP LWHUDWLYHO\ :H KDYH WR VRPHKRZ PDNH WKH SUREOHP VLPSOHU 6R LQVWHDG RI DVNLQJ WKH SHUVRQ LQ IURQW RI PH ZKDW SODFH , DP LQ OLQH , VKRXOG DVN WKH SHUVRQ ZKDW SODFH KH RU VKH LV LQ OLQH 610 +-----------------------------------------+ O -----| I want to know what place I am in line. | /|\ | So, what place are you in line? | | +-----------------------------------------+ / \ +--------------------------------+ O -----| Let me try to figure that out. | /|\ +--------------------------------+ | / \ 7KH NH\ GLIIHUHQFH LV WKDW WKH SHUVRQ LQ IURQW RI PH LV FORVHU WR WKH IURQW RI WKH OLQH 6XSSRVH IRU H[DPSOH WKDW , P WK LQ OLQH 7KHQ WKH SHUVRQ LQ IURQW RI PH LV WK LQ OLQH 7KDW V FORVHU WR WKH IURQW %XW QRWLFH WKDW , P DVNLQJ WKH SHUVRQ LQ IURQW RI PH WR WKLQN DERXW WKH H[DFW VDPH NLQG RI TXHVWLRQ , P WKLQNLQJ DERXW :H UH ERWK WU\LQJ WR ILJXUH RXW RXU SODFH LQ OLQH DOWKRXJK WKH SHUVRQ LQ IURQW RI PH LV FORVHU WR WKH IURQW WKDQ , DP 7KDW V ZKHUH WKH LGHD RI UHFXUVLRQ FRPHV LQ 7KH SUREOHP UHFXUV LQ WKDW HDFK RI XV ZDQWV WR DQVZHU WKH VDPH TXHVWLRQ 7KH LGHD LV WR VHW XS D FKDLQ UHDFWLRQ RI SHRSOH DOO DVNLQJ WKH SHUVRQ LQ IURQW RI WKHP WKH VDPH TXHVWLRQ +----------------+ O -----| What place are | /|\ | you in line? | | +----------------+ / \ +----------------+ O -----| What place are | /|\ | you in line? | | +----------------+ / \ +----------------+ O -----| What place are | /|\ | you in line? | | +----------------+ / \ ... 7KH OLQH KDV WR HQG VRPHZKHUH VR HYHQWXDOO\ WKLV SURFHVV KDV WR HQG (YHQWXDOO\ ZH KDYH WR UHDFK D SHUVRQ ZKR LV DW WKH IURQW RI WKH OLQH 611 ... +----------------+ O -----| What place are | /|\ | you in line? | | +----------------+ / \ +----------------+ O -----| What place are | /|\ | you in line? | | +----------------+ / \ +----------------+ O -----| What place are | /|\ | you in line? | | +----------------+ / \ +-------------------+ O -----| I'm at the front, | /|\ | so I'm 1st. | | +-------------------+ / \ $W WKLV SRLQW ZH KDYH UHDFKHG ZKDW ZH VRPHWLPHV UHIHU WR DV WKH ERWWRP RI WKH UHFXUVLRQ :H KDYH JRWWHQ D EXQFK RI SHRSOH LQYROYHG LQ FROOHFWLYHO\ VROYLQJ WKH SUREOHP DQG ZH YH ILQDOO\ UHDFKHG D SRLQW ZKHUH ZH FDQ VWDUW DVVHPEOLQJ WKH DQVZHU 7KH SHUVRQ DW WKH IURQW LV LQ SRVLWLRQ 7KDW PHDQV WKH SHUVRQ MXVW EHIRUH LV DW SRVLWLRQ DQG WKH SHUVRQ MXVW EHIRUH LV DW SRVLWLRQ DQG VR RQ 6R DIWHU ZH UHDFK WKH ERWWRP RI WKH UHFXUVLRQ ZH XQZLQG WKH UHFXUVLRQ DQG ILJXUH RXW WKH RYHUDOO DQVZHU +-------------------+ O -----| I'm at the front, | /|\ | so I'm 1st. | | +-------------------+ / \ +----------------+ O -----| Then I must be | /|\ | 2nd in line. | | +----------------+ / \ +----------------+ O -----| Then I must be | /|\ | 3rd in line. | | +----------------+ / \ +----------------+ O -----| Then I must be | /|\ | 4th in line. | | +----------------+ / \ 7KHVH GLDJUDPV LQFOXGHG MXVW IRXU LQGLYLGXDOV WR PDNH WKHP ILW RQ D SDJH EXW \RX FDQ LPDJLQH WKLV SURFHVV ZRUNLQJ HYHQ LI WKHUH ZHUH SHRSOH LQ OLQH RU SHRSOH LQ OLQH 2QH RI WKH NH\ DVSHFWV WR QRWLFH KHUH LV WKDW UHFXUVLRQ LQYROYHV PDQ\ FRRSHUDWLQJ HQWLWLHV WKDW HDFK VROYH D OLWWOH ELW RI WKH SUREOHP ,QVWHDG RI RQH SHUVRQ ZKR GRHV DOO RI WKH FRXQWLQJ ZH KDYH HDFK LQGLYLGXDO DVNLQJ RQH TXHVWLRQ DV ZH JR WRZDUGV WKH IURQW RI WKH OLQH DQG DQVZHULQJ RQH TXHVWLRQ DV ZH FRPH EDFN RXW 612 ,Q SURJUDPPLQJ WKH LWHUDWLYH VROXWLRQ RI KDYLQJ RQH SHUVRQ GRLQJ DOO WKH FRXQWLQJ LV OLNH KDYLQJ D ORRS WKDW UHSHDWV VRPH DFWLRQ 7KH UHFXUVLYH VROXWLRQ RI KDYLQJ PDQ\ SHRSOH HDFK GRLQJ D OLWWOH ELW RI ZRUN ZLOO WXUQ LQWR PDQ\ GLIIHUHQW PHWKRG FDOOV HDFK RI ZKLFK SHUIRUP D OLWWOH ELW RI ZRUN 6R OHW V VHH DQ H[DPSOH RI KRZ D VLPSOH LWHUDWLYH VROXWLRQ FRXOG EH WXUQHG LQWR D UHFXUVLYH VROXWLRQ $Q ,WHUDWLYH 6ROXWLRQ &RQYHUWHG WR 5HFXUVLRQ 6XSSRVH WKDW \RX ZDQW WR KDYH D PHWKRG FDOOHG ZULWH6WDUV WKDW ZLOO WDNH DQ LQWHJHU SDUDPHWHU Q DQG WKDW ZLOO SURGXFH D OLQH RI RXWSXW ZLWK H[DFWO\ Q VWDUV RQ LW :H FDQ VROYH WKLV ZLWK D VLPSOH IRU ORRS public static void writeStars(int n) { for (int i = 1; i <= n; i++) { System.out.print("*"); } System.out.println(); } 7KH DFWLRQ EHLQJ UHSHDWHG KHUH LV WKH FDOO RQ 6\VWHP RXW SULQW WKDW SULQWV WKH Q VWDUV ,I ZH ZDQW WR ZULWH WKLV UHFXUVLYHO\ ZH QHHG WR WKLQN DERXW GLIIHUHQW FDVHV :H PLJKW DVN WKH PHWKRG WR SURGXFH D OLQH ZLWK VWDUV RU ZH PLJKW DVN WKH PHWKRG WR SURGXFH D OLQH ZLWK VWDUV RU ZH PLJKW DVN WKH PHWKRG WR SURGXFH D OLQH RI VWDUV 2I DOO RI WKH SRVVLEOH VWDU ZULWLQJ WDVNV ZH PLJKW DVN LW WR SHUIRUP ZKLFK RQH LV WKH VLPSOHVW" 6WXGHQWV RIWHQ DQVZHU WKDW SULQWLQJ D OLQH RI VWDU LV YHU\ HDV\ DQG WKH\ UH ULJKW WKDW LW V HDV\ %XW WKHUH LV D WDVN WKDW LV HYHQ HDVLHU 3ULQWLQJ D OLQH RI VWDUV UHTXLUHV DOPRVW QR ZRUN DW DOO :H FDQ FUHDWH VXFK D OLQH E\ FDOOLQJ 6\VWHP RXW SULQWOQ 6R ZH EHJLQ RXU UHFXUVLYH GHILQLWLRQ ZLWK D WHVW IRU WKLV FDVH public static void writeStars2(int n) { if (n == 0) { System.out.println(); } else { ... } ,Q WKH HOVH SDUW ZH ZDQW WR GHDO ZLWK OLQHV WKDW KDYH PRUH WKDQ VWDUV RQ WKHP <RXU LQVWLQFW LV SUREDEO\ WR ILOO LQ WKH HOVH SDUW ZLWK WKH IRU ORRS ZH XVHG EHIRUH <RX KDYH WR ILJKW WKH LQVWLQFW WR VROYH WKH HQWLUH SUREOHP WKDW ZD\ 7R VROYH WKLV VHFRQG SDUW RI WKH SUREOHP LW LV LPSRUWDQW WR WKLQN DERXW KRZ ZH FDQ GR MXVW D VPDOO DPRXQW RI ZRUN WKDW ZLOO JHW XV FORVHU WR EHLQJ GRQH ,I WKH QXPEHU RI VWDUV LV JUHDWHU WKDQ WKHQ ZH NQRZ ZH KDYH WR SULQW DW OHDVW RQH VR ZH FDQ DGG WKDW WR RXU FRGH public static void writeStars2(int n) { if (n == 0) { System.out.println(); } else { System.out.print("*"); // what is left to do? ... } 613 $W WKLV SRLQW LQ WKH SURFHVV \RX KDYH WR EH DEOH WR PDNH D FHUWDLQ OHDS RI IDLWK <RX KDYH WR EHOLHYH WKDW UHFXUVLRQ DFWXDOO\ ZRUNV 2QFH ZH KDYH ZULWWHQ D VLQJOH VWDU ZKDW GR ZH KDYH OHIW WR GR" 7KH DQVZHU LV WKDW ZH ZDQW WR ZULWH Q PRUH VWDUV DORQJ ZLWK D SULQWOQ ,Q RWKHU ZRUGV DIWHU ZULWLQJ RQH VWDU ZKDW ZH KDYH OHIW WR GR LV WR ZULWH D OLQH RI Q VWDUV <RX VKRXOG ILQG \RXUVHOI WKLQNLQJ ,I RQO\ , KDG D PHWKRG WKDW ZRXOG SURGXFH D OLQH RI Q VWDUV WKHQ , FRXOG FDOO WKDW PHWKRG %XW \RX GR KDYH VXFK D PHWKRG ,W V WKH PHWKRG ZH DUH ZULWLQJ 6R DIWHU ZULWLQJ D VLQJOH VWDU ZH FDQ FDOO WKH ZULWH6WDUV PHWKRG LWVHOI WR FRPSOHWH WKH OLQH RI RXWSXW public static void writeStars2(int n) { if (n == 0) { System.out.println(); } else { System.out.print("*"); writeStars2(n - 1); } } 0DQ\ QRYLFHV FRPSODLQ WKDW WKLV VHHPV OLNH FKHDWLQJ :H DUH VXSSRVHG WR EH ZULWLQJ WKH PHWKRG FDOOHG ZULWH6WDUV VR KRZ FDQ ZH FDOO ZULWH6WDUV IURP LQVLGH RI ZULWH6WDUV " :HOFRPH WR WKH ZRUOG RI UHFXUVLRQ ,Q RXU QRQSURJUDPPLQJ H[DPSOH ZH WDONHG DERXW KDYLQJ PDQ\ SHRSOH VWDQGLQJ LQ OLQH ZKR VROYHG D SUREOHP WRJHWKHU 7R XQGHUVWDQG D UHFXUVLYH PHWKRG OLNH ZULWH6WDUV LW LV XVHIXO WR LPDJLQH HDFK PHWKRG LQYRFDWLRQ DV EHLQJ OLNH D SHUVRQ 7KH NH\ LQVLJKW LV WKDW WKHUH LVQ W MXVW RQH SHUVRQ ZKR FDQ GR WKH ZULWH6WDUV WDVN :H KDYH DQ HQWLUH DUP\ RI SHRSOH ZKR FDQ HDFK GR WKH ZULWH6WDUV WDVN /HW V WKLQN DERXW ZKDW KDSSHQV ZKHQ , FDOO WKH PHWKRG DQG UHTXHVW D OLQH RI writeStars2(3); VWDUV :H FDOO XS WKH ILUVW SHUVRQ IURP WKH ZULWH6WDUV DUP\ DQG VD\ , ZDQW D OLQH RI VWDUV 7KH SHUVRQ ORRNV DW WKH FRGH LQ WKH PHWKRG DQG VHHV WKDW WKH ZD\ \RX ZULWH D OLQH RI VWDUV LV WR System.out.print("*"); writeStars2(2); ,Q RWKHU ZRUGV WKH ILUVW PHPEHU RI WKH DUP\ ZULWHV D VWDU DQG FDOOV XS WKH QH[W PHPEHU RI WKH DUP\ WR ZULWH D OLQH RI VWDUV -XVW DV ZH KDG D VHULHV RI SHRSOH ILJXULQJ RXW ZKDW SODFH WKH\ ZHUH LQ OLQH ZH KDYH D VHULHV RI SHRSOH FDOOHG XS ZKR HDFK SULQW RQH VWDU DQG WKHQ FDOO RQ VRPHRQH HOVH WR ZULWH WKH UHVW RI WKH OLQH :LWK WKH SHRSOH VWDQGLQJ LQ OLQH ZH HYHQWXDOO\ UHDFKHG WKH SHUVRQ DW WKH IURQW RI WKH OLQH ,Q WKLV FDVH ZH UHDFK D SRLQW ZKHUH WKH UHTXHVW LV WR ZULWH D OLQH RI VWDUV ZKLFK OHDGV XV LQWR WKH LI EUDQFK UDWKHU WKDQ WKH HOVH EUDQFK DQG ZH FRPSOHWH WKH WDVN ZLWK D VLPSOH SULQWOQ %HORZ LV D WUDFH RI WKH FDOOV WKDW ZRXOG EH PDGH writeStars2(3); // n > 0, execute else System.out.print("*"); writeStars2(2); // n > 0, execute else System.out.print("*"); writeStars2(1); // n > 0, execute else System.out.print("*"); writeStars2(0); // n == 0, execute if System.out.println(); 614 6R D WRWDO RI IRXU GLIIHUHQW FDOOV DUH PDGH RQ WKH PHWKRG ,I ZH WKLQN RI WKH FDOOV DV EHLQJ OLNH SHRSOH D WRWDO RI IRXU PHPEHUV RI WKH DUP\ DUH FDOOHG XS WR VROYH WKH WDVN WRJHWKHU (DFK RQH ZDV VROYLQJ D VWDU ZULWLQJ WDVN EXW WKH\ ZHUH VROYLQJ VOLJKWO\ GLIIHUHQW WDVNV VWDUV VWDUV VWDU VWDUV 7KLV LV VLPLODU WR WKH QRQSURJUDPPLQJ H[DPSOH LQ ZKLFK WKH YDULRXV SHRSOH VWDQGLQJ LQ OLQH ZHUH DOO DQVZHULQJ WKH VDPH NLQG RI TXHVWLRQ EXW WKH\ ZHUH VROYLQJ VOLJKWO\ GLIIHUHQW SUREOHPV EHFDXVH RI ZKHUH WKH\ ZHUH VWDQGLQJ LQ OLQH FORVHU WR WKH IURQW RU IXUWKHU IURP WKH IURQW 6WUXFWXUH RI 5HFXUVLYH 6ROXWLRQV :ULWLQJ UHFXUVLYH VROXWLRQV UHTXLUHV D FHUWDLQ OHDS RI IDLWK EXW WKHUH LV QRWKLQJ PDJLFDO DERXW UHFXUVLRQ )RU H[DPSOH WKH IROORZLQJ PHWKRG LV QRW D VROXWLRQ WR WKH WDVN RI ZULWLQJ D OLQH RI Q VWDUV public static void writeStars3(int n) { writeStars3(n); } 7KLV YHUVLRQ SURGXFHV LQILQLWH UHFXUVLRQ )RU H[DPSOH LI , DVN WKH PHWKRG WR ZULWH D OLQH RI VWDUV LW WULHV WR DFFRPSOLVK WKDW E\ DVNLQJ WKH PHWKRG WR ZULWH D OLQH RI VWDUV ZKLFK DVNV WKH PHWKRG WR ZULWH D OLQH RI VWDUV ZKLFK DVNV WKH PHWKRG WR ZULWH D OLQH RI VWDUV DQG VR RQ ,Q RWKHU ZRUGV WKLV VROXWLRQ LV WKH UHFXUVLYH HTXLYDOHQW RI DQ LQILQLWH ORRS (YHU\ UHFXUVLYH VROXWLRQ WKDW \RX ZULWH ZLOO KDYH WZR NH\ LQJUHGLHQWV D EDVH FDVH DQG D UHFXUVLYH FDVH %DVH &DVH $ FDVH WKDW LV VR VLPSOH WKDW LW FDQ EH VROYHG GLUHFWO\ ZLWKRXW D UHFXUVLYH FDOO 5HFXUVLYH &DVH $ FDVH WKDW LQYROYHV UHGXFLQJ WKH RYHUDOO SUREOHP WR D VLPSOHU SUREOHP RI WKH VDPH NLQG WKDW FDQ EH VROYHG E\ D UHFXUVLYH FDOO %HORZ LV WKH ZULWH6WDUV PHWKRG ZLWK LWV EDVH FDVH DQG UHFXUVLYH FDVH LQGLFDWHG ZLWK FRPPHQWV public static void writeStars2(int n) { if (n == 0) { // base case System.out.println(); } else { // recursive case System.out.print("*"); writeStars2(n - 1); } } 615 7KH EDVH FDVH LV WKH WDVN RI ZULWLQJ D OLQH RI ]HUR VWDUV 7KLV WDVN LV VR VLPSOH WKDW LW FDQ EH GRQH LPPHGLDWHO\ 7KH UHFXUVLYH FDVH LV WKH WDVN RI ZULWLQJ OLQHV ZLWK RU PRUH VWDUV 7R VROYH WKH UHFXUVLYH FDVH ZH EHJLQ E\ ZULWLQJ D VLQJOH VWDU ZKLFK UHGXFHV WKH UHPDLQLQJ WDVN WR WKDW RI ZULWLQJ D OLQH RI Q VWDUV 7KLV LV D WDVN WKDW WKH ZULWH6WDUV PHWKRG LV GHVLJQHG WR VROYH DQG LW LV VLPSOHU WKDQ WKH RULJLQDO WDVN VR ZH FDQ VROYH LW E\ PDNLQJ D UHFXUVLYH FDOO $V DQ DQDORJ\ VXSSRVH WKDW \RX DUH DW WKH WRS RI D ODGGHU ZLWK Q UXQJV RQ LW ,I \RX KDYH D ZD\ WR JHW IURP RQH UXQJ WR WKH RQH EHORZ DQG LI \RX FDQ UHFRJQL]H ZKHQ \RX YH UHDFKHG WKH JURXQG WKHQ \RX FDQ KDQGOH DQ\ KHLJKW ODGGHU *RLQJ IURP RQH UXQJ WR WKH RQH EHORZ LV OLNH WKH UHFXUVLYH FDVH ZKHUH \RX SHUIRUP VRPH VPDOO DPRXQW RI ZRUN WKDW UHGXFHV WKH SUREOHP WR D VLPSOHU RQH RI WKH VDPH IRUP JHW GRZQ IURP UXQJ Q YHUVXV JHW GRZQ IURP UXQJ Q 5HFRJQL]LQJ ZKHQ \RX UHDFK WKH JURXQG LV OLNH WKH EDVH FDVH WKDW FDQ EH VROYHG GLUHFWO\ VWHS RII WKH ODGGHU 6RPH SUREOHPV LQYROYH PXOWLSOH EDVH FDVHV DQG VRPH SUREOHPV LQYROYH PXOWLSOH UHFXUVLYH FDVHV EXW WKHUH ZLOO DOZD\V EH DW OHDVW RQH RI HDFK ,I \RX DUH PLVVLQJ HLWKHU \RX UXQ LQWR WURXEOH :LWKRXW WKH DELOLW\ WR VWHS GRZQ IURP RQH UXQJ WR WKH RQH EHORZ \RX G EH VWXFN DW WKH WRS RI WKH ODGGHU :LWKRXW WKH DELOLW\ WR UHFRJQL]H ZKHQ \RX UHDFK WKH JURXQG \RX G NHHS FOLPELQJ HYHQ ZKHQ WKHUH DUH QR UXQJV OHIW LQ WKH ODGGHU %HFDXVH UHFXUVLYH VROXWLRQV LQFOXGH VRPH FRPELQDWLRQ RI EDVH FDVHV DQG UHFXUVLYH FDVHV \RX ZLOO ILQG WKDW WKH\ DUH RIWHQ ZULWWHQ ZLWK DQ LI HOVH VWDWHPHQW D QHVWHG LI VWDWHPHQW RU VRPH PLQRU YDULDWLRQ <RX ZLOO DOVR ILQG WKDW UHFXUVLYH SURJUDPPLQJ JHQHUDOO\ LQYROYHV D FDVH DQDO\VLV ZKHUH \RX FDWHJRUL]H WKH SRVVLEOH IRUPV WKH SUREOHP PLJKW WDNH LQWR GLIIHUHQW FDVHV DQG \RX ZULWH D VROXWLRQ IRU HDFK FDVH $ %HWWHU ([DPSOH RI 5HFXUVLRQ :KLOH LW PLJKW EH LQWHUHVWLQJ WR VROYH WKH ZULWH6WDUV WDVN ZLWK UHFXUVLRQ LW LVQ W D YHU\ FRPSHOOLQJ H[DPSOH /HW V ORRN LQ GHWDLO DW D SUREOHP ZKHUH UHFXUVLRQ VLPSOLILHV WKH ZRUN ZH KDYH WR GR 6XSSRVH WKDW \RX KDYH D 6FDQQHU WKDW LV WLHG WR DQ H[WHUQDO LQSXW ILOH DQG \RX ZDQW WR SULQW WKH OLQHV RI WKH ILOH LQ UHYHUVH RUGHU )RU H[DPSOH WKH ILOH PLJKW FRQWDLQ WKH IROORZLQJ IRXU OLQHV RI WH[W this is fun no? :H ZDQW WR SULQW WKHVH OLQHV LQ UHYHUVH RUGHU VR ZH G OLNH WR SURGXFH WKLV RXWSXW no? fun is this 7R VROYH WKH SUREOHP LWHUDWLYHO\ ZH G QHHG VRPH NLQG RI GDWD VWUXFWXUH IRU VWRULQJ WKH OLQHV RI WH[W :H PLJKW IRU H[DPSOH XVH DQ $UUD\/LVW6WULQJ! :LWK UHFXUVLRQ ZH FDQ VROYH WKH SUREOHP ZLWKRXW XVLQJ D GDWD VWUXFWXUH 616 5HPHPEHU WKDW UHFXUVLYH SURJUDPPLQJ LQYROYHV WKLQNLQJ DERXW FDVHV :KDW ZRXOG EH WKH VLPSOHVW ILOH WR UHYHUVH" $ RQH OLQH ILOH ZRXOG EH IDLUO\ HDV\ WR UHYHUVH EXW LW ZRXOG EH HYHQ HDVLHU WR UHYHUVH DQ HPSW\ ILOH 6R ZH FDQ EHJLQ ZULWLQJ RXU PHWKRG DV IROORZV public static void reverse(Scanner input) { if (!input.hasNextLine()) { // base case (empty file) ... } else { // recursive case (nonempty file) ... } } )RU WKLV SUREOHP WKH EDVH FDVH LV VR VLPSOH WKDW WKHUH LVQ W DQ\WKLQJ WR GR $Q HPSW\ ILOH KDV QR OLQHV WR UHYHUVH 6R IRU WKLV SUREOHP LW PDNHV PRUH VHQVH WR WXUQ DURXQG WKH LI HOVH VR WKDW ZH WHVW IRU WKH UHFXUVLYH FDVH 7KDW ZD\ ZH FDQ PDNH LW D VLPSOH LI WKDW KDV DQ LPSOLHG HOVH WKHUH LV QRWKLQJ WR GR public static void reverse(Scanner input) { if (input.hasNextLine()) { // recursive case (nonempty file) ... } } $JDLQ WKH FKDOOHQJH LV WR GR D OLWWOH ELW +RZ GR ZH JHW D ELW FORVHU WR EHLQJ GRQH" :H FDQ UHDG RQH OLQH RI WH[W IURP WKH ILOH public static void reverse(Scanner input) { if (input.hasNextLine()) { // recursive case (nonempty file) String line = input.nextLine(); ... } } 6R LQ RXU H[DPSOH WKLV ZRXOG UHDG WKH OLQH WKLV LQWR WKH YDULDEOH OLQH DQG ZRXOG OHDYH XV ZLWK WKHVH WKUHH OLQHV RI WH[W LQ WKH 6FDQQHU is fun no? :H UH WU\LQJ WR SURGXFH WKLV RYHUDOO RXWSXW no? fun is this <RX PLJKW EH DVNLQJ \RXUVHOI TXHVWLRQV OLNH ,V WKHUH DQRWKHU OLQH RI LQSXW WR SURFHVV" 7KDW V QRW KRZ WR WKLQN UHFXUVLYHO\ ,I \RX UH WKLQNLQJ UHFXUVLYHO\ \RX G WKLQN DERXW ZKDW D FDOO RQ WKH PHWKRG ZRXOG JHW \RX 6LQFH WKH 6FDQQHU LV SRVLWLRQHG LQ IURQW RI WKH WKUHH OLQHV LV IXQ QR" D FDOO RQ UHYHUVH VKRXOG UHDG LQ WKRVH OLQHV DQG SURGXFH WKH ILUVW WKUHH OLQHV RI RXWSXW WKDW ZH UH ORRNLQJ IRU ,I WKDW ZRUNV WKHQ ZH G MXVW KDYH WR ZULWH RXW WKH OLQH WKLV DIWHUZRUGV WR FRPSOHWH WKH RXWSXW 617 7KLV LV ZKHUH WKH OHDS RI IDLWK FRPHV LQ :H KDYH WR EHOLHYH WKDW WKH UHYHUVH PHWKRG DFWXDOO\ ZRUNV %HFDXVH LI LW GRHV WKHQ WKLV FRGH FDQ EH FRPSOHWHG DV IROORZV public static void reverse(Scanner input) { if (input.hasNextLine()) { // recursive case (nonempty file) String line = input.nextLine(); reverse(input); System.out.println(line); } } ,W WXUQV RXW WKDW WKLV ZRUNV 7R UHYHUVH D VHTXHQFH RI OLQHV \RX UHDG WKH ILUVW RQH LQ UHYHUVH WKH RWKHUV DQG WKHQ ZULWH WKH ILUVW RQH RXW ,W GRHVQ W VHHP WKDW LW VKRXOG EH WKDW VLPSOH EXW LW LV 0HFKDQLFV RI 5HFXUVLRQ 1RYLFHV VHHP WR XQGHUVWDQG UHFXUVLRQ EHWWHU ZKHQ WKH\ NQRZ PRUH DERXW WKH XQGHUO\LQJ PHFKDQLFV WKDW PDNH LW ZRUN %HIRUH ZH H[DPLQH D UHFXUVLYH PHWKRG LQ GHWDLO ZH VKRXOG PDNH VXUH ZH XQGHUVWDQG KRZ QRQUHFXUVLYH PHWKRGV ZRUN &RQVLGHU WKH IROORZLQJ VLPSOH SURJUDP 1 2 3 4 5 6 7 8 9 10 11 12 13 14 public class DrawTriangles { public static void drawTriangle() { System.out.println(" *"); System.out.println(" ***"); System.out.println("*****"); System.out.println(); } public static void drawTwoTriangles() { drawTriangle(); drawTriangle(); } } ,W VLPSO\ SULQWV WKUHH WULDQJOHV * *** ***** * *** ***** * *** ***** +RZ GR ZH GHVFULEH WKH PHWKRG FDOOV WKDW WDNH SODFH LQ WKLV SURJUDP" ,PDJLQH WKDW HDFK PHWKRG KDV EHHQ ZULWWHQ RQ D GLIIHUHQW SLHFH RI SDSHU :H EHJLQ SURJUDP H[HFXWLRQ E\ JRLQJ WR PHWKRG PDLQ VR LPDJLQH JUDEELQJ WKH VKHHW RI SDSHU ZLWK PDLQ RQ LW 618 +---------------------------------------------+ | public static void main(String args) { | | drawTriangle(); | | drawTwoTriangles(); | | } | +---------------------------------------------+ 7R H[HFXWH WKH PHWKRG ZH JR WKURXJK HDFK RI WKH VWDWHPHQWV IURP ILUVW WR ODVW DQG H[HFXWH HDFK RQH 6R ZH ILUVW KLW WKH FDOO RQ GUDZ7ULDQJOH +---------------------------------------------+ | public static void main(String args) { | | --> drawTriangle(); | | drawTwoTriangles(); | | } | +---------------------------------------------+ :H NQRZ WKDW DW WKLV SRLQW LQ WLPH WKH FRPSXWHU VWRSV H[HFXWLQJ PDLQ DQG WXUQV LWV DWWHQWLRQ WR WKH GUDZ7ULDQJOH PHWKRG $ JRRG ZD\ WR WKLQN DERXW WKLV LV WKDW \RX JUDE WKH SLHFH RI SDSHU ZLWK GUDZ7ULDQJOH ZULWWHQ RQ LW DQG \RX SXW LW RYHU WKH SLHFH RI SDSHU ZLWK PDLQ RQ LW +----------------------------------------------+ | public static void main(String args) { | | +----------------------------------------------+ | | public static void drawTriangle() { | | | System.out.println(" *"); | +--| System.out.println(" ***"); | | System.out.println("*****"); | | System.out.println(); | | } | +----------------------------------------------+ 1RZ ZH H[HFXWH H...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern