Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 MA/CSSE 473 Day 19 Subset Generation

2 MA/CSSE 473 Day 19 HW 7 due today Exam 1 Thursday There will be significant time for your questions in tomorrow's class HW 8 has grace days to allow you to finish it early in the break if you wish. HW9 will be due on Tuesday after the break, HW 10 on Friday With HW 11, we get back to Monday-Thursday schedule Student Questions Subset Generation

3 Preliminary: Reversing a string Definition of reverse: –The reverse of the empty string is the empty string –If a is a character and w is a string, (aw) R = w R a. Lemma: –For any two strings, x and y, (xy) R = y R x R Proof by induction on length of y –Base case: y is empty –Otherwise, y = za for some string z and some character a. Assume by induction that the property is true for the shorter string z.

4 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.

5 Generating Subsets of {a 1, …, a n } Decrease by one: 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  {a n } : s  S n-1 } Another approach: –Each subset of {a 1, …, a n } corresponds to an bit string of length n, where the i th bit it 1 iff a i is in the subset

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

7 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

8 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

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

10 Quote of the Day There are 10^11 stars in the galaxy. That used to be a huge number. But it's only a hundred billion. It's less than the national deficit! We used to call them astronomical numbers. Now we should call them economical numbers. - Richard FeynmanRichard Feynman


Download ppt "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."

Similar presentations


Ads by Google