Вы находитесь на странице: 1из 25

Discrete Mathematics: lecture 16

Barbara Morawska

March 18, 2019


Bit strings
Definitions
I A bit is a 1 or 0. (Usually represents truth or false – Boolean
values)
I Logical operators defined on bits are called bit operators.
OR : ∨, AND : ∧, XOR : Y
I A bit string is a sequence of zero or more bits.
I Bit operations extended to bit strings:
bitwise OR, bitwise AND, bitwise XOR on two strings of the
same length.
Bit strings
Example
Let 01 1011 0110 and 11 0001 1101 be two bit strings.

01 1011 0110
11 0001 1101
bitwise OR : 11 1011 1111
bitwise AND : 01 0001 0100
bitwise XOR : 10 1010 1011

Exercises: 36, 37, 38 after section 1.1 in the textbook.


Bit strings
Method of representing sets using bit strings.
I Assume you have to implement a finite set of elements
(universal set U).
I You can choose an order on the elements of the set (number
them)
I Now you can represent any subset of the universal set by a bit
string:
where i element of the bit string is 1 if the i’th element of U
is in the subset,
otherwise it is 0.
Bit strings
Example
I Let U = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
I The elements of U are ordered in the increasing order.
I The bit string that represents U is:
11 1111 1111
I The bit string that represents the odd integers in U:
10 1010 1010
I The bit string representing the even integers in U:
01 0101 0101
I The bit string that represents the integers in U that smaller or
equal to 5:
11 1110 0000
Bit strings
When we represent sets as bit strings, the set operations become
easy.
Example
Let U = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
Let A = {1, 2, 3, 4, 5}, represented by 11 1110 0000
Let B = {1, 3, 5, 7, 9}, represented by 10 1010 1010
I Complement of B is represented by: 01 0101 0101 (flipping
the 1’s and 0’s)
I Union of A and B is obtained by bitwise OR: 11 1110 1010
I Intersection is obtained by bitwise AND: 10 1010 0000
I Symmetric difference is obtained by bitwise XOR:
01 0100 1010

Exercises 50 – 56 after section 2.2.


Basic counting principles
The product rule
I Assume we have a procedure which can be broken to two
subprocedures.
I There are n1 ways to do the first one.
I There are n2 ways to do the second one.
I How many ways to execute the procedure?
I n1 × n2 ways.

Example
Assume we have 2 employees and 12 office rooms available for
them. How many ways there are to assign a room for each
employee.
I n1 – ways to assign a room to the first employee. (12 ways)
I n2 – ways to assign a room to the second one. (11 ways)
I There are 12 × 11 = 132 ways to assign these rooms.
Product rule
Example
Chairs in an auditorium are to be assigned a letter and a positive
integer less than 100. How many chairs can be labeled with a
different label in this way?
I Number of letters: 26
I Number of digits: 100
I The number of different labels: 26 × 100 = 2600

Example
Assume there are 32 computers, each has 24 ports. How many
different ports are there?
32 × 24 = 768 ports.
Product rule
General formulation
Suppose we have a procedure composed of n tasks:
T1 , T2 , . . . , Tn , each can be done in mi ways.
Then there are m1 × m2 × · · · × mn ways to carry out the
procedure.

Example
How many different bit strings of length 7 are there?
2 × 2 × 2 × 2 × 2 × 2 × 2 = 27 = 128

Example
Assume a licence plate for a car contains 3 letters followed by 3
digits. How many different licence plates are available?
___ ___

