Generating Permutations and Combinations

Similar documents
Week 4-5: Generating Permutations and Combinations

MATH 433 Applied Algebra Lecture 14: Functions. Relations.

Relations MATH Relations. Benjamin V.C. Collins, James A. Swenson MATH 2730

3. R = = on Z. R, S, A, T.

9 RELATIONS. 9.1 Reflexive, symmetric and transitive relations. MATH Foundations of Pure Mathematics

Sets. A set is a collection of objects without repeats. The size or cardinality of a set S is denoted S and is the number of elements in the set.

Economics 204 Summer/Fall 2017 Lecture 1 Monday July 17, 2017

Chapter 3. Cartesian Products and Relations. 3.1 Cartesian Products

Relations and Equivalence Relations

Week 4-5: Binary Relations

Relations (3A) Young Won Lim 3/27/18

CIS 375 Intro to Discrete Mathematics Exam 3 (Section M004: Blue) 6 December Points Possible

CIS 375 Intro to Discrete Mathematics Exam 3 (Section M001: Green) 6 December Points Possible

Week 4-5: Binary Relations

a + b = b + a and a b = b a. (a + b) + c = a + (b + c) and (a b) c = a (b c). a (b + c) = a b + a c and (a + b) c = a c + b c.

Properties of the Integers

Definition: A binary relation R from a set A to a set B is a subset R A B. Example:

Lecture 7: Relations

Relations. We have seen several types of abstract, mathematical objects, including propositions, predicates, sets, and ordered pairs and tuples.

Chapter 1. Sets and Numbers

Seminaar Abstrakte Wiskunde Seminar in Abstract Mathematics Lecture notes in progress (27 March 2010)

Discrete Structures (2IT50)

CSC Discrete Math I, Spring Relations

Introduction. Foundations of Computing Science. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

Chapter 6. Relations. 6.1 Relations

Section Summary. Relations and Functions Properties of Relations. Combining Relations

Packet #5: Binary Relations. Applied Discrete Mathematics

Relations --- Binary Relations. Debdeep Mukhopadhyay IIT Madras

Selected Solutions to Even Problems, Part 3

Foundations of algebra

XMA2C011, Annual Examination 2012: Worked Solutions

Reading 11 : Relations and Functions

Foundations of Mathematics MATH 220 FALL 2017 Lecture Notes

Relations. Relations of Sets N-ary Relations Relational Databases Binary Relation Properties Equivalence Relations. Reading (Epp s textbook)

Relations. Relations. Definition. Let A and B be sets.

EQUIVALENCE RELATIONS (NOTES FOR STUDENTS) 1. RELATIONS

HL Test 2018 Sets, Relations and Groups [50 marks]

cse303 ELEMENTS OF THE THEORY OF COMPUTATION Professor Anita Wasilewska

Chapter 9: Relations Relations

Discrete Mathematics with Applications MATH236

Section 7.1 Relations and Their Properties. Definition: A binary relation R from a set A to a set B is a subset R A B.

Relations. Definition 1 Let A and B be sets. A binary relation R from A to B is any subset of A B.

Copyright c 2007 Jason Underdown Some rights reserved. statement. sentential connectives. negation. conjunction. disjunction

Report 1 The Axiom of Choice

MATH1240 Definitions and Theorems

We want to show P (n) is true for all integers

Chapter VI. Relations. Assumptions are the termites of relationships. Henry Winkler

Gray Codes and Overlap Cycles for Restricted Weight Words

Combinatorial algorithms

Massachusetts Institute of Technology 6.042J/18.062J, Fall 02: Mathematics for Computer Science Professor Albert Meyer and Dr.

Relations, Functions, and Sequences

Notes. Relations. Introduction. Notes. Relations. Notes. Definition. Example. Slides by Christopher M. Bourke Instructor: Berthe Y.

MATH 13 SAMPLE FINAL EXAM SOLUTIONS

Equivalence relations

COMP 182 Algorithmic Thinking. Relations. Luay Nakhleh Computer Science Rice University

Solutions to In Class Problems Week 4, Mon.

Graph Theory. Thomas Bloom. February 6, 2015

Relations Graphical View

21-301, Spring 2019 Homework 4 Solutions

Homework #2 Solutions Due: September 5, for all n N n 3 = n2 (n + 1) 2 4

Definitions. Notations. Injective, Surjective and Bijective. Divides. Cartesian Product. Relations. Equivalence Relations

