Download presentation

Presentation is loading. Please wait.

Published byJase Simonds Modified over 2 years ago

1
CS1022 Computer Programming & Principles Lecture 6.2 Combinatorics (2)

2
Plan of lecture Binomial expansion Rearrangement theorem Efficiency of algorithms 2 CS1022

3
Binomial expansion (1) Numbers C(n, k) arise naturally when we expand expressions of the form (a b) n algebraically For example: (a b) 3 (a b) (a b) (a b) aaa aab aba abb baa bab bba bbb a 3 3a 2 b 3ab 2 b 3 Terms are from multiplying variables from brackets – Three terms (abb, bab, bba) with one a and two bs – This is because there are C(3, 2) = 3 ways of selecting two bs from the three brackets 3 CS1022

4
Binomial expansion (2) Coefficients in simplified expansion are – C(3, 0) 1, C(3, 1) 3, C(3, 2) 3, C(3, 3) 3 To obtain values from C(n, k), we assume 0! = 1 – There is only one way to select nothing from a collection 4 CS1022

5
Binomial expansion (3) (a b) n expansion contains terms (a n – k b k ) – Multiplying a from (n – k) brackets and b from remaining k brackets (k takes values from 0 up to n) – Since there are C(n, k) ways of selecting k brackets, – There are precisely C(n, k) terms of the form (a n – k b k ), for k 0, 1,..., n – Therefore (a b) n C(n,0)a n C(n,1)a n – 1 b C(n,2)a n – 2 b 2... C(n, n)b n This formula is called binomial expansion – C(n, k), in this context, is called binomial coefficient 5 CS1022

6
Pascals triangle (1) Binomial coefficients arranged as Pascals triangle C(0, 0) C(1, 0) C(1, 1) C(2, 0) C(2, 1) C(2, 2) C(3, 0) C(3, 1) C(3, 2) C(3, 3) C(4, 0) C(4, 1) C(4, 2) C(4, 3) C(4, 4) C(5, 0) C(5, 1) C(5, 2) C(5, 3) C(5, 4) C(5, 5)......... C(n, 0) C(n, 1)... C(n, n – 1) C(n, n) – Entry in row n 1 correspond to coefficients (in order) in the binomial expansion of (a b) n 6 CS1022

7
Pascals triangle (2) If we calculate numerical values we obtain 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1... – Since C(n, 0) C(n, n) 1, the outer edges of Pascals triangle are indeed 1 – Vertical symmetry also holds as C(n, k) C(n, n – k) 7 CS1022

8
Rearrangement theorem (1) Consider the problem of finding rearrangements of letters in DEFENDER – There are 8 letters which can be arranged in 8! Ways – However, the 3 Es are indistinguishable – they can be permuted in 3! ways without changing the arrangement – Similarly, 2 Ds can be permuted without changing the arrangement – Therefore, the number of distinguishable arrangements is just 8 CS1022

9
Rearrangement theorem (2) In general, there are different arrangements of n objects of which – n 1 are of type 1 – n 2 are of type 2 – and so on, up to – n r of type r 9 CS1022

10
Rearrangement theorem (3) In how many ways can 15 students be divided into 3 tutorial groups with 5 students in each group? Solution: – There are 15 objects to be arranged into 3 groups of 5 – This can be done in different ways 10 CS1022

11
Efficiency of algorithms Issue: design & analysis of efficient algorithms – Two solutions: which one is better? – Is your algorithm the best possible? – What is better/best? – How can we compare algorithms? Measure time and space (memory) – Not size of algorithms (fewer lines not always good) 11 CS1022

12
Towers of Hanoi (1) A simple problem/game: the Towers of Hanoi 3 rods and n disks of different sizes Start: disks in ascending order on one rod Finish: disks on another rod 12 CS1022 Rules: Only one disk to be moved at a time Move: take upper disk from a rod & slide it onto another rod, on top of any other disks No disk may be placed on top of a smaller disk.

13
Towers of Hanoi (2) 13 CS1022 Animation of solution for 4 disks:

14
Towers of Hanoi (3) Many algorithms to solve this problem: 14 CS1022 Alternate between smallest and next-smallest disks For an even number of disks: make the legal move between pegs A and B make the legal move between pegs A and C make the legal move between pegs B and C repeat until complete For an odd number of disks: make the legal move between pegs A and C make the legal move between pegs A and B make the legal move between pegs B and C repeat until complete To move n discs from peg A to peg C: move n 1 discs from A to B move disc n from A to C move n 1 discs from B to C

