Union of Deterministic Context Free Language and Regular Language results? - union

Given L1 is Deterministic context free languages & L2 is Regular language.
L1 U L2 results DCFL or regular?
please give some examples with the context

The resulting language must be a DCFL. Intuitively, you could check if a string is in the union of a DCFL and a regular language by getting a DPDA for the DCFL, a DFA for the regular language, then running the two in parallel and seeing if either accept. You can simulate that process by using a variation of the product construction that shows that the regular languages are closed under union: build a DPDA with one state for each combination of a DFA state and a DPDA state, then structure the transitions so that they simulate following a transition from the DPDA and the DFA in parallel. You only need one stack for this, so the construction should work just fine.
Hope this helps!

l2=sigma* and L1=a^nb^n
l1 is dcfl and l2 is regular.but L1 union l2=l1 is dcfl but not regular

Related

Maximum minimum and mean in Datalog

I cannot figure how calculate a mean, maximum and minimum using Datalog declarative logic programming language.
Eg. Considering this simple schema
Flows(Stream, River)
Rivers(River, Length)
If I want
a) the mean length of the rivers,
b) the longest river,
c) and the river with less Streams
what are the right Datalog queries?
I have read the Datalog theory, but cannot figure how these simple in another language queries could be solved with Datalog and haven't found any similar sample.
NOTE
The datalog that I use is with basic arithmetic functions like y is z+1, y is z-1, y is z\1 or y is z*1, and you can use X<Y or Y>X statements, and negation, so theoretically should be possible to do this kind of interrogation in some way since It has enough expressive power.
Is negation supported or not? If so, we can do max (or min) as follows:
shorterRivers(R1, L1) :- Rivers(R1, L1), Rivers(R2, L2), L1 < L2.
longestRivers(R1, L1) :- Rivers(R1, L1), !shorterRivers(R1,L1).
"mean" will be harder to do, as it require "SUM" and "COUNT" aggregations.
Standard Datalog supports first-order logic only, which does not include aggregate functions. However, some datalog implementations, such as pyDatalog, supports aggregate functions as an extension.

Updating values in an array with logical indexing with a non-constant value

A common problem I encounter when I want to write concise/readable code:
I want to update all the values of a vector matching a logical expression with a value that depends on the previous value.
For example, double all even entries:
weights = [10 7 4 8 3];
weights(mod(weights,2)==0) = weights(mod(weights,2)==0) * 2;
% weights = [20 7 8 16 3]
Is it possible to write the second line in a more concise fashion (i.e. avoiding the double use of the logical expression, something like i+=3 for i=i+3 in other languages). If I often use this kind of vector operation in different contexts/variables, and I have long conditionals, I feel that my code is less concise and readable than it could be.
Thanks!
How about
ind = mod(weights,2)==0;
weights(ind) = weights(ind)*2;
This way you avoid calculating the indices twice and it's easy to read.
Starting your other comment to Wauzl, such powerful operation capabilities is the Fortran side. This is purely matlab's design that is quickly getting obsolete. Let's use this horribleness further:
for i=1:length(weights),if (mod(weights(i),2)==0)weights(i)=weights(i)*2;end,end
It is even slightly faster than your two liner because you are doing the conditional indexing twice on both sides. In general, consider switching to Python3.
Well, I after more searching around, I found this link that deals with this issue (I used search before posting, I swear!), and there is interesting further discussion regarding this topic in the links in that thread. So apparently there are issues with ambiguity when introducing such an operator.
Looks like that is the price we have to pay in terms of syntactic limitations for having such powerful matrix operation capabilities.
Thanks a lot anyway, Wauzl!

Find-S concept learning algorithm

I am implementing and analysing the Find-S Algorithm (which I understood quite well). However, for the testing part, I am not sure whether the order of the examples in the training set affect the output.
Is this known or still unproven?
The order of examples will not affect the output if the function which expands the hypothesis is associative -- that is, if f(f(h0),x1),x2) = f(f(h0,x2),x1) for all h0,x1,x2.
The order of instances will affect your output because when FIND-S try to maximally specific hypothesis, it looks attributes and their values. It is discussed in Tom Mitchell's Machine Learning Book under title of '2.4 FIND-S FINDING A MAXIMALLY SPECIFIC HYPOTHESIS'.

What is supported in First Order Logics which is not supported in Description Logic?

While studying description logics (DL), it is very common to read that it is a fragment of first order logics (FOL), but it is hard to read something explicitely on what is excluded from DL which is part of FOL, which makes DL (with all its dialects ALC, SHOIN etc...) decidable.
Or in another words, could you provide me some examples in FOL which are not expressible
through DL (and which are the reason for semi/non-decidability in FOL) ?
The following facts about description logics are closely related to decidability:
(a form of) tree-model property — this property is important for tableu methods;
embeddability into multimodal systems — which are known to be "robustly decidable";
embeddability into the so-called guarded fragments of FOL — see below;
embeddability into two-variables FOL fragments — which are decidable;
locality — see below.
Some of these facts are syntactical, while some are semantical. Here below are two interesting, decidability-related, and more or less syntactical characteristics of description logics:
Locality (from The Description Logic Handbook, 2nd edition, section 3.6):
One of the main reasons why satisfiability and subsumption in many Description Logics are decidable – although highly complex – is that
most of the concept constructors can express only local properties
about an element 〈...〉 Intuitively, this implies that
a constraint regarding x will not “talk about” elements which are
arbitrarily far (w.r.t. role links) from x. This also means that in
ALC, and in many Description Logics, an assertion on an individual
cannot state properties about a whole structure satisfying it.
However, not every Description Logic satisfies locality.
Guarded fragment (from The Description Logic Handbook, 2nd edition, section 4.2.3)
Guarded fragments are obtained from first-order logic by allowing the
use of quantified variables only if these variables are guarded by
appropriate atoms before they are used in the body of a formula.
More precisely, quantifiers are restricted to appear only in the form
     ∃y(P(x,y) ∧ Φ(y))
        or      ∀y(P(x,y) ⊃ Φ(y))
              (First Guarded Fragment)
     ∃y(P(x,y) ∧ Φ(x,y))
     or      ∀y(P(x,y) ⊃ Φ(x,y))
           (Guarded Fragment)