26 × 26 × 26 × 10 × 10 × 10 = 17 576 000
Product rule
Example
I How many functions are there from a set A of m elements to
a set of n elements?
For each element in A we have n choices. Hence:
n × n × . . . n = nm
I How many injections (one-to-one) functions are there from a
set A of m elements to a set of n elements?
I If m > n, there is no such injection.
I Let m ≤ n and A = {a1 , a2 , . . . , am }.
We have to count: n possibilities for a1 ,
n − 1 possibilities for a2 ,
...,
n − m + 1 possibilities for the last one.
Hence there are n × (n − 1) × · · · (n − m + 1) possible
injections.
Product rule
Example
I How many logical operators of n arguments can there be?
I o(p1 , p2 , . . . , pn )
I A truth-table of all possible values for p1 , . . . , pn has
n × n × · · · × n = 2n different rows.
I Hence the column for final values for o has 2n entries. How
many different columns can exist?
n
I 2 × 2 × · · · × 2 = 22 . Hence there are double-exponentially
many possible logical operators of n arguments.
1
I For example: n = 1, 22 = 4. We know negation: ¬, that
yields the opposite value to the value of its argument. But
there is an operator that yields
I 1 to any value of a propositional variable,
I the same value that the propositional variable has,
I 0 to any value of a propositional variable.
I For example: n = 2, 24 = 16 possible logical operators. We
know 5 of them: ∧, ∨, Y, →, ↔.
Product rule
Example
How many different telephone numbers may be there?
I Old plan

area code−office code−station code


NYX −NNX −XXXX

N ∈ {2, 3, . . . , 9}, Y ∈ {0, 1}, X ∈ {0, 1, . . . , 9}


I area codes: 8 × 2 × 10 = 160
I office codes: 8 × 8 × 10 = 640
I station codes: 10 × 10 × 10 × 10 = 10 000
Hence 160 × 640 × 10 000 = 1 024 000 000 different
telephone numbers.
Product rule
Example
How many different telephone numbers may be there?
I New plan

area code−office code−station code


NXX −NXX −XXXX

N ∈ {2, 3, . . . , 9}, X ∈ {0, 1, . . . , 9}


I area codes: 8 × 10 × 10 = 800
I office codes: 8 × 10 × 10 = 800
I station codes: 10 × 10 × 10 × 10 = 10 000
Hence 800 × 800 × 10 000 = 6 400 000 000 different
telephone numbers.
Product rule
Example
Assume the following pseudocode is a part of a procedure.

1 k←0
2 for i1 = 1 to n1 do
3 for i2 = 1 to n2 do
..
4 .
5 for im = 1 to nm do
6 k ←k +1
What is the value of k after the execution of the code?
I The innermost loop executes nm times . . .
I But it executes nm times for each execution of its outer loop.
And this loop executes nm−1 times, for each of the executions
of its outer loop and so on . . . .
I Hence k is incremented n1 × n2 × · · · × nm times.
Product rule
Example
I We can use product rule to count the number of subsets in a
finite set S.
I Order elements in S.
I Associate with each subset C of S a bit string string(C ):
ai ∈ C iff stringi (C ) = 1
I By the product rule we can count all possible bit strings:
2 × 2 × · · · × 2 = 2|S| possible strings, hence this is the
number of all subsets of S.
I How many elements are in the Cartesian product of m sets:
A1 × A2 × · · · × Am .
By the product rule the number of all m-tuples is:
|A1 | × |A2 | × · · · × |Am |.
Sum rule (for disjoint sets)
Definition
I If a task can be done in n1 ways and in n2 ways (both disjoint
sets of possibilities), then the task can be done in n1 + n2
ways.
I More general: If a task can be done in n1 , n2 , . . . , or nm ways,
then it can be done in n1 + n2 + · · · + nm ways.

Example
I There is 37 members of faculty in mathematics, and 83
students math majors. A representative has to be chosen. We
can choose in 37 + 83 = 120 ways.
I A student can choose a project from 3 disjoint lists of
projects: n1 = 23, n2 = 15, n3 = 19. Hence he is choosing
from 23 + 15 + 19 = 57 projects.
Sum rule (for disjoint sets)
Example
Assume the following pseudocode is a part of a procedure.

