1 Chapter 4 Generating Permutations and Combinations.

Slides:



Advertisements
Similar presentations
DEFINITION Let f : A  B and let X  A and Y  B. The image (set) of X is f(X) = {y  B : y = f(x) for some x  X} and the inverse image of Y is f –1 (Y)
Advertisements

Covers, Dominations, Independent Sets and Matchings AmirHossein Bayegan Amirkabir University of Technology.
Longest Common Subsequence
4.4 Permutations and Combinations of multisets
Recursive Definitions and Structural Induction
COUNTING AND PROBABILITY
Counting Chapter 6 With Question/Answer Animations.
1 Chapter Equivalence, Order, and Inductive Proof.
PC -1 Permutations and Combinations Appendix. PC -2 Permutations # of m-permutations of n items: 3-permutations of {1,2,3,4} in lexicographic order: 1.
Lecture 5 Counting 4.3, Permutations r-permutation: An ordered arrangement of r elements of a set of n distinct elements. Example: S={1,2,3}:
ENM 207 Lecture 5. FACTORIAL NOTATION The product of positive integers from 1 to n is denoted by the special symbol n! and read “n factorial”. n!=1.2.3….(n-2).(n-1).n.
Combinations We should use permutation where order matters
1 Section 6.1 Recurrence Relations. 2 Recursive definition of a sequence Specify one or more initial terms Specify rule for obtaining subsequent terms.
Copyright © Cengage Learning. All rights reserved.
Applied Combinatorics, 4th Ed. Alan Tucker
Induction and recursion
Chapter 8 With Question/Answer Animations 1. Chapter Summary Applications of Recurrence Relations Solving Linear Recurrence Relations Homogeneous Recurrence.
BY MISS FARAH ADIBAH ADNAN IMK
Copyright © Cengage Learning. All rights reserved. CHAPTER 11 ANALYSIS OF ALGORITHM EFFICIENCY ANALYSIS OF ALGORITHM EFFICIENCY.
Advanced Counting Techniques
Copyright © Cengage Learning. All rights reserved. CHAPTER 9 COUNTING AND PROBABILITY.
Chapter 6 The Inclusion-Exclusion Principle and Applications
Chapter 8. Section 8. 1 Section Summary Introduction Modeling with Recurrence Relations Fibonacci Numbers The Tower of Hanoi Counting Problems Algorithms.
Basic Counting. This Lecture We will study some basic rules for counting. Sum rule, product rule, generalized product rule Permutations, combinations.
Chapter The Basics of Counting 5.2 The Pigeonhole Principle
Generalized Permutations and Combinations
Discrete Mathematical Structures (Counting Principles)
Chapter 6 With Question/Answer Animations 1. Chapter Summary The Basics of Counting The Pigeonhole Principle Permutations and Combinations Binomial Coefficients.
1 Chapter 7 Generating functions. 2 Summary Generating functions Recurrences and generating functions A geometry example Exponential generating functions.
Copyright © Cengage Learning. All rights reserved. CHAPTER 4 ELEMENTARY NUMBER THEORY AND METHODS OF PROOF.
Fall 2002CMSC Discrete Structures1 One, two, three, we’re… Counting.
March 10, 2015Applied Discrete Mathematics Week 6: Counting 1 Permutations and Combinations How many different sets of 3 people can we pick from a group.
5.6 Generating Permutations and Combinations Generating Permutations Many different algorithms have been developed to generate the n! permutations of this.
FUNCTIONS Definition Let A = {1, 2, 3,..., n}, and f : A → A be a bijective function; then f is called a permutation on n. QUESTION: for a set with N elements.
Chapter 7 Advance Counting Techniques. Content Recurrence relations Generating function The principle of inclusion-exclusion.
Lecture 5 Counting 4.3, Permutations r-permutation: An ordered arrangement of r elements of a set of n distinct elements. Example: S={1,2,3}:
Chapter 5 The Binomial Coefficients
Copyright © Cengage Learning. All rights reserved. CHAPTER 9 COUNTING AND PROBABILITY.
Simple Arrangements & Selections. Combinations & Permutations A permutation of n distinct objects is an arrangement, or ordering, of the n objects. An.
4.1.2 Pigeonhole principle:Strong Form  Theorem 4.2: Let q 1,q 2,…,q n be positive integers. If q 1 +q 2 +…+q n -n+1 objects are put into n boxes, then.
FUNCTIONS.
CS 103 Discrete Structures Lecture 13 Induction and Recursion (1)
Block Ciphers and the Advanced Encryption Standard
ALGORITHMS.
1 Chapter 5-1 Greedy Algorithms Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
1 Chapter 2 Pigeonhole Principle. 2 Summary Pigeonhole principle –simple form Pigeonhole principle –strong form Ramsey’s theorem.
Discrete Mathematics Lecture # 22 Recursion.  First of all instead of giving the definition of Recursion we give you an example, you already know the.
CompSci 102 Discrete Math for Computer Science March 13, 2012 Prof. Rodger Slides modified from Rosen.
FUNCTIONS COSC-1321 Discrete Structures 1. Function. Definition Let X and Y be sets. A function f from X to Y is a relation from X to Y with the property.
2/24/20161 One, two, three, we’re… Counting. 2/24/20162 Basic Counting Principles Counting problems are of the following kind: “How many different 8-letter.
Copyright © Peter Cappello 2011 Simple Arrangements & Selections.
Fr: Discrete Mathematics by Washburn, Marlowe, and Ryan.
Resource-Constrained Project Scheduling Problem (RCPSP)
MA/CSSE 473 Day 16 Combinatorial Object Generation Permutations.
Sorting by placement and Shift Sergi Elizalde Peter Winkler By 資工四 B 周于荃.
Discrete Mathematics and Its Applications Sixth Edition By Kenneth Rosen Chapter 5 Counting 歐亞書局.
Generating Permutations and Combinations “Traveling Salesman Problem” – A salesman must visit 6 cities; what’s the best order in which to visit them? Assume.
Fall 2002CMSC Discrete Structures1 Combinations An r-combination of elements of a set is an unordered selection of r elements from the set. Thus,
11.1 CompSci 102© Michael Frank Today’s topics CountingCounting –Generalized Pigeonhole Principle –Permutations –Combinations –Binomial Coefficients –Writing.
Math 3121 Abstract Algebra I Lecture 6 Midterm back over+Section 7.
Section 6.3. Section Summary Permutations Combinations.
Modeling with Recurrence Relations
MA/CSSE 473 Day 13 Finish Topological Sort Permutation Generation
Discrete Structure II: Introduction
CS100: Discrete structures
Heap Sort The idea: build a heap containing the elements to be sorted, then remove them in order. Let n be the size of the heap, and m be the number of.
Copyright © Cengage Learning. All rights reserved.
Quadratic Sorts & Breaking the O(n2) Barrier
Lecture Sets 2.2 Set Operations.
AND.
Presentation transcript:

