In Context Free Grammer, do we replace all variable during a substitution? or can we apply substitution rule to only of the variable of same type? - theory

Imagine we have a Context Free Grammer, CFG, as follows:
S -> a ...(1)
S -> SS ...(2)
And i derive a string in the specified language as follows:
S ..start state
SS ..using (2)
aS ...using (1) <- is this valid, like only applying the subsititution rule on 1 variable instead of all same variables?
So my question is if i were to apply rule (1) on "SS", do i have the option to apply (1) to only 1 S of the "SS" or do i have to apply to both of them?

You can apply the rule to only one S, or as many as you like. Here is a slightly more complicated example that maybe better illustrates the idea:
S -> a (1)
S -> b (2)
S -> SS (3)
So, what strings are in this language? Here are the first few strings with derivations:
a: S -> a
b: S -> b
aa: S -> SS -> aS -> aa
S -> SS -> Sa -> aa
ab: S -> SS -> aS -> ab
S -> SS -> Sb -> as
ba: S -> SS -> bS -> ba
S -> SS -> Sa -> ba
bb: S -> SS -> bS -> bb
S -> SS -> Sb -> bb
aaa: S -> SS -> aS -> aSS -> aaS -> aaa
S -> SS -> aS -> aSS -> aSa -> aaa
S -> SS -> Sa -> SSa -> aSa -> aaa
S -> SS -> Sa -> SSa -> Saa -> aaa
aab
...
Anyway, we find that the grammar generates all non-empty strings of a's and b's, including those with mixed up a's and b's. If we had to replace all S's with the same rule, we would get a much, much smaller language, if we'd get a (non-empty) language at all.

Related

How to deal with loops when converting from context free to CNF?

Let's say there is a grammar
S -> PQT
R -> T
U -> aU | bX
X -> Y
P -> bQ
Y -> SX | c | X
Q -> aRY
T -> U
There is a loop:
X -> Y
Y -> X
How to eliminate it when converting to CNF?
I don't think it's fine to add a rule to grammar (as in unit elimination)
X -> X, right, because it s basically another loop?
If X -> Y and Y -> X, the nonterminal symbols are interchangeable and you can safely replace all instances of either of the two with the other of the two, eliminating one of the two completely. As you also pointed out, rules of the form X -> X can be safely eliminated. So this grammar is equivalent to the one you give:
S -> PQT
R -> T
U -> aU | bX
P -> bQ
X -> SX | c
Q -> aRX
T -> U
And so is this one:
S -> PQT
R -> T
U -> aU | bY
P -> bQ
Y -> SY | c
Q -> aRY
T -> U

how to find the minimal cover for a set of functional dependencies?

I want to find the minimal cover for the following set of functional dependencies:
A -> BC
B -> C
A -> B
AB -> C
first step: Break down the RHS of each functional dependency into a single attribute:
A -> B
A -> C
B -> C
A -> B
AB -> C
then I will remove of the two A -> B, so we will get:
A -> B
A -> C
B -> C
AB -> C
second step: Trying to remove unnecessary attributes from the LHS of each functional dependency (whose LHS has 2 or more attributes):
for AB -> C , check if A is necessary by:
replace AB -> C with B -> C so if B+ contains C then A is unnecessary:
B+ = B (B -> C)
= BC (so A is unnecessary)
check if B is necessary by:
replace AB -> C with A -> C so if A+ contains C then B is unnecessary:
A+ = A (A -> B)
= AB (A -> C)
= ABC (so B is unnecessary)
now we have:
A -> B
A -> C
B -> C
third step: Trying to remove unecessary functional dependencies:
for A -> B check if A+ contains B without using A -> B:
A+ = A (A -> C)
= AC (so A -> B is necessary)
for A -> C check if A+ contains C without using A -> C:
A+ = A (A -> B)
= AB (so A -> C is necessary)
for B -> C check if B+ contains C without using B -> C:
B+ = B (so B -> C is necessary)
now we have:
A -> B
A -> C
B -> C
Finally, group the functional dependencies that have common LHS together:
A -> BC
B -> C
so we can say that these functional dependencies are the minimal cover of the set, is that true ? and how we can deduce the key(s) of the set?
To compute a canonical cover for F:
Use the union rule to replace any dependencies with common left-side.
so Combine A ->BC and A -> B into A -> BC
Set is now {A -> BC, B -> C, AB -> C}
A is extraneous in AB -> C
Check if the result of deleting A from AB -> C is implied by the other dependencies
Yes: in fact, B -> C is already present!
Set is now {A -> BC, B -> C}
C is extraneous in A -> BC
Check if A -> C is logically implied by A  B and the other dependencies
Yes: using transitivity on A -> B and B -> C.
Can use attribute closure of A in more complex cases
The canonical cover is: A ->B, B -> C
source:Korth,sudarshan DBMS book.

