Docsity

Introduction to Computer science - Programming Abstractions, Lecture 10, Lecture notes of Programming Abstractions

Toggle sidebar
bg1
Admin
Today’s topics
•Recursive backtracking
Reading
•Reader ch. 6 (today)
•Next: pointers! 2.2-2.3, linked lists 9.5(sort of)
Terman cafe after class
Lec ture #10
Refresh: permute code
void RecPermute(string soFar, string rest)
{
if (rest == "") {
cout << soFar << endl;
} else {
for (int i = 0; i < rest.length(); i++) {
string next = soFar + rest[i];
string remaining = rest.substr(0, i)
+ rest.substr(i+1);
RecPermute(next, remaining);
}
}
}
// "wrapper" function
void ListPermutations(string s)
{
RecPermute("", s);
}
Tree of recursive calls
P("ac","bd")
Permute("", "abcd")
P("b","acd") P("c","abd") P("d","abc")
P("abc","d")
P("abcd","")
P("a","bcd")
P("ab","cd")
P("abd","c")
P("abdc","")
P("ad","bc")
P("acb","d")
P("acbd","")
P("acd","b")
P("acdb","")
Subsets
Enumerate all subsets of input
•"abc" has subsets "a", "b", "ab", "ac", …
•Order doesn't matter, "ab" is same as "ba"
Solving recursively
•Separate one element from input
•Can either include in current subset or not
•Recursively form subsets including it
•Recursively form subsets not including it
•What is the base case?
Remind you of any other problem you've seen?
•Same patterns often resurface!
pf3
pf4

Partial preview of the text

Download Introduction to Computer science - Programming Abstractions, Lecture 10 and more Lecture notes Programming Abstractions in PDF only on Docsity!

Admin

Today’s topics

  • Recursive backtracking

Reading

  • Reader ch.^ 6 (today)
  • Next:^ pointers! 2.2-2.3,^ linked lists 9.5(sort of)

Terman cafe after class

Lecture # 10

Refresh: permute code

void RecPermute(string soFar, string rest) { if (rest == "") { cout << soFar << endl; } else { for (int i = 0; i < rest.length(); i++) { string next = soFar + rest[i]; string remaining = rest.substr(0, i)

  • rest.substr(i+1); RecPermute(next, remaining); } } } // "wrapper" function void ListPermutations(string s) { RecPermute("", s); }

Tree of recursive calls

P("ac","bd") Permute("", "abcd") P("b","acd") P("c","abd") P("d","abc") P("abc","d") P("abcd","") P("a","bcd") P("ab","cd") P("abd","c") P("abdc","") P("ad","bc") P("acb","d") P("acbd","") P("acd","b") P("acdb","")

Subsets

Enumerate all subsets of input

  • "abc" has subsets "a",^ "b",^ "ab",^ "ac",^ …
  • Order doesn't matter,^ "ab" is same as "ba"

Solving recursively

  • Separate one element from input
  • Can either include in current subset or not
  • Recursively form subsets including it
  • Recursively form subsets not including it
  • What is the base case?

Remind you of any other problem you've seen?

  • Same patterns often resurface!

Subset strategy

Result is empty, starting input is "abcd"

Consider first element: "a"

Add to subset, remaining input is "bcd"

Recursively find all subsets from here

Repeat recursion without including "a"

Subsets code

void RecSubsets(string soFar, string rest) { if (rest == "") cout << soFar << endl; else { // add to subset, remove from rest, recur RecSubsets(soFar + rest[0], rest.substr(1)); // don't add to subset, remove from rest,recur RecSubsets(soFar, rest.substr(1)); } } void ListSubsets(string str) { RecSubsets("", str); }

Tree of recursive calls

Subsets("", "abcd")

S("a","bcd")

S("ab","cd")

S("abc","d")

abcd

S("ab","d")

abc abd^ ab ad^ a

S("","bcd")

S("a","cd")

S("ac","d") S("a","d")

acd ac

Exhaustive recursion

Permutations/subsets are about choice

  • Both have deep/wide tree of recursive calls
  • Depth represents total number of decisions made
  • Width of branching represents number of available options per decision

Exhaustive recursion is, well, exhaustive

  • Explores every possible option at every decision point
  • Typically very expensive
    • N! permutations, 2 N^ subsets
    • (Recursion isn't the problem, there just is a huge space to explore)

Consider partial exploration of exhaustive space

  • Similar exhaustive structure,^ but stop at first "satisfactory" outcome

N queens code

bool Solve(Grid &board, int col) { if (col >= board.numCols()) return true; // base case for (int rowToTry=0; rowToTry<board.numRows(); rowToTry++) { if (IsSafe(board,rowToTry,col)) { PlaceQueen(board, rowToTry, col); if (Solve(board, col + 1)) return true; RemoveQueen(board, rowToTry, col); } } return false; }