Recall Brute Force as a Problem Solving Technique

Slides:



Advertisements
Similar presentations
1 Counting Techniques: Possibility Trees, Multiplication Rule, Permutations.
Advertisements

A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
© 2004 Goodrich, Tamassia Using Recursion1 Programming with Recursion.
Multiplication Rule. A tree structure is a useful tool for keeping systematic track of all possibilities in situations in which events happen in order.
© 2004 Goodrich, Tamassia Using Recursion1. © 2004 Goodrich, Tamassia Using Recursion2 Recall the Recursion Pattern (§ 2.5) Recursion: when a method calls.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
© 2004 Goodrich, Tamassia Using Recursion1. © 2004 Goodrich, Tamassia Using Recursion2 Recall the Recursion Pattern (§ 2.5) Recursion: when a method calls.
Dynamic Programming A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River,
Fall 2006CSC311: Data Structures1 Chapter 3 Arrays, Linked Lists, and Recursion Objectives –Using Arrays –Singly Linked Lists –Doubly Linked Lists –Circularly.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 11 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Using Recursion1 Recursion © 2010 Goodrich, Tamassia.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
RAIK 283: Data Structures & Algorithms
MA/CSSE 473 Day 14 Permutations wrap-up Subset generation (Horner’s method)
MA/CSSE 473 Day 18 Permutations by lexicographic order number.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 9 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3 rd ed., Ch. 1 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Introduction to Digital Electronics Lecture 5: Function Minimisation.
Recursion1 © 2013 Goodrich, Tamassia, Goldwasser.
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Sorting and Runtime Complexity CS255. Sorting Different ways to sort: –Bubble –Exchange –Insertion –Merge –Quick –more…
Generating Permutations and Combinations “Traveling Salesman Problem” – A salesman must visit 6 cities; what’s the best order in which to visit them? Assume.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Chapter 11 Sorting Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount.
Brute Force A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples: Computing.
Recursion 5/4/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Recursion 5/22/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Brute Force A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples: Computing.
Decrease-and-Conquer
More Simple Recursive Problems
What is an algorithm? An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate.
What is an algorithm? An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate.
What is an algorithm? An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate.
CS212D: Data Structures Week 5-6 Linked List.
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
What is an algorithm? An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate.
Divide-and-Conquer The most-well known algorithm design strategy:
What is an algorithm? An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate.
CS212D: Data Structures Week 5-6 Linked List.
Decrease-and-Conquer
Divide-and-Conquer The most-well known algorithm design strategy:
Space-for-time tradeoffs
Decrease-and-Conquer
What is an algorithm? An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate.
Recursion © 2013 Goodrich, Tamassia, Goldwasser Recursion Sequences
Decrease-and-Conquer
Decrease-and-Conquer
Brute Force A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples: Computing.
Brute Force A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples: Computing.
Divide-and-Conquer The most-well known algorithm design strategy:
Space-for-time tradeoffs
Recall Some Algorithms And Algorithm Analysis Lots of Data Structures
Divide-and-Conquer The most-well known algorithm design strategy:
Space-for-time tradeoffs
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
Programming with Recursion
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
Sequences 6/1/2019 7:49 PM Using Recursion Using Recursion.
Recursion © 2013 Goodrich, Tamassia, Goldwasser Recursion Sequences
Data Structures & Programming
What is an algorithm? An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate.
Presentation transcript:

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

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

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

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

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

Slide by Matt Stallmann included with permission. Example cbb + ba = abc a,b,c stand for 7,8,9; not necessarily in that order 799 + 98 = 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

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

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

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.

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

Johnson-Trotter Algorithm

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.

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.

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.