Discrete Structure - Open University of Hong Kong

Download Report

Transcript Discrete Structure - Open University of Hong Kong

Discrete Structure
Li Tak Sing(李德成)
Lectures 16,17,18,19
1
From grammar to inductive definition
Consider the following grammar:
S|aB
B b|bB
Basis: , abL(G)
Induction: if ay  L(G), then aby  L(G).
2
Constructing grammars
1. {an|nN}
S|aS
2. {anbn|n N}
S |aSb
3. {(ab)n|nN}
S |abS
3
Combining grammars
 Suppose M and N are languages whose grammars have
disjoint sets of non-terminals. Suppose also that the
start symbols for the grammars of M and N are A and B
respectively. Then we have the following new languages
and grammars:
 Union rule: the language MN starts with the two productions:
SA|B
 Product Rule: the language MN starts with the productions:
S AB
 Closure Rule: The language M* starts with the productions:
S AS|
4
Examples
Write the grammar for each of the
following languages.
1.
2.
3.
4.
5.
{anbcm|m,nN}
{, a, ab, aab, aaab, ....}
{a, ba, bba, bbba, ....}
{anbn|n N}*
{anbn|n N}{cn|n1}
5
Solution
1. SaS|Sc|b
2. S |aB
B|bB
3. SbS|a
4. SAS|
A aAb| 
5. S A|B
A aAb| 
Bc|cB
6
Meaning and ambiguity
A grammar is said to be ambiguous if its
language contains some string that has
two different parse trees. This is
equivalent to saying that some string has
two distinct leftmost derivations or that
some string has two distinct rightmost
derivations.
7
Ambiguity
Consider the grammar.
Ea|b|E-E
The string a-b-a has the following distinct
derivations.
EE-E a-E a-E-E a-b-E a-b-a
E E-E E-E-E a-E-E a-b-E a-b-a
The former one has the meaning of a-(b-a)
The latter one has the meaning of (a-b)-a
8
Removing the ambiguity
If we change the grammar as this:
E E-T|T
T a|b
In the above grammar, T can only be
replaced by a or b. So only the right
operand can be further expands to another
expression that have -. So a-b-a will only
be expanded to have the meaning of (a-b)a
9
Ambiguity
 Show that each of the following grammers is