Given a relation with FDs, compute the closure of set of attributes

Consider the relation schema R = ABCDG with following functional dependencies (FD)
AB -> C
C -> A
BC -> D
ACD -> D
D -> EG
BE -> C
CG -> BD
CE -> AG
Compute closure of BD and CA.
How we can find them?
The closure of a set of functional dependencies, F, means all the functional dependencies logically implied by F. For example, given
BC -> D, and
D -> EG
we can apply Armstrong's axioms to derive
D -> E,
D -> G,
BC -> E,
BC -> G,
and so on.
When you've derived every FD implied by F, you have the closure of F with respect to R. In your case, you want to derive every FD logically implied by BD and CA.
As far as I know, every textbook on relational database theory includes one or more algorithms to compute the closure of a set of functional dependencies. Your best bet is to follow one of the algorithms in your textbook, if you have one.
Here is a simple algorithm for computing the closure of a set of attributes X:
Closure(X, F)
1 INITIALIZE V:= X
2 WHILE there is a Y -> Z in F such that:
- Y is contained in V and
- Z is not contained in V
3 DO add Z to V
4 RETURN V

Armstrongs Axioms Proof

I am having issues proving functional dependencies with Armstrong's Axioms. This one i'm struggling
with. Let R(A,B,C,D,E) be a relation schema and F = {A→CD, C→E, B→D} 1. Prove: F: BC-> DE
What I have:
1 Given B->D
1.
Augment C on 1, BC->DC
2.
Decomposition on 2, BC->D BC->C
3.
Transitivity on BC->C, BC->E
4.
Union on BC ->D and and 4, BC->DE
Unsure if this is a proper solution.
Also Prove: AC-> BD I don't think this can be proven.
Please Help!
your solutions are correct, apart from some apparent misspelling:
Given B->D, C->E
Augment C on 1: BC -> DC
Decomposition on 2: BC -> C (3.1), BC -> D (3.2)
Transitivity on 1, 3.1: BC -> C, C -> E: BC -> E
Union on 3.2 and 4: BC -> DE
alternatively:
B->D, C->E
augment(1.1, c): bc -> dc
augment(1.2, d): cd -> ed
trans(2, 3): bc -> de (note: bc -> dc <=> bc -> cd <=> cb -> cd <=> cb -> dc)
ac -> bd cannot be proven in general: inspecting the armstrong axioms you notice that for some X to appear on the rhs of a derived fd, it must occur on the rhs of one of the original fds, except for
X is a subset of some X' on the lhs of an original fd
or
the fd is derived by augmentation with X
1.) constitutes a constraint never mentioned.
if 2.) applies, X would appear on a lhs of the original fd too. the only way to eliminate X is by transitivity which requires X to appear on the rhs of one of the original fds.
take b as X to see that ac -> bd is unprovable.
Abbreviations:
Shorthand
Expansion
fd(s)
Functional dependency(/-cies)
lhs
Left-hand side (of an equation / a derivation )
rhs
Right-hand side

calculate candidate keys given functional dependencies

Given the following functional dependendies on a relation R(A B C D E F G)
AB → CF
BG → C
AEF → C
ABG → ED
CF → AE
A → CG
AD → FE
AC → B
I have worked out the candidate keys by using the method where you put the attribute in either a left, middle, right column depending if it is seen on the left hand side of a dependency, right hand side or both. Left means that the attribute is necessary, middle is unknown and right means not part of a key.
I got this:
L | M | R
--|---------|----
- | ABCDEFG | -
From here I worked out the closures for each individual attribute and the permutations: BC, BD, BE, BF, BG, CD, CF...
I found that only the closure of A and CF contain all attributes and therefore are candidate keys however the solution the problem also has BFG.
Can someone explain what I am doing wrong in calculating candidate keys?
Thanks
This algorithm tries to find shortcuts (pg 3), but in your case it doesn't find any. To determine whether any particular combination of attributes is a key, you try to figure out whether that combination determines every other attribute. In your case, you've done all the work; you're just missing something about BFG.
Logic Attributes
--
BFG -> BFG, ∴ ... { B FG}
BG -> C, ∴ BFG -> CF { BC FG}
BFG -> CF and CF -> AE, ∴ BFG -> AE {ABC EFG}
BFG -> AE, ∴ BFG -> A
BFG -> A and ABG -> ED, ∴ BFG -> ED {ABCDEFG}
So BFG is a candidate key.

Resources