MA/CSSE 473 Day 14 Permutations wrap-up Subset generation (Horner’s method)

Slides:



Advertisements
Similar presentations
CSC 421: Algorithm Design & Analysis
Advertisements

22C:19 Discrete Structures Trees Spring 2014 Sukumar Ghosh.
Chapter 5: Decrease and Conquer
Chap 5: Decrease & conquer. Objectives To introduce the decrease-and-conquer mind set To show a variety of decrease-and-conquer solutions: Depth-First.
Design & Analysis of Algorithms CS315
Decrease-and-Conquer
Lecture 8 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Dynamic Programming Reading Material: Chapter 7..
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Algorithm Design Techniques: Induction Chapter 5 (Except Section 5.6)
Chapter 10: Algorithm Design Techniques
Dynamic Programming A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River,
The Design and Analysis of Algorithms
Induction and recursion
MA/CSSE 473 Day 22 Gray Code More Decrease and Conquer Algorithms No class Day 22 in because of Abby's broken arm.
Section 11.4 Language Classes Based On Randomization
MA/CSSE 473 Day 03 Asymptotics A Closer Look at Arithmetic With another student, try to write a precise, formal definition of “t(n) is in O(g(n))”
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
MA/CSSE 473 Day 13 Permutation Generation. MA/CSSE 473 Day 13 HW 6 due Monday, HW 7 next Thursday, Student Questions Tuesday’s exam Permutation generation.
A few leftover Decrease-and-conquer Algorithms
MA/CSSE 473 Day 02 Some Numeric Algorithms and their Analysis.
1 Decrease-and-Conquer 1. Reduce problem instance to smaller instance of the same problem 2. Solve smaller instance 3. Extend solution of smaller instance.
CSC401: Analysis of Algorithms CSC401 – Analysis of Algorithms Chapter 5 – 2. Divide-and-Conquer Objectives: Introduce the Divide-and-conquer paradigm.
Analysis of Algorithms
MA/CSSE 473 Day 17 Permutations by lexicographic order number.
Combinatorial Algorithms Reference Text: Kreher and Stinson.
RAIK 283: Data Structures & Algorithms
MA/CSSE 473 Day 18 Permutations by lexicographic order number.
MA/CSSE 473 Day 19 Exam Prep. MA/CSSE 473 Day 19 If you want additional practice problems for Tuesday's exam: –The "not to turn in" problems from various.
MA/CSSE 473 Day 20 Questions before exam More decrease and Conquer.
MA/CSSE 473 Day 02 Some Numeric Algorithms and their Analysis.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 7.
Analysis & Design of Algorithms (CSCE 321)
MA/CSSE 473 Day 19 Subset Generation. MA/CSSE 473 Day 19 HW 7 due today Exam 1 Thursday There will be significant time for your questions in tomorrow's.
MA/CSSE 473 Day 21 In , Day 21 was the exam.
MA/CSSE 473 Day 16 Combinatorial Object Generation Permutations.
MA/CSSE 473 Day 09 Modular Division Revisited Fermat's Little Theorem Primality Testing.
MA/CSSE 473 Day 12 Amortization (growable Array) Knuth interview Brute Force Examples.
MA/CSSE 473 Day 14 Strassen's Algorithm: Matrix Multiplication Decrease and Conquer DFS.
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
MA/CSSE 473 Day 11 Knuth interview Amortization (growable Array) Brute Force Examples.
Hard Problems Some problems are hard to solve.  No polynomial time algorithm is known.  E.g., NP-hard problems such as machine scheduling, bin packing,
1 Section 5.1 Analyzing Algorithms Let P be a problem and A an algorithm to solve P. The running time of A can be analyzed by counting the number of certain.
(Proof By) Induction Recursion
MA/CSSE 473 Day 15 Return Exam Student questions Towers of Hanoi
Some more Decrease-and-conquer Algorithms
Decrease-and-Conquer
The Design and Analysis of Algorithms
CSC 421: Algorithm Design & Analysis
CSC 421: Algorithm Design & Analysis
Analysis of Algorithms
MA/CSSE 473 Day 13 Finish Topological Sort Permutation Generation
MA/CSSE 473 Day 16 Answers to your questions Divide and Conquer
MA/CSSE 473 Day 02 Some Numeric Algorithms and their Analysis
CSC 421: Algorithm Design & Analysis
Decrease-and-Conquer
Chapter 5 Decrease-and-Conquer
Decrease-and-Conquer
Decrease-and-Conquer
Decrease-and-Conquer
CSE 2010: Algorithms and Data Structures Algorithms
Recall Brute Force as a Problem Solving Technique
CSC 421: Algorithm Design & Analysis
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
CSC 421: Algorithm Design & Analysis
Presentation transcript:

