0% found this document useful (0 votes)
5 views

CS 201 Lecture 19 - Counting

Uploaded by

aljubehjihad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

CS 201 Lecture 19 - Counting

Uploaded by

aljubehjihad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Dr.

Kholoud Nairoukh
Department of Computer Science
German-Jordanian University
Lecture 19
• The study of the number of ways to put
things together into various combinations.

Example 1: In a contest entered by 100 people,


how many different top-10 outcomes could
occur?

Example 2: If a password is 6-8 letters and/or


digits, how many passwords can there be?

Dr. Kholoud Nairoukh


• 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,
– 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.
Dr. Kholoud Nairoukh
• 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B,


and |AB|=|A|+|B|
– The ways to do both task 1 and 2 can be
represented as AB, and |AB|=|A|·|B|

Dr. Kholoud Nairoukh


Dr. Kholoud Nairoukh
Dr. Kholoud Nairoukh
• Some facts about the Internet Protocol, version 4:
– Valid computer addresses are in one of 3 types:
• A class A IP address contains a 7-bit “netid”, and a 24-
bit “hostid”
• A class B IP address has a 14-bit netid and a 16-bit
hostid.
• A class C IP address has 21-bit netid and an 8-bit
hostid.
– The 3 classes have distinct headers (0, 10, 110)
– netids that are all 1s are not allowed.
– Hostids that are all 0s or all 1s are not allowed.
• How many valid computer addresses are there?

Dr. Kholoud Nairoukh


• (# addresses) = (# 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)

Dr. Kholoud Nairoukh


• This principle is applied when we are counting more
than one set or event. Then if the two sets A and B
(events) are disjoint, then |AUB| = |A|+|B|.

• Examples:
– There are 10 red chairs and 12 blue chairs. How
many ways can you pick a chair to sit in?
– The are three front doors and 2 in the back. How
many ways can you enter the house?
– You have a choice of rolling a die or flipping a
coin. How many outcomes are possible?
You must remember before applying this rule to
ensure that the sets are disjoint
Dr. Kholoud Nairoukh
• This rule deals with counting a series of events and
finding all possible outcomes.
• It states that if you have a series of events the
total possible outcomes of that series is the
product (multiplication) of the number of outcomes
in each event.
• Examples
– If you flip a coin three times, how many possible
outcomes of heads and tails do you have?
– If you roll a pair of dice, how many possible
outcomes?
– If you flip a coin and then roll a die, how many
outcomes of the combined event?
Dr. Kholoud Nairoukh
• There are m ways of doing task 1.
• There are n ways of doing task 2.
• Suppose that km of the ways of doing task
1 also simultaneously accomplish 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
|AB|=|A|+|B|−|AB|.
– If they are disjoint, this simplifies to
|A|+|B|.

Dr. Kholoud Nairoukh


• Some hypothetical rules for passwords:

– Passwords must be 2 characters long.


– Each character must be a letter a-z, a digit
0-9, or one of the 10 punctuation
characters !@#$%^&*().
– Each password must contain at least 1 digit
or punctuation character.

Dr. Kholoud Nairoukh


• A legal password has a digit or punctuation
character in position 1 or position 2.
– These cases overlap, so the principle applies.
• (# of passwords symbol in position #1) =
(10+10)·(10+10+26) = (20)(46) = 920
• (# of passwords symbol in position #2) =
(10+10)·(10+10+26) = (20)(46) = 920
• (# of passwords symbol in both places) =
(10+10)·(10+10) = (20)(20) = 400
(Each password must contain at least 1 digit or
punctuation character)
• Answer: 920+920−400 = 1,440

Dr. Kholoud Nairoukh


Dr. Kholoud Nairoukh
Dr. Kholoud Nairoukh
Dr. Kholoud Nairoukh
• If # objects ≥k+1 are assigned to k places,
then at least 1 place must be assigned ≥2
objects.

• In terms of the assignment function:


If f:A→B and |A|≥|B|+1, then some element
of B has ≥2 preimages under f.
• That is: f is not one-to-one.

Dr. Kholoud Nairoukh


• There are 101 possible numeric grades (0-100)
rounded to the nearest integer. Also, there
are >101 students in this class.

• Therefore, there must be at least one


(rounded) grade that will be shared by at
least 2 students at the end of the semester.
– The function from students to rounded
grades is not a one-to-one function.

Dr. Kholoud Nairoukh


• If N objects are assigned to k places,
then at least one place must be
assigned at least N/k objects.
• Example: there are N=280 students in
this class. There are k=52 weeks in the
year.
– Therefore, there must be at least 1 week
during which at least 280/52= 5.38=6
students in the class have a birthday.

Dr. Kholoud Nairoukh


• Given: There are 280 students in the class.
– Without knowing anybody’s birthday,
what is the largest value of n for which
we can prove using the Generalized
Pigeonhole Principle that at least n
students must have been born in the
same month?

• Answer: 280/12 = 23.3 = 24


Dr. Kholoud Nairoukh

You might also like