r* expression NFA - nfa

I have found this image, which represents r* expression NFA.
My question is: there shouldn't be an arrow linking the second node to the third node? This way if I have a "rr" string, when the first symbol is read I get into the second node, but from there can't go anywhere because there aren't outgoing arrows.
http://imageshack.us/f/641/screenshot20111021at114.png/

The linked image implies a couple of assumptions.
Arrows labeled "E" are implied to mean "epsilon transition", a changing state that doesn't modify the current symbol. following such an arrow doesn't "consume any input"
The rectangular region labeled "R" is implied to mean "An automaton accepting R". if you reach the starting state on that region (the second circle from the left in the image), the boxed region will accept R for an arbitrary sublanguage. R is used as a variable, just as it is in the base regular expression R*. We don't see any arrows between the starting and ending states because we don't define what R means; it's variable, we could use anything.

Related

understanding constraint satisfaction problem: map coloring algorithm

I am trying to implement this recursive-backtracking function for a constraint satisfaction problem from the given algorithm:
function BACKTRACKING-SEARCH(csp) returns solution/failure
return RECURSIVE-BACKTRACKING({},csp)
function RECURSIVE-BACKTRACKING(assignment,csp) returns soln/failure
if assignment is complete then return assignment
var <- SELECT-UNASSIGNED-VARIABLE(VARIABLES[csp],assignment,csp)
for each value in ORDER-DOMAIN-VALUES(var,assignment,csp) do
if value is consistent with assignment given CONSTRAINT[csp] then
add {var = value} to assignment
result <- RECURSIVE-BACKTRACKING(assignment, csp)
if result != failure then return result
remove {var = value} from assignment
return failure
The input for csp in BACKTRACKING-SEARCH(csp) is a csp class that contains a) a list of states, b) the list of colors, and c) an ordered dictionary with a state as the key and the value is the list of neighbors of the state that cannot have the same color.
The problem is that I am having a hard time understanding how the algorithm works correctly. If anyone can give me a proper explanation of this algorithm, it would be very much appreciated. Some specific questions I have is:
if assignment is complete then return assignment
I assume that since assignment is inputted as an empty dictionary {}, that this will return the solution, that is, the dictionary that contains states and their colors. However, I don't understand how I can check if the assignment is complete? Would it be something like checking the size of the dictionary against the number of states?
var <- SELECT-UNASSIGNED-VARIABLE(VARIABLES[csp],assignment,csp)
The input csp class contains a list of states, I assume this could just be var equal to popping off a value in the list? I guess, what's confusing me is I'm not sure what the parameters (VARIABLES[csp], assignment, csp) are doing, given my input.
for each value in ORDER-DOMAIN-VALUES(var,assignment,csp) do
Again, confused on what the inputs of (var, assignment, csp) are doing exactly. But I assume that it'll go through each value (neighbor) in dictionary of the state?
if value is consistent with assignment given CONSTRAINT[csp] then
add {var = value} to assignment
result <- RECURSIVE-BACKTRACKING(assignment, csp)
if result != failure then return result
remove {var = value} from assignment
How do I properly check if value is consistent with assignment given constraints[csp]? I assume that constraints should be something that should be apart of my csp class that I haven't implemented yet? I don't understand what this if statement is doing in terms of checking. It would be quite useful if someone can clearly explain this if statement and the body of the if statement in depth.
So after rehashing some college literature (Peter Norvig's Artificial Intelligence: A Modern Approach), it turns out the problem in your hands is the application of Recursive Backtracking as a way to find a solution for the Graph Coloring Problem, which is also called Map Coloring (given its history to solve the problem of minimize colors needed to draw a map). Replacing each country in a map for a node and their borders with edges will give you a graph where we can apply recursive backtracking to find a solution.
Recursive backtracking will descend the graph nodes as a depth-first tree search, checking at each node for whether a color can be used. If not, it tries the next color, if yes, then it tries the next unvisited adjacent node. If for a given node no color satisfies the condition, it will step back (backtrack) and move on to a sibling (or the parent's sibling if no siblings for that node).
So,
I assume that since assignment is inputted as an empty dictionary {}, that this will return the solution, that is, the dictionary that contains states and their colors
...
Would it be something like checking the size of the dictionary against the number of states?
Yes and yes. Once the dictionary contains all the nodes of the graph with a color, you'll have a solution.
The input csp class contains a list of states, I assume this could just be var equal to popping off a value in the list?
That pseudocode syntax is confusing but the general idea is that you'll have a way to find out a node of the graph that hasn't been colored yet. One simply way is to return a node from the dictionary that doesn't have a value assigned to it. So if I understand the syntax correctly, var would store a node.
VARIABLES[csp] seems to me like a representation of the list of nodes inside your CSP structure.
I'm not sure what the parameters (VARIABLES[csp], assignment, csp) are doing, given my input
The assignment parameter is a dictionary containing the nodes evaluated so far (and the future solution), as mentioned above, and csp is the structure containing a,b and c.
Again, confused on what the inputs of (var, assignment, csp) are doing exactly. But I assume that it'll go through each value (neighbor) in dictionary of the state?
ORDER-DOMAIN-VALUES appears to be a function which will return the ordered set of colors in your CSP structure. The FOR loop will iterate over each color so that they're tested to satisfy the problem at that level.
if value is consistent with assignment given CONSTRAINT[csp] then
Here, what you're doing is testing the constraint with that value, to ensure it's true. In this case you want to check that any nodes adjacent to that node does not have that color already. If an adjacent node has that color, skip the IF and iterate the for loop to try the next color.
If no adjacent nodes have that color, then enter the IF body and add that node var with color value to the assigment dictionary (I believe {var = value} is a tuple representation, which I would write {var,value}, but oh well).
Then call the function recursive backtracking again, recursively.
If the recursive call returns non-failure, return its results (it means the solution has been found).
If it returns a failure (meaning, it tried all the colors and all of them happened to be used by another adjacent node), then remove that node ({var,value}) from the assignment (solution) array and move on to the next color. If all colors have been exausted, return failure.

Creating a state diagram from high level description

For the language L = {ww | w∈{a,b}*} over Σ={a,b}
For the high level description I wrote,
"1. Place marker in front
Sweeping back and forth, check if length is even, if odd reject.
Place marker at end
Sweeping back and forth, move each marker towards the middle. Compare each letter, erase if equal, reject is not.
If markers meet in the middle, accept."
I just have no idea how to turn this turing machine into a state diagram.

{ w | at every odd position of w is a 1}

The task is to construct a DFA for this language over the alphabet {0,1}.
I have constructed a DFA that consists of 4 states and that does not accept an empty word. However, in the answers they give a 3 state DFA that accepts it.
Why should my DFA accept an empty word if in the empty word there is no 1 at the odd position which means that it is not in the language?
The only requirement is that any symbol at an odd position must be 1. There is no requirement for a particular number of symbols, and specifically not that there be at least one.
Therefore, a DFA with an initial state where 0 leads to a rejection state and where 1 leads to a second state which accepts either symbol and returns to the start would be an acceptable answer, and would accept the empty string. This would be a three-state machine:
I think you are confused why should an empty string be a part of a mentioned set.
Let's take a look at another example. Consider you have a set of all possible strings having every character equal to 0. Such strings would be 0, 00, 000, 00000, etc. What about an empty string *? It actually pertain to this set as well. Empty string does not violate the definition of the set.
Compare this example with yours. You should check every odd position of the string and if you'll find anything other than 1 you should say that it is not an element of you set. It is not said anything about whether a string should have an odd position to be checked.

Regex that does NOT match something I matched before

As part of a question I asked earlier today, my goal is to validate all the moves a rook can make in chess notation.
This consists of:
The letter R
An optional disambiguation, the source of the problem (discussed in detail later)
An optional x to indicate a capture was made
The square to which the rook moved (the columns ["files" in chess] are lettered a-h and the rows ["ranks"] are numbered 1-8)
Disregarding disambiguation, we have the simple
/Rx?[a-h][1-8]/
Disambiguation
It often happens that two rooks can move to a square, and one does. When this happens, a disambiguating letter or number is used. So, if two rooks are on d3 and h5, and the one on h5 moves to d5, it is written Rhd5. Similarly, a rook on d8 moving to d3 when another rook is on d1 is written R8d3.
Files take precedence over ranks. In the first example, if the rook on d3 moved to d5, it could be disambiguated as R3d5 or Rdd5. Only the latter is correct.
The limits on rook disambiguation are:
Any letter may be used for file disambiguation, and
Any number may be used for rank disambiguation, but the number of the square moved to must not be 1 or 8 (R3d1 is not valid because of files' precedence over ranks and should be Rdd1), and it must not be the same number as the number of the square (R3d3 is also invalid)
With the above in mind, I constructed this:
/R([a-h]?x?[a-h][1-8]|([1-8])x?[a-h][2-7&&[^\1]])/
The problem lies in the last characters, [2-7&&[^\1]]. Ruby interprets [^\1] literally, that is as all characters other than \ or 1. If I try putting the \1 outside the brackets ([2-7&&[^]\1]), Ruby complains about the character class with no elements. And if I use an arbitrary placeholder that will never occur, say "z" ([2-7&&[^z]\1]), it doesn't work (I can't explain why)
So how can I use grouping to NOT match what I matched before?
Your question is long and dense, so I will address the core question and let you implement the technique:
How can I use grouping to NOT match what I matched before?
We'll proceed step by step. The following is not an exact chess example, but an illustration of how to accomplish what you want.
Let's say I want a string that matches letters a through h. My regex is ^[a-h]$
Next I want to match a digit and a dash. My regex becomes ^[a-h][0-9]-$
Next I want to match a letter, but not the one we matched before. My regex becomes ^([a-h])[0-9]-(?!\1)[a-h]$, where the ([a-h]) captures the first letter to Group 1, and the negative lookahead (?!\1) asserts that what follows is not the content of what was matched by Group 1 (i.e., it is not that letter).
Let's add a final digit just for balance: ^([a-h])[0-9]-(?!\1)[a-h][0-9]$. This will match a1-b2 but not a1-a2.
Let me know if you have any questions.
Reference
Lookahead and Lookbehind Zero-Length Assertions
Mastering Lookahead and Lookbehind

"Squares" Logic Riddle Solution in Prolog

"Squares"
Input:
Board size m × n (m, n ∈ {1,...,32}), list of triples (i, j, k), where i ∈ {1,...,m}, j ∈ {1,...,n}, k ∈ {0,...,(n-2)·(m-2)}) describing fields with numbers.
Output:
List of triples (i, j, d) showing solved riddle. Triple (i, j, d) describes square with opposite vertices at coordinates (i, j) and (i+d, j+d).
Example:
Input:
7.
7. [(3,3,0), (3,5,0), (4,4,1), (5,1,0), (6,6,3)].
Output:
[(1,1,2), (1,5,2), (2,2,4), (5,1,2), (4,4,3)]
Image:
Explanation:
I have to find placement for x squares(x = fields with numbers). On the
circuit of the each square, exactly at one of his corners should be
only one digit equal to amount of digits inside the square. Sides of
squares can't cover each other, same as corners. Square lines are
"filling fields" so (0,0,1) square fills 4 fields and have 0 fields inside.
I need a little help coding solution to this riddle in Prolog. Could someone direct me in the right direction? What predicates, rules I should use.
Since Naimads life is worth saving(!), here is some more directive help.
Programmers tend to work out their projects either in bottomp-up (starting from "lower" levels of functionality, building up to a complete application) or top-down (starting with a high level "shell" of functionality that provides input/output, but with the solution processing stubbed in for later refinement).
Either way I can recommend an approach for Prolog progrmming that has been championed by the Ruby programming community: Test Driven Development.
Let's pick a couple of functional requirement, one high and one low, and discuss how this bit of philosophy might help.
The high level predicate in Prolog is one which takes all the input and output arguments and semantically defines the problem. Note that we can write down such a predicate without giving any care as to how the solution process will get implemented:
/* squaresRiddle/4 takes inputs of Height and Width of a "board" with
a list of (nonnegative) integers located at cells of the board, and
produces a corresponding list of squares having one corner at each
of those locations "boxing in" exactly the specified counts of them
in their interiors. No edges can overlap nor corners coincide. */
squaresRiddle(Height,Width,BoxedCountList, OutputSquaresList).
So far we have just framed the problem. The useful progress is given by the comment (clearly defining the inputs and outputs at a high level), and the code at this point just guarantees "success" with whatever arguments are passed. So a "test" of this code:
?= squaresRiddle(7,7,ListIn,ListOut).
won't produce anything except free variables.
Next let's give some thought to how the input list and output list ought to be represented. In the Question it is proposed to use triples of integers as entries of these lists. I would recommend instead using a named functor, because the semantics of those input triples (giving an x,y coordinate of a cell with a count) are subtly different from that of the output triples (giving an x,y coordinate of upper left corner and an "extent" (height&width) of the square). Note in particular that an output square may be described by a different corner than the one in the corresponding input item, although the one in the input item needs to be one of the four corners of the output square.
So it tends to make the code more readable if these constructs are distinguished by simple functor names, e.g. BoxedCountList = [box(3,3,0),box(3,5,0),box(4,4,1),box(5,1,0),box(6,6,3)] vs. OutputSquaresList = [sqr(1,1,2), sqr(1,5,2), sqr(2,2,4), sqr(5,1,2), sqr(4,4,3)].
Let's give a little thought to how the search for solutions will proceed. The output items are in 1-to-1 correspondence with the input items, so the lists will have equal length in the end. However choosing the kth output item depends not only on the kth input item, but on all the input items (since we count how many are in the interior of the output square) and on the preceding output items (since we disallow corners that meet and edges that overlap).
There are several ways to manage the flow of decision making consistent with this requirement, and it seems to be the crux of this exercise to pick one approach and make it work. If you are familiar with difference lists or accumulators, you have a head start on ways to do it.
Now let's switch to discussing some lower level functionality. Given an input box there are potentially a number of output sqr that could correspond to it. Given a positive "extent" for the output, the X,Y coordinates of the input can be met with any of the four corners of the output square. While more checking is required to verify the solution, this aspect seems a good place to start testing:
/* check that coordinates X,Y are indeed a corner of candidate square */
hasCorner(sqr(SX,SY,Extent),X,Y) :-
(SX is X ; SX is X + Extent),
(SY is Y ; SY is Y + Extent).
How would we make use of this test? Well we could generate all possible squares (possibly limiting ourselves to those that fit inside the height and width of our "board"), and then check whether any have the required corner. This might be fairly inefficient. A better way (as far as efficiency goes) would use the corner to generate possible squares (by extending from the corner by Extent in any of four directions) subject to the parameters of board size. Implementation of this "optimization" is left as an exercise for the reader.

Resources