1 k←0
2 for i1 = 1 to n1 do
3 k←k +1
4 for i2 = 1 to n2 do
5 k←k +1
..
6 .
7 for im = 1 to nm do
8 k ←k +1
What is the value of k after the execution of the code?
It is n1 + n2 + · · · + nm .
Product and sum rule
Example
I Consider language BASIC
I Variables are strings of one or two characters (letters or
digits), but the first one must be a letter.
I Variable must not be one of 5 reserved two character strings.
I How many possible variables are there?
I How many variables on one character? V1
I How many variables of two characters? V2
I V = V1 + V2
I V1 = 26
I V2 = 26 × 36 − 5 = 931
I Hence 26 + 931 = 957 variables.
Product and sum rule
Example
Consider possible passwords, with the following restrictions:
I six to eight characters
I each is an uppercase letter or a digit
I must contain at least one digit
P = P6 + P7 + P8
I P6 : Find number of 6 letter passwords and then subtract
those without a digit.
366 − 266 = 1 867 866 560
I P7 : 367 − 267 = 70 332 353 920
I P8 : 368 − 268 = 2 612 282 842 880
I P = 2 684 483 063 360
Product and sum rule
Example
Consider possible internet addresses. Internet Protocol Version 4
(IPv4).
Address is a string of 32 bits containing of two parts:
I network identifier: netid
I host identifier: hostid
There are 3 forms of actual addresses:
A (largest networks): 0 + 7bitnetid + 24bithostid
B (medium size networks): 10 + 14bitnetid + 16bithostid
C (smallest networks): 110 + 21bitnetid + 8bithostid
D (reserved for multicasting): 1110 + 28bits
E (reserved for future use): 11110 + 27bits
Restrictions:
I The string 1111111 is not allowed as netid in A
I hostid of 1’s only and 0’s only are not allowed.
How many internet addresses are there?
By sum rule: X = XA + XB + XC
XA netids: 27 − 1 = 127 (because 1111111 is not allowed)
hostids: 224 − 2 = 16 777 214 (because 1’s only not allowed,
0’s only not allowed)
Together: 127 × 16 777 214 = 2 130 706 178
XB netids: 214 = 16 384
hostids: 216 − 2 = 65 534
Together: 16 384 × 65 534 = 1 073 709 056
XC netids: 221 = 2 094 152
hostids: 28 − 2 = 254
Together: 2 094 152 × 254 = 532 676 608
Hence there are possible X = XA + XB + XC =
2 130 706 178 + 1 073 709 056 + 532 676 608 = 3 737 091 842
internet addresses.
Pigeonhole principle
Idea:
Suppose there are 20 pigeons and only 19 pigeonholes. Then we
can be sure that there is at least one pigeonhole with at least 2
pigeons.
Theorem (Pigeonhole principle)
Let k be a positive integer. Let k + 1 or more objects be placed
into k boxes, then there is at least one box containing two or more
of the objects
Proof by contradiction.
I Assume that k + 1 or more objects are placed into k boxes.
I Assume for contradiction that all k boxed contain at most 1
object each.
I Then (from the above assumption) the total number of
objects is at most k.
I This is the contradiction with the assumption that there are
k + 1 objects or more.
Pigeonhole principle
Corollary
A function from a set of k + 1 or more elements to a set of k
elements is not injection (one-to-one).

Proof.
I Let f : A → B, |B| = k, |A| ≥ k + 1.
I See each b ∈ B as a box for elements of A:
{a ∈ A | f (a) = b}
I If f is injective, then each such box contains 0 or 1 element.
I Pigeonhole principle tells us that there is at least one box that
contains more than one element.
I Hence f cannot be injective.
Pigeonhole principle
Example
Among a group of 367 people, there must be at least 2 with the
same birthday,
because there are only 366 (leap year) possible birthdays.

Example
In any group of 27 English words, there must be at least 2 such
that they begin with the same letter.

Example
Assume students can score from 0 to 60 points at the exam. How
many students there must be to guarantee that at least two
students get the same score?
I There are 61 possible scores.
I Hence there must be at least 62 students to guarantee that at
least two of them will get the same score.
Pigeonhole principle
Example
I Show that for every integer n
I there is a multiple of n such that
I it is written only with 0’s and 1’s

Proof.
I Consider n + 1 integers of the form: 1, 11, 111, . . . , 11...1,
where the last one contains n + 1 1’s.
I If we divide any integer by n there are only n possible
remainders.
I By Pigeonhole Principle there must be at least two integers
n1 , n2 with the same remainder when divided by n.
I n1 = nq1 + r , n2 = nq2 + r .
I Hence n1 − n2 = nq1 − nq2 . Hence n1 − n2 is a multiple of n
and is written only with 0’s and 1’s

Вам также может понравиться