Professional Documents
Culture Documents
3rd DMA 2nd Part
3rd DMA 2nd Part
f(n) = f(n/2) + 2
when n is even.
Example: Merge Sort
• The merge sort algorithm splits a list of n (assuming n is even) items
to be sorted into two lists with n/2 items. It uses fewer than n
comparisons to merge the two sorted lists.
• Hence, the number of comparisons required to sort a sequence of
size n, is no more than than M(n) where
M(n) = 2M(n/2) + n.
Example: Fast Multiplication of Integers
• An algorithm for the fast multiplication of two 2n-bit integers (assuming n is even) first splits each of the 2n-bit integers into two blocks, each of n bits.
• Suppose that a and b are integers with binary expansions of length 2n. Let
a = (a2n−1a2n−2 … a1a0)2 and b = (b2n−1b2n−2 … b1b0)2 .
• Let a = 2nA1 + A0, b = 2nB1 + B0 , where
A1 = (a2n−1 … an+1an)2 , A0 = (an−1 … a1a0)2 ,
B1 = (b2n−1 … bn+1bn)2 , B0 = (bn−1 … b1b0)2.
• The algorithm is based on the fact that ab can be rewritten as:
ab = (22n + 2n)A1B1 +2n (A1−A0)(B0 − B1) +(2n + 1)A0B0.
• This identity shows that the multiplication of two 2n-bit integers can be carried out using three multiplications of n-bit integers, together with additions, subtractions, and shifts.
• Hence, if f(n) is the total number of operations needed to multiply two n-bit integers, then
f(2n) = 3f(n) + Cn
where Cn represents the total number of bit operations; the additions, subtractions and shifts that are a constant multiple of n-bit operations.
Estimating the Size of Divide-and-Conquer
Functions
Theorem 1: Let f be an increasing function that
satisfies the recurrence relation
f(n) = af(n/b) + cnd
whenever n is divisible by b, where a≥ 1, b is an
integer greater than 1, and c is a positive real number.
Then
Note that log 3 ≈ 1.6. Therefore the fast multiplication algorithm is a substantial
improvement over the conventional algorithm that uses O(n2) bit operations.
Discrete Mathematics
Advanced Counting Techniques
Discrete Mathematics
Generating Functions
Inclusion-Exclusion
Generating functions
Definition
The generating function for the sequence a0, a1, ...,ak, ... of real
numbers is the infinite series
Σ∞
G (x) = a0 + a1x + · · · + ak x k + · · · = ak x k .
k=0
Discrete Mathematics
Generating Functions
Inclusion-Exclusion
Generating functions
Definition
The generating function for the sequence a0, a1, ...,ak, ... of real
numbers is the infinite series
Σ∞
G (x) = a0 + a1x + · · · + ak x k + · · · = ak x k .
k=0
Discrete Mathematics
Remark
We can define generating functions for finite sequences of real
numbers by extending a finite sequence a0, a1, . . . , an into an
infinite sequence by setting an+1 = 0, an+2 = 0, and so on. The
generating function G (x ) of this infinite sequence {a n } is a
polynomial of degree n:
Generating functions
Example
Let m be a positive integer. Let ak = C (m, k) for
k = 0, 1, 2,. . . , m. What is the generating function for the
sequence a0, a1, ...,am?
Prof. Steven Evans Discrete Mathematics
Generating functions
• Example
• Let m be a positive integer. Let ak = C (m, k)for
• k = 0, 1, 2,. . . , m. What is the generating function
for the sequence a0, a1, ...,am?
• The generating function for this sequence is
Prof. Steven Evans Discrete Mathematics
. Σ . Σ . Σ . Σ
m m m 2 m m
G(x) = + x+ x +· · · + x
0 1 2 m
Generating functions
• Example
• Let m be a positive integer. Let ak = C (m, k)for
• k = 0, 1, 2,. . . , m. What is the generating function for the sequence
a0, a1, ...,am?
• The generating function for this sequence is
Prof. Steven Evans Discrete Mathematics
. Σ . Σ . Σ . Σ
m m m 2 m m
G(x) = + x+ x +· · · + x = (1 + x) m.
0 1 2 m
Useful facts about power series
Example
The function f (x ) = 1/(1 − ax ) is the generating function of the
sequence 1, a, a2, a3, . . . , because
Prof. Steven Evans Discrete Mathematics
1
= 1 + ax + a2x 2 + · · ·
1 − ax
when |ax|< 1, or equivalently, for |x|< 1/|a|for a ƒ= 0.
Useful facts about power series
Theorem
Discrete Mathematics
Useful facts about power series
Example
Let f (x) = 1/(1 − x)2. Find the coefficients for its Taylor
expansion.
Discrete Mathematics
Useful facts about power series
Example
Let f (x) = 1/(1 − x)2. Find the coefficients for its Taylor
expansion.
We know that 1/(1 − x) = 1 + x + x 2 + · · · . Using the product
Discrete Mathematics
∞ k ∞
2
1/(1 − x) = Σ Σ 1 xk = Σ (k + 1)xk.
k=0 j =0 k=0
Useful facts about power series
Definition
Discrete Mathematics
.
Useful facts about power series
Example
When u = − n is a negative integer, the extended binomial
coefficient can be expressed in terms of an ordinary one:
−n (−n)(−n − 1) · · · (−n − r + 1)
. Σ =
r r!
Prof. Steven Evans Discrete Mathematics
Useful facts about power series
Example
When u = − n is a negative integer, the extended binomial
coefficient can be expressed in terms of an ordinary one:
. Σ
−n (−n)(−n − 1) · · · (−n − r + 1)
=
r r!
(−1) r n(n + 1) · · · (n + r − 1)
Prof. Steven Evans Discrete Mathematics
=
r!
Useful facts about power series
Example
When u = − n is a negative integer, the extended binomial
coefficient can be expressed in terms of an ordinary one:
. Σ
−n (−n)(−n − 1) · · · (−n − r + 1)
=
r r!
(−1) r n(n + 1) · · · (n + r − 1)
Prof. Steven Evans Discrete Mathematics
=
r!
(−1) r (n + r − 1)(n + r − 2) · · · n
=
r!
Useful facts about power series
Example
When u = − n is a negative integer, the extended binomial
coefficient can be expressed in terms of an ordinary one:
. Σ
−n (−n)(−n − 1) · · · (−n − r + 1)
=
r r!
(−1) r n(n + 1) · · · (n + r − 1)
Prof. Steven Evans Discrete Mathematics
=
r!
(−1) r (n + r − 1)(n + r − 2) · · · n
=
r!
(−1) r (n + r − 1)!
=
r!(n − 1)!
Useful facts about power series
Example
When u = − n is a negative integer, the extended binomial
coefficient can be expressed in terms of an ordinary one:
. Σ
−n (−n)(−n − 1) · · · (−n − r + 1)
=
r r!
(−1) r n(n + 1) · · · (n + r − 1)
Prof. Steven Evans Discrete Mathematics
=
r!
(−1) r (n + r − 1)(n + r − 2) · · · n
=
r!
(−1) r (n + r − 1)!
=
r!(n − 1)!
. Σ
n + r− 1
= (−1) r
r
Useful facts about power series
Theorem
Let x be a real number with |x|< 1 and let u be a real number.
Then: ∞
Σ Σu k
(1 + x ) =
u . x .
Prof. Steven Evans Discrete Mathematics
k
k=0
Useful facts about power series
Theorem
Let x be a real number with |x|< 1 and let u be a real number.
Then:
Σ∞ . uΣ
(1 + x )u = xk.
Prof. Steven Evans Discrete Mathematics
k
k=0
Example
Find the generating function for (1 + x ) −n and (1 − x ) −n , where n
is a positive integer.
Useful facts about power series
Solution
Using the extended binomial theorem, we have
∞
−n k
(1 + x) −n
= Σ. Σ x .
k
k=0
∞ .
n+ k − 1 k
(1 + x)− n = Σ (− 1)k Σk x .
k=0
Useful facts about power series
Solution
Using the extended binomial theorem, we have
Σ∞ . − nΣ
(1 + x)− n = xk.
k
k=0
Σ∞ . Σ
n+ k − 1 k
(1 + x)− n = (− 1)k x .
k
k=0
Σ∞ . n + k − 1 Σ
(1 − x)− n = x k.
k
k=0
Useful facts about power series
Example
Find the number of solutions of e1 + e2 + e3 = 17, where e1, e2,
and e3 are nonnegative integers with 1 + i ≤ ei ≤ 4 + i .
Σ∞ Σ∞
G (x) − 3xG(x) = ak x k − 3 ak− 1 x k
k=0 k=1
Σ∞
= a0 + (ak − 3ak− 1 )x k= 2.
k=1
Σ∞
G (x) − 1 = (8an− 1 xn + 10n−1xn)
n=1
Σ∞ Σ∞
= 8x an− 1x n−1 + x 10n−1 xn−1
n=1 n=1
= 8xG(x) + x/(1 − 10x).
Using generating functions to solve
recurrence relations
Solution, continued
G(x) − 1 = 8xG(x) + 1/(1 − 10x).
Solving for G (x) gives
. Σ
1 − 9x 1 1 1
G(x) = = + .
(1 − 8x)(1 − 10x) 2 1 − 8x 1 − 10x
Prof. Steven Evans Discrete Mathematics
Using generating functions to solve
recurrence relations
Solution, continued
G(x) − 1 = 8xG(x) + 1/(1 − 10x).
Solving for G (x) gives
. Σ
1 − 9x 1 1 1
G(x) = = + .
(1 − 8x)(1 − 10x) 2 1 − 8x 1 − 10x
Prof. Steven Evans Discrete Mathematics
• it is linear because the right-hand side is a sum of the previous terms of the sequence each
multiplied by a function of n.
• it is homogeneous because no terms occur that are not multiples of the ajs. Each coefficient is a
constant.
• the degree is k because an is expressed in terms of the previous k terms of the sequence.
Therefore by Theorem 1
,
Solving, we obtain .
Hence,
The Solution when there is a Repeated Root
a0 = 1 = α1 and a1 = 6 = α1 ∙ 3 + α2 ∙3.
Theorem 3: Let c1, c2 ,…, ck be real numbers. Suppose that the characteristic equation
rk – c1rk−1 –⋯ – ck = 0
has k distinct roots r1, r2, …, rk. Then a sequence {an} is a solution of the recurrence
relation
an = c1an−1 + c2an−2 + ….. + ck an−k
if and only if
Theorem 4: Let c1, c2 ,…, ck be real numbers. Suppose that the characteristic equation
rk – c1rk−1 –⋯ – ck = 0
has t distinct roots r1, r2, …, rt with multiplicities m1, m2, …, mt, respectively so that mi ≥ 1 for i =
0, 1, 2, …,t and m1 + m2 + … + mt = k. Then a sequence {an} is a solution of the recurrence relation
an = c1an−1 + c2an−2 + ….. + ck an−k
if and only if
Because F(n)= 2n is a polynomial in n of degree one, to find a particular solution we might try a linear function in n, say pn = cn + d, where c and d are constants. Suppose that pn = cn + d is such a solution.
Then an = 3an−1 + 2n becomes cn + d = 3(c(n− 1) + d)+ 2n.
Simplifying yields (2 + 2c)n + (2d − 3c) = 0. It follows that cn + d is a solution if and only if
2 + 2c = 0 and 2d − 3c = 0. Therefore, cn + d is a solution if and only if c = − 1 and d = − 3/2.
Consequently, an(p) = −n − 3/2 is a particular solution.
By Theorem 5, all solutions are of the form an = an(p) + an(h) = −n − 3/2 + α3n, where α is a constant.
To find the solution with a1 = 3, let n = 1 in the above formula for the general solution.
Then 3 = −1 − 3/2 + 3 α, and α = 11/6. Hence, the solution is an = −n − 3/2 + (11/6)3n.
Relations and Their
Properties
• Relations and Functions
• Properties of Relations
• Reflexive Relations
• Symmetric and Antisymmetric Relations
• Transitive Relations
• Combining Relations
Binary Relations
Definition: A binary relation R from a set A to a set B is a subset R ⊆ A
× B.
Example:
• Let A = {0,1,2} and B = {a,b}
• {(0, a), (0, b), (1,a) , (2, b)} is a relation from A to B.
• We can represent relations from a set A to a set B graphically or using a table:
Note that these relations are on an infinite set and each of these relations is an infinite
set.
Which of these relations contain each of the pairs
(1,1), (1, 2), (2, 1), (1, −1), and (2, 2)?
Solution: Checking the conditions that define each relation, we see that the pair
(1,1) is in R1, R3, R4 , and R6: (1,2) is in R1 and R6: (2,1) is in R2, R5, and R6: (1,
−1) is in R2, R3, and R6 : (2,2) is in R1, R3, and R4.
Reflexive Relations
Definition: R is reflexive iff (a,a) ∊ R for every element a ∊ A.
Written symbolically, R is reflexive if and only if
∀x[x∊U ⟶ (x,x) ∊ R]
Example: The following relations on the integers are reflexive:
If A = ∅ then the empty relation is
R1 = {(a,b) | a ≤ b},(1, 1) reflexive vacuously. That is the empty
R3 = {(a,b) | a = b or a = −b}, relation on an empty set is reflexive!
R4 = {(a,b) | a = b}.
The following relations are not reflexive:
R2 = {(a,b) | a > b} (note that 3 ≯ 3),
R5 = {(a,b) | a = b + 1} (note that 3 ≠3 + 1),
R6 = {(a,b) | a + b ≤ 3} (note that 4 + 4 ≰ 3).
Symmetric Relations
Definition: R is symmetric iff (b,a) ∊ R whenever (a,b) ∊ R for all a,b ∊
A. Written symbolically, R is symmetric if and only if
∀x∀y [(x,y) ∊R ⟶ (y,x) ∊ R]
Example: The following relations on the integers are symmetric:
R3 = {(a,b) | a = b or a = −b},
R4 = {(a,b) | a = b},
R6 = {(a,b) | a + b ≤ 3}.
The following are not symmetric:
R1 = {(a,b) | a ≤ b} (note that 3 ≤ 4, but 4 ≰ 3),
R2 = {(a,b) | a > b} (note that 4 > 3, but 3 ≯ 4),
R5 = {(a,b) | a = b + 1} (note that 4 = 3 + 1, but 3 ≠4 + 1).
Antisymmetric Relations
Definition:A relation R on a set A such that for all a,b ∊ A if (a,b) ∊ R and
(b,a) ∊ R, then a = b is called antisymmetric. Written symbolically, R is
antisymmetric if and only if
∀x∀y [(x,y) ∊R ∧ (y,x) ∊ R ⟶ x = y]
• Example: The following relations on the integers are antisymmetric:
R1 = {(a,b) | a ≤ b},
R2 = {(a,b) | a > b}, For any integer, if a a ≤ b and a
R4 = {(a,b) | a = b}, ≤ b , then a = b.
R5 = {(a,b) | a = b + 1}.
The following relations are not antisymmetric:
R3 = {(a,b) | a = b or a = −b}
(note that both (1,−1) and (−1,1) belong to R3),
R6 = {(a,b) | a + b ≤ 3} (note that both (1,2) and (2,1) belong to R6).
Transitive Relations
Definition: A relation R on a set A is called transitive if whenever (a,b) ∊ R
and (b,c) ∊ R, then (a,c) ∊ R, for all a,b,c ∊ A. Written symbolically, R is
transitive if and only if
∀x∀y ∀z[(x,y) ∊R ∧ (y,z) ∊ R ⟶ (x,z) ∊ R ]
• Example: The following relations on the integers are transitive:
R1 = {(a,b) | a ≤ b}, For every integer, a ≤ b
and b ≤ c, then a ≤ c.
R2 = {(a,b) | a > b},
R3 = {(a,b) | a = b or a = −b},
R4 = {(a,b) | a = b}.
The following are not transitive:
R5 = {(a,b) | a = b + 1} (note that both (3,2) and (4,3) belong to R5, but not (3,3)),
R6 = {(a,b) | a + b ≤ 3} (note that both (2,1) and (1,2) belong to R6, but not (2,2)).
Combining Relations
• Given two relations R1 and R2, we can combine them using basic set
operations to form new relations such as R1 ∪ R2, R1 ∩ R2, R1 − R2,
and R2 − R1.
• Example: Let A = {1,2,3} and B = {1,2,3,4}. The relations R1 =
{(1,1),(2,2),(3,3)} and R2 = {(1,1),(1,2),(1,3),(1,4)} can
be combined using basic set operations to form new relations:
R1 ∪ R2 ={(1,1),(1,2),(1,3),(1,4),(2,2),(3,3)}
R1 ∩ R2 ={(1,1)} R1 − R2 ={(2,2),(3,3)}
R2 − R1 ={(1,2),(1,3),(1,4)}
Composition
Definition: Suppose
• R1 is a relation from a set A to a set B.
• R2 is a relation from B to a set C.
Then the composition (or composite) of R2 with R1, is a relation from
A to C where
• if (x,y) is a member of R1 and (y,z) is a member of R2, then (x,z) is a member
of R2∘ R1.
Composition
• What is the composite of the relations R and S, where R is the
relation from {1, 2, 3} to {1, 2, 3, 4} with R = {(1, 1), (1, 4), (2,
3), (3, 1), (3, 4)} and S is the relation from {1, 2, 3, 4} to {0, 1, 2} with
• R = {(1, 1), (1, 4), (2, 3), (3, 1), (3, 4)}
• S = {(1, 0), (2, 0), (3, 1), (3, 2), (4, 1)}?
• (1,0)((1,1)(2,1)(2,2)(3,0)(3,1)
Composition
• Solution:- S ◦R is constructed using all ordered pairs in R and
ordered pairs in S, where the second element of the ordered pair in R
agrees with the first element of the ordered pair in S.
• For example, the ordered pairs (2, 3) in R and (3, 1) in S
produce the ordered pair (2, 1) in S ◦R.
• Computing all the ordered pairs in the composite, we find S ◦ R = {(1,
0), (1, 1), (2, 1), (2, 2), (3, 0), (3, 1)}.
Powers of a Relation
Definition: Let R be a binary relation on A. Then the powers Rn of the
relation R can be defined inductively by:
• Basis Step: R1 = R
• Inductive Step: Rn+1 = Rn ∘ R
Example 2: Let A = {a1,a2, a3} and B = {b1,b2, b3,b4, b5}. Which ordered
pairs are in the relation R represented by the matrix
R = {(a1, b2), (a2, b1),(a2, b3), (a2, b4),(a3, b1), {(a3, b3), (a3, b5)}.
Matrices of Relations on Sets
a
b
c d
• Reflexive? No, there are no loops
• Symmetric? No, there is an edge from a to b, but not from b to a
• Antisymmetric? No, there is an edge from d to b and b to d
• Transitive? No, there are edges from a to c and from c to b,
but there is no edge from a to d
Determining which Properties a Relation
has from its Digraph – Example 4
b
a
c d
• Reflexive? No, there are no loops
• Symmetric? No, for example, there is no edge from d to a
• Antisymmetric? Yes, whenever there is an edge from one vertex
to another, there is not one going back
• Transitive? Yes (trivially), there are no two edges where the first
edge ends at the vertex where the second edge begins
Example of the Powers of a Relation
a b a b
d c d c
R R2
a b b
a
d c d c
R4 R3
The pair (x,y) is in Rn if there is a path of length n from x to y in R
(following the direction of the arrows).
Hasse Diagram
• Let A = {1, 2, 3, 9, 18} and consider the “divides” relation
on A: For all a, b A,
Example:
•This, of course, assumes the “same definition” for the relation on the subset
as for the set as a whole. A set A is called a totally ordered set with respect
to a relation if, and only if, A is partially ordered with respect to and
is a total order.
Partially and Totally Ordered Sets
•A maximal element in a partially ordered set is an element that is
greater than or equal to every element to which it is comparable.
(There may be many elements to which it is not comparable.)
•Solution:
•There is just one maximal element, g, which is also the greatest element. The
minimal elements are c, d, and i, and there is no least element.
The least element of the poset with Hasse diagram (a) is a. This poset has no
greatest element. The poset
with Hasse diagram (b) has neither a least nor a greatest element.
The poset with Hasse
diagram (c) has no least element. Its greatest element is d.
The poset with Hasse diagram
(d) has least element a and greatest element d.
• POSET (S, < ).
• If ‘u’ is an element of ‘S’ such that a < u for all elements a ∈ A, then
‘u’ is called an upper bound of A.
• If ‘l’ is an element of ‘S’ such that l < a for all elements a ∈ A, then
‘l’ is called a lower bound of A.
• The element x is called the least upper bound of the subset A if x is an
upper bound that is less than every other upper bound of A.
• Similarly greatest lower bound
• Find the lower and upper bounds of the subsets
{a, b, c}, {j, h}, and {a, c, d, f } in the poset with the
Hasse diagram shown in Figure 7.
• The upper bounds of {a, b, c} are e, f, j, and h, and its only lower
bound is a.
• There are no upper bounds of {j, h}, and its lower bounds are a, b, c,
d, e, and f .
• The upper bounds of {a, c, d, f } are f , h, and j , and its lower bound is
a.
Lattice
• A partially ordered set in which every pair of elements has both a
least upper bound and a greatest lower bound is called a lattice.
Topological Sorting
105
9 6 3 4 8 2 0 5 1 7
0 1 2 3 4
5 6 7 8 9
4 6
2 3
108
Assemble an Automobile
1) Build Frame
2) Install engine, power train components, gas tank.
3) Install brakes, wheels, tires.
4) Install dashboard, floor, seats.
5) Install electrical lines.
6) Install gas lines.
7) Attach body panels to frame
8) Paint body.
109
Prerequisites
Task Immediately Preceding Time Needed to
Tasks Perform Task
1 7 hours
2 1 6 hours
3 1 3 hours
4 2 6 hours
5 2, 3 3 hours
6 4 1 hour
7 2, 3 1 hour
8 4, 5 2 hours
9 6, 7, 8 5 hours
110
Example – Job Scheduling
What is the total order compatible with it?
Task 4
6 hours Task 6
Task 2
1 hour
6 hours
Task 1
Task 5
7 hours
3 hours
Task 3 Task 9
Task 8
3 hours 5 hour
2 hour
Task 7
1 hour
111
112
113