Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong."— Presentation transcript:

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

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

3 3 e.g.1 List all possible ways of choosing 2 balls from 4 balls

4 4 e.g.1 List all possible ways of choosing 2 balls from 4 balls

5 5 e.g.1 List all possible ways of choosing 2 balls from 4 balls

6 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 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 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 order of choosing balls is considered.

9 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 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 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 = 2j = 3j = 4j = 54 comparisons

12 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 = 2j = 3j = 4j = 54 comparisons i = 2j = 3j = 4j = 5 3 comparisons

13 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 = 2j = 3j = 4j = 54 comparisons i = 2j = 3j = 4j = 5 3 comparisons i = 3j = 4j = 5 2 comparisons

14 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 = 2j = 3j = 4j = 54 comparisons i = 2j = 3j = 4j = 5 3 comparisons i = 3j = 4j = 5 2 comparisons i = 4j = 5 1 comparisons Total number of comparisons = 4 + 3 + 2 + 1 = 10

15 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 = 1j = 2j = 3j = 4j = 54 comparisons i = 2j = 3j = 4j = 5 3 comparisons i = 3j = 4j = 5 2 comparisons i = 4j = 5 1 comparisons Total number of comparisons = 4 + 3 + 2 + 1 = 10 A set of all comparisons A smaller set These smaller sets are non-overlapping (or mutually disjoint).

16 16 e.g.4 (Page 16) E.g., The following 3 sets are disjoint.

17 17 e.g.5 (Page 16) E.g., The following 2 sets are non-disjoint.

18 18 e.g.6 (Page 16) Which sets are disjoint? b c a e f d S1S1 S2S2 S3S3

19 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 Ada Raymond

20 20 e.g.6 (Page 10) Are the following sets disjoint? A set of students surnamed WongA 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 Chan Raymond Wong/Chan

21 21 e.g.7 (Page 17) E.g., The following 4 sets are mutually disjoint sets. S1S1 S2S2 S3S3 S4S4 a b c d f g e

22 22 e.g.8 (Page 17) E.g., The following 4 sets are not mutually disjoint sets. S1S1 S2S2 S3S3 S4S4 a b c d e f g h

23 23 e.g.9 (Page 19) S1S1 a b c S2S2 d e f S3S3 a g f h S4S4 g h S 1 U S 2 = S1S1 a b c S2S2 d e f = S 1 U S 2 a b c d e f

24 24 e.g.10 (Page 19) S1S1 a b c S2S2 d e f S3S3 a g f h S4S4 g h S 1 U S 3 = = S1S1 b c S3S3 a g f h b c S 1 U S 3 a g f h

25 25 e.g.11 (Page 19) S1S1 a b c S2S2 d e f S3S3 a g f h S4S4 g h S 1 U S 2 U S 3 = = de S1S1 b c S3S3 a f g h S2S2 b c S 1 U S 2 U S 3 a f g h ed

26 26 e.g.12 (Page 19) S1S1 a b c S2S2 d e f S3S3 a g f h S4S4 g h S 1 U S 2 U S 4 = = S1S1 a b c S2S2 d e f S4S4 g h a b c S 1 U S 2 U S 4 d e f g h

27 27 e.g.13 (Page 20) S1S1 a b c S2S2 d e f S3S3 a g f h S4S4 g h S 1 U S 2 U S 3 = de S1S1 b c S3S3 a f g h S2S2 Are S 1, S 2, S 3 a partition of S? b c S a f g h ed No 3 3 3 8 3 + 3 + 3  8

28 28 e.g.14 (Page 20) S1S1 a b c S2S2 d e f S3S3 a g f h S4S4 g h S 1 U S 2 U S 4 = S1S1 a b c S2S2 d e f S4S4 g h b c S a f g h ed Are S 1, S 2, S 4 a partition of S?Yes 3 3 2 8 3 + 3 + 2 = 8

29 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 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 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 = 1j = 1k = 1k = 2k = 3k = 4 j = 2k = 1k = 2k = 3k = 4 j = 3k = 1k = 2k = 3k = 4 i = 2 j = 1 k = 1k = 2k = 3k = 4 j = 2 k = 1k = 2k = 3k = 4 j = 3 k = 1k = 2k = 3k = 4 4 multiplications

32 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 = 1j = 1k = 1k = 2k = 3k = 4 j = 2k = 1k = 2k = 3k = 4 j = 3k = 1k = 2k = 3k = 4 i = 2 j = 1 k = 1k = 2k = 3k = 4 j = 2 k = 1k = 2k = 3k = 4 j = 3 k = 1k = 2k = 3k = 4 4 multiplications Total number of multiplications = 4*3*2 = 24

33 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 = 1j = 1k = 1k = 2k = 3k = 4 j = 2k = 1k = 2k = 3k = 4 j = 3k = 1k = 2k = 3k = 4 i = 2 j = 1 k = 1k = 2k = 3k = 4 j = 2 k = 1k = 2k = 3k = 4 j = 3 k = 1k = 2k = 3k = 4 4 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 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 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 = 2j = 3j = 4j = 5 A[1] > A[2] A[1] > A[3] A[1] > A[4] A[1] > A[5]

36 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 = 2j = 3j = 4j = 5 i = 2j = 3j = 4j = 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 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 = 2j = 3j = 4j = 5 i = 2j = 3j = 4j = 5 i = 3j = 4j = 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 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 = 2j = 3j = 4j = 5 i = 2j = 3j = 4j = 5 i = 3j = 4j = 5 i = 4j = 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 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 = 2j = 3j = 4j = 5 i = 2j = 3j = 4j = 5 i = 3j = 4j = 5 i = 4j = 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 comparisons is equal to the total number of ways of choosing 2 balls out of 5 balls. Note that there are 5 variables A[i]’s.balls.

40 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 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 order of choosing balls is considered. How can we obtain this number using some formula?

42 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 order of choosing balls is considered. How can we obtain this number using some formula?

43 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 order of choosing balls is considered. How can we obtain this number using some formula?

44 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 order of choosing balls is considered. How can we obtain this number using some formula?

45 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 order of choosing balls is considered. How can we obtain this number using some formula?

46 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 order of choosing balls is considered. There are 4 x 3 ways of choosing 2 balls from 4 balls when the order of choosing balls is considered. How can we obtain this number using some formula?

47 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 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 order of choosing balls is considered. There are 4 x 3 ways of choosing 2 balls from 4 balls when the order of choosing balls is considered.

49 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 subset This number can be derived from 12 (derived from the previous example).


Download ppt "1 Basic Counting Supplementary Notes Prepared by Raymond Wong Presented by Raymond Wong."

Similar presentations


Ads by Google