1. BITS Pilani, Hyderabad Campus
Session -6
Topic -1 : Combinatorics
Combinatorics is the study of finite or countable discrete
structures and
includes counting the structures of a given kind and size,
deciding when certain criteria can be met, and constructing
and
analyzing objects meeting the criteria, finding "largest",
"smallest", or "optimal" objects, and studying combinatorial
structures arising in an algebraic context, or applying
algebraic techniques to combinatorial problems.
2. Basic Counting Principles
• Sum Rule
• Product Rule
• THE PRODUCT RULE Suppose that a
procedure can be broken down into a
sequence of two tasks. If there are n1
ways to do the first task and for each of
these ways of doing the first task, there
are n2 ways to do the second task, then
there are n1n2 ways to do the procedure.
3. There are 32 microcomputers in a computer
center. Each microcomputer has 24 ports. How
many different ports to a microcomputer in the
center are there?
4. 4
Binomial Coefficients
• (a + b)4 = (a + b)(a + b)(a + b)(a + b)
= a4
4
0
+ a3b
4
1
+ a2b2
4
2
+ ab3
4
3
+ b4
4
4
Binomial Theorem: Let x and y be variables, and let n be any
nonnegative integer. Then
(x y)n
n
j
j 0
n
xn j
y j
5.
(x y)n
n
j
j 0
n
xn j
y j
• What is the coefficient of a8b9 in
the expansion of (3a +2b)17?
What is n? 17
What is j? 9
What is x? 3a
What is y? 2b
17
9
(3a)8
(2b)9
17
9
38
29
a8
b9
6. Binomial Coefficients
• (a + b)2 = a2 + 2ab + b2
• (a + b)3 = a3 + 3a2b + 3ab2 + b3
• (a + b)4 = a4 + 4a3b + 6a2b2 + 4ab3 + b4
What is coefficient
of a9b3 in (a + b)12?
A. 36
B. 220
C. 15
D. 6
E. No clue
Pascal’s triangle
A.: 220
7. j
j
n
n
0
j
n
y
x
j
n
y
x
)
(
Binomial Coefficients
• Sum each row of Pascal’s Triangle:
Powers of 2
n
j
j 0
n
2n Suppose you have a set of size
n. How many subsets does
it have?
2n
How many subsets of size 0
does it have? nC0
How many subsets of size 1
does it have? nC1
How many subsets of size 2
does it have? nC2
Add them up we have the result.
8. j
j
n
n
0
j
n
y
x
j
n
y
x
)
(
n
j
j 0
n
2n
Pick x=1 and y=1 !
n
j
j 0
n
1n j
1j
(1 1)n
n
j
j 0
n
2n
• Alternative (clever) proof? Look at binomial
theorem…
x and y are variables; can pick
any numbers… hmm…
9. There are 32 microcomputers in a computer
center. Each microcomputer has 24 ports. How
many different ports to a microcomputer in the
center are there?
Solution: The procedure of choosing a port
consists of two tasks, first picking a
microcomputer and then picking a port on this
microcomputer. Because there are 32 ways to
choose the microcomputer and 24 ways to
choose the port no matter which microcomputer
has been selected, the product rule shows that
there are 32 · 24 = 768 ports.
10. BITS Pilani, Hyderabad Campus
Session -6
Topic -1 : Combinatorics
The study of the number of ways to put things together into
various combinations.
E.g. If a password is 6-8 letters and/or digits, how many
passwords can there be?
Combinatorics is the study of finite or countable discrete structures
and includes counting the structures of a given kind and size,
deciding when certain criteria can be met, and constructing and
analyzing objects meeting the criteria, finding "largest", "smallest",
or "optimal" objects, and studying combinatorial structures arising
in an algebraic context, or applying algebraic techniques to
combinatorial problems.
11. BITS Pilani, Hyderabad Campus
Session -6
Topic -1 : Combinatorics
The study of the number of ways to put things together into
various combinations.
E.g. If a password is 6-8 letters and/or digits, how many
passwords can there be?
Combinatorics is the study of finite or countable discrete structures
and includes counting the structures of a given kind and size,
deciding when certain criteria can be met, and constructing and
analyzing objects meeting the criteria, finding "largest", "smallest",
or "optimal" objects, and studying combinatorial structures arising
in an algebraic context, or applying algebraic techniques to
combinatorial problems.
12. BITS Pilani, Hyderabad Campus
Sum and Product Rules
Let
m be the number of ways to do task 1 and
n the number of ways to do task 2,
with each number independent of how the other task is
done,
and also assume that no way to do task 1
simultaneously also accomplishes task 2.
Then, we have the following rules:
The sum rule: The task “do either task 1 or task 2,
but not both” can be done in m+n ways.
The product rule: The task “do both task 1 and task 2”
can be done in mn ways.
13. BITS Pilani, Hyderabad Campus
Set Theoretic Version
If
A is the set of ways to do task 1, and B the set of ways
to do task 2, and if A and B are disjoint, then:
The ways to do either task 1 or 2 are A U B, and
|AUB|=|A|+|B|
The ways to do both task 1 and 2 can be represented
as AXB, and |AXB|=|A|·|B|
14. BITS Pilani, Hyderabad Campus
IPAddress Example
The Internet Protocol, version 4 (IPv4)
– Valid computer addresses are in one of 3 types:
A class A IP address contains a 7-bit “netid” ≠ 17, and a
24-bit “hostid”
A class B address has a 14-bit netid and a 16-bit hostid.
A class C addr. Has 21-bit netid and an 8-bit hostid.
–The 3 classes have distinct headers (0, 10, 110)
–Hostids that are all 0s or all 1s are not allowed.
How many valid computer addresses are there?
e.g., xxx.edu is 128.227.74.58
15. BITS Pilani, Hyderabad Campus
IP address solution
(# addrs)
= (# class A) + (# class B) + (# class C)
(by sum rule)
# class A = (# valid netids)·(# valid hostids) (by
product rule)
(# valid class A netids) = 27 − 1 = 127.
(# valid class A hostids) = 224 − 2 =
16,777,214. Continuing in this fashion we find
the answer is: 3,737,091,842 (3.7 billion IP
addresses)
16. BITS Pilani, Hyderabad Campus
Inclusion-Exclusion Principle
• Let
• m be the number of ways to do task 1 and
• n be the number of ways to do task 2,
• Suppose that k<m of the ways of doing task 1 also
simultaneously accomplish task 2.
• – And thus are also ways of doing task 2.
• Then, the number of ways to accomplish “Do either task
1 or task 2” is m+n-k.
• Set theory: If A and B are not disjoint, then
|AUB|=|A|+|B|-|A∩B|.
• – If they are disjoint this simplifies to |A|+|B|.
17. BITS Pilani, Hyderabad Campus
Inclusion/Exclusion Example
Some hypothetical rules for passwords: Passwords must be
2 characters long. Each character must be
– a letter a-z (#=26),
– a digit 0-9 (#=10), or
– one of the punctuation characters (#=10)
!@#$%^&*().
Each password must contain at least 1 digit or punctuation
character.
18. BITS Pilani, Hyderabad Campus
Setup of Problem
A legal password has a digit or punctuation character
in position 1 or position 2.
These cases overlap, so the principle applies. (# of
passwords w. OK symbol in
position #1) = (10+10)·(10+10+26)
(# w. OK sym. in pos. #2): also 20·46 (# w. OK sym
both places): 20·20 Answer: 920+920−400 = 1,440
19. How many strings of eight English letters are there
a) that contain no vowels, if letters can be repeated?
b) that contain no vowels, if letters cannot be repeated?
c) that start with a vowel, if letters can be repeated?
d) that start with a vowel, if letters cannot be repeated?
e) that contain at least one vowel, if letters can be
repeated?
f ) that contain exactly one vowel, if letters can be
repeated?
g) that start with X and contain at least one vowel, if letters
can be repeated?
h) that start and end with X and contain at least one vowel,
if letters can be repeated?
20.
21. The name of a variable in the JAVA programming language is a string of
between 1 and 65,535 characters, inclusive, where each character can be an
uppercase or a lowercase letter, a dollar sign, an underscore, or a digit,
except that the first character must not be a digit. Determine the number of
different variable names in JAVA.
22. The name of a variable in the JAVA programming language is a string of
between 1 and 65,535 characters, inclusive, where each character can be an
uppercase or a lowercase letter, a dollar sign, an underscore, or a digit,
except that the first character must not be a digit. Determine the number of
different variable names in JAVA.