how to compute the follow-position of a regular expression(R.E) from a parse tree - algorithm

I was computing the follow position of a regular expression R.E = (a|b)*abb
by using these algorithms. From given algorithm by using second property i found a problem and can not get the answer. How can be the i is a position in lastposition(n)
1.If n is a cat-node with left child C1 and right child C2 then for every position i in lastpos(C1) , all positions in firstpos(C2) are in
followpos(i).
2.If n is a star-node, and i is a position in lastpos(n) , then all positions in firstpos(n) are in followpos(i).
The fistpostion and the lastposition at every node computed first.

Related

Half cell boundary condition

I have a problem, I am solving set of equations via this numerical scheme
uN(i+1/2) = uP(i+1/2) + dt*f( uP((i+1/2), uP(i-1/2), P(i), P(i-1) )
uN ... velocity in New time step
uP ... velocity in Previous time step
P ... pressure in previous time step
i ... number of cell boundary
i+1/2 ... number of cell center
P0 is given as left boundary condition, enter code here, I dont know how to determine u1/2, original text proposes using half cell. But I am not sure how exactly formulate the equation for half cell.

what is the greedy approach of this?

suppose we have some 2D boxes in a container surrounded by water. Unfortunately there is a hole in left hand side wall of container. The height of this hole is more than height of all boxes.length of all boxes is 1 m and their height is an integer.all boxes are put in one line.in every second the amount of water which insert through container is one square meter(take care our problem is in 2D context).we want to find how long it takes till a given box is one meter under the water.
is there any greedy algorithm to efficiently solve this?
I think this algorithm can solve this in O(n).
from the i-th box we first go to the end of line to find the first box which is a higher than the i-th box.since differences between all boxes' height is at least 1 meter so water can't go further than this box.then we go from the i-th box to the beginning of the line.we save height of the i-th box in Max_Height variable.each time we find a box higher than this Max_Height we update Max-Height.so whenever we find a box shorter than the Max_Height we add (Max_Height - box's height) to our result.at the end we have computed area of all previous and next wells.so we just need to compute the distance between the first next higher box and the first previous higher box and add it to our result.
That's it.

Breadth First Search on Labyrinth in Matlab [duplicate]

This question already has an answer here:
Representing and solving a maze given an image
9 answers
I'm trying on implementing an breadth first algorithm, that solves a labyrinth. As an input I have a n*m binary matrix, where '1' stands for obstacle/wall and '0' for path/ free cell.
I do get how the algorithm generally works, but I'm struggling with how to store and proceed the information in matlab.
So basically I start with my start cell, and check all it's direct neighbours for obstacles. If they are free, I mark them as a potential path, and then I do the same recursively for all those cells again.
But i just can't figure out how to store the information, so that in the end i will only have one path.
any ideas?
You can use open and closed list like in the a star pathfinding algorithm. Check the all neighbours and if the neighbour is not a obstacle, put in the open list. Examine all neighbours and the neighbour which has a min cost, put it in the closed list. In the end, you have the optimal path in the closed list. Basically it is something like that..
This is a function that search a connected component in an unoriented graph by deep first search. BFS should be even easier to code.
function comp = findNodeComponentDFS(G, node)
%Find a connected component of the given node in the unoriented graph. Use
%Deep first search (Cormen, Rivest, Leiserson)
%G - connectivity matrix
%node - given node
%comp - contain the numbers of all nodes in the found connected component
%except the node itself
N = length(G);
white = ones(1,N);%unexamined vertices
grey = zeros(1,N);%vertices that are currently examining but with not all edges have been examined yet
black = zeros(1,N);%vertices with all the edges have been examined
prev = zeros(1,N);
current = node;
stop=false;
while (~stop)
grey(current) = 1;
white(current) = 0;
next = find(G(current,:) & white,1);
if (isempty(next))
black(current) = 1;
if (prev(current)==0)
stop = true;
else
current = prev(current);%back to previous vertice
end
else
prev(next) = current;
current = next;
end
end
comp = find(black);

Algorithm for Octree for nearest neighbor seach

Problem Statement: To find the nearest GRID ID of each of the particles using Octree.
Fig[1]:
Fig[2]:
I have a system of particles(~6k, movable) for which I need to check which grid point (rigid; in picture) is nearest to. Somebody have suggested me to go for Octree as it is fast(est) for 3D Grids.
Is this the correct Algorithm for recursive Octree to get the nearest grid point of the grid?
Get a input as point P Start coordinate C (first time it [0,0,0])
Start Size = [Sx, Sy, Sz]
Get all 8 mid point Mi = {M1,..,M8} get minimum distance of Mi and P
Say M get start position of M as Cn set size Sn = [Sx/8, Sy/8, Sz/8]
if distance of M and P is less than 2 * (Grid Space G):
5.1. Iterate all the grid points from Cn to Sn
5.2. Print least as result
else
6.1. set Start coordinate as Cn
6.2. set Size as Sn
6.3. Goto 1
Problem: The last iteration eat all speed if the particle is out or nearly on the border as it checks all A x B x C.
Please suggest if you have a better way to solve this problem.
There is no need to use an octree here. Octree is useful for the reverse problem (given a grid point, find the nearest particule) but completely useless here.
Assuming the size of a grid cell is (a, b, c), then the nearest grid point from (x, y, z) is (a*floor(x/a+0.5), b*floor(y/b+0.5), c*floor(z/c+0.5)).

A particular tiling of a square with rectangles

Suppose we have a square with side length S, and N copies of rectangular tile with length X and width Y. The program must show all the ways in which these copies can be arranged in a grid so that no two copies can touch each other.
By showing, I mean that it must show the set of coordinates of upper left corners of every copy in a grid.
I tried to do it in the following way:
Find a base case where I try to place every copy with 1 square separation.
For example, for 6 copies of a 1x2 tile on a 6x6 grid, the base case is
xx_xx_
______
xx_xx_
______
xx_xx_
______
Move the last tile to possible positions.
Return the last tile to the base case, move the tile before the last one to the possible position. Repeat Step 2.
Do it back for every tile.
Basically the problem is that I cannot find the cases where the difference in row or column numbers is 1 but they don't touch each other. For example, I cannot find this case:
xx____ This tile
___xx_ and this one has a difference in row numbers 1.
xx____
___xx_
xx____
___xx_
Can you suggest something? Or maybe a more efficient algorithm?
NOTE: I try to implement it in Prolog.
You'll find that the problem lends itself to constraint programming (which isn't that far from Prolog what you are trying to use):
given S,
you want a set of pairs A={(x,y)} where x elem {1..S} and y elem {1..S} and x and y denotes the top left corner of your tile,
such that for all (x,y) (x+1, y) is not in A and (x+2, y) is not in A and (x+3,y) is not in A and (x,y+1) is not in A ...more constraints, meaning that if you have a tile on (x,y) no tile 'starts' on (x+1,y) or (x+2,y) or (x+3,y) ie they don't overlap and don't touch.
The beauty is that with the above you declaratively specified the problem, and then your favourite constraint solver (I'd go with GECODE) goes and finds all solutions for you. And if your specification is not complete you get tiles which touch or overlap in unexpected ways, you can modify the specification and don't have to reinvent the wheel. This will work for quite large instances of the problem...when you can add clever ways of pruning the search tree, you only have to start inventing clever algorithms if you need large S. Sort of pay as you go.
You can use a bitmask for the previous row each time you fill a specific row. For example:
If previous row is like:
XX----
Then have a bitmask like 110000. To fill next row, see that you don't use the places where there are 1's in the bitmask.
So you can do this:
for(int i=0;i<(1<<S);i++)
if(i & bitmask)
{
//can't place tile in this fashion as few tiles of previous row touch this row's tiles
continue;
}
else
{
//No conflicts between rows, but with in this row there could be touching tiles as in 111100
//Use a for loop to check if the given row has no two tiles touching
//Check if each string of 1's is of length X
//If all is well recursively call this function to fill the next row using i as the bitmask
}
I will let you figure out the actual implementation.

Resources