for atoms P, vectors of variables x and y and (first) guarded fragment
formulae Φ with free variables in y and x (resp. in y).
From these points of view, analyze the examples from #JoshuaTaylor's comments:
∀x.(C(X) ↔ ∃y.(likes(x,y) ∧ ∃z.(likes(y,z) ∧ likes(z,x))))
∀x.(C(x) ↔ ∃z.(favoriteTeacher(x,z) ∧ firstGradeTeacherOf(x,z)))
The reasons why DL is preferred to FOL for knowledge representation are not only decidability or computational complexity related. Look at the slide called "FOL as Semantic Web Language?" in this lecture.
As shown by Turing and Church, FOL is undecidable, because there is no algorithm for deciding if a FOL formula is valid. Many description logics are decidable fragments of first-order logic, however, some description logics have more features than FOL, and many spatial, temporal, and fuzzy description logics are undecidable as well.

Steps to creating an NFA from a regular expression

I'm having issues 'describing each step' when creating an NFA from a regular expression. The question is as follows:
Convert the following regular expression to a non-deterministic finite-state automaton (NFA), clearly describing the steps of the algorithm that you use:
(b|a)*b(a|b)
I've made a simple 3-state machine but it's very much from intuition.
This is a question from a past exam written by my lecturer, who also wrote the following explanation of Thompson's algorithm: http://www.cs.may.ie/staff/jpower/Courses/Previous/parsing/node5.html
Can anyone clear up how to 'describe each step clearly'? It just seems like a set of basic rules rather than an algorithm with steps to follow.
Maybe there's an algorithm I've glossed over somewhere but so far I've just created them with an educated guess.
Short version for general approach.
There's an algo out there called the Thompson-McNaughton-Yamada Construction Algorithm or sometimes just "Thompson Construction." One builds intermediate NFAs, filling in the pieces along the way, while respecting operator precedence: first parentheses, then Kleene Star (e.g., a*), then concatenation (e.g., ab), followed by alternation (e.g., a|b).
Here's an in-depth walkthrough for building (b|a)*b(a|b)'s NFA
Building the top level
Handle parentheses. Note: In actual implementation, it can make sense to handling parentheses via a recursive call on their contents. For the sake of clarity, I'll defer evaluation of anything inside of parens.
Kleene Stars: only one * there, so we build a placeholder Kleene Star machine called P (which will later contain b|a).
Intermediate result:
Concatenation: Attach P to b, and attach b to a placeholder machine called Q (which will contain (a|b). Intermediate result:
There's no alternation outside of parentheses, so we skip it.
Now we're sitting on a P*bQ machine. (Note that our placeholders P and Q are just concatenation machines.) We replace the P edge with the NFA for b|a, and replace the Q edge with the NFA for a|b via recursive application of the above steps.
Building P
Skip. No parens.
Skip. No Kleene stars.
Skip. No contatenation.
Build the alternation machine for b|a. Intermediate result:
Integrating P
Next, we go back to that P*bQ machine and we tear out the P edge. We have the source of the P edge serve as the starting state for the P machine, and the destination of the P edge serve as the destination state for the P machine. We also make that state reject (take away its property of being an accept state). The result looks like this:
Building Q
Skip. No parens.
Skip. No Kleene stars.
Skip. No contatenation.
Build the alternation machine for a|b. Incidentally, alternation is commutative, so a|b is logically equivalent to b|a. (Read: skipping this minor footnote diagram out of laziness.)
Integrating Q
We do what we did with P above, except replacing the Q edge with the intermedtae b|a machine we constructed. This is the result:
Tada! Er, I mean, QED.
Want to know more?
All the images above were generated using an online tool for automatically converting regular expressions to non-deterministic finite automata. You can find its source code for the Thompson-McNaughton-Yamada Construction algorithm online.
The algorithm is also addressed in Aho's Compilers: Principles, Techniques, and Tools, though its explanation is sparse on implementation details. You can also learn from an implementation of the Thompson Construction in C by the excellent Russ Cox, who described it some detail in a popular article about regular expression matching.
In the GitHub repository below, you can find a Java implementation of Thompson's construction where first an NFA is being created from the regex and then an input string is being matched against that NFA:
https://github.com/meghdadFar/regex
https://github.com/White-White/RegSwift
No more tedious words. Check out this repo, it translates your regular expression to an NFA and visually shows you the state transitions of an NFA.

Resources