SOME TRANSFINITE INDUCTION DEDUCTIONS

1.4 Equivalence Relations and Partitions

Intersecting curves (variation on an observation of Maxim Kontsevich) by Étienne GHYS

On sets, functions and relations

Practice Exam 1 CIS/CSE 607, Spring 2009

Multiple Choice Questions for Review

Set theory. Math 304 Spring 2007

The Advantage Testing Foundation Olympiad Solutions

arxiv: v2 [cs.dm] 29 Mar 2013

A BRIEF INTRODUCTION TO ZFC. Contents. 1. Motivation and Russel s Paradox

cse303 ELEMENTS OF THE THEORY OF COMPUTATION Professor Anita Wasilewska

SYSTEMS ANALYSIS LECTURE 2 SYSTEM IDENTIFICATION. Zuzana Bělinová

DISCRETIZED CONFIGURATIONS AND PARTIAL PARTITIONS

REVIEW QUESTIONS. Chapter 1: Foundations: Sets, Logic, and Algorithms

Complete Induction and the Well- Ordering Principle

Chapter 4. Measure Theory. 1. Measure Spaces

Simplification by Truth Table and without Truth Table

School of Mathematics and Statistics. MT1003 Pure Mathematics. Handout 0: Course Information

Finite Automata and Regular Languages

What are relations? f: A B

CHAPTER 1. Relations. 1. Relations and Their Properties. Discussion

5 Flows and cuts in digraphs

Chapter 5: Integer Compositions and Partitions and Set Partitions

5. Partitions and Relations Ch.22 of PJE.

Bulletin of the Iranian Mathematical Society

Discrete Structures May 13, (40 points) Define each of the following terms:

MODEL ANSWERS TO THE SEVENTH HOMEWORK. (b) We proved in homework six, question 2 (c) that. But we also proved homework six, question 2 (a) that

Discrete Mathematics Fall 2018 Midterm Exam Prof. Callahan. Section: NetID: Multiple Choice Question (30 questions in total, 4 points each)

Ranking Score Vectors of Tournaments

Basic Proof Examples

Chapter 5: Integer Compositions and Partitions and Set Partitions

Midterm Exam. There are 6 problems. Your 5 best answers count. Please pay attention to the presentation of your work! Best 5

CSE 20 DISCRETE MATH. Winter

Strange Combinatorial Connections. Tom Trotter

MATH 220 (all sections) Homework #12 not to be turned in posted Friday, November 24, 2017

Preliminaries to the Theory of Computation

Generating p-extremal graphs

CMPSCI 250: Introduction to Computation. Lecture #11: Equivalence Relations David Mix Barrington 27 September 2013

MITOCW Lec 11 MIT 6.042J Mathematics for Computer Science, Fall 2010

Transcription:

