Generating Permutations and Combinations
E N D
Presentation Transcript
Generating Permutations and Combinations • “Traveling Salesman Problem” – A salesman must visit 6 cities; what’s the best order in which to visit them? • Assume real road distances that are not uniform….How many orders (permutations) exist? 1 6 2 3 5 4
Generating Permutations and Combinations • Some numbers from the set • {x1, x2, x3, x4, x5, x6} • Add up to 100….find which ones. • How? • Find all of the subsets of this set…or, the power set. There are 2n possibilities.
Generating Permutations and Combinations • A laboratory has 95 employees. A group of 12 of them with a particular set of 25 skills is needed for a project. How do we select them? • Theorem 2: There are C(n+r-1,r) r-combinations from a set with n elements when repetition of elements is allowed
Generating Permutations • To generate all of the permutations of a set… • Place the elements of the set in one-to-one correspondence with the set {1, 2, 3…n} • List the permutations of the set of elements by generating the permutations of the n integers, then replacing them with the corresponding set elements. • Procedure for this based on: • Lexicographic ordering: Permutations ordered based on precedence: a1, a2,… anprecedes b1, b2,… bn if for some k from 1 to n, a1= b1, a2= b2,… but ak< bk.
Lexicographic Ordering • Generate the permutations of the integers 1, 2, 3 in lexicographic order. • {1, 2, 3} • 123 • 132 • 213 • 231 • 312 • 321
Lexicographic Ordering • What is the lexicographic ordering of the following members of the set • {1, 2, 3, 4, 5}?? • 52143 31425 12543 31245 • What is the permutation that follows 52143? • 52144? • NO!!!! This is not a permutation.
Lexicographic Ordering • What is the next after 52143 in lexicographic ordering of the set • {1, 2, 3, 4, 5}?? • Begin by swapping the last two elements: 52134…. But this is smaller • Rearrange the last THREE elements: 52431, 52314, 52413, 52341….which is next in order? • 52314 < 52341 < 52413 < 52431 • So…52314.
Generating Next Largest • See Algorithm 1, Page 346
Generating Combinations • To generate all of the combinations of a set… • Since a combination is just a subset, use the correspondence between the subsets of the set {1, 2, 3…n} and the bit string of length n. • There are 2n subsets • Example: Find the next largest 2-combination of {1, 2, 3, 4} after {2, 3}. • {2,4}
Generating Combinations • Example: Find the next largest 4 -combination of {1, 2, 3, 4, 5, 6} after {1, 2, 5, 6}. • {1, 3, 5, 6} • Example: Find the next largest bit string after 101011 • 101100 • See algorithm 2, page 347, and algorithm 3, page 348.
Homework • Page 348: 1, 3, 5, 7