1 Chapter 4 Generating Permutations and Combinations

2 Summary Generating permutations Inversions in Permutations Generating combinations Generating r-combinations

3 Mobile Integer Given an integer k we assign a direction to it by writing an arrow above it pointing to the left or to the right. Consider a permutation of {1,2,…,n} in which each of the integers is given a direction. The integer k is called mobile if its arrow points to a smaller integer adjacent to it.

4 Example for mobile integer For only 3, 5, and 6 are mobile. Integer 1 can never be mobile since there is no integer smaller than 1. The integer n is mobile, except in two cases: i)n is the first integer and its arrow points to the left. ii)n is the last integer and its arrow points to the right.

5 Algorithm for Generating the Permutations of {1,2,…,n} Begin with While there exists a mobile integer, do 1)find the largest mobile integer m. 2)switch m and the adjacent integer its arrow points to. 3)switch the direction of all integers p with p>m.

6 Illustration of the Algorithm

7 Exercises For n =3, illustrate the above algorithm For n =4, illustrate the above algorithm

8 Inversions in Permutations Let i 1 i i n be a permutation of the set {1,2, …,n}. The pair (i k, i l ) is called an inversion if k i l. An inversion in a permutation corresponds to a pair of numbers which are out of their natural order. The only permutation of {1,2, …,n} with no inversions is 1 2 … n.

9 The number of inversions Let a j denote the number of inversions. It equals the number of integers which precede j in the permutation but are greater than j; it measures how much j is out of order. The sequence of numbers a 1, a 2, …, a n is called the inversion sequence of the permutation i 1 i i n. The number a 1 + a 2 + …+ a n measures the disorder of a permutation.

10 Examples of inversions Consider the permutation It has four inversions, namely (3, 1), (3, 2), (5, 2), (5, 4). The inversion sequence of the permutation is 1, 2, 0, 1, 0.

11 A theorem Let b 1, b 2, …, b n be a sequence of integers satisfying Then there exists a unique permutation of {1, 2, …, n} whose inversion sequence is b 1, b 2, …, b n.

12 Algorithm I n: write down n. n-1: consider b n-1. We are given that If b n-1 =0, then n-1 must be placed before n. If b n-1 =1, then n-1 must be placed after n. n-2: consider b n-2. We are given that If b n-2 = 0, then n-2 must be placed before the two numbers from step n-1. If b n-2 =1, then n-2 must be placed between the two numbers from step n-1. If b n-2 =2, then n-2 must be placed after the two numbers from step n-1.

