What is the intersection of two languages with different alphabets? [closed] - theory

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I did some googling on this and nothing really definitive popped up.
Let's say I have two languages A and B.
A = { w is a subset of {a,b,c}* such that the second to the last character of w is b }
B = { w is a subset of {b,d}* such that the last character is b }
How would one define this? I think the alphabet would be the union of both, making it {a,b,c,d} but aside from that, I wouldn't know how to make a DFA of this.
If anyone could shed some light on this, that would be great.

From a set-theoretic perspective, each language is just a set of strings over some alphabets Σ1 and Σ2. If you intersect two languages, the only strings that can possibly be in the resulting set are those strings that are made of characters in Σ1 ∩ Σ2, since any characters not in that set must belong to strings purely in one set.
As for how to build a DFA for this - I would suggest starting off by answering this question - given any regular language L over alphabet Σ, how would you modify that DFA to have language Σ ∪ { x }, where x ∉ Σ? One way to do this would be to add in a new "dead state" with a transition to itself on all characters in Σ ∪ { x }, then to add a transition from every state in the DFA to the dead state on character { x }. You can then use this to transform the DFAs for the original languages over Σ1 and Σ2 to have alphabets Σ1 ∪ Σ2. Once you've done that, you can use the normal algorithm for intersecting two DFAs to compute their intesection and thus get back a DFA for the intersection of the two languages.
Hope this helps!

Related

How do programs that can find dictionary words within anagrams work? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Examples include http://www.thewordfinder.com/, http://www.anagram-solver.org/, or the various applications for "cheating" at anagram based games such as Words With Friends.
Where would one begin if they were looking to create an application with similar functionality using Swift?
Since this seems to be getting down-voted, can someone tell me where the best place to ask this question is?
Converting a word list into a prefix tree (especially one that uses extra memory to avoid sparse arrays) should allow one to efficiently check all permutations of an input word, since entire branches of the search can be eliminated quickly.
Let's consider a simplified example. Assume our dictionary contains 3 words: aab, abb, and abc. If we convert it to a prefix tree, that would look like:
a
ab
b
b
c
Where indentation indicates children of the row above. All the words in our dictionary start with an a, followed by either ab or b, and in the latter case followed by either b or c. As you can see, we don't branch on every letter, but rather on alternatives. This helps keep the size of the tree down.
Now, if we iterate over all permutations of our input word cba:
abc
acb
bac
bca
cab
cba
All permutations that start with a letter other than a can be eliminated quickly since our prefix tree has no matching entry at the root level. acb can be eliminated on the second check, meaning only abc would find a match. If you integrate the check into your algorithm for generating permutations, you could match incrementally and avoid generating partial permutations that don't match the current branch of the tree you're exploring.
The comment about avoiding sparse trees is to avoid having to use binary search to match children of the current branch. Rather, an array of 26 elements, indexed by the ascii values of the letters (pick upper or lowercase and be consistent), should allow very quick lookups, at the cost of additional memory.

