1 Inclusion-Exclusion Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.
1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.
-
date post
19-Dec-2015 -
Category
Documents
-
view
215 -
download
1
Transcript of 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.
1
Basic Counting
Supplementary Notes
Prepared by Raymond WongPresented by Raymond Wong
2
e.g.1 (Page 2) List all possible ways of choosing 2
balls from 4 balls
3
e.g.1 List all possible ways of choosing 2
balls from 4 balls
4
e.g.1 List all possible ways of choosing 2
balls from 4 balls
5
e.g.1 List all possible ways of choosing 2
balls from 4 balls
6
e.g.1 List all possible ways of choosing 2
balls from 4 balls
There are totally 6 possible ways of choosing 2 balls from 4 balls.
7
e.g.2 (Page 6) List all possible ways of choosing 2
balls from 4 balls when the order of choosing balls is considered
8
e.g.2 List all possible ways of choosing 2
balls from 4 balls when the order of choosing balls is considered
There are totally 12 possible ways of choosing 2 balls from 4 balls when the orderof choosing balls is considered.
9
e.g.3 (Page 10)
How many times is the comparison
“A[i] > A[j]” made in line 3 of an algorithm (called selection-sort)?
(1) for i = 1 to n – 1 (2) for j = i + 1 to n(3) if (A[i] > A[j])(4) exchange A[i] and A[j]
10
e.g.3
(1) for i = 1 to n – 1 (2) for j = i + 1 to n(3) if (A[i] > A[j])(4) exchange A[i] and A[j]
n=5
4
5
11
e.g.3
(1) for i = 1 to n – 1 (2) for j = i + 1 to n(3) if (A[i] > A[j])(4) exchange A[i] and A[j]
n=5
4
5
i = 1
2
j = 2 j = 3 j = 4 j = 5 4 comparisons
12
e.g.3
(1) for i = 1 to n – 1 (2) for j = i + 1 to n(3) if (A[i] > A[j])(4) exchange A[i] and A[j]
n=5
4
5
i = 1
3
j = 2 j = 3 j = 4 j = 5 4 comparisons
i = 2
j = 3 j = 4 j = 5 3 comparisons
13
e.g.3
(1) for i = 1 to n – 1 (2) for j = i + 1 to n(3) if (A[i] > A[j])(4) exchange A[i] and A[j]
n=5
4
5
i = 1
4
j = 2 j = 3 j = 4 j = 5 4 comparisons
i = 2
j = 3 j = 4 j = 5 3 comparisons
i = 3
j = 4 j = 5 2 comparisons
14
e.g.3
(1) for i = 1 to n – 1 (2) for j = i + 1 to n(3) if (A[i] > A[j])(4) exchange A[i] and A[j]
n=5
4
5
i = 1
5
j = 2 j = 3 j = 4 j = 5 4 comparisons
i = 2
j = 3 j = 4 j = 5 3 comparisons
i = 3
j = 4 j = 5 2 comparisons
i = 4
j = 5 1 comparisons
Total number of comparisons = 4 + 3 + 2 + 1 = 10
15
e.g.3
(1) for i = 1 to n – 1 (2) for j = i + 1 to n(3) if (A[i] > A[j])(4) exchange A[i] and A[j]
n=5
4
5
i = 1
j = 2 j = 3 j = 4 j = 5 4 comparisons
i = 2
j = 3 j = 4 j = 5 3 comparisons
i = 3
j = 4 j = 5 2 comparisons
i = 4
j = 5 1 comparisons
Total number of comparisons = 4 + 3 + 2 + 1 = 10
A set of all comparisons
A smaller set
A smaller set
A smaller set
A smaller set These smaller sets
are non-overlapping (or mutually disjoint).
16
e.g.4 (Page 16)
E.g., The following 3 sets are disjoint.
17
e.g.5 (Page 16)
E.g., The following 2 sets are non-disjoint.
18
e.g.6 (Page 16)
Which sets are disjoint?
b
ca
e
fd
S1
S2 S3
19
e.g.6 (Page 10)
Are the following sets disjoint?A set of men
John
Peter
Raymond
Mary
Emily
Ada
A set of women
OR
John
Peter
Mary
Emily
AdaRaymond
20
e.g.6 (Page 10)
Are the following sets disjoint?A set of students surnamed Wong A set of students surnamed Chan
John Wong
Peter Wong
Raymond Wong
Mary Chan
Emily Chan
Ada Chan OR
John Wong
Peter Wong
Mary Chan
Emily Chan
Ada ChanRaymond Wong/Chan
21
e.g.7 (Page 17)
E.g., The following 4 sets are mutually disjoint sets.
S1S2 S3
S4
a
b
c
d
f
ge
22
e.g.8 (Page 17)
E.g., The following 4 sets are not mutually disjoint sets.
S1 S2S3
S4
a
bc
d
ef
g
h
23
e.g.9 (Page 19)
S1
a
bc
S2
d
ef
S3
a
gf
h
S4
g
h
S1 U S2 =
S1
a
bc
S2
d
ef
=
S1 U S2
a
bc
d
ef
24
e.g.10 (Page 19)
S1
a
bc
S2
d
ef
S3
a
gf
h
S4
g
h
S1 U S3 = =
S1
bc
S3
a
g
fh
bc
S1 U S3
a
g
fh
25
e.g.11 (Page 19)
S1
a
bc
S2
d
ef
S3
a
gf
h
S4
g
h
S1 U S2 U S3 = =
deS1
bc
S3a
f
gh
S2
bc
S1 U S2 U S3
a
f
gh
e d
26
e.g.12 (Page 19)
S1
a
bc
S2
d
ef
S3
a
gf
h
S4
g
h
S1 U S2 U S4 = =
S1
a
bc
S2
d
ef
S4
g
h
a
bc
S1 U S2 U S4
d
ef
g
h
27
e.g.13 (Page 20)
S1
a
bc
S2
d
ef
S3
a
gf
h
S4
g
h
S1 U S2 U S3 =
deS1
bc
S3a
f
gh
S2
Are S1, S2, S3 a partition of S?
bc
S
a
f
gh
e d
No
3 3 3
8
3 + 3 + 3 8
28
e.g.14 (Page 20)
S1
a
bc
S2
d
ef
S3
a
gf
h
S4
g
h
S1 U S2 U S4 =
S1
a
bc
S2
d
ef
S4
g
h
bc
S
a
f
gh
e d
Are S1, S2, S4 a partition of S? Yes
3 32
8
3 + 3 + 2 = 8
29
e.g.15 (Page 29)
How many times is the multiplications “A[i,k]*B[k,j]” made in line 5 of an algorithm?(1) for i = 1 to r
(2) for j = 1 to m(3) S = 0(4) for k = 1 to n(5) S = S + A[i, k]*B[k, j](6) C[i, j] = S
30
e.g.15
(1) for i = 1 to r (2) for j = 1 to m(3) S = 0(4) for k = 1 to n(5) S = S + A[i, k]*B[k, j](6) C[i, j] = S
r = 2
2
m = 3
n = 4
3
4
31
e.g.15
(1) for i = 1 to r (2) for j = 1 to m(3) S = 0(4) for k = 1 to n(5) S = S + A[i, k]*B[k, j](6) C[i, j] = S
r = 2
2
m = 3
n = 4
3
4
i = 1
j = 1 k = 1
k = 2
k = 3
k = 4
j = 2 k = 1
k = 2
k = 3
k = 4
j = 3 k = 1
k = 2
k = 3
k = 4
i = 2
j = 1 k = 1
k = 2
k = 3
k = 4
j = 2 k = 1
k = 2
k = 3
k = 4
j = 3 k = 1
k = 2
k = 3
k = 4
4 multiplications4 multiplications4 multiplications4 multiplications4 multiplications4 multiplications
32
e.g.15
(1) for i = 1 to r (2) for j = 1 to m(3) S = 0(4) for k = 1 to n(5) S = S + A[i, k]*B[k, j](6) C[i, j] = S
r = 2
2
m = 3
n = 4
3
4
i = 1
j = 1 k = 1
k = 2
k = 3
k = 4
j = 2 k = 1
k = 2
k = 3
k = 4
j = 3 k = 1
k = 2
k = 3
k = 4
i = 2
j = 1 k = 1
k = 2
k = 3
k = 4
j = 2 k = 1
k = 2
k = 3
k = 4
j = 3 k = 1
k = 2
k = 3
k = 4
4 multiplications4 multiplications4 multiplications4 multiplications4 multiplications4 multiplications
Total number of multiplications = 4*3*2 = 24
33
e.g.15
(1) for i = 1 to r (2) for j = 1 to m(3) S = 0(4) for k = 1 to n(5) S = S + A[i, k]*B[k, j](6) C[i, j] = S
r = 2
2
m = 3
n = 4
3
4
i = 1
j = 1 k = 1
k = 2
k = 3
k = 4
j = 2 k = 1
k = 2
k = 3
k = 4
j = 3 k = 1
k = 2
k = 3
k = 4
i = 2
j = 1 k = 1
k = 2
k = 3
k = 4
j = 2 k = 1
k = 2
k = 3
k = 4
j = 3 k = 1
k = 2
k = 3
k = 4
4 multiplications4 multiplications4 multiplications4 multiplications4 multiplications4 multiplications
Total number of multiplications = 4*3*2 = 24
A set of all multiplications
A smaller set
Product Principle: The size of the union of 6 disjoint sets, each of size 4, is equal to 4*6.
34
e.g.16 (P.35)
(1) for i = 1 to n – 1 (2) for j = i + 1 to n(3) if (A[i] > A[j])(4) exchange A[i] and A[j]
n=5
4
5
35
e.g.16
(1) for i = 1 to n – 1 (2) for j = i + 1 to n(3) if (A[i] > A[j])(4) exchange A[i] and A[j]
n=5
4
5
i = 1
2
j = 2 j = 3 j = 4 j = 5
A[1] > A[2]
A[1] > A[3]
A[1] > A[4]
A[1] > A[5]
36
e.g.16
(1) for i = 1 to n – 1 (2) for j = i + 1 to n(3) if (A[i] > A[j])(4) exchange A[i] and A[j]
n=5
4
5
i = 1
3
j = 2 j = 3 j = 4 j = 5
i = 2
j = 3 j = 4 j = 5
A[1] > A[2]
A[1] > A[3]
A[1] > A[4]
A[1] > A[5]
A[2] > A[3] A[2] > A[4] A[2] > A[5]
37
e.g.16
(1) for i = 1 to n – 1 (2) for j = i + 1 to n(3) if (A[i] > A[j])(4) exchange A[i] and A[j]
n=5
4
5
i = 1
4
j = 2 j = 3 j = 4 j = 5
i = 2
j = 3 j = 4 j = 5
i = 3
j = 4 j = 5
A[3] > A[4] A[3] > A[5]
A[1] > A[2]
A[1] > A[3]
A[1] > A[4]
A[1] > A[5]
38
e.g.16
(1) for i = 1 to n – 1 (2) for j = i + 1 to n(3) if (A[i] > A[j])(4) exchange A[i] and A[j]
n=5
4
5
i = 1
5
j = 2 j = 3 j = 4 j = 5
i = 2
j = 3 j = 4 j = 5
i = 3
j = 4 j = 5
i = 4
j = 5
There is only ONE comparison between A[1] and A[2].
A[4] > A[5]
A[1] > A[2]
A[1] > A[3]
A[1] > A[4]
A[1] > A[5]
There is only ONE comparison between A[i] and A[j] for each i and each j
Note that there are 5 variables A[i]’s.
39
e.g.16
(1) for i = 1 to n – 1 (2) for j = i + 1 to n(3) if (A[i] > A[j])(4) exchange A[i] and A[j]
n=5
4
5
i = 1
5
j = 2 j = 3 j = 4 j = 5
i = 2
j = 3 j = 4 j = 5
i = 3
j = 4 j = 5
i = 4
j = 5
There is only ONE comparison between A[1] and A[2].
A[4] > A[5]
A[1] > A[2]
A[1] > A[3]
A[1] > A[4]
A[1] > A[5]
There is only ONE comparison between A[i] and A[j] for each i and each jany two balls.
The total number of comparisonsis equal to the total number ofways of choosing 2 balls out of 5 balls.
Note that there are 5 variables A[i]’s.
balls.
40
e.g.17 (Page 35) List all possible ways of choosing 2
balls from 4 balls
There are totally 6 possible ways of choosing 2 balls from 4 balls
How can we obtain this number using some formula?
41
e.g.17 List all possible ways of choosing 2
balls from 4 balls when the order of choosing balls is considered
There are totally 12 possible ways of choosing 2 balls from 4 balls when the orderof choosing balls is considered.
How can we obtain this number using some formula?
42
e.g.17 List all possible ways of choosing 2
balls from 4 balls when the order of choosing balls is considered
There are totally 12 possible ways of choosing 2 balls from 4 balls when the orderof choosing balls is considered.
How can we obtain this number using some formula?
43
e.g.17 List all possible ways of choosing 2
balls from 4 balls when the order of choosing balls is considered
There are totally 12 possible ways of choosing 2 balls from 4 balls when the orderof choosing balls is considered.
How can we obtain this number using some formula?
44
e.g.17 List all possible ways of choosing 2
balls from 4 balls when the order of choosing balls is considered
There are totally 12 possible ways of choosing 2 balls from 4 balls when the orderof choosing balls is considered.
How can we obtain this number using some formula?
45
e.g.17 List all possible ways of choosing 2
balls from 4 balls when the order of choosing balls is considered
There are totally 12 possible ways of choosing 2 balls from 4 balls when the orderof choosing balls is considered.
How can we obtain this number using some formula?
46
e.g.17 List all possible ways of choosing 2
balls from 4 balls when the order of choosing balls is considered
There are totally 12 possible ways of choosing 2 balls from 4 balls when the orderof choosing balls is considered.
There are 4 x 3 ways of choosing 2 balls from 4 ballswhen the order of choosing balls is considered.
How can we obtain this number using some formula?
47
e.g.18 (Page 35) List all possible ways of choosing 2
balls from 4 balls
There are totally 6 possible ways of choosing 2 balls from 4 balls
This number can be derived from 12 (derived from the previous example).
48
e.g.18 List all possible ways of choosing 2
balls from 4 balls when the order of choosing balls is considered
There are totally 12 possible ways of choosing 2 balls from 4 balls when the orderof choosing balls is considered.
There are 4 x 3 ways of choosing 2 balls from 4 ballswhen the order of choosing balls is considered.
49
e.g.18 List all possible ways of choosing 2
balls from 4 balls
There are totally 6 possible ways of choosing 2 balls from 4 balls
There are (4 x 3)/2 ways of choosing 2 balls from 4 balls
4-element set
2-element set
2-element set
2-element subset 2-element subset
This number can be derived from 12 (derived from the previous example).