ambiguous. In other words, find a string that has
two different parse trees (equivalently, two
different leftmost derivations or two different
rightmost derivations)
1.
2.
3.
4.
5.
Sa|SbS.
S abB|AB and A |Aa and B |bB.
S aS|Sa|a
S aS|Sa|b
S Ab|A and A b|bA
10
Solution
1. Consider the string ababa. This can be
expanded as:
SSbS abS abSbS ababS
ababa, or
S SbS SbSbS abSbS ababS
ababa
2. Consider the string abb
S abB abbB abb, or
S AB AaB aB abB abbB abb
11
Solution
3. Consider the string aaa
S aS aaS aaa, or
S Sa Saa aaa
4. Consider the string aba
S aS aSa aba,or
S Sa aSa aba
5. Consider the string bb
S Ab bb, or
S A bA bb
12
More Examples
Find grammars for the following
languages:
1.
2.
3.
4.
5.
{anbmcmdn| m,n N}
{anbmcdn| m,n N}
{anbmcmd| m,n N}
{abmcmd| m,n N}
abmcdm| m N}
13
Solution
1. SaSd|A
A bAc|
2. S aSd|A
A bA|c
3. S aS|Ad
A bAc| 
4. S aAd
A bAc| 
5. S aA
A bAd|c
14
Chapter 4
Properties of Binary Relations
Three special properties
For a binary relation R on a set A, we have the
following definitions.
R is reflexive if xRx for all xA.
R is symmetric if xRy implies yRx for all x,y A
R is transitive if xRy and yRz implies xRz for all x,y,z
A
15
Two opposite properties
For a binary relation R on a set A, we have
the following definitions.
R is irreflexive if (x,x)R for all xA.
R is antisymmetric if xRy and yRx implies x=y
for all x,yA.
16
Example
R is a binary relation on N
aRb if (a+b) mod 2 = 0
R is reflexive because (a+a) mod 2 =0 for all a N
R is symmetric because if aRb, then (a+b) mod 2 =
0, then (b+a) mod 2 =0, then bRa
R is transitive, because if aRb and bRc, then (a+b)
mod 2 =0 and (b+c) mod 2 =0, then (a+2b+c) mod
2 =0, then (a+c) mod 2 =0, then aRc
17
Example
Give examples of binary relations over the
set {a,b,c,d} with the stated properties:
1. Reflexive and not symmetric and not transitive
2. Symmetric and not reflexive and not transitive
3. transitive and not reflexive and not symmetric
18
Solution
1. {(a,a),(b,b),(c,c),(d,d),(a,b),(b,c)}
2. {(a,b),(b,a)}
3. {(a,b),(b,b)}
19
Composition of relations
If R and S are binary relations, then the
composition of R and S, which we denote
by SR, is the following relation:
SR={(a,c)|(a,b)R and (b,c) S for some
element b}
20
More examples
For each of the following binary relations
state which of the three properties,
reflexive, symmetric and transitive are
satisfied.
1. xRy iff |x-y| is odd, over the integers.
2. xRy iff x is a parent of y, over the set of
people.
21
Solution
1. Not reflexive, symmetric, not transitive,
irreflexive, not anti-symmetric
2. Not reflexive, not symmetric, not
transitive, irreflexive, anit-symmetric
22
Grandparents
Given the isParentOf relation. So a
isParentOf b represents the fact that a is
the parent of b.
isGrandparentOf can then be defined in
terms of isParentOf.
isGrandparentOf=isParentOfisParentOf
So a isGrandparentOf b if there is c so that
a isParentOf c and c isPrentOf b.
23
More examples
Given the following binary relations over
{a,b,c,d}.
R={(a,a),(a,c),(b,a),(b,d),(c,b)}
S={(a,b),(a,c),(c,b),(d,c)}
1. Find SR
2. Find RS
24
Solution
1. {(a,b),(a,c),(b,b),(b,c)}
2. {(a,a),(a,d),(a,b),(c,a),(c,d),(d,b)}
25
Representations
If R is a binary relation on A, then we'll
denote the composition of R with itself n
times by writing
R n.
For example,
isGrandparentOf=isParentOf2
isGreatGrandParentOf=isParentOf3
26
Inheritance properties
If R is reflexive, then Rn is reflexive.
If R is symmetric, then Rn is symmetric.
If R is transitive, then Rn is transitive.
27
Example
Let R={(x,y)ZZ|x+y is odd}. We want to
find out R2 and R3.
28
Solution
R2 ={(x,y)|x+y is even}
R3 =R
29
Closures
If R is a binary relation and p is some
property, then the p closure of R is the
smallest binary relation containing R that
satisfies property p.
30
Reflexive closure
A reflexive closure of R is the smallest
reflexive relation that contains R.
A reflexive closure of R is denoted as r(R)
R is a relation over {a,b,c} and
R={(a,b),(b,c)}
Then, r(R)={(a,a),(b,b),(c,c),(a,b),(b,c)}
31
Symmetric closure
A symmetric closure of R is the smallest
symmetric relation that contains R.
A symmetric closure of R is denoted as
s(R)
R={(a,b),(b,c)}, s(R)={(a,b),(b,a),(b,c),(c,b)}
32
Transitive closure
A transitive closure of R is the smallest
transitive relation that contains R. It is
denoted as t(R).
R= {(a,b),(b,c)},
then t(R)= {(a,b),(b,c),(a,c)}
33
Constructing Closures
If R is a binary relation over a set A, then:
r(R)=RRo (Ro is the equality relation)
s(R)=R Rc (Rc is the converse relation)
t(R)=R R2 R3 R4....
If A is finite with n elements, then t(R)= R R2
R3 R4.... Rn
34
Example
Given the set A={a,b,c,d}. Draw a directed
graph to represent the indicated closure
for each of the following binary relations
over A.
1. r(R), where R={(a,d)}
2. s(R) where R={(a,b), (c,d)}
3. t(R) where R={(a,b),(d,a),(d,c),(c,b)}
35
Solution
1. r(R)={(a,a),(b,b),(c,c),(d,d),(a,d)}
2. s(R)={(a,b),(b,a),(c,d),(d,c)}
1. t(R) ={(a,b),(d,a),(d,c),(c,b),(d,b)}
36
Equivalence relations
Any binary relation that is reflexive,
symmetric, and transitive is called an
equivalence relation.
37
Sample equivalence relations
R is over N, xRy means x+y is even
R is over real number, xRy means
sin(x)=sin(y)
38
Examples
1. Let R be defined on N by xRy iff |x-y| is
odd. Show that R is not an equivalance
relation on N.
2. Given the relation over the integers
defined by a~b iff |a|=|b|, either prove that
~ is an equivalence relation or prove that
~ not an equivalence relation.
39
Solution
1. R is not reflexive, is symmetric, is not
transitive.
2. ~ is an equivalence relation. It is reflexive
because |a|=|a| for all aZ.
It is symmetric because a~b|a|=|b|
|b|=|a| b~a
It is transitive because a~b and b~c
|a|=|b| and |b|=|c| |a|=|c|
a~c
40
Intersection property of equivalence
If E and F are equivalence relations on the
set A, then EF is an equivalence relation
on A.
41
Kernel relations
If f is a function with domain A, then the
relation ~ defined by
x~y iff f(x)=f(y)
is an equivalence relation on A, and it is
called the kernel relation of f.
42
Equivalence classes
Let R be an equivalence relation on a set
S. If aS, then the equivalence class of a,
denoted by [a], is the subset of S
consisting of all elements that are
equivalent to a. In other words, we have
[a]={x S | xRa}
43
Example of equivalence class
a~b iff a+b is even
a~b iff sin(a)=sin(b)
44
Property of equivalences
 Let S be a set with an equivalence relation R. If