MA/CSSE 473 Day 14 Permutations wrap-up Subset generation (Horner’s method)

MA/CSSE 473 Day 14 Student questions Monday will begin with "ask questions about exam material” time. Exam details are Day 16 of the schedule page. Today's topics: –Permutations wrap-up –Generating subsets of a set –(Horner’s method)

Permutations and order Given a permutation of 0, 1, …, n-1, can we directly find the next permutation in the lexicographic sequence? Given a permutation of 0..n-1, can we determine its permutation sequence number? numberpermutationnumberpermutation Given n and i, can we directly generate the i th permutation of 0, …, n-1?

Yesterday's Discovery Which permutation follows each of these in lexicographic order? – –Try to write an algorithm for generating the next permutation, with only the current permutation as input.

Lexicographic Permutation class These are the basics of the class setup. The next() method provides an iterator over the permutations. How should it get from one permutation to the next?

Main permutation method

More discoveries Which permutation follows each of these in lexicographic order? – –Try to write an algorithm for generating the next permutation, with only the current permutation as input. If the lexicographic permutations of the numbers [0, 1, 2, 3, 4] are numbered starting with 0, what is the number of the permutation 14032? –General algorithm? How to calculate efficiency? In the lexicographic ordering of permutations of [0, 1, 2, 3, 4, 5], which permutation is number 541? –General algorithm? How to calculate efficiently? –Application: Generate a random permutation

Memoized factorial function

Find a permutation's sequence #

Find permutation from sequence #

SUBSET GENERATION Bottom-up, “numeric order”, binary reflected Gray code

Generate all Subsets of a Set Sample Application: –Solving the knapsack problem –In the brute force approach, we try all subsets If A is a set, the set of all subsets is called the power set of A, and often denoted 2 A If A is finite, then So we know how many subsets we need to generate.

Generating Subsets of {a 1, …, a n } Decrease by one (bottom up): Generate S n-1, the collection of the 2 n-1 subsets of {a 1, …, a n-1 } Then S n = S n-1  { S n-1  {a n } : s  S n-1 } Numeric approach: –Each subset of {a 1, …, a n } corresponds to an bit string of length n, where the i th bit is 1 iff a i is in the subset

Details of numeric approach: Each subset of {a 1, …, a n } corresponds to a bit string of length n, where the J th bit is 1 if and only if a J is in the subset def allSubsets(a): n = len(a) subsets=[] for i in range(2**n): subset = [] current = i for j in range (n): if current % 2 == 1: subset += [a[j]] current /= 2 subsets += [subset] return subsets Output for a=[1, 2, 3]: [[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]

Gray Codes Named for Frank Gray An ordering of the 2 n n-bit binary codes such that any two consecutive codes differ in only one bit Example: 000, 001, 011, 010, 110, 111, 101, 100 Note also that only one bit changes between the last code and the first code. A Gray code can be represented by its transition sequence: indicates which bit changes each time In above example: 0, 1, 0, 2, 0, 1, 0 Traversal of the edges of a (hyper)cube. In terms of subsets, the transition sequence tells which element to add or remove from one subset to get the next subset

Recursively Generating a Gray Code Binary Reflected Gray Code T 1 = 0 T n+1 = T n, n, T n reversed Show by induction that T n reversed = T n Thus T n+1 = T n, n, T n

Iteratively Generating a Gray Code We add a parity bit, p. Set all bits (including p) to 0. * Based on Knuth, Volume 4, Fascicle 2, page 6.

Side road: Polynomial Evaluation Given a polynomial p(x) = a n x n + a n-1 x n-1 + … + a 1 x + a 0 How can we efficiently evaluate p(c) for some number c? Apply this to evaluation of " " or any other string that represents a positive integer. Write and analyze (pseudo)code

Horner's method code

OTHER DECREASE-AND-CONQUER ALGORITHMS Decrease by a constant factor Decrease by a variable amount

Fake Coin Problem We have n coins All but one have the same weight One is lighter We have a balance scale with two pans. All it will tell us is whether the two sides have equal weight, or which side is heavier What is the minimum number of weighings that will guarantee that we find the fake coin? Decrease by factor of two.

Decrease by a Constant Factor Examples that we have already seen: –Binary Search –Exponentiation (ordinary and modular) by repeated squaring –Recap: Multiplication à la Russe (The Dasgupta book that I followed for the first part of the course called it "European" instead of "Russian") Example Then strike out any rows whose first number is even, and add up the remaining numbers in the second column.

Decrease by a variable amount Search in a Binary Search Tree Interpolation Search –See Levitin, pp –Also Weiss, Section Median Finding –Find the k th element of an (unordered) list of n elements –Start with quicksort's partition method –Best case analysis