15
Towers of Hanoi (4) How can we compare solutions? What about counting the number of moves? – The fewer moves, the better! Instead of using a stop-clock, we count how often – The most frequent instruction/move is performed OR – The most expensive instruction/move is performed We simplify our analysis, so we dont consider – Computer, operating system, or language we might use 15 CS1022

16
Towers of Hanoi (5) 64 disks require 2 64 moves Thats 18,446,744,073,709,551,616 moves – If we carry out one move per millisecond, we have 18,446,744,073,709,551,616 ms 18,446,744,073,709,551.616 sec 307,445,734,561,825.87 min 5,124,095,576,030.43 hrs 213,503,982,334.60 days 584,942,417.36 years 16 CS1022

17
Estimating time We dont measure time with a stop-clock! – Its an algorithm, so it wont run... – Implement it and then time it (not good either) – Some algorithms take too long (centuries!) We estimate the time the algorithm takes as a function of the number of values processed – Simplest way: count, for an input of size n, the number of elementary operations carried out Important: same considerations about time apply to space needed (memory) 17 CS1022

18
Searching in a dictionary (1) Search for word X in a dictionary with n words Simple solution: sequential search – Check if X is the 1 st word, if not check if its the 2 nd, etc. – Worst case: X is not in dictionary (n comparisons) Another solution: binary search – Check if X is the middle word (words are ordered) – If not, decide if X could be on the first or second half – Dictionary always half the size – Worst case: 1 + log 2 n comparisons 18 CS1022

19
n1 + log 2 n Searching in a dictionary (2) Performance (in terms of comparisons) 19 CS1022 log 2 2 k = k n1 + log 2 n 84 n 84 647 n1 + log 2 n 84 647 2 18 250,000 19

20
ABCDE n3n23n2 2n 2 + 4nn3n3 2n2n 11ms3ms6ms1ms2ms 1010ms300ms240ms1s1.024s 100100ms30s20.4s0.28h 4 10 17 centuries 10001000ms0.83h0.56h11.6 days10 176 centuries ABCDE n3n23n2 2n 2 + 4nn3n3 2n2n 11ms3ms6ms1ms2ms 1010ms300ms240ms1s1.024s 100100ms30s20.4s0.28h 4 10 17 centuries 10001000ms0.83h0.56h11.6 days10 176 centuries Comparing algorithms (1) 20 CS1022 Suppose we have a choice of algorithms A, B, C, D, E require n, 3n 2, 2n 2 + 4n, n 3, 2 n elementary operations (respectively) – Each operation takes 1 millisecond Find overall running time for n = 1, 10, 100 & 1000

21
Comparing algorithms (2) 21 CS1022 Table shows huge difference in times – Formulae with powers of n (polynomial functions) – Formulae with powering by n (exponential functions) When the same highest power of n is involved, running times are comparable – For instance, B and C in the previous table If f(n) and g(n) measure efficiency of 2 algorithms – They are called time-complexity functions f(n) is of order at most g(n), written as O(g(n)), – If there is a positive constant c, |f(n)| c|g(n)|, n N

22
Comparing algorithms (3) 22 CS1022 Suppose two functions f(n) and g(n), such that |f(n)| c 1 |g(n)| and |g(n)| c 2 |f(n)| one is of order at most the other They have same order of magnitude Their times are comparable – They perform, when n is big enough, similarly BC 3n23n2 2n 2 + 4n 13ms6ms 10300ms240ms 10030s20.4s 10000.83h0.56h

23
Hierarchy of functions (1) 23 CS1022 We can define a hierarchy of functions – Each has greater order of magnitude than predecessors Example of hierarchy: Left-to-right: greater order of magnitude – As n increases, the value of latter functions increases more rapidly 1log nn n 2... n k...2n2n constant logarithmic polynomial exponential

24
Hierarchy of functions (2) 24 CS1022 Growth of functions as a graph

25
Complexity of functions 25 CS1022 We compare algorithms based on the complexity of the function describing their performance We simplify a function to work out which curve (or which class in the hierarchy) bounds it Example: f(n) = 9n + 3n 6 + 7 log n – Constants do not affect magnitudes, so 9n is O(n) 3n 6 = O(n 6 ) 7 log n = O(log n) – Since n and log n occur earlier than n 6 in hierarchy, we say that 9n and 7 log n are in O(n 6 ) – Hence f(n) is O(n 6 ), as the fastest growing term is 3n 6 – The function increases no faster than function n 6

26
Further reading R. Haggarty. Discrete Mathematics for Computing. Pearson Education Ltd. 2002. (Chapter 6) Combinatorics @ Wikipedia Analysis of algorithms @ Wikipedia 100 solutions to the Tower of Hanoi problem 100 solutions to the Tower of Hanoi problem 26 CS1022

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google