{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

15-recursive_backtracking - CSE 143 Lecture 15 Recursive...

Info icon This preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CSE 143 Lecture 15 Recursive Backtracking slides created by Marty Stepp http://www.cs.washington.edu/143/ ideas and examples taken from Stanford University CS slides/lectures 2 Exercise: Permutations • Write a method permute that accepts a string as a parameter and outputs all possible rearrangements of the letters in that string. The arrangements may be output in any order. – Example: permute("MARTY") outputs the following sequence of lines: MARTY MARYT MATRY MATYR MAYRT MAYTR MRATY MRAYT MRTAY MRTYA MRYAT MRYTA MTARY MTAYR MTRAY MTRYA MTYAR MTYRA MYART MYATR MYRAT MYRTA MYTAR MYTRA AMRTY AMRYT AMTRY AMTYR AMYRT AMYTR ARMTY ARMYT ARTMY ARTYM ARYMT ARYTM ATMRY ATMYR ATRMY ATRYM ATYMR ATYRM AYMRT AYMTR AYRMT AYRTM AYTMR AYTRM RMATY RMAYT RMTAY RMTYA RMYAT RMYTA RAMTY RAMYT RATMY RATYM RAYMT RAYTM RTMAY RTMYA RTAMY RTAYM RTYMA RTYAM RYMAT RYMTA RYAMT RYATM RYTMA RYTAM TMARY TMAYR TMRAY TMRYA TMYAR TMYRA TAMRY TAMYR TARMY TARYM TAYMR TAYRM TRMAY TRMYA TRAMY TRAYM TRYMA TRYAM TYMAR TYMRA TYAMR TYARM TYRMA TYRAM YMART YMATR YMRAT YMRTA YMTAR YMTRA YAMRT YAMTR YARMT YARTM YATMR YATRM YRMAT YRMTA YRAMT YRATM YRTMA YRTAM YTMAR YTMRA YTAMR YTARM YTRMA YTRAM
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 3 Examining the problem • Think of each permutation as a set of choices or decisions : Which character do I want to place first? Which character do I want to place second? – ... solution space : set of all possible sets of decisions to explore • We want to generate all possible sequences of decisions. for (each possible first letter): for (each possible second letter): for (each possible third letter): ... print! 4 Decision trees chosen available M A R T Y M A R T Y M A R T Y M A R T Y M A R T Y M A R T Y M R A T Y M T A R Y M Y A R T A M R T Y M A T R Y M A Y R T M A R Y T M A R Y T M A Y R T M A Y R T M A Y T R M A Y T R M Y A R T ... ... ... ... ... ...
Image of page 2
3 5 Backtracking backtracking : A general algorithm for finding solution(s) to a computational problem by trying partial solutions and then abandoning them ("backtracking") if they are not suitable.
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

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

{[ snackBarMessage ]}