13 Algorithm I (cont’d) n-k: (general step) consider b n-k. We are given that In step n-k+1, the k numbers n, n-1, …, n-k+1 have already been placed in the required order. If b n-k = 0, then n-k must be placed before all the numbers from step n-k+1. If b n-k =1, then n-k must be placed between the first two numbers ……. If b n-k =k, then n-k must be placed after all the numbers from step n-k+1. 1: We must place 1 after the b 1st number in the sequence constructed in step n-1.

14 Comments on Algorithm I The algorithm can determine the unique permutation of {1, 2, …, n} whose inversion sequence is b 1, b 2, …, b n. The disadvantage of this algorithm is that the location of each integer in the permutation I not known until the very end; only the relative positions of the integers remain fixed throughout the algorithm.

15 Algorithm II Begin with n empty locations which label 1, 2, …, n from left to right 1: put 1 in location number b : put 2 in the (b 2 + 1)st empty location …… k: (general step) counting from the left we put k in the (b k +1)st empty location. n: put n in the one remaining empty location.

16 Comments on Algorithm II The algorithm can determine the unique permutation of {1, 2, …, n} whose inversion sequence is b 1, b 2, …, b n. The advantage of this algorithm is that the location of each integer in the permutation can be determined.

17 Example Determine the permutation of {1, 2, 3, 4, 5, 6,7,8} whose inversion sequence is 5, 3, 4,0, 2, 1, 1,

18 Exercises Determine the permutation of {1, 2, 3, 4, 5, 6,7,8} whose inversion sequence is 7, 3, 4,4, 2, 1, 1, 0. Algorithm I: insert the integers one by one Algorithm II: find the locations of the integers one by one.

19 An example Bring the permutation to by successive switches of adjacent numbers. The inversion sequence is Hence there will be 6 times of switch

20 Generating combinations Let S = {x n-1, …, x 1, x 0 } be a set of n elements. We now seek an algorithm which generates all of the 2 n combinations (subsets) of S. We identify the 2 n combinations with the 2 n n- tuples (a n-1, …, a 1, a 0 ) = a n-1 …a 1 a 0 of 0’s and 1’s, where the ith term a i correspond to the element x i for each i = 0, 1, …, n-1.

21 Base 2 algorithm for generating the combinations of {x n-1, …, x 1, x 0 } Begin with a n-1 …a 1 a 0 = 0…00. while a n-1 …a 1 a 0 ≠ 1…11, do: 1) find the smallest integer j (between n-1 and 0) such that a j = 0. 2) replace a j by 1 and each of a j-1, …, a 0 (which by our choice of j all equal 1) by 0.

22 Generating r-combinations Let A and B be two r-combinations of the set {1, 2, …, n}. Then, we say that A precedes B in the lexicographic order provided the smallest integer which is in their union A ∪ B but not in their intersection A ∩ B is in A.

23 Example and Exercise Let 5-combinations A and B of {1, 2,…,8} be given by A = {2,3,4,7,8}, B= {2, 3,5,6,7}. Then the smallest element which is in one but not both of the sets is 4. Hence A precedes B in the lexicographic order. Consider 5-combinations of {1,2,…,9}. What 5- combination immediately follows 12389?

24 A theorem Let a 1 a 2 …a r ≠ (n-r+1)(n-r+2)…n be an r- combination of {1, 2, …, n}. Let k be the largest integer such that a k < n and a k +1 is different from each of a 1, a 2, …a r.Then the r-combination which is the immediate successor of a 1 a 2 …a r in the lexicographic ordering is a 1 …a k-1 (a k +1)(a k +2)…(a k +r- k+1).

25 Algorithm for generating the r- combinations Begin with the r-combination a 1 a 2 …a r =12…r. while a 1 a 2 …a r ≠ (n-r+1)(n- r+2)…n, do 1) determine the largest integer k such that a k +1 ≤ n and a k +1 is not one of a 1, a 2, …a r. 2) replace a 1 a 2 …a r with the r-combination a 1 a 2 …a k-1 (a k +1)(a k +2)…(a k +r-k+1).

26 Example for illustrating the algorithm Generate the 4-combinations of S = {1,2,3,4,5,6}. 1234; 1235; 1236; 1245;1246; 1256; 1345; 1346; 1356; 1456; 2345; 2346; 2356; 2456; 3456.

27 Comments Now we understand how to generate permutations and r-combinations of a set. Question: How to generate r-permutations of an n-element set? If we combine the algorithm for generating permutations of a set with the algorithm for generating r-combinations of an n-element set, we obtain an algorithm for generating r- permutations of an n-element set.

28 Exercise Generate the 3-permutations of {1,2,3,4}.