Kth shortest path in a given MAZE c++ - path

You are given a cost matrix of dimensions m X n.The problem is to find the minimal path from
top-left corner to some cell in the matrix.The total cost of a path is sum total of cost of all cells visited in the path.Only 2 moves are allowed: either to go down by a row, or move right by a column. You cannot leave the matrix at any time Also, some of the cells are marked as obstacles and cannot be stepped upon.
Several queries of the form: tx ty k are to be answered. The output for this query should be kth
minimum cost of a path from top-left corner to cell indexed at ty column in tx row
CONSTRAINTS:
1<= m,n <= 100 0 <= tx < m 0 <= ty <n 1 <= k <= 101
The obstacles in matrix are marked by "##" (quotes only for clarity)
The values in every cell that is not an obstacle, varies from -9 to 99 only.
tx and ty are both 0-indexed.
If k exceeds the total paths to cell (tx, ty) output "Not so many paths"
If (tx, ty) is an obstacle, output "Obstacle".
There will never be an obstacle on the top-left corner or bottom-right corner.
Input:
The first line contains the number of test cases T,
The first line of each test case contains two space-separated integers m and n.
The next m lines each contain n integers, denoting the matrix.
The next line contains an integer q, denoting the number of queries to come.
The next q lines each contain 3 space separated integers, tx ty and k
Output:
For each query output the kth shortest path
What i tried is backtracking which resulted in TLE.(time limit was given 1 sec).Whenever i reach the destination cell i stored that path cost in vector..and in the end after sorting the vector printed the Kth value in vector...But i need more efficient way to solve the problem ..can dynamic programming be used here..???

I have a dynamic programming solution for this.
Use a 3D matrix cost[M][N][K] where cost[x][y][i] will represent the ith shortest path to reach cell (x, y) from the top right hand corner.
Notice that each cell (x, y) can only be reached from cell (x-1, y) and (x, y-1). Therefore, we can process the cells in sequential order using for loops. This will ensure cell (x-1, y) and (x, y-1) will be processed before cell (x, y) for any cell (x, y).
for (int x = 0; x < m; x++) {
for (int y = 0; y < n; y++) {
//Process
}
}
Assuming we have already computed all the k shortest path for cell (x-1, y) and (x, y-1), you can compute the k shortest path for cell (x, y) by sorting the 2*k shortest paths leading to cell (x-1, y) and (x, y-1) by picking the smallest cost ones and adding the cost of reaching cell (x, y) to the picked paths.
This can be done using a conventional sorting algorithm yielding O(k log k) time per cell. However, since the k shortest paths for cell (x-1, y) and (x, y-1) are already sorted, one can actually sort them in O(k) running time by using a technique similar to mergesort if the time limit is really tight. This yields a best possible run time of O(nmk) and memory O(nmk).
Memory can be reduced by only saving the last row since only the x-1 row is required when computing the xth row, hence we can discard all the rows before the x-1 row.

Related

What is b / ||w|| in support vector machine?