a, b S, then either [a]=[b] or [a][b]=.
 Proof. If [a][b], and [a][b]  , there is an
element c so that c[a] and c[b]. So we have
aRc and cRb, therefore aRb. Now, for any x
[a] , xRa. Since aRb, therefore we have xRb
which implies that x [b]. Therefore, [a][b].
Similarly, we have [b][a].
45
Partitions
 By a partition of a set we mean a collection of
nonempty subsets that are disjoint from each
other and whose union is the whole set.
 If R is an equivalence relation on the set S, then
the equivalence classes form a partition of S.
Conversely, if P is a partition of a set S, then
there is an equivalence relation on S whose
equivalence classes are sets of P.
46
Refinement of a partition
Suppose that P and Q are two partitions of
a set S. If each set of P is a subset of a
set in Q, then P is a refinement of Q. The
finest of all partitions on S is the collection
of singleton sets. The coarsest of all
partitions of S is the set S itself.
47
Example
aRb iff ab (mod 2)
aSb iff ab (mod 4)
48
Example
Given the following set of words.
{rot, tot, root, toot, roto, toto, too, to ,otto}.
1. Let f be the function that maps word to its set
of letters. For kernel relation of f, describe the
equivalence classes.
2. Let f be the function that maps a word to its
bag of letters. For the kernel relation of f,
describe the equivalence classes.
49
Solution
1. [rot]={rot,root,roto}
[to]={to,toot,toto,too,to,otto}
50
Kruskai's algorithm for minimal
spanning trees
In the spanning tree problems, we can
define a relation R so that aRb if there is a
path between a and b. The algorithm is:
51
Kruskal's Algorithm
 Sort the edges of the graph by weight, and let L be the
sorted list.
 Let T be the minimal spanning tree and initialize T:=.
 For each vertex v of the graph, create the equivalence
class [v]={v}
 while there are 2 or more equivalence classes do
Let {a,b} be the edge at the head of L;
L:=tail(L);
if [a][b] then
T:=T{{a,b}};
Replace the equivalence classes [a] and [b] by [a]
[b]
fi
 od
