Lecture Preview: Recursive Backtracking 2

(Suggested book reading: Programming Abstractions in C++, Chapter 9)

Today we will cover more recursive backtracking. As a preview to what we'll talk about, think about the following problem:

Write a function named 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. For example, the call of permute("MARTY") produces the following output:

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

How would we solve this problem using backtracking? 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? Etc. We will "explore" it together (no pun intended!) in class.

This document and its content are copyright © Marty Stepp, 2018. All rights reserved. Any redistribution, reproduction, transmission, or storage of part or all of the contents in any form is prohibited without the authors' expressed written permission.