In the picture of SVM from Wikipedia, at the lower left corner - pointed by the red arrow, there's b / ||w||. How is that calculated? In other words, why is the line in the picture b / ||w||? Thanks.
The line represents the affine subspace of points x, whose scalar product with the weight vector w yields b. As w is typically not a unit vector (i.e. need not have length 1), one has to divide b by the norm (the "length") of w to get the actual distance from the origin.
More precisely: imagine a vector x starting at the origin and reaching out to a point on the red line and let u be the unit vector in the direction of w, i.e. u = w / ||w||. Then the scalar product of x and u multiplied with u is the projection of x onto the unit vector u and its length corresponds to the distance of the red line from the origin. If you calculate the scalar product of <x,w> (written as x*w in the graphics) instead, you still get a projection on u, which has length b (that's actually how b is defined), so to get back the distance from the origin one has to calculate b/||w||.

How to calculate *VC dimension* of H in this case?

Let H denote the set of axis-parallel rectangles in Rn. Each
rectangle defines a binary classifier that assigns label +1 to points
inside the rectangle and label -1 to points outside the rectangle.
Each rectangle is defined by an interval [ai,bi] in each dimension 1
≤ i ≤ n. A pointx=(x1,...,xn)∈Rn is in the rectangle if ai ≤xi ≤bi
for1≤i≤n.
What is the VC dimension of H? Justify your answer.
It's typed using LaTex; this is the original text.
From my understanding, VC dimension is kinda largest integer d such that there exists a sample of size d that can be shattered by the hypothesis set H, but in this case, how could we calculate this if H is rectangles?
For an axis-aligned rectangle in Rn, the VC dimension is (at least) 2*n.
Consider first a rectangle in R1, which is just a pair of min/max values (a1, b1) along the x1 axis (not actually a rectangle). This pair of values has VC dimension of 2 because for any two points in R1, you can set (a1, b1) such that one, both, or neither of the two points is between them. But when you add a third point along that axis, there is no way you can include the two outer points in the range (a1, b1) without also including the middle point.
To make it easier to visualize, suppose the two points lie at x1 = -2 and x1 = 2, respectively. You can shatter the set by defining your rectangle bounds (a1, b1) as
(-3, 3) -> both points included
(-3, 1) -> first point only
(-1, 3) -> second point only
(-1, 1) -> neither point included
Now suppose you add an additional dimension so your space is in R2 (i.e., now it is a true rectangular region). Add two more points and let the x1 coordinates of the new points be 0 so they will always be included along the first dimension (for the rectangles defined above) and set the x2 coordinates of the new points to -2 and 2, respectively. Also, set the x2 coordinates of the first two points to 0.
When you set (a1, b1, a2, b2) to (-3, 3, -3, 3) you will include all 4 points. And you can obviously exclude any point by simply be reducing the magnitude of one of the 4 bounds from 3 to 1. Since you can include any subset of the 4 points by changing the R2 rectangle's corresponding bound, the VC dimension of the R2 rectangle is at least 4.
It should be fairly obvious that you can repeat this procedure for an arbitrary number of dimensions. Each time you add a new dimension xi, set the xi coordinate of all points to 0 except for the two new points you add for that dimension (they will be at xi = -2 and xi = 2, respectively).
Since you can shatter 2 additional points for every dimension, then for an axis-aligned rectangle in Rn, the VC dimension will be at least 2*n.

Vectorization of mulivariable gradient descent

I've been doing the homework 1 in Andrew Ng's machine learning course. But I'm stuck on my understanding of what he was talking about when vectorizing the multivariable gradient descent.
his equation is presented as follows:
theta := theta - alpha*f
f is supposed to be created by 1/m*sum(h(xi)-yi)*Xi where i is the index
now here is where I get confused, I know that h(xi)-y(i) can be rewritten as theta*xi where xi represents a row of feature elements (1xn) and theta represents a column (nx1) producing a scalar which I then subtract from an individual value of y, which I then multiply by Xi where Xi represents a column of 1 features values?
so that would give me mx1 vector? which then has to be subtracted from an nx1 vector?
is it that Xi represents a row of feature values? and if so how can I do this without indexing over all of these rows?
I'm specifically referring to this image:
I'll explain it with the non-vectorized implementation
so that would give me mx1 vector? which then has to be subtracted from
an nx1 vector?
yes it will give you m x 1 vector, but instead to be subtracted from n x 1 vector, it has to be subtracted from m x 1 vector too. How?
I know that h(xi)-y(i) can be rewritten as theta*xi where xi
represents a row of feature elements (1xn) and theta represents a
column (nx1) producing a scalar
you have answer it actually, theta * xi produce a scalar, so when you have m samples it will give you a m x 1 vector. If you see carefully in the equation the scalar result from h(xi) - y(i) is multiplied with a scalar too which is x0 from sample i (x sup i sub 0) so it will give you a scalar result, or m x 1 vector if you have m samples.

Implementing convolutional neural network backprop in ArrayFire (gradient calculation)

I modified equation 9.12 in http://www.deeplearningbook.org/contents/convnets.html to center the MxN convolution kernel.
That gives the following expression (take it on faith for now) for the gradient, assuming 1 input and 1 output channel (to simplify):
dK(krow, kcol) = sum(G(row, col) * V(row+krow-M/2, col+kcol-N/2); row, col)
To read the above, the single element of dK at krow, kcol is equal to the sum over all of the rows and cols of the product of G times a shifted V. Note G and V have the same dimensions. We will define going outside V to result in a zero.
For example, in one dimension, if G is [a b c d], V is [w x y z], and M is 3, then the first sum is dot (G, [0 w x y]), the second sum is dot (G, [w x y z]), and the third sum is dot (G, [x y z 0]).
ArrayFire has a shift operation, but it does a circular shift, rather than a shift with zero insertion. Also, the kernel sizes MxN are typically small, e.g., 7x7, so it seems a more optimal implementation would read in G and V once only, and accumulate over the kernel.
For that 1D example, we would read in a and w,x and start with [a*0 aw ax]. Then we read in b,y and add [bw bx by]. Then read in c,z and add [cx cy cz]. Then read in d and finally add [dy dz d*0].
Is there a direct way to compute dK in ArrayFire? I can't help but think this is some kind of convolution, but I've been unable to wrap my head around what the convolution would look like.
Ah so. For a 3x3 dK array, I use unwrap to convert my MxN input arrays to two MxN column vectors. Then I do 9 dot products of shifted subsets of the two column vectors. No, that doesn't work since the shift is in 2 dimensions.
So I need to create intermediate arrays of 1 x (MxN) and (MxN) x 9 in size, where each column of the latter is a shifted MxN window of the original with a pad border of zeros of size 1, and then do a matrix multiply.
Hmm, that requires too much memory (sometimes.) So the final solution is to do a gfor over the output 3x3, and for each loop, do a dot product of the unwrapped-once G and the unwrapped-repeatedly V.
Agreed?

How to find straight line segments nearest to a straight line segment?

consider an image matrix in which i have multiple line segments. And i have information's like start point, end points, length of the line segment, centroid and slope of all those line segments. In this scenario how do i find line segments that are nearest to a particular line segment. Also once i got nearest line segments is it possible to detect rectangles if they exist? .An example image is in this link sample.
The geometry of segment/segment distance is not so simple.
Imagine two line segments in general position, and dilate one of them. I mean, draw two parallel segments at a distance d and two half circles of radius d centered on the endpoints. This is the locus of constant distance d from the segment. You need to find the smallest d such that the other segment is hit.
You can decompose the plane in three areas: the band between the two perpendiculars through the endpoints, and the two outer half planes. You can clip the other segment in these three areas and process the clipped segments separately.
Inside the band, either the segments intersect and the distance is zero. Or they don't and the distance is the shortest distance between the line of support of the first segment and the endpoints of the other.
Inside the half planes, the distance is the shortest of the distances between the considered endpoint and both endpoints of the other segment, and the distance between the endpoint and the other segment, provided then endpoint projects inside the other segment.
ALTERNATIVE:
Maybe it is easier to use the parametric equations of the two segments and minimize the (squared) distance, like:
Min(p, q) ((Xa (1-p) + Xb p) - (Xc (1-q) + Xd q))^2 + ((Ya (1-p) + Yb p) - (Yc (1-q) + Yd q))^2 under constraints 0 <= p, q <=1.
First, you have to encode all the points in homogeneous coordinates [x, y, 1]T since this creates a symmetric relations between lines and points. Namely, in homogeneous coordinates the intersection of two lines l1 and l2 is a point p=l1xl2, where x means vector product. By the same, coin a line that passes through two points p1, p2 is l=p1xp2. Line that lie on a segment can be expressed as l=p1xp2=[a, b, c]T. The line equation then will be lT.p=0 or in Cartesian coordinates a*x+b*y+c=0
As for your task, there are two cases:
1. segments cross and then their intersection can be simply calculated as l1xl2;
2. segments don’t cross and then the closest points between two lines is the closest point between two of their 4 endpoints. To calculate 4 possible distances and choose the smallest distance between a line segment x1 and x2 and a point x0 use this formula: (x2-x1)x(x1-x0)/|x2-x1|
Let the segments be AB and CD, and running parameters along them p and q, such that 0 <= p, q <= 1.
Using vectors, the squared distance between any two points on the segments is given by:
D² = (AC - p AB + q CD)²
Let us minimize this expression by zeroing the derivatives wrt p and q:
AB.(AC - p AB + q CD) = 0
CD.(AC - p AB + q CD) = 0
When AB and CD are not parallel, this implies AC - p AB + q CD = 0, which gives you the intersection point by solving a 2x2 system, and the distance is zero.
But it can turn out that p (or q) falls out of the allowed range, let p < 0 (or p > 1). In this case, we recast the problem with p = 0 (p = 1). This amounts to finding the distance (A, CD).
D² = (AC + q CD)²
yielding
CD.(AC + q CD) = 0
even easier.
And if it turns out that q is also out of range, let q < 0, we end up with the distance (A, C):
D² = AC²
Similarly for the other out-of-range cases.
In case of parallel segments, the 2x2 system is indeterminate (both equations are equivalent). You need to solve:
CD.AC - p CD.AB + q CD² = 0
It suffices to try all four combinations with p/q = 0/1 and see if the left hand side takes different signs. This proves that there exists a solution and the distance is the same as distance (A, CD). Otherwise, the answer is one of the endpoint-to-endpoint distance.

Resources