52
Spanning trees
b
a
e
2
1
d
1
2
2
c
2
1
2
f
53
Solution
L
[v]
T
ad,bc,cf,ea,ab,ed,dc,ef
{a},{b},{c},{d},{e},{f}
bc,cf,ea,ab,ed,dc,ef
{a,d},{b},{c},{e},{f}
ad
cf,ea,ab,ed,dc,ef
{a,d},{b,c},{e},{f}
ad,bc
ea,ad,ed,dc,ef
{a,d},{b,c,f},{e}
ad,bc,cf
ad,ed,dc,ef
{a,d,e},{b,c,f}
ad,bc,cf,ea
ed,dc,ef
{a,d,e},{b,c,f}
ad,bc,cf,ea
dc,ef
{a,d,e},{b,c,f}
ad,bc,cf,ea
ef
{a,d,e,b,c,f}
ad,bc,cf,ea,dc
54
Spanning trees
a
2
1
e
3
b
2
2
1 f
3
d
1
1
c
55
Solution
L
{v}
T
ab,bf,bc,dc,ea,ac,ed,ef,fd
{a},{b},{c},{d},{e},{f}
bf,bc,dc,ea,ac,ed,ef,fd
{b,f},{a},{c},{d},{e}
ab
bc,dc,ea,ac,ed,ef,fd
{b,c,f},{a},{d},{e}
ab,bf
dc,ea,ac,ed,ef,fd
{b,c,f},{a},{d},{e}
ab,bf
ea,ac,ed,ef,fd
{b,c,f,d},{a},{e}
ab,bf,dc
ac,ed,ef,fd
{b,c,f,d},{a,e}
ab,bf,dc,ea
ed,ef,fd
{b,c,f,d,a,e}
ab,bf,dc,ea,ac
56
Order relations
Two essential properties of order
it is transitive: if a>b and b>c then a>c
it is anitsymetric: we cannot have a<b and b<a.
If ab and b a, then a=b.
57
A Pancake recipe
 Suppose we have the following recipe for
making pancakes.
1. Mix the dry ingredients (flour, sugar, baking
powder) in a bowl.
2. Mix the wet ingredients (milk, eggs) in a bowl.
3. Mix the wet and dry ingredients together.
4. Oil the pan.
5. Heat the pan.
6. Make a test pancake and throw it away.
7. Make pancakes.
58
A Pancake recipe
We see that some steps are need to be
done before others. We can show this
order as a digraph:
1
2
3
4
5
6
7
59
A Pancake recipe
We can see that there can be many valid
orders:
4,5,2,1,3,6,7
1,2,3,4,5,6,7
....
60
A Pancake recipe
We can define a relation R as:
xRy means that step x must be done
before step y
R is irreflexive because a step cannot be
done before itself.
R is antisymetric because when a step has
to be done before another, it cannot done
the other way round.
R is transitive.
61
Partial order
 A binary relation is called a partial order if it is
anitsysmetric and transitive. The set over which
a partial order is defined is called a partially
ordered set- or poset for short. If we want to
emphasize the fact that R is the partial order that
makes S a poset, we'll write <S,R> and call it a
poset.
 It is called partial order because not all elements
are related.
 For example, steps 1 and 2 of the pancake
example are not related, i.e., there is no order of
steps 1,2.
62
Total order
 Suppose R is a binary relation on a set S and x,
