15-recursive_backtracking

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

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: 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 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 ... ... ... ... ... ... 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. a "brute force" algorithmic technique (tries all paths; not clever) often (but not always) implemented recursively Applications: producing all permutations of a set of values parsing languages games: anagrams, crosswords, word jumbles, 8 queens path-planning (e.g., robots or parts assembly) 6 Backtracking algorithms A general pseudo-code algorithm for backtracking problems:...
View Full Document

This note was uploaded on 09/29/2010 for the course CSE 143 taught by Professor Sr during the Spring '08 term at University of Washington.

Page1 / 9

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

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online