MORE Counting
CS 30 : Discrete Mathematics for Computer Science First Semester, AY 2012-2013
https://sites.google.com/a/dcs.upd.edu.ph/nhsh_classes/cs 30
sablay-logo
Nestine Hope S. Hernandez Algorithms and Complexity Laboratory Department of Computer Science University of the Philippines, Diliman nshernandez@dcs.upd.edu.ph updilseal
Day 15
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
MORE Counting
Combinatorics and Computing
sablay-logo
1
MORE Counting Generating Permutations and Combinations
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
MORE Counting
Generating Permutations and Combinations
Introduction
Sometimes permutations or combinations need to be generated, not just counted.
sablay-logo
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
MORE Counting
Generating Permutations and Combinations
Introduction
Sometimes permutations or combinations need to be generated, not just counted.
sablay-logo
Suppose that a salesman must visit six di erent cities. In which order should these cities be visited to minimize total travel time?
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
MORE Counting
Generating Permutations and Combinations
Introduction
Sometimes permutations or combinations need to be generated, not just counted.
sablay-logo
Suppose that a salesman must visit six di erent cities. In which order should these cities be visited to minimize total travel time? Suppose we are given a set of six positive integers and wish to nd a subset of them that has 100 as their sum, if such a subset exists. updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
MORE Counting
Generating Permutations and Combinations
Introduction
Sometimes permutations or combinations need to be generated, not just counted.
sablay-logo
Suppose that a salesman must visit six di erent cities. In which order should these cities be visited to minimize total travel time? Suppose we are given a set of six positive integers and wish to nd a subset of them that has 100 as their sum, if such a subset exists. Suppose a laboratory has 95 employees where each employee can have one or more skills. A group of 12 of these employees with a particular set of 25 skills is needed for a project.
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
MORE Counting
Generating Permutations and Combinations
Lexicographic ordering
In this ordering, the permutation a1 a2 · · · an precedes b1 b2 · · · bn , if for some k , with 1 ≤ k ≤ n, a1 = b1 , a2 = b2 , · · · , ak −1 = bk −1 , and ak < bk .
the permutation
sablay-logo
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
MORE Counting
Generating Permutations and Combinations
Lexicographic ordering
In this ordering, the permutation a1 a2 · · · an precedes the permutation b1 b2 · · · bn , if for some k , with 1 ≤ k ≤ n, a1 = b1 , a2 = b2 , · · · , ak −1 = bk −1 , and ak < bk . (i.e. the permutation with the smaller number in the rst position where the permutations disagree goes rst)
sablay-logo
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
MORE Counting
Generating Permutations and Combinations
Lexicographic ordering
In this ordering, the permutation a1 a2 · · · an precedes the permutation b1 b2 · · · bn , if for some k , with 1 ≤ k ≤ n, a1 = b1 , a2 = b2 , · · · , ak −1 = bk −1 , and ak < bk . (i.e. the permutation with the smaller number in the rst position where the permutations disagree goes rst)
sablay-logo
Example: 23415 precedes 23514
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
MORE Counting
Generating Permutations and Combinations
Generating Permutations
sablay-logo
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
MORE Counting
Generating Permutations and Combinations
Generating Permutations
To produce the n! permutations of the integers 1, 2, 3, . . . , n, begin with the smallest permutation in lexographic order, namely, 123 · · · n, and successively apply the procedure for producing the next larger permutation n! − 1 times. This yields all the permutations of the n smallest integers in lexicographic order.
sablay-logo
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
MORE Counting
Generating Permutations and Combinations
sablay-logo
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
MORE Counting
Generating Permutations and Combinations
Generating Combinations
How can we generate all the combinations of the elements of a nite set?
sablay-logo
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
MORE Counting
Generating Permutations and Combinations
Generating Combinations
How can we generate all the combinations of the elements of a nite set? Because a combination is just a subset, we can use the correspondence between subsets of {a , a , 路 路 路 , an } and bit strings of length n. 1
2
sablay-logo
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
MORE Counting
Generating Permutations and Combinations
Generating Combinations
How can we generate all the combinations of the elements of a nite set? Because a combination is just a subset, we can use the correspondence between subsets of {a , a , · · · , an } and bit strings of length n. 1
2
Recall that a bit string of length n is also the binary expansion of an integer between 0 and 2n − 1. The 2n bit strings can be listed in order of their increasing size as integers in their binary expansions.
sablay-logo
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
MORE Counting
Generating Permutations and Combinations
Generating Combinations
How can we generate all the combinations of the elements of a nite set? Because a combination is just a subset, we can use the correspondence between subsets of {a , a , · · · , an } and bit strings of length n. 1
2
Recall that a bit string of length n is also the binary expansion of an integer between 0 and 2n − 1. The 2n bit strings can be listed in order of their increasing size as integers in their binary expansions.
sablay-logo
Example: 10 0010 0111 precedes 10 0010 1000 updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
MORE Counting
Generating Permutations and Combinations
sablay-logo
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
MORE Counting
An algorithm for generating the
Generating Permutations and Combinations
r -combinations
of the set
1, 2, 3, . . . , n
An r -combination can be represented by a sequence containing the elements in the subset in increasing order.
sablay-logo
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
MORE Counting
An algorithm for generating the
Generating Permutations and Combinations
r -combinations
of the set
1, 2, 3, . . . , n
An r -combination can be represented by a sequence containing the elements in the subset in increasing order. The r -combinations can be listed using lexicographic order on these sequences.
sablay-logo
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
MORE Counting
An algorithm for generating the
Generating Permutations and Combinations
r -combinations
of the set
1, 2, 3, . . . , n
An r -combination can be represented by a sequence containing the elements in the subset in increasing order. The r -combinations can be listed using lexicographic order on these sequences. The next combinations after a a 路 路 路 ar can be obtained in the following way: 1
2
sablay-logo
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
MORE Counting
Generating Permutations and Combinations
sablay-logo
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30
MORE Counting
sablay-logo
Questions? See you next meeting!
updilseal
dcs-logo
acl-logo Discrete Mathematics for Computer Science
CS 30