Recall Brute Force as a Problem Solving Technique
Recall • Brute Force as a Problem Solving Technique • Exhaustive Search as a Problem Solving Technique • Interesting problems Selection Sort, Bubble Sort* String Matching Closest-Pair Traveling Salesman Knapsack Assignment Problem • • Algorithms for generating permutations • Algorithms for generating all subsets 1
Outline • Algorithms for generating permutations • Algorithms for generating all subsets • Decrease-and-Conquer 2
Recall from Lecture 7 q summation puzzles w pot + pan = bib w dog + cat = pig w boy + girl = baby q Two conditions are assumed: n n the correspondence between letters and decimal digits is one-to-one the digit zero does not appear as the leftmost digit in any of the numbers. © 2013 Goodrich, Tamassia, Goldwasser Recursion 3
Recall from Lecture 7 q Multiple recursion: n n makes potentially many recursive calls not just one or two © 2013 Goodrich, Tamassia, Goldwasser Recursion 4
Algorithm for Multiple Recursion Algorithm Puzzle. Solve(k, S, U): Input: Integer k, sequence S, and set U (universe of elements to test) Output: Enumeration of all k-length extensions to S using elements in U without repetitions for all e in U do Remove e from U {e is now being used} Add e to the end of S if k = 1 then Test whether S is a configuration that solves the puzzle if S solves the puzzle then return “Solution found: ” + S else Puzzle. Solve(k - 1, S, U) Add e back to U {e is now unused} Remove e from the end of S 5
Slide by Matt Stallmann included with permission. Example cbb + ba = abc 799 + 98 = 897 a, b, c stand for 7, 8, 9; not necessarily in that order [] {a, b, c} [b] {a, c} b=7 [a] {b, c} a=7 [ab] {c} a=7, b=8 c=9 [ac] {b} a=7, c=8 b=9 [c] {a, b} c=7 [ca] {b} c=7, a=8 b=9 [ba] {c} b=7, a=8 c=9 © 2013 Goodrich, Tamassia, Goldwasser [cb] {a} c=7, b=8 a=9 [bc] {a} b=7, c=8 a=9 Recursion 6
Visualizing Puzzle. Solve Initial call Puzzle. Solve (3, (), {a, b, c}) Puzzle. Solve (2, b, {a, c}) Puzzle. Solve (2, a, {b, c}) Puzzle. Solve (1, ab, {c}) Puzzle. Solve (1, ba, {c}) abc bac Puzzle. Solve (2, c, {a, b}) Puzzle. Solve (1, ca, {b}) cab Puzzle. Solve (1, ac, {b}) Puzzle. Solve (1, bc, {a}) Puzzle. Solve (1, cb, {a}) acb bca cba © 2013 Goodrich, Tamassia, Goldwasser Recursion 7
Generating Permutations Algorithm: If n = 1 return 1; otherwise, generate recursively the list of all permutations of 12…n-1 and then insert n into each of those permutations by starting with inserting n into 12. . . n-1 by moving right to left and then switching direction for each new permutation Example: n=3 start insert 2 into 1 right to left insert 3 into 12 right to left insert 3 into 21 left to right 1 12 123 321 21 132 231 312 213 A. Levitin “Introduction to the Design & Analysis of Algorithms, ” 3 rd ed. , Ch. 4 © 2012 Pearson Education, Inc. Upper Saddle River, NJ. All 8
Other permutation generating algorithms b Johnson-Trotter [L] page 145 b Lexicographic-order algorithm [L] page 146 b Heap’s algorithm (Problem 9 in Assignment 4) A. Levitin “Introduction to the Design & Analysis of Algorithms, ” 3 rd ed. , Ch. 4 © 2012 Pearson Education, Inc. Upper Saddle River, NJ. All 9
Johnson-Trotter Algorithm b associate a direction with each element k in a permutation b The element k is said to be mobile in such an arrow-marked permutation if its arrow points to a smaller number adjacent to it 10
Johnson-Trotter Algorithm 11
Generating Subsets b IDEA: • All subsets correspond to the binary string of length n • For n=3 b Can we find minimal-change algorithm? • Yes A. Levitin “Introduction to the Design & Analysis of Algorithms, ” 3 rd ed. , Ch. 4 © 2012 Pearson Education, Inc. Upper Saddle River, NJ. All 12
Generating Subsets Binary reflected Gray code: minimal-change algorithm for generating 2 n bit strings corresponding to all the subsets of an n-element set where n > 0 If n=1 make list L of two bit strings 0 and 1 else generate recursively list L 1 of bit strings of length n-1 copy list L 1 in reverse order to get list L 2 add 0 in front of each bit string in list L 1 add 1 in front of each bit string in list L 2 append L 2 to L 1 to get L return L A. Levitin “Introduction to the Design & Analysis of Algorithms, ” 3 rd ed. , Ch. 4 © 2012 Pearson Education, Inc. Upper Saddle River, NJ. All 13
Binary reflected Gray code A. Levitin “Introduction to the Design & Analysis of Algorithms, ” 3 rd ed. , Ch. 4 © 2012 Pearson Education, Inc. Upper Saddle River, NJ. All 14
- Slides: 14