Tags
 IOS SQL HTML C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6

# Minimum cost path in matrix using recursion

By : Ayoub Boulaich Dilal
Date : September 22 2020, 04:00 PM
may help you . If you write down the recursive calls that are executed during your first solution, you will see that the invalid answer (-2147483647) emerges from adding the last element of the matrix (1) to INT_MAX, which is the value returned by your first recursion exit case.
Below, min_cost(x, y)[z] denotes the call of min_cost with start_x=x, start_y=y, z being the index of this recursive call (first call, second call etc.)
code :
``````min_cost(0, 0)[0] -> min(1 + min_cost(1, 0)[1], 1 + min_cost(0, 1)[2])

min_cost(1, 0)[1] -> min(1 + min_cost(2, 0)[3], 1 + min_cost(1, 1)[4])
min_cost(2, 0)[3] -> INT_MAX
min_cost(1, 1)[4] -> 1
``````

Share :

## Finding minimum cost in a binary matrix

By : vijay
Date : March 29 2020, 07:55 AM
will be helpful for those in need This idea is due to Greig, Porteous, and Seheult. Treat the matrix as a capacitated directed graph with vertices corresponding to matrix entries and arcs from each vertex to its four neighbors, each with capacity b. Introduce two more vertices, a source and a sink, and arcs of capacity a: from the source to each vertex with a corresponding 0 entry, and to the sink from each vertex with a corresponding 1 entry. Find a minimum cut; the entries with value 0 after changes are the vertices on the source side of the cut, and the entries with value 1 after changes are the vertices on the sink side of the cut.
The cost of this cut is exactly your objective. Of the capacity-a from-source arcs, the ones crossing the cut correspond to changes from 0 to 1. Of the capacity-a to-sink arcs, the ones crossing the cut correspond to changes from 1 to 0. Of the capacity-b arcs, the ones crossing the cut correspond to those instances where there is an arc from a 0 to a 1.

## Find path with minimum cost and maximum length given a maximum cost

By : Christian Dragoş
Date : March 29 2020, 07:55 AM
it helps some times Idea 1:
In my opinion your problem is a variation of the pareto optimal shortest path search problem. Because you refer to 2 different optimality metrics:

## minimum cost path through a cost matrix with positive and negative cost

By : Inez Jester
Date : March 29 2020, 07:55 AM
hope this fix your issue Let's say that H[i, j] is the minimum health the player needs when starting from square (i, j). We are interested in H[1, 1], which is the minimum health needed from the starting square.
I assume that all values in the cost matrix M are integers. Therefore, the smallest positive health is 1.
code :
``````H[m, n] = max(1 - M[m, n], 1)
``````
``````H[m, i] = max(H[m, i+1] - M[m, i], 1)
H[j, n] = max(H[j+1, n] - M[j, n], 1)
``````
``````H[i, j] = min(max(H[i, j+1] - M[i, j], 1),
max(H[i+1, j] - M[i, j], 1))
``````
``````int[] H = new int[m, n];

H[m, n] = max(1 - M[m, n], 1);

// remember to loop backwards
for (int i = m-1; i >= 1; i--)
H[m, i] = max(H[m, i+1] - M[m, i], 1);
for (int j = n-1; j >= 1; j--)
H[j, n] = max(H[j+1, n] - M[j, n], 1);

// again, loop backwards
for (int i = m-1; i >= 1; i--)
for (int j = n-1; j >= 1; j--)
H[i, j] = min(max(H[i, j+1] - M[i, j], 1),
max(H[i+1, j] - M[i, j], 1));

return H[1, 1];
``````

## minimum cost path in matrix

By : Olga Mineeva
Date : March 29 2020, 07:55 AM
Does that help I don't think it's correct to return 0 when you fall off the edge of the grid. That makes it look like you've succeeded. So I think the 2 that you are erroneously reporting is the 1 in upper left plus the 1 in lower left, followed by a "successful" falling off the bottom of the grid. I recommend you adjust your what-to-return logic so it looks like this:
code :
``````if at right or bottom edge:
there is only one direction to go, so
return the result of going in that direction
else you do have options, so
return the minimum of the two choices, like you do now
``````

## Minimum cost path / least cost path

By : Jodan Louis
Date : March 29 2020, 07:55 AM
like below fixes the issue This should be possible much more efficiently by directly interacting with the skimage.graph.MCP Cython class. The convenience wrapper route_through_array isn't general enough. Assuming I understand your question correctly, what you are looking for is basically the MCP.find_costs() method.
Your code will then look like (neglecting imports)
code :
``````img = np.random.rand(400,400)
img = img.astype(dtype=int)
starts = [[1,1], [2,2], [3,3], [4,5], [6,17]]
ends = [[301,201], [300,300], [305,305], [304,328], [336,317]]

# Pass full set of start and end points to `MCP.find_costs`
from skimage.graph import MCP
m = MCP(img)
cost_array, tracebacks_array = m.find_costs(starts, ends)

# Transpose `ends` so can be used to index in NumPy
ends_idx = tuple(np.asarray(ends).T.tolist())
costs = cost_array[ends_idx]

# Compute exact minimum cost path to each endpoint
tracebacks = [m.traceback(end) for end in ends]
``````