1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

49
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.

Page 1: 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

Page 2: 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

2

e.g.1 (Page 2) List all possible ways of choosing 2

balls from 4 balls

Page 3: 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

3

e.g.1 List all possible ways of choosing 2

balls from 4 balls

Page 4: 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

4

e.g.1 List all possible ways of choosing 2

balls from 4 balls

Page 5: 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

5

e.g.1 List all possible ways of choosing 2

balls from 4 balls

Page 6: 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

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.

Page 7: 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

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

Page 8: 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

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.

Page 9: 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

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]

Page 10: 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

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

Page 11: 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

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

Page 12: 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

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

Page 13: 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

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

Page 14: 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

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

Page 15: 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

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).

Page 16: 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

16

e.g.4 (Page 16)

E.g., The following 3 sets are disjoint.

Page 17: 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

17

e.g.5 (Page 16)

E.g., The following 2 sets are non-disjoint.

Page 18: 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

18

e.g.6 (Page 16)

Which sets are disjoint?

b

ca

e

fd

S1

S2 S3

Page 19: 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

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

Page 20: 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

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

Page 21: 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

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

Page 22: 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

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

Page 23: 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

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

Page 24: 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

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

Page 25: 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

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

Page 26: 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

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

Page 27: 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

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

Page 28: 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

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

Page 29: 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

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

Page 30: 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

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

Page 31: 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

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

Page 32: 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

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

Page 33: 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

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.

Page 34: 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

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

Page 35: 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

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]

Page 36: 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

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]

Page 37: 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

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]

Page 38: 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

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.

Page 39: 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

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.

Page 40: 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

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?

Page 41: 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

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?

Page 42: 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

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?

Page 43: 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

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?

Page 44: 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

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?

Page 45: 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

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?

Page 46: 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

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?

Page 47: 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

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).

Page 48: 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

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.

Page 49: 1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong.

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).