How should I write this generic algorithm [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Suppose you have two strings. Each string has lines, seperated by a newline character. Now you want to compare both strings and then find the best method (shortest number of steps) by only adding or deleting lines of one string, to transform the second string in to the first string.
i.e.
string #2:
abc
def
efg
hello
123
and string #1:
abc
def
efg
adc
123
The best (shortest steps) solution to transform string #2 in to string #1 would be:
remove line at line position 3 ('hello')
add 'abc' after line
position 3
How would one write a generic algorithm to find the quickest, least steps, solutions for transforming one string to another, given that you can only add or remove lines?
This is a classic problem.
For a given set of allowed operations the edit distance between two strings is the minimal number of operations required to transform one into the other.
When the set of allowed operations consists of insertion and deletion only, it is known as the longest common subsequence edit distance.
You'll find everything you need to compute this distance in Longest common subsequence problem.
Note that to answer this question fully, one would have to thoroughly cover the huge subject of graph similarity search / graph edit distance, which I will not do here. I will, however, point you in directions where you can study the problem more thoroughly on your own.
... to find the quickest, least steps, solutions for transforming
one string to another ...
This is a quite common problem known as the (minimum) edit distance problem (or, originally, the specific 'The String-to-String Correction problem', by R. Wagner and M. Fischer), which is a non-trivial problem for the optimal (minimum = least steps) edit distance, which is what you ask for in your question.
See e.g.:
https://en.wikipedia.org/wiki/Edit_distance
https://web.stanford.edu/class/cs124/lec/med.pdf
The minimum edit distance problem for string similarity is in itself a subclass of the more general minimum graph edit distance problem, or graph similarity search (since any string or even sequenced object, as you have noted yourself, can be represented as a graph), see e.g. A survey on graph edit distance.
For details regarding this problem here on SO, refer to e.g. Edit Distance Algorithm and Faster edit distance algorithm.
This should get you started.
I'd tag this problem rather as a math problem (algorithmic instructions) rather than language specific problems, unless someone could guide you to an existing language (C) library for solving edit distance problems.
The fastest way would be to remove all sub-strings, then append (not insert) all new sub-strings; and to do "all sub-strings at once" if you can (possibly leading to a destPointer = sourcePointer approach).
The overhead of minimising the amount of sub-strings removed and inserted will be higher than removing and inserting/appending without checking if its necessary. It's like spending $100 to pay a consultant to determine if you should spend $5.

Producing all subsets of a set gievn a restriction [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
Assume we have a set of N items say, S = {t1, t2, t3}. I would like to produce all the possible subsets of S given the restriction that t1 must appear in every set. Therefore, all possible subsets of S are {t1}, {t1,t2}, {t1,t3}, and {t1,t2,t3}. How can I write a recursive function that takes two sets {t1} and {t2,t3} and returns the subsets listed above.
Also if I had 100s of subsets such as S, storage of all subsets becomes a problem. My program goes in iterations and at every iteration I only need to manipulate one subset from each set. Is there I way I can produce the subsets of a set in steps rather than all at once? i.e. every time I call next(S), I get a new subset.
Note I'm coding in C.
Your "restriction" amounts to the following
Remove all the elements from S that must appear in the final sets. Call the remaining set S` and the removed elements B.
Produce the powerset of S` taking the union with B for each item to add in the required elements.
The powerset is a standard recursive function and outside the scope of your question. Indeed, there are many examples of how to do so on Stack Overflow.

How to create an evaluation function for a TIC-TAC-TOE variant game [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm actually working on a board game which is a variant of the TIC-TAC-TOE game. The specifics of the game are the followings :
1. The game is played on a nxn board, with n variable.
2. A player wins if he succeeds in placing k alignments the first, k is variable.
3. An alignment is constituted of l marks (X or O) in a horizontal, vertical or diagonal. l is fixed.
4. If the nxn grid is full (no player can add a mark either X or O) and no player succeeded in placing k alignments so the game is drawn.
I'm using an minmax with alpha-beta prunning algorithm. This is my first program with artificial intelligence and I don't know exactly how to create the evaluation function to be used by the algorithm. I saw some examples on the net which use a material weighting to evaluate a position but I can't apply that in my case. Actually, I'm using a radom evaluation function which returns a value between -100 and 100.
float Conf_eval(Configuration c)
{
return (rand()%201)-100;
}
Any idea on how can I evaluate a given board configuration ?
This is thoroughly discussed in the book Artificial Intelligence - A Modern Approach
There are also excellent implementations available (this is java, there is also python, you can Google for more) based on the book series. Including for tic-tac-toe (and alpha-beta pruning agents).
If you're using the min-max algorithm with alpha-beta prunning, you can use a sorted "Actions" list to perform better in addition to your heuristic function (a trivial utility-function would be assigning 1 to a victory, 0 to a tie and -1 to a loss - these are all leaf nodes of the min-max expanded tree).
To sort the actions you can, for say, prefer actions that add your symbol(X, O) to a clear-to-victory path. This should eventually lead to better prunning.

lecture slide about AI [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
Below is a lecture slide about AI. I think it's a pseudo code about something.
But I don't know what these symbols mean. I even can't get the main points of this slide.
Please help me. Thank you :)
It's not pseudocode. Please check out Norvig and Russell, "Artificial Intelligence: A Modern Approach". This looks like effects of an action for a reflex agent but you need at least the previous slide and probably the previous few slides to determine that. It could also deal with the frame problem in some way.
At any rate, presumably given an action with a precondition p and an effect e and a state s, the set of effects are the four logic statements: x , not x, x and not x. The first two logic statements say x when x has the value 1 in the effect e, etc. The third and fourth logic statements say x if in the set of logic clauses phi then x has the value 1 in effect e and the state s entails (|=) the set of logic clauses phi.
So a pair precondition,effect is executable in a state s if and only if s entails p and the set of effects in s is consistent.
But as I said more information is actually needed.

Resources