1_1_counting
1_1_counting
Contents Contents
• Combinatorics is the branch of mathematics devoted to calculate the 1. Basic counting principles
number of ways in which a specified process can be carried out.
Problems of this type often occur in computing. A typical problem is 2. Elementary combinatorial configuration
to determine how many times a particular sequence of steps in an
algorithm will be executed. You might need to know this in order to 3. The inclusion-exclusion principle
estimate how long the algorithm will take to run.
4. Recurrence relation
• For example: determine the shortest path for a signal to travel through
a communication network 5. Generating function
– Algorithm 1 (simplest): calculate the lengths of all the possible paths through the
network.
– Algorithm 2: just calculate the lengths of some selected paths
In order to determine how efficient these algorithms are, and to be
able to compare them with each other and with other algorithms, we
must first answer the question: ‘How many such paths are there
altogether?’ 7 8
1. Basic counting principles 1.1. The sum rule
1.1. The sum rule Example:
• 40 textbooks on Mathematics; 50 textbooks on English
1.2. The product rule • to select 1 book: 40+50 choices
• What about selecting 2 books?
problem decompose combine
Task 1
Independent tasks
Let us consider two tasks:
m1 is the number of ways to do task 1
m2 is the number of ways to do task 2
Tasks are independent of each other, i.e.,
Performing task 1 does not accomplish task 2 and vice versa. Task 2
Sum rule: the number of ways that “either task 1 or task 2 can be done, but not
both”, is m1 + m2.
9
Generalizes to multiple tasks ...
Things 1 2 3 … k
Generalized sum rule:
ways m1 m2 m3 … mk Tasks 1 2 3 … k
ways m1 m2 m3 … mk
select one of them: m1 +m2 +m3 +...+ mk ways
select one of them: m1 +m2 +m3 +...+ mk ways
1.1. The sum rule 1.1. The sum rule
Example: How many 6-character strings are there, each of which is Generalized sum rule: If we have tasks T1, T2, …, Tk that can be
taken from the set {A, B, C, D, E} containing at least 3 different done in m1, m2, …, mk ways, respectively, and any two of these
characters? For example, the string ABCCAC is the string that satisfies tasks can not be done at the same time, then there are m1 + m2 +
the problem condition, while the string ABABAB does not.
… + mk ways to do one of these tasks.
14
Object type 1 2 3 … k
Quality m1 m2 m3 … mk
The number of ways to choose one object from these k types of objects
17
= m1 +m2 +m3 +...+ mk 18
m1 * m2 * *mk
24
The product rule The product rule
• If each element ai of k- tuple (a1, a2, ..., ak) has mi number of In many counting problems, it is only after building the first element, we just know how
ways to select (i = 1, 2, ..., k), then the number of tuples could be to build the second element; after building the first two elements we only know how to
generated is the product of these m1m2 ... mk build the third element,… In that case, we use the general product rules:
Suppose we construct a k-tuple (a1, a2, ..., ak) by building each element in turnand
• a1 can be chosen by m1 way;
• After a1 is selected, a2 can be chosen by m2 way;
• ...
• After a1, a2, ..., ak-1 are chosen, ak can be chosen by mk ways;
Then the number of k-tuples generated is m1m2 ... mk
Example: 6 persons are competing for 4 prizes. How many different outcomes are
A B C
possible? 1st prize 2nd prize 3rd prize 4th prize
D E F
27 28
1.2. The product rule 1.2. The product rule
Example 4: How many strings of 4 decimal digits Example 5: Consider the following nested loop:
(a) do not contain the same digit twice? for (i=1;i<=9;i++)
We want to choose a digit, then another that is not the same, then for (j=1;j<=7;j++) {
another…
[ Statement 1;
• 1st digit: 10 possibilities (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
Statement 2; ]
• 2nd digit: 9 possibilities (all but not first digit)
• 3rd digit: 8 possibilities
}
• 4th digit: 7 possibilities How many times the statements in the inner loop will be executed?
Total = 10*9*8*7 = 5040 Solution: 9 × 7 = 63 times (based on the product rule)
• Combining the product rule and the sum rule. rule of sum rule of product
Example 2: How many integers from 1 through 999 do not have any repeated
• Thus we can solve more interesting and complex problems. digits?
Let A = integers from 1 to 999 not having repeated digits.
Partition A into 3 sets:
• A1=one-digit integers not having repeated digits;
• A2=two-digit integers not having repeated digits;
• A3=three-digit integers not having repeated digits.
|A| = |A1| + |A2| + |A3| (by the sum rule)
31 32
= 9 + 9×9 + 9×9×8 = 738 (by the product rule)
Count the number of ways to put things together into various combinations. Count the number of ways to put things together into various combinations.
Example 3: Passwords consist of character strings of 6 to 8 characters. Each Example 3: Passwords consist of character strings of 6 to 8 characters. Each
character is an upper case letter or a digit. character is an upper case letter or a digit.
26 (A-Z) 10 (0-9) 26 (A-Z) 10 (0-9)
Each password must contain at least one digit. How many passwords are Each password must contain at least one digit. How many passwords are
possible? constraint possible? constraint
– Let P – total number of possible passwords – Let P – total number of possible passwords
– Pi – total number of passwords of length i, i = 6,7,8 P = P6 + P7 + P8 = 366-266 + 367 - 267 + 368 – 268 =2,684,483,063,360
P = P6 + P7 + P8 (the sum rule)
Pi – computing it directly is tricky (hmm…) How?? Comment: If your computer can try 200 million passwords per second, then how
– “popular” counting trick: let’s calculate all of them, including those with no long can the password be determined, so the intruder could penetrate this computer
digits and then subtract the ones with no digits. system?
P6 = (# possibilities without constraint) – (# passwords with no digits) (2 684 483 063 360/200 000 000) / (60 * 60) hours
= 366 – 266 Nearly 4 hours!
P7 = 367 - 267
P8 = 368 – 268
33 34
P = P6 + P7 + P8 = 366-266 + 367 - 267 + 368 – 268 =2,684,483,063,360
Example 4: An address is a string of 32 bits – it begins with a network id Example 5: Consider a wedding picture of 6 people
(netid), followed by a host number (hostid), which identifies a computer as a – There are 10 people, including the bride and groom
member of a particular network. (a) How many possibilities are there if the bride must be in the picture?
• Main computer addresses are in one of 3 types: • Product rule: place the bride AND then place the rest of the party
– Class A (largest networks): address contains a 0 followed by 7-bit
• First place the bride:
“netid”, and a 24-bit “hostid”
She can be in one of 6 positions
– Class B (medium networks): address contains a 10 followed by a 14-bit
netid and a 16-bit hostid. • Next, place the other 5 people via the product rule:
– Class C (smallest networks): address contains a 110 has 21-bit netid and There are 9 people to choose for the second person, 8 for the
an 8-bit hostid. third, etc. Total = 9*8*7*6*5 = 15120
Netids all 1s are not allowed. Hostids that are all 0s or all 1s are not allowed.
Product rule yields 6 * 15120 = 90,720 possibilities
35
How many valid IP addresses are there?
Wedding picture Wedding picture
Example 5: Consider a wedding picture of 6 people Example 5: Consider a wedding picture of 6 people
– There are 10 people, including the bride and groom – There are 10 people, including the bride and groom
c) How many possibilities are there if only one the bride or the groom must be in the
b) How many possibilities are there if the bride and the groom must be picture?
in the picture? Sum rule: place only the bride
• Product rule: place the bride/groom AND then place the rest of the party • Product rule: place the bride AND then place the rest of the party
• First place the bride and groom: – First place the bride
She can be in one of 6 positions She can be in one of 6 positions
– Next, place the other 5 people via the product rule (We can’t choose the
He can be in one of 5 remaining positions groom!!!)
Total of 6*5 = 30 possibilities There are 8 people to choose for the second person, 7 for the third, etc.
• Next, place the other 4 people via the product rule: Total = 8*7*6*5*4 = 6720
There are 8 people to choose for the 3rd, 7 for the 4th, etc. Product rule yields 6 * 6720 = 40,320 possibilities
Total = 8*7*6*5 = 1680 OR place only the groom (hmm… quickly, how many?)
• Same possibilities as for bride: 40,320
Product rule yields 30 * 1680 = 50,400 possibilities
Sum rule yields 40,320 + 40,320 = 80,640 possibilities
The permutation that will open the lock is 942, we do care about the
order.
41 42
x1 x2 x3 xn
x1
1st
x2
2nd
x3
3rd
x2
1st
x1
2nd
x3
3rd
47 48
2.1.2. k-permutation 2.1.2. k-permutation
• A k-permutation of a set A of n objects x1, x2, ..,xn is an ordered Example 2: Suppose you “have” time to listen to 10 songs on your daily
arrangement of k distinct elements of A. jog around campus. There are 6 A tunes, 8 B tunes, and 3 C tunes
The number of k-permutations of a set A with n=|A| elements is to choose from.
P(n, k) = n(n−1)…(n−k+1) = n! / (n−k)!
Finally, suppose you still want 4 A, 4 B, and 2 C tunes, and the order
of the groups doesn’t matter, but you get dizzy and fall down if all the
Example 1: 6 persons are competing for 4 prizes. How many different
outcomes are possible? songs by any one group aren’t played together.
How many playlists are there?
1st prize 2nd prize 3rd prize 4th prize
D F C E B A
6!
P(6, 4) 6*5* 4*3
2!
P(n, k): k unique choices out of n objects, order matters
49 50
55 56
2. Elementary combinatorial configuration 2.1.4. Permutations of multisets
2.1. Permutation • A multiset M is a collection whose members need not be distinct.
Example: The collection
2.1.1. Permutation
M = (a, a, a, b, b, c, d, d, d, 1, 2, 2, 2, 3, 3, 3, 3)
2.1.2. k-permutation is a multiset; and sometimes it is convenient to write
2.1.3. Circulation permutation M = (3a, 2b, c, 3d, 1, 32, 43).
2.1.4. Permutation of multisets • A multiset M over a set S can be viewed as a function v : S → ℕ from
S to the set ℕ of nonnegative integers; each element x S is repeated
2.2. Combination t(x) times in M; we write M = (S; t).
• Let M be a multiset and |M| = n.
– A k-permutation of M is an ordered arrangement of k objects
selected from n objects of M.
– A n-permutation of M is called a permutation of M.
57 58
Proposition 1. Let M be a multiset of r different types where each type has Proposition 1. Let M be a multiset of r different types where each type has
infinitely elements. Then the number of k-permutations of M equals 𝑟 infinitely elements. Then the number of k-permutations of M equals 𝑟
Example 1. What is the number of binary numerals with at most 4 digits? 1st position 2nd position 3rd position …. kth position
Solution: The question is to find the number of 4-permutations of the
r choice r choice r choice
multiset (0, 1). Thus the answer is 24 = 16.
Example 2. What is the number of ternary numerals with at most 4 digits? The total = rk
• Solution: The question is to find the number of 4-permutations of the
59 60
multiset (0, 1, 2). Thus the answer is 34 = 81.
2.1.4. Permutation of multisets 2.1.4. Permutation of multisets
Proposition 2. Let M be a multiset of r different types with repetition numbers n1, Proposition 2. Let M be a multiset of r different types with
n2,…, nr respectively. Let n = n1+ n2 +… + nr . Then the number of permutations of M
equals repetition numbers n1, n2,…, nr respectively. Let n = n1+ n2
𝒏! +… + nr . Then the number of permutations of M equals
𝒏𝟏 ! 𝒏𝟐 ! … 𝒏𝒓 !
Proof.
𝒏!
• List the elements of M as
nr 𝒏𝟏 ! 𝒏 𝟐 ! … 𝒏 𝒓 !
• Let S be the set consisting of the elements
𝑎 ,𝑎 ,…,𝑎 , 𝑏 ,𝑏 ,…,𝑏 ,…,𝑑 ,𝑑 ,…,𝑑 ,
Example 3: The number of 0-1 words of length n with exactly
Let X be the set of all permutations of S, and let Y be the set of all permutations of M.
There is a map f : X → Y, sending each permutation of S to a permutation of M by
m ones and (n – m) zeros equals to
removing the subscripts of the elements. Note that for each permutation of M there
are n1!, n2!,…,nr! ways to put the subscripts of the first, the second, ..., and the rth type n! n
elements back, respectively. Cnm .
Thus there are n1!n2! … nr! elements of X sent to Y by f. Therefore m !( n m)! m
|X| n!
| Y | 61 62
n1 ! n2 !...nr ! n1 ! n2 !...nr !
65 66
Difference between permutation and combination Difference between permutation and combination
• A combination is a set while a permutation is a sequence. When dealing with any counting problem, we should ask
Let’s consider the difference between unordered and ordered selections: ourselves about the importance of order in the problem:
1. Two ordered selections are the same if • When order is relevant, we think in terms of permutations
– the elements chosen are the same; and arrangements and the rule of product.
– the elements chosen are in the same order.
• When order is not relevant, combinations could play a key
• Ordered selections correspond to k-permutations.
• The number of all k-permutations is P(n, k) = n! / (n−k)!
role in solving the problem.
2. Two unordered selections are the same if
– the elements chosen are the same (regardless of the order in which the
elements are chosen)
• Unordered selections correspond to k-combinations
• The number of all k-combinations is C(n, k) = n! / k! (n−k)!
71 72
Difference between permutation and combination 2.2. Combination
Example: A club has 25 members. 2.1.1. Definitions
a. How many ways are there to choose 4 members of the club 2.2.2. Binomial coefficients
to serve on an executive committee?
2.2.3. Combinations of Multisets
2.2.4. Multinomial coefficients
b. How many ways are there to choose a president, vice
president, secretary, and treasurer of the club?
73 74
Binomial Theorem: Let x and y be variables, and let n be any nonnegative integer.
Then What is the coefficient of a8b9 in the expansion of (3a +2b)17?
n n What is n? 17
(x y ) n
j x nj
y j
What is j? 9
17 8 9 17 8 9 8 9
(3a ) (2b) 3 2 a b
j 0 9
9
What is x? 3a
What is y? 2b
75 76
Binomial Coefficients Binomial Coefficients
n
Sum each row of Pascal’s Triangle: n 2 n
n
n
(x y)n x n j y j
j
What is coefficient
j 0 j
j 0
(a + b)2 = a2 + 2ab + b2 of a9b3 in (a + b)12? Powers of 2
Proof ???
(a + b)3 = a3 + 3a2b + 3ab2 + b3
(a + b)4 = a4 + 4a3b + 6a2b2 + 4ab3 + b4 Suppose you have a set of size
n. How many subsets does 2n
A. 36
Pascal’s triangle it have?
B. 220
C. 15
How many subsets of size 0 𝑛
D. 6
does it have? 0
E. No clue
How many subsets of size 1 𝑛
does it have?
1
j 0
2.2. Combination 2.2.3. Combinations of multisets
• Let M be a multiset {a1, a2, …, an} (M has n distinct objects):
2.1.1. Definitions – A k-combination of M is an unordered collection of k objects selected from n
2.2.2. Binomial coefficients types of objects of M.
– A k-combination of M is also called an k-combination with repetition allowed.
2.2.3. Combinations of Multisets – The number of k-combination of M is C(n+ k-1, k) = C(n+ k-1, n-1)
(the number of selections, with repetitions, of k objects from n distinct objects)
2.2.4. Multinomial coefficients
Need to divide k candies for n kids B1, B2, …,Bn. How many different ways to divide?
Let tj be the number of candies for kid Bj, j=1,…,n. At this point, the above problem
leads to the problem:
Let k and n be non-negative integers. How many non-negative integers in the following
equation have?
t1 t2 t3 tn k
81 t1 , t2 ,, tn Z 82
Convert the symbol "O" to zero 0 and the stick "|" to one 1, any such placement is 1 3 6 0 10
converted into a 0-1 sequence of length k+n–1 with exactly k zeros and n–1 ones
Answer 2: (bài toán chia kẹo)
Now the problem becomes counting the number of 0-1 words of length k+(n–1) with Select with repetition from x1, x2, x3, x4 10 times.
exactly k zeros and n–1 ones For example if x1 is selected twice then x1= 2 in the final solution.
Therefore: k-combination with repetition allowed (n = 4, k =10):
= C(n+ k-1, k) = C(n+ k-1, n-1) 83 84
C(4+10-1,10)
2.3.3. Combinations of multisets The number of k-combination of M is C(n+ k-1, k)
Example 2: How many nonnegative integer solutions are there to the inequality Equivalence of the following: The number C(n+k–1, k) equals
x1 x 2 x 6 10 ? • the number of selections, with repetition, of size k from a
collection of n types objects
It is equivalent to
• the number of nonnegative integer solutions of the equation
x1 x2 x6 x7 10, 0 xi , 1 i 6, 0 x7
x1 + x2 + … + xn = k
which can be transformed to
y1 y2 y6 y7 9, y i 0 integer • the number of ways to place k identical objects into n
distinct containers.
where y i x i for 1 i 6
• the number of non decreasing sequences of length k whose
and y7 x7 1
terms are taken from the set {1, 2, …, n}.
Ans1: 10 locations for bars, pick 6 locations allowing repetitions [k = 6, n = 10]
k-combination with repetition C(6+10-1,6)=C(15,6) =5005
87 88
2.2.4. Multinomial coefficients Summary: select or order k objects from n distinct objects
• The Multinomial Theorem: Order matters Order does not matter
1 𝑛! 2 𝑛!
Repetition 𝑃 𝑛, 𝑘 = 𝐶 𝑛, 𝑘 =
n k1 k2 km 𝑛−𝑘 ! 𝑘! 𝑛 − 𝑘 !
( x1 x2 ...xm ) n
not allowed
x1 x2 ...xm (k-permutation of n-set) (k-combination of n-set)
k ,
1 2 k ,..., k m
3 𝑛 4 𝑛+𝑘−1 !
Repetition 𝐶 𝑛 + 𝑘 − 1, 𝑘 =
allowed (k-permutation with repetition 𝑘! 𝑛 − 1 !
where the summation is over all sequences of non-negative integers allowed) (k-combination with repetition allowed)
(k1, k2, ..., km) such that k1 + k2 + ... + km = n. The set A = {S,O,H,N}
The 2-permutation (sequence) of set A is: P(4,2) = 12 The 2-combination (set) of set A is: C(4, 2) = 6
SO, SH, SN, OH, ON, OS, HN, HS, HO, NS, NO, NH {S,O},{S,H},{S,N},{O,H},{O,N},{H,N}
1 2
The multi set B = {S, O, H, N}
The 2-permutation with repetition allowed of set B is: 42 = 16 The 2-combination with repetition allowed of
SS, OO, HH, NN, set B is: C(4+2-1,2)=10
SO, SH, SN, OH, ON, OS, HN, HS, HO, NS, NO, NH
{S, S}, {O, O}, {H, H}, {N, N},
89 {S,O},{S,H},{S,N},{O,H},{O,N},{H,N}
3 4
91 92
3.1. Inclusion-exclusion principle 3.1. Inclusion-exclusion principle
The inclusion–exclusion principle is an equation relating the sizes of two sets More generally, for finite sets A1, A2,…,Am
and and their union. It states that if A and B are two (finite) sets, then
|A B| = |A| + |B| - |A B|
𝐴 ∪ 𝐴 ∪ ⋯ ∪ 𝐴 = 𝑁 − 𝑁 + ⋯ + (−1) 𝑁
where:
The meaning of the statement is that
the number of elements in the union Nk | Ai1 Ai2 ... Aik |, k 1, 2,..., m
1i1 i2 ... ik m
of the two sets is the sum of the
elements in each set, respectively,
Note: Nk is the sum of the cardinalities of all intersections of k
minus the number of elements that
are in both. from m given sets. For example:
𝐴 ∪𝐴 ∪𝐴 ∪𝐴 =?
N1 = |A1| + ... + |Am|
Nm = |A1 A2 ... Am|
Similarly, for three sets A, B and C: m
|A B C| = |A| + |B| + |C| - |A B| - |A C| - |B C| + |A B C| | A1 A2 ... Am | | Ai | | Ai A j | | Ai A j Ak | ...
i 1 pairs ( ij ) triples ( ijk )
This can be seen by counting how many times each region in the figure is 93 ( 1) m 1 | Ai A j Ak ... Am | 94
included in the right hand side.
99 100
3. Inclusion-exclusion principle 3.2. Derangement
3.1. Inclusion-exclusion principle A derangement of {1, 2, …, n} is a permutation on the set such that none
of elements i is placed at position ith in the permutation.
3.2. Derangement (In other words, derangement is a permutation that has no fixed points)
Example: n = 5
• Permutation (2,3,5,4,1) is not a derangement
• Permutation (2,3,5,1,4) is a derangement
101 102
• |Ai Aj Ak | = (n-3)! since if exactly three of the n objects are placed at their
original positions, that leaves the other (n-3) objects to be freely permuted in
(n-3)! possible ways. Hence,
109 110
(3) Move the n-1 disks (following the rules of the game) from tower (b) to tower (c), placing
them on top of the largest disk
Tower a Tower c
Toán rời rạc
Tower b Tower a Tower c
Toán rời rạc
Tower b
Tower of Hanoi Tower of Hanoi: n=5
• h1 = 1 (1) Move the top n - 1 disks (following the rules of the game) from tower (a) to tower (b)
• For n ≥ 2, we need to do 3 following steps to transfer all disks from tower (a) to tower (c):
The problem of n-1 disks #moves = hn-1
(1) Move the top n - 1 disks (following the rules of the game) from tower (a) to tower (b)
(2) Move the largest disk to the tower (c)
The problem of n-1 disks #moves = hn-1 #moves = 1
(2) Move the largest disk to the tower (c) (3) Move the n-1 disks (following the rules of the game) from tower (b) to tower (c) , placing
#moves = 1 them on top of the largest disk
(3) Move the n-1 disks (following the rules of the game) from tower (b) to tower (c), placing The problem of n-1 disks #moves = hn-1
them on top of the largest disk
The problem of n-1 disks #moves = hn-1
hn = 2hn-1 + 1, n ≥ 2
h1 = 1
Tower a Tower c
Toán rời rạc
Tower b Tower a Tower c
Toán rời rạc
Tower b
Factorial(n); Factorial(n);
How many times is function Factorial called when we execute the statement Factorial(n); ? T(n) = T(n − 1) + 1, n >1
• When n = 1, Factorial is called 1 T(1) = 1 = [T(n-2) +1] + 1 [has 2 ones]
• Otherwise: =[[T(n-3)+1]+1]+1 [has 3 ones]
– We perform 1. T(n) = 1 + T(n-1) =….
– Plus the number of calls in the recursive call in Factorial(n − 1) T(n-1) = T(n – (n-1)) + (n-1) [has n-1 ones]
This can be expressed as a formula (similar to the definition of n!): = T(1) + (n-1)
T(1) = 1 Find the formula to calculate T(n) for = 1 + (n-1)
T(n) = 1 + T(n − 1), n >1 any natural number n and can avoid the =n
This is known as a recurrence relation. iteration. explicit formula
4.1. Recurrence relations 4.1. Recurrence relations
Example 6. Performance of Recursive Binary Search
Input: An array A consists of n elements: A[0],…,A[n-1] in ascending
order; Value key with the same data type as array A.
Output: the index in array if key is found, -1 if key is not found
To solve this problem, the divide and conquer technique is built based on the following
argument: The given key is either
How many multiplications M(n) does Factorial perform? equal to the element at the middle of array A
• When n = 1 we don’t perform any M(1) = 0 or equal to the element at the left (L) half of array A
or equal to the element at the right (R) half of array A.
• Otherwise:
or not in array
– We perform 1.
– Plus the number of multiplications we perform in the recursive call, Factorial(n − 1) M(n-1)
This can be expressed as a formula (similar to the definition of n!):
M(0) = 0; M(1) = 0 Find the formula to calculate T(n) for
M(n) = 1 + M(n − 1), n >1 any natural number n and can avoid the 130
This is known as a recurrence relation. iteration.
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
lo mid hi lo hi
binsearch(0, 14, A, 33); binsearch(0, 14, A, 33);
The section to be binsearch(0, 6, A, 33); binsearch(0, 6, A, 33);
investigated is halved
133 134
after each iteration
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
lo mid hi lo hi
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
lo mid hi lo
hi
binsearch(0, 14, A, 33); binsearch(0, 14, A, 33);
The section to be binsearch(0, 6, A, 33); binsearch(0, 6, A, 33);
investigated is halved binsearch(4, 6, A, 33); binsearch(4, 6, A, 33);
137 138
after each iteration binsearch(4, 4, A, 33); binsearch(4, 4, A, 33);
}
return binsearch(mid+1, high, A, key);
}
return binsearch(mid+1, high, A, key);
key=31??
else return -1; else return -1;
} }
6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
lo lo
hi hi
mid binsearch(0, 14, A, 33); mid binsearch(0, 14, A, 33);
binsearch(0, 6, A, 33); binsearch(0, 6, A, 33);
binsearch(4, 6, A, 33); binsearch(4, 6, A, 33);
139 140
binsearch(4, 4, A, 33); binsearch(4, 4, A, 33);
4.1. Recurrence relations 4.1. Recurrence relations
Example 6. Performance of Recursive Binary Search Example 6. Performance of Recursive Binary Search
int binsearch(int low, int high, int A[], int key)
Input: An array A consists of n elements: A[0],…,A[n-1] in ascending {
order; Value key with the same data type as array A. if (low <= high) T(0) = ?
{
binsearch(0, -1, A, key);
Output: the index in array if key is found, -1 if key is not found mid = (low + high) / 2;
int binsearch(int low, int high, int A[], int key)
if (A[mid]==key) return mid; T(1) = ?
else if (key < S[mid])
{ return binsearch(low, mid-1, A, key); binsearch(0, 0, A, key);
if (low <= high) else
{ return binsearch(mid+1, high, A, key);
binsearch(0, -1, A, key);
mid = (low + high) / 2; } binsearch(1, 0, A, key);
if (A[mid]==key) return mid; else return -1;
else if (key < S[mid]) }
return binsearch(low, mid-1, A, key); binsearch(0, n-1, A, key);
else
return binsearch(mid+1, high, A, key);
Let T(n): the number of times that binsearch is called in the worst case when array A has n elements
} • T(0) = 1 Recurrence relation:
else return -1;
} • T(1) = 2 T(n) = T(n/2) + 1
binsearch(0, n-1, A, key); • T(2) = T(1) + 1 = 3 T(0) = 1
How many times is binsearch called in the worst case ? • T(4) = T(2) + 1 = 4 T(1) = 2
141
• T(8) = T(4) + 1 = 4 + 1 = 5 142
• T(n) = T(n/2) + 1
147 148
A linear homogeneous recurrence relation of degree k with
constant coefficients
Example 1
• Theorem 1. Let c1 and c2 be real numbers. Fibonacci sequence is given by the following recurrence relation:
Fn = Fn-1 + Fn-2, n 2,
Suppose that r2 - c1 r - c2 = 0 has 2 distinct roots r1 and r2. Then the F0 = 0, F1 = 1.
sequence {an} is a solution of the recurrence relation Find the explicit formula for Fn.
an = c1 an-1 + c2 an-2 Solution: Solve the characteristic equation:
r2 - r - 1 = 0,
if and only if its characteristic roots are:
an = 1(r1)n + 2(r2)n (1) r1
1 5
; r2
1 5
2 2
n = 0, 1, ..., where 1 and 2 are constants. Leonardo Fibonacci
1170-1250
Therefore
1
n n
1 5 1 5 , n 0.
Fn
5 2 2
151 152
Example 2 General case
Solving the following recurrence relation Theorem 3. Let c1, c2, ..., ck be real numbers. Assume the characteristic
an = 6 an-1 - 9 an-2
equation
with initial conditions a0 = 1 and a1 = 6.
Solution: rk - c1 rk-1 - c2 rk-2 - . . . - ck = 0
Characteristic equation: has k distinct roots r1, r2, ..., rk . Then a sequence {an} is a solution of
r2 - 6 r + 9 = 0 has one root r = 3. The solution is:
the recurrence relation:
an = 1 3n + 2 n 3n
To determine 1, 2 , using the initial conditions, we have: an = c1 an-1 + c2 an-2 +...+ ck an-k,
a0 = 1 = 1 , if and only if
a1 = 6 = 1 * 3 + 2 *1* 3
Solving these equations, we have 1 = 1 and 2 = 1.
an = 1 r1n + 2 r2n + . . . + k rkn
Therefore, the solution of the recurrence relation is: where n = 0, 1, 2,..., and 1, 2, ..., k are constants
an = 3n + n 3n
Recurrence relation: an = c1an−1 + … + ckan−k
Characteristic equation: rk − c1rk−1 − … − ck = 0
153 154
Example 3 Example 3
Solving the recurrence relation: Using the initial conditions, we have following equations to
an = 6 an-1 - 11 an-2 + 6 an-3 determine the value for constants 1, 2, 3:
where initial conditions a0 = 2 = 1 + 2 + 3
a0 = 2, a1 = 5, a2 = 15. a1 = 5 = 1 + 2.2 + 3.3
Solution: Characteristic equation
a2 = 15 = 1 + 2.4 + 3.9.
r3 - 6 r2 + 11 r - 6 = 0
Solving above equations, we have
has 3 distinct roots r1 = 1, r2 = 2, r3 = 3.
Therefore, the solution is
1 = 1, 2 = -1 and 3 = 2.
an = 1 1n + 2 2n + 3 3n for some constants 1, 2, 3 Therefore, the solution of the recurrence relation is
an = 1 - 2n + 2. 3n
Given linear homogeneous recurrence relation of degree k with constant • Linear nonhomogeneous recurrence relation with constant
coefficients k
an ci ani coefficients) has the term F(n) dependent on n (and
Its characteristic equation is: i 1 independent on any value of ai ) :
k
r k ci r k i 0 an = c1an−1 + … + ckan−k + F(n)
i 1
157
Solving Linear nonhomogeneous recurrence relation Solving Linear nonhomogeneous recurrence relation
The following result is the basis for solving the nonhomogeneous Then, we have the method for solving linear nonhomogeneous recurrence relation:
recurrence relation: 1) Find the solution h(n) of linear homogeneous part.
– If an = p(n) is a particular solution of linear nonhomogeneous 2) Find particular solution p(n) of linear nonhomogeneous recurrence relation.
recurrence relation 3) The solution of linear nonhomogeneous recurrence relation is of the form:
k
an ci ani F (n) an = h(n) + p(n)
i1 4) Determine the constants from equations obtained by initial conditions
To solve the recurrence relation, we could use the substitution method: Solving:
an = 2 an−1 + 1 an = 2an-1 + 1, n 1;
= 2 (2 an−2 + 1) + 1 = 22 an−2 + 2 + 1 a1 = 1.
= 22(2 an−3 + 1) + 2 + 1 = 23 an−3 + 22 + 2 + 1 Solution:
… (1) Linear homogeneous part: an = 2an-1
= 2n−1 a1 + 2n−2 + … + 2 + 1 Characteristic equation: r - 2 = 0 has one root r = 2
= 2n−1 + 2n−2 + … + 2 + 1 (as a1 = 1) Solution of linear homogeneous part is: h(n) = c12n
= 2n − 1 (2) Linear Nonhomogenenous part F(n) = 1, then the particular solution is
of the form
p(n) = C
Example 2 Example 3
Solving the recurrence relation Solving recurrence relation:
an=5an-1 - 6an-2+7n, n2, an = an-1 + n, n 1; a1 = 2
a0 = 0; a1 = 1 Solution:
Solution: (1) Linear homogeneous part: an = an-1
(3) The solution of linear nonhomogeneous recurrence relation is Characteristic equation r - 1 = 0 has the root r = 1
an = p(n) + h(n) = (49/20)7n + c13n + c22n The solution of linear homogeneous part is: h(n) = c11n
(2) Nonhomogeneous part F(n) = n1n, and 1 is the root of multiplicities 1,
(4) The constants c1, c2 are determined by using initial conditions:
thus the particular solution is of the form
a0 = c1 + c2 + 49/20 = 0
p(n) = n1(C2 + C3n)1n
a1 = 3c1 + 2c2 +(49/20).7 = 1
c1 = ?, c2 = ? Substitute to the recurrence relation, we have:
Hence, the solution of linear nonhomogeneous recurrence relation is n (C2 + C3n) = (n-1) [C2 + C3(n-1)] + n
an = … Thus, C2 = ½ and C3 = ½ . The particular solution is
p(n) = (n+1)n/2
Example 3 Example 4
Solving recurrence relation: Solving the recurrence relation: an = 6an-1 -9an-2 + F(n)
an = an-1 + n, n 1; a1 = 2 Characteristic equation: r2 – 6r + 9 = (r - 3)2=0
Characteristic root with multiplicities 2: r = 3
Consider some cases F(n) = G(n)sn where G(n) is polynomial of n:
• (1) Assume F(n) = n22n
(3) The solution of linear nonhomogeneous recurrence relation is – As: F(n) = G(n)sn = n22n
Degree of G(n) is t = 2, s = 2 is not the characteristic root
an = c1+ (n+1)n/2
– Thus, the particular solution is of the form:
(4) The constant c1 is determined by using initial condition: (ptnt + pt-1nt-1 + ... + p0)sn
a 1 = c1 + 1 = 2 =(p2n2 + p1n + p0)2n
• (2) Assume F(n) = n23n
c1 = 1 – As F(n) = G(n)sn = n23n
The solution of linear nonhomogeneous recurrence relation is Degree of G(n) is t = 2, s = 3 is characteristic root with multiplicities 2
– Thus, the particular solution is of the form:
an = 1+ (n+1)n/2, n 1
nm(ptnt + pt-1nt-1 + ... + p0)sn
=n2(p2n2 + p1n1 + p0)3n
1 f(n/)
T(n/) T(n/) T(n/)
2 T(n/2) 2f(n/2)
T(n/2) T(n/2) T(n/2)
. .
. .
i i f(n/i)
. .
log n
• The value of T(n) is the sum of the cost of all level on the tree:
2.5. Recurrence relations and generating functions 2.5.2. Generating functions
2.5.1. Recurrence relations Generating functions are a tool to solve a wide variety of counting
problems and recurrence relations.
2.5.2. Generating functions Example 1:
How many ways to give 12 oranges for three children: A, B and C such that:
A gets at least four, and B and C gets at least two, but C gets no more than five
177 178
i 0 F ( x) ak x k G ( x) bk x k
is called the generating function for the given sequence. k 0
k 0
Some useful generating function: F ( x) G ( x) ak bk x k Match all terms with equal powers in x.
k 0
Ex1: (1 xn+1) = (1 x)(1 + x + x2 + x3 + ...+ xn). k
So (1-xn+1)/(1-x) = 1 + x + x2 + x3 + ...+ xn, F ( x)G( x) a j bk j x k
k 0 j 0
(1-xn+1)/(1-x) is the generating function for the sequence:
1, 1, 1, …, 1, 0, 0, 0, … . (where the first n+1 terms are 1) Ex4: Example why multiplying generating functions is useful:
Ex2: From Ex1, If n and |x| < 1, then 1 = (1 x)(1 + x + x2 + x3 + ...). = =(1+x+x2+x3+...)(1+x+x2+x3+...) where |x| < 1
( )
So, 1/(1-x) = 1 + x + x2 + x3 + ... k k
1 x k * 1 x k 1 x ( k 1) x
k
1/(1-x) where |x| < 1 is the generating function for the sequence: k 0 k 0 k 0 j 0 k 0
1, 1, 1, …, 1, … .
Ex3: From Ex2, set x = ay |ay| < 1, then 1/(1-x)2 is the generating function for the sequence 1,2,3,4,….
1/(1-x) = 1/(1-ay) = 1 + ay + (ay)2 + (ay)3 + ….
183 184
1/(1-ay) where |ay| < 1 is the generating function for the sequence 1, a, a2, a3,…
2.5.2. Generating functions 2.5.2. Generating functions
Another way to show that 1/(1-x)2 is the generating function of the sequence 1, Ex5: Show that x/(1-x)2 is the generating function for the sequence 0,1,2,3,….
2,3, 4…: We have
1 1
= 1 + 2𝑥 + 3𝑥 + ⋯
=1+𝑥+𝑥 +𝑥 +⋯ (1 − 𝑥)
(1 − 𝑥)
= 𝑥(1 + 2𝑥 + 3𝑥 + ⋯ )
( )
= 1+𝑥+𝑥 +𝑥 +⋯
= 0x0 + x1 + 2x2 + 3x3+….
( )
= 1 + 2𝑥 + 3𝑥 + ⋯
Ex6: Show that (x+1)/(1-x)3 is the generating function for the sequence 12,22,32,42,….
1/(1-x)2is the generating function for the sequence 1,2,3,4,…. From Ex5 we have:
Ex5: Show that x/(1-x)2 is the generating function for the sequence 0,1,2,3,…. 𝑥
= 𝑥 + 2𝑥 + 3𝑥 + ⋯
We have (1 − 𝑥)
1
= 1 + 2𝑥 + 3𝑥 + ⋯ = 𝑥 + 2𝑥 + 3𝑥 + ⋯
( )
(1 − 𝑥)
( ) = 𝑥(1 + 2𝑥 + 3𝑥 + ⋯ ) =1+2 𝑥+3 𝑥 +⋯
( )
𝑔 𝑥 −𝑎 −3𝑥 𝑎 𝑥
= 𝑥 + 2𝑥 + 3𝑥 + ⋯
𝑔 𝑥 −1 − 3𝑥𝑔(𝑥) = 𝑥 + 2𝑥 + 3𝑥 + ⋯
2𝑥 3 − 11𝑥 + 10𝑥
1 − 5𝑥 + 6𝑥 𝑔 𝑥 = 3 − 8𝑥 + =
1−𝑥 1−𝑥
3 − 11𝑥 + 10𝑥 (3 − 5𝑥)(1 − 2𝑥) (3 − 5𝑥)
𝑔 𝑥 = = =
(1 − 𝑥) 1 − 5𝑥 + 6𝑥 (1 − 𝑥)(1 − 3𝑥)(1 − 2𝑥) (1 − 𝑥)(1 − 3𝑥)
(3 − 5𝑥) 𝐴 𝐵
𝑔 𝑥 = = + ⇒ 𝐴 = 1; 𝐵 = 2
(1 − 𝑥)(1 − 3𝑥) (1 − 𝑥) (1 − 3𝑥)
1 2
𝑔 𝑥 = + =2 (3𝑥) + (𝑥)
(1 − 𝑥) (1 − 3𝑥)