y in S. We say that x and y are comparable if
either xRy or yRx. In other words, elements that
are related are comparable. If every pair of
distinct elements in a partial order are
comparable, then the order is called a total order
(also called a linear order). If R is a total order
on the set S, then we also say that S is a totally
ordered set or linearly ordered set.
63
Example
<N,|> is a poset because
| is transitive: a|b and b|c implies a|c
| is antisymmetric: if a|b, then either a=b or b
does not divides b.
<N,|> is not total ordered because there are
some elements in N that is not related under |.
For example, 3 and 5 are not related under |.
<{2n|nN},|> is a totally ordered set
because for any m,n N, either 2n|2m or
2m|2n.
64
reflexive partial order and irreflexive
partial order
If a partial order is reflexive, we call it
reflexive partial order.
If a partial order is irreflexive, we call it
irreflexive partial order.
<N,≤> is reflexive partial order.
<N,<> is irreflexive partial order.
65
Notation for Partial orders
When talking about partial orders, we'll
often use the symbols
≺ and ≼
to stand for an irreflexive partial order and
a reflexive partial order respectively. We
can read 𝑎 ≺ 𝑏 as "a is less than b", and
𝑎 ≼ 𝑏 as "a is less than or equal to b".
66
Notation for Partial orders
The two symbols can be defined in terms of
each other. For example, if < 𝐴, ≺> is a
poset, then we can define the relation ≼ in
terms of ≺ by writing:
≼=≺∪ 𝑥, 𝑥 𝑥 ∈ 𝐴 .
In other words, ≼ is the reflexive closure of
≺. So𝑥 ≼ 𝑦 always means 𝑥 ≺ 𝑦 or 𝑥 = 𝑦 .
67
Notation for Partial orders
Similarly, if < 𝐵, ≼> is a poset, then we can
define the relation ≺ in terms of ≼ by writing:
≺=≼ − 𝑥, 𝑥 𝑥 ∈ 𝐵 .
68
Chains
A set of elements in a poset is called a chain
if all the elements are comparable-linked-to
each other. For example, any totally ordered
set is itself a chain. A sequence of elements
𝑥1 , 𝑥2 ,... in a poset is said to be descending
chain if 𝑥1 ≻ 𝑥2 ≻ 𝑥3 ≻...
69
Predecessors and Successors
If 𝑥 ≺ 𝑦, then we say that 𝑥 is a predecessor
of 𝑦, or y is a successor of 𝑥.
Suppose that 𝑥 ≺ 𝑦 and there is no elements
between 𝑥 and 𝑦. In other words, suppose
we have the following situation:
𝑧∈𝐴𝑥≺𝑧≺𝑦 =∅
When this is the case, we say that 𝑥 is an
immediate predecessor of 𝑦, or 𝑦 is an
immediate successor of 𝑥.
70
Poset diagrams
A poset can be represented by a special
graph called a poset diagram. Whenever
𝑥 ≺ 𝑦 and 𝑥 is immediate predecessor of 𝑦,
then place an edge (𝑥, 𝑦) in the poset
diagram with x at a lower level than y.
A poset diagram can often help us observe
certain properties of a poset.
71
Example
Draw a poset diagram of the partial order
defined by the subset relation on
power({a,b,c}).
72
Maxima, Minima, and Bounds
Suppose that S is any nonempty subset of a
poset P. An element xS is called a minimal
element of S if x has no predecessors in S.
An element x S is called the lease element
of S if x is minimal and 𝑥 ≼ 𝑦 for all y S
73
Maxima, Minima, and Bounds
For example:
<power({a,b,c}), > has a least element 
<{{a,b},{a},{b}} has two minimal elements,
{a} and {b} but no least element.
<{2,4,5,10}, |> has two minimal elements, 2
and 5 but no least element.
74
Maxima, Minima, and Bounds
An element xS is called a maximal element
of S if x has no successors in S. An element
x S is called the greatest element of S if x
is maximal and 𝑦 ≼ 𝑥 for all y S.
75
Maxima, Minima, and Bounds
For example:
<power({a,b,c}), > has a greatest element
{a,b,c}
<{{a,b},{a},{b}} has a greatest element {a,b}
<{2,4,5,10}, |> has two maximal elements, 4
and 10.
76
Maxima, Minima, and Bounds
Some sets may not have any minimal
elements, yet still be bounded below by
some element. For example, the set of
positive rational numbers has no least
element yet is bounded below by the
number 0.
77
Maxima, Minima, and Bounds
If S is a nonempty subset of a poset P, an
element xP is called a lower bound of S if
𝑥 ≼ 𝑦 for all y S. An element xP is called
the greatest lower bound (or glb) of S if x is
a lower bound and 𝑧 ≼ 𝑥 for all lower bounds
z of S.
78
Maxima, Minima, and Bounds
If S is a nonempty subset of a poset P, an
element xP is called an upper bound of S if
𝑥 ≽ 𝑦 for all y S. An element xP is called
the lowest upper bound (or lub) of S if x is a
upper bound and 𝑧 ≽ 𝑥 for all upper bounds
z of S.
79
Example
Given the set P=ZZ and the partial order ≺
on P defined by
(𝑎, 𝑏) ≺ 𝑐, 𝑑 iff 𝑎 < 𝑐
Let S=NN. Describe each of the following.
1. The minimal elements of S.
2. The least element of S.
3. The maximal elements of S.
4. The greatest element of S.
80
Example
5.
6.
7.
8.
The lower bounds of S.
The upper bounds of S.
The lub of S.
The glb of S.
81
Solution
1.
2.
3.
4.
5.
6.
7.
8.
0, 𝑥 𝑥 ∈ 𝑁
No least element.
no maximal elements
No greatest element.
𝑥, 𝑦 𝑥 ∈ 𝑍 −
No upper bounds
no lub
no glb
82