Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recall Brute Force as a Problem Solving Technique

Similar presentations


Presentation on theme: "Recall Brute Force as a Problem Solving Technique"— Presentation transcript:

1 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

2 Outline Algorithms for generating permutations
Algorithms for generating all subsets Decrease-and-Conquer

3 Recall from Lecture 7 summation puzzles Two conditions are assumed:
pot + pan = bib dog + cat = pig boy + girl = baby Two conditions are assumed: the correspondence between letters and decimal digits is one-to-one the digit zero does not appear as the left-most digit in any of the numbers. © 2013 Goodrich, Tamassia, Goldwasser Recursion

4 Recall from Lecture 7 Multiple recursion:
makes potentially many recursive calls not just one or two © 2013 Goodrich, Tamassia, Goldwasser Recursion

5 Algorithm for Multiple Recursion
Algorithm PuzzleSolve(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 PuzzleSolve(k - 1, S,U) Add e back to U {e is now unused} Remove e from the end of S

6 Slide by Matt Stallmann included with permission.
Example cbb + ba = abc a,b,c stand for 7,8,9; not necessarily in that order = 897 [] {a,b,c} [a] {b,c} a=7 [b] {a,c} b=7 [c] {a,b} c=7 [ab] {c} a=7,b=8 c=9 [ac] {b} a=7,c=8 b=9 [ba] {c} b=7,a=8 [bc] {a} b=7,c=8 a=9 [ca] {b} c=7,a=8 [cb] {a} c=7,b=8 © 2013 Goodrich, Tamassia, Goldwasser Recursion

7 Visualizing PuzzleSolve
( 3 , () ,{ a b c } ) Initial call 2 1 ab ac cb ca bc ba abc acb bac bca cab cba © 2013 Goodrich, Tamassia, Goldwasser Recursion

8 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 1 insert 2 into 1 right to left 12 21 insert 3 into 12 right to left insert 3 into 21 left to right A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 4 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

9 Other permutation generating algorithms
Johnson-Trotter [L] page 145 Lexicographic-order algorithm [L] page 146 Heap’s algorithm (Problem 9 in Assignment 4) A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 4 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

10 Johnson-Trotter Algorithm
associate a direction with each element k in a permutation 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

11 Johnson-Trotter Algorithm

12 Generating Subsets IDEA: Can we find minimal-change algorithm?
All subsets correspond to the binary string of length n For n=3 Can we find minimal-change algorithm? Yes A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 4 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

13 Generating Subsets Binary reflected Gray code: minimal-change algorithm for generating 2n 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 L1 of bit strings of length n-1 copy list L1 in reverse order to get list L2 add 0 in front of each bit string in list L1 add 1 in front of each bit string in list L2 append L2 to L1 to get L return L A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 4 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.

14 Binary reflected Gray code
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 4 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.


Download ppt "Recall Brute Force as a Problem Solving Technique"

Similar presentations


Ads by Google