Generating Permutations and Combinations March 0, 005 Generating Permutations We have learned that there are n! permutations of {,,, n} It is important in many instances to generate a list of such permutations For example, for the permutation of {,,, }, we may insert 5 in to generate five permutations of {,,,, 5} as follows: 5, 5, 5, 5, 5 If we have a complete list of permutations for {,,, n }, then we can obtain a complete list of permutations for {,,, n} by inserting n in n ways to each permutation of the list for {,,, n } For n =, the list is just For n =, the list is For n =, the list is = = To generate a complete list of permutations for the set {,,, n}, we assign a direction to each integer k {,,, n} by writing an arrow above it pointing to the left or to the right: k or k We consider permutations of {,,, n} in which each integer is given a direction; such permutations are called directed permutations An integer k in a directed permutation is called mobile of its arrow points to a smaller integer adjacent to it For example, for 5 6, the integers, 5, and 6 are mobile It follows that can never be mobile since there is no integer in {,,, n} smaller than The integer n is mobile, except two cases: (i n is the first integer and its arrow points to the left, ie, n ; (ii n is the last integer and its arrow points to the right, ie, n

For n =, we have the list = Algorithm Algorithm for Generating Permutations of {,,, n}: Step 0 Begin with n Step Find the largest mobile integer m Step Switch m and the adjacent integer its arrow points to Step Switch the directions for all integers p > m Step Write down the resulting permutation with directions and return to Step For example, for n =, we have and For n =, we have,,,,, For n =, the algorithm produces the list Proof Observe that when n is not the largest mobile the direction of n must be either like n or n

in the permutation When the largest mobile m (with m < n is switched with its target integer to produce a new permutation, the direction of n will be changed simultaneously, and the permutation with direction becomes n or n Now n is the largest mobile Switching n with its target integer for n times to produce n more permutations, we obtain exactly n new permutations (including the one before switching n The algorithm stops at the permutation n Inversions of Permutations Let u u u n be a permutation of S = {,,, n} We can view u u u n as as a bijection π : S S defined by π( = u, π( = u,, π(n = u n If u i > u j for some i < j, then (u i, u j is called an inversion of π The number of inversions of π is denoted by inv(π For example, the permutation 765 of {,,, 7} has the inversions: (,, (,, (,, (,, (6, 5, (7, 5, (7, 6 For k {,,, n} and u j = k, let a k be the number of integers that precede k in the permutation u u u n but greater than k, ie, a k = #{u i : u i > u j = k, i < j} = #{π(i : π(i > π(j = k, i < j} It measures how much k is out of order by counting the numbers of integers larger than k but located before k The tuple (a, a,, a n is called the inversion sequence (or inversion table of the permutation π = u u u n The sum a + a + + a n measures the total disorder of a permutation Example The inversion sequence of the permutation 765 of {,,, 7} is (,, 0, 0,,, 0 It is clear that for any permutation π of {,,, n}, the inversion sequence (a, a,, a n of π satisfies 0 a n, 0 a n,, 0 a n, a n = 0 ( It is easy to see that the number of sequences (a, a,, a n satisfying ( equals n (n = n! This suggests that the inversion sequences may be characterized by ( Theorem Let (a, a,, a n be an integer sequence satisfying 0 a n, 0 a n,, 0 a n, a n = 0 Then there is a unique permutation π of {,,, n} whose inversion sequence is (a, a,, a n Proof We give two algorithms to uniquely construct the permutation whose inversion sequence is (a, a,, a n Algorithm I Construction of a Permutation from Its Inversion Sequence: Step 0 Write down n Step If a n = 0, place n before n; if a n =, place n after n Step If a n = 0, place n before the two members n and n ; if a n =, place n between n and n ; if a n =, place n after both n and n Step k If a n k = 0, place n k to the left of the first position; if a n k =, place n k to the right of the st existing number; if a n k =, place n k to the right of the nd existing number; ; if a n k = k, place n k to the right of the last existing number In general, insert n k to the right of the a n k th existing number

Step n If a = 0, place before all existing numbers; otherwise, place to the right of the a th existing number For example, for the inversion sequence (a, a,, a 8 = (, 6,, 0,,,, 0, its permutation can be constructed by Algorithm I as follows: 8 Write down 8 87 Since a 7 =, insert 7 to the right of the first number 8 867 Since a 6 =, insert 6 to the right of the first number 8 8675 Since a 5 =, insert 5 to the right of the third number 7 8675 Since a = 0, insert to the left of the first number 8 8675 Since a =, insert to the right of the first number 8675 Since a = 6, insert to the right of the sixth number 5 8675 Since a =, insert to the right of the fifth number 6 Algorithm II Construction of a Permutation from Its Inversion Sequence: Step 0 Mark down n empty spaces Step Put into the (a + th empty space from left Step Put into the (a + th empty space from left Step k Put k into the (a k + th empty space from left Step n Put n into the (a n + th empty space (the last empty box from left For example, the permutation for the inversion sequence (a, a,, a 8 = (, 6,, 0,,,, 0 can be constructed by Algorithm II as follows: Mark down 8 empty spaces Since a =, put into the 5th empty space Since a = 6, put into the 7th empty space Since a =, put into the nd empty space Since a = 0, put into the st empty space 5 Since a 5 =, put 5 into the th empty space 6 5 Since a 6 =, put 6 into the nd empty space 675 Since a 7 =, put 7 into the nd empty space 8675 Since a 8 = 0, put 8 into the st empty space Generating Combinations Let S be an n-set For convenience of generating combinations of S, we take S to be the set S = {x n, x n,, x, x, x 0 } Each subset A of S can be identified as a function χ A : S {0, }, called the characteristic function of A, defined by { if x A χ A (x = 0 if x A In practice, χ A is represented by a 0- sequence or a base numeral For example, for S = {x 7, x 6, x 5, x, x, x, x, x 0 }, 00000000 {x 7, x 5, x, x } 0000 {x 6, x 5, x, x, x 0 } 000 {x 7, x 6, x 5, x, x, x, x, x 0 }

Algorithm The algorithm for Generating Combinations of {x n, x n,, x, x, x 0 }: Step 0 Begin with a n a a 0 = 0 00 Step If a n a a 0 =, stop Step If a n a a 0, find the smallest integer j such that a j = 0 Step Change a j, a j,, a 0 (from 0 to or from to 0, write down a n a a 0, and return to Sept For n =, the algorithm produces the list 0000 000 000 00 000 00 00 0 000 00 00 0 00 0 0 The unit n-cube Q n is a graph whose vertex set is the set of all 0- sequences of length n, and two sequences are adjacent if they differ in only one place A Gray code of order n is a path of Q n that visits every vertex of Q n exactly once, ie, a Hamilton path of Q n For example, 000 00 0 00 0 00 0 is a Gray code of order It is obvious that this Gray code can not be a part of any Hamilton cycle since 000 and are not adjacent A cyclic Gray code of order n is a Hamilton cycle of Q n For example, the closed path 000 00 0 00 0 0 00 000 is a cyclic Gray code of order For n =, we have the Gray code 0 For n =, we use 0 to produce the path 00 0 by adding a 0 in the front, and use 0 to produce 0 by adding a in the front, then combine the two paths to produce the Gray code 00 0 0 For n =, we use the Gray code (of order 00 0 0 to produce the path 000 00 0 00 by adding 0 in the front, and use the Gray code 0 0 00 (the reverse of 00 0 0 to produce the path 0 0 00 Combine the two paths to produce the Gray code of order 000 00 0 00 0 0 00 The Gray codes obtained in this way are called reflected Gray codes Algorithm Algorithm for Generating reflected Gray codes of order n: Step 0 Begin with a n a n a 0 = 00 0 Step If a n a n a 0 = 0 0, stop Step If a n + a n + + a 0 = even, then change a 0 (from 0 to or to 0 Step If a n + a n + a 0 = odd, find the smallest j such that a j = and change a j+ (from 0 to or to 0 Step Write down a n a n a 0 and return to Step We note that if a n a n a 0 0 0 and a n + a n + + a 0 = odd, then j n so that j + n and a j+ is defined We also note that the smallest number j in Step may be 0, ie, a 0 = ; if so there is no i < j such that a i = 0 and we change a j+ = a as instructed in Step Proof We proceed by induction on n For n =, it is obviously true For n =, we have 00 0 0 Let n and assume that it is true for,,, n ( When the algorithm is applied, by the induction hypothesis the first resulted n words form the reflected Gray code of order n with a 0 attached to each word; the n th word is 00 0 ( Continuing the algorithm, we have 00 0 0 0 5

Now for each word of the form b n b 0, the parity of b n b 0 is the same as the parity of b n b 0 Continuing the algorithm, the next n words (including 0 0 form a reflected Gray code (of order n with a attached at the beginning; the last word is 0 0 ( Continuing the algorithm, we have 0 0 00 0 The next n words (including 00 0 form a reflected Gray code (of order n with a 0 attached at the beginning; the last word is 00 0 ( 00 0 000 0 ; there are n words with 00 attached at the beginning (n Continuing the algorithm, we have 0 000 0 0000 The next words (including 0 000 form a reflected Gray code (of order with 0 0 attached at the beginning; the last word is 0 00 (n 0 00 0 000 0 00; there are words 0 000 and 0 00 (of order with 0 00 attached at the beginning (n 0 00 0 000 The algorithm produces only word 0 000 (n + Finally, the algorithm ends at 0 000 0 0000 Note that all words produced in Steps (k (n + are distinct from the words produced in Step (k, where k n + Thus the words produced by the algorithm are distinct and the total number of words is + + + + + n = n This implies that the sequence of the words produced forms the reflected Gray code of order n Generating r-combinations Let S = {,,, n} When an r-combination or r-subset A = {a, a,, a r } of S is given, we always assume that a < a < < a r For two r-combinations A = {a, a,, a r } and B = {b, b,, b r } of S, if there is an integer k ( k r such that a = b, a = b,, a k = b k, a k < b k, we say that A precedes B in the lexicographic order, written A < B Then the set P r (S of all r-subsets of S is linearly ordered by the lexicographic order For simplicity, we write an r-combination {a, a,, a r } as an r-permutation a a a r with a < a < < a r Theorem Let a a a r be an r-combination of {,,, n} The first r-combination in lexicographic order is r, and the last r-combination in lexicographic order is (n r + (n n If a a a k a r (n r + (n n and k is the largest integer such that a k n r + k, then the successor of a a a r is a a a k (a k + (a k + (a k + r k + Proof Since a i (n r + i for all i r, then a k n r + k implies a k < n r + k Algorithm Algorithm for Generating r-combinations of {,,, n} in Lexicographic Order: Step 0 Begin with the r-combination a a a r = r Step If a a a r = (n r + (n n, stop Step If a a a r (n r + (n n, find the largest k such that a k < n r + k Step Change a a a r to a a k (a k + (a k + (a k + r k +, write down a a a r, and return back to Step 6

Example The collection of all -combinations of {,,,, 5, 6} are listed by the algorithm: 5 5 56 56 5 6 6 5 56 6 56 56 6 56 Theorem Let a a a r be an r-combination of {,,, n} Then the number of r-combinations up to the place a a a r in lexicographic order equals ( ( ( ( ( n n a n a n ar n ar r r r Proof The r-combinations b b b r after a a a r can be classified into r kinds: ( b > a ; there are ( n a r such ( r-combinations ( b = a, b > a ; there are n a r such r-combinations ( b = a, b = a, b > a ; there are such r-combinations ( n a r (r b = a,, b r = a r, b r > a r ; there are ( n a r such r-combinations (r b = a,, b r = a r, b r > a r ; there are ( n a r such r-combinations Since the number of r-combinations of {,,, n} is ( n r, the conclusion follows immediately Example The -combinations of {,,,, 5} are as follows:,, 5,, 5, 5,, 5, 5, 5 The -permutations of {,,,, 5} can be obtained by making! permutations for each -combination: 5 Partially Ordered Sets 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 Definition 5 A relation on a set X is a subset R of the product set X X A relation R on X is called reflexive if xrx for all x X; irreflexive if x Rx for all x X; symmetric provided that if xry for some x, y X then yrx; antisymmetric provided that if xry and yrx for some x, y X then x = y; 5 transitive provided that if xry and yrz for some x, y, z X then xrz Example 5 ( The relation of subset,, is a reflexive and transitive relation on the power set P (X ( The relation of divisibility,, is a reflexive and transitive relation on the set of positive integers A partial order on a set X is a reflexive, antisymmetric, and transitive relation A strict partial order on a set X is an irreflexive, antisymmetric, and transitive relation If a relation R is a partial order, we usually denote R by ; the relation < is defined by a < b if and only if a b but a b Conversely, for a strict partial order < on a set X, the relation defined by a b if and only if a < b or a = b is a partial order A set X with a partial order is called a partially ordered set (or poset for short and is denoted by (X, A linear order or total order on a set X is a strict order < such that for any two distinct elements a and b, either a < b or b < a 7

Let and be two partial orders on a set X The poset (X, is called an extension of the poset (X, if, whenever a b, then a b In particular, an extension of a partial order has more compatible pairs We show that every finite poset has a linear extension, that is, an extension which is a linearly ordered set Theorem 5 Let (X, be a finite partially ordered set Then there is a linear order such that (X, is an extension of (X, Proof We need to show that the elements of X can be listed in some order x, x,, x n in such a way that if x i x j then x i comes before x j in this list, ie, i j The following algorithm does the job Algorithm 5 Algorithm for a Linear Extension of an n-poset: Step Choose a minimal element x from X (with respect to the ordering Step Delete x from X; choose a minimal element x from X {x } Step Delete x from X {x }; choose a minimal element x from X {x, x } Step n Delete x n from X {x,, x n } and choose the only element x n in X {x,, x n } A relation R on X is called an equivalence relation if R is reflexive, symmetric, and transitive For an equivalence relation R on a set X and an element x X, we call the set [x] = {y X : xry} an equivalence class of R and x a representative of the equivalence class [x] Theorem 5 Let R be an equivalence relation on a set X Then for any x, y X, the following are logically equivalent: (i [x] [y] ; (ii [x] = [y]; and (iii xry A collection P = {A, A,, A k } of nonempty subsets of a set X is called a partition of X if A i A j = for i j and X = k i= A i We will show below that if R is an equivalence relation on a set X, then the collection P R = {[x] : x X} is a partition of X Conversely, if P = {A, A,, A k } is a partition of X, then the relation is an equivalence relation on X R P = k A i A i i= Theorem 55 Let R be an equivalence relation on a set X, and let P = {A, A,, A k } be a partition of X Then ( P R is a partition of X; ( R P is an equivalence relation on X; ( R PR = R, P RP = P 8