Find path on a matrix to get max value - java

I know, this is an old problem, if given a matrix like:
[1 1 2 3,
2 3 4 4,
3 4 1 3,
2 1 3 4]
Start at a given position, from right to left, only move right or up or down, can not go back in same way and stop at right, find a path to get max value.
I am considering to use DP(May need try all possible path and calculate value), but it seems it will cost a lot of memory since it store all the possible solutions and may also be slow.
I am wondering if there are any other thoughts or ways to make a better solution? A quicker solution if possible?

I think that there is a way to do a DP and I just can't quickly find it. Because no one answered it so far I will give a graph method. Create a directed graph where an edge from A to B will be equal to the number in that vertex. It is clear from your description that it will not have any cycles. You will get a grid graph like this, but only directed:
Now get an vertex source which is somewhere on the right and connect it to the first layer (put all the edges equal to 0). The same with a destination, but it is on the left.
Now run longest path in directed acyclic graph

If you want to optimize for memory, you might try Backtracking. This would involve only storing the current path, and the path and value for the best solution yet.
Outline is:
You store a list of steps (right, up, down)
You go depth-first so to speak, try to make a new step if you can
If you can't, just go back one and change the step to the next possible direction. (Store this path if it's better than previously stored one).
If there is no next possible direction for the step, repeat 3.
If all possibilities are exhausted, return the stored best path.
Backtracking at wikipedia: https://en.wikipedia.org/wiki/Backtracking

Related

Find the possible shortest path between two vertices

So let's say I have a graph like this, I can only visit 6 vertices at a time, for example, I can visit maybe 123654 for the first time and when I move again I need to start at the end vertex I visited last time so for the example given I have to start at 4, then I can do 432567. The goal is to start from 1 and end at exactly 7 for my last element of any moves.
Is there any way to achieve this? I have been stuck on this problem for weeks now, so far my idea is to keep exploring all the possible routes I may find but I don't think it is a correct algorithm, is there a better idea?
Step 1. Find all the paths of the length 6 from vertex 1 (V1). You can use DFS for that:
123456
123654
125436
125634
I assume, that you can't visit the same vertex twice in the same "run". If you can you'll get a bigger list.
Step 2. Find all the paths of the length 6 from V7:
765432
765234
763452
763254
Step 3. Find a vertex you can reach in a single run from both V1 and V7
It's vertex number 4. Then you can construct two runs that let you go from V1 to V7:
123654
432567
Step 4. You can generalize this algorithm to an arbitrary graph.
Using DFS or BFS build a list of vertices reachable from V1.
Repeat this step for every vertex reachable from V1 until you reach V7 (or V[Last]).
What you need is to run a short DFS (6-vertex "runs") within a long DFS (vertices reachable after each run).

Finding shortest path from a list of points

If I have a list of points returned from a breadth-first-search through a type of 2D array maze in Java, how could I find the shortest path in that list of points?
For example, if my potential target points are [2, 4] and [6, 0] and I have a list of points that goes to each target point, how can I find out which route is shortest?
I'm thinking I might have to use Djikstra's Algorithm or something, but I'm unsure.
Thanks very much
You can use Dijkstra's algorithm here. Given the array maze you mentioned, the first step would be to get all the edges in your maze between 2 adjacent nodes, and the edge costs. For the nodes, you would need to know if each node has been visited, and the current cost of visiting that node.
Now, if you are not concerned with getting the shortest path, you might not need Dijkstra's. Instead, you can use a DP/Recursion approach to get possible paths from a source coordinate to a target coordinate. If you want to see a Dijkstra's implementation, this is something I had written. To get the shortest path, you would obviously need the distance between nodes.
To just find a path between 2 points, I would suggest something like this implementation. It includes both DP and recursion implementations and compares the running times for both (recursion can take very long to run for large datasets).
I feel this much should be enough to get you started. Let me know if you need some other information.
I didn't end up using Dijkstra's, but instead changed my breadth-first-search to add a "level" value or "distance" value from the origin point that would count upward with each visited node. The counts would stay consistent if the path ever branched, and since I already knew the end point, all it would take is to check out what the "count" was in my end points and compare.
Thank you for your help though! You'll get an upvote if the site lets me.
For those facing a similar problem:
I made a simple class called PointC which inherits from Point, but with an added "count" value. The count was updated appropriately with each step of the breadth first search, then the end points were compared at the end to obtain the most optimal path.

Java - Graph critical links

I have a graph in which i have two nodes (0 and 6) and i have to cut the least edges possible so that they aren´t connected. For example in this graph
Being the nodes 0 and 6 the least edges that i have to cut are 2-7 and 3-7.
My idea was finding the shortest path between the both using bfs, i find one (0-2-7-6) but then i don´t know how to find the other (0-3-7-6). Even then i have no idea how choose the edges to cut.
It would be nice if someone could give me some pointers on this matter.
This problem seems very similar to that of finding articulation nodes within a graph. The technical definition of an articulation point, or a biconnected component is a node whose removal would cause a graph to be split in two.
The discovery of articulated nodes from a graph is a largely solved problem and you can find more details about it here: http://en.wikipedia.org/wiki/Biconnected_component
It seems to me that the way you would like to approach a problem like this in general would something along these lines:
1. Find all articulation points
2. Do a bfs from each node and determine articulation points along the path
3. Split graph at the articulation point, choosing the side with minimal edges
4. Continue until the two nodes are not connected
In the above example, 7 is the only articulation point and so you would snip the edges between 7, 2 and 3 since there are only two edges between 7 and the 0-4 graph and 3 edges between 7 and the 5,6,8 graph.
There is a more established algorithm for doing this (read: one that I didn't come up with) called Karger's algorithm that can solve your problem, albeit in n^2 time.
That algorithm works by effectively joining adjacent nodes to each other until there are only two nodes and then by counting the number of edges left between the two nodes. The number of edges is then the minimum number of cuts required to split the graph.
The way you would implement Karger's algorithm in your problem would just need the caveat that you should always be joining nodes to the two nodes you want to split. Additionally, in order to be able to go backward to the original edges you need to cut you should keep a reference to which nodes the edges originally belonged to.
There's a great visualization of Karger's algorithm here: http://en.wikipedia.org/wiki/Karger%27s_algorithm
What you want is a min s-t cut. The usual way to find one in a general graph is to run an algorithm like push relabel with source 0 and sink 6, which computes a min s-t cut as a byproduct of computing a maximum flow.
Karger's algorithm finds a min cut, i.e., it minimizes over s and t as well as cuts. Since s and t are fixed for you, Karger's is not appropriate. An articulation vertex is a vertex whose removal disconnects the graph. You are interested in removing edges, not vertices.

Ideas to represent such a case

I am designing a flight simulation program and I am looking for ideas on how to properly
implement such requirement.
Kindly look at my picture below. The points represents location.
The idea is this, I wanted to properly create a data structure to best represent such scenario
in java such that
when I am in Point 1, how far I am from the last point which is Point 8?
Points 2, 3 and 5 are at the same distance from Point 8
From Point 1, I could traverse to Point 3 to Point 6 then 7 then eight that would equate to 4 Steps.
when I am in Point 0
I could traverse to Point 4 then 5 then 7 then reach point 8 which would equate to 4 steps also.
I just wanted to assist user to help them find different route.
Is this possible and which java data structure should best fit this requirement? Also any design ideas how to implement this?
Sorry if my question might be vague, I am just trying to get as much information as I can to properly handle such requirement.
What you have is a weighted graph where the weights represent the distance between the nodes (which is very common). You could easily implement this yourself (and it's a great way to learn!), but there are lots of java source code for this out there.
Of course, this is not a java data structure. It's simply a data structure (or a concept), used by everyone, everywhere.
Calculating steps and distances is very easy once you've implemented a weighted graph.
There are massive amounts of documentation on all of this, especially here on Stackoverflow.
This is a Shortest Path problem, a common Graph problem. The usual way to represent the data is an Adjancency List or Matrix:
An adjancency list keeps, for every node, all 1-step reachable destinations. This is often implemented as a Linked List. It's the proper choice if your graph is relatively sparse (i.e. few destinations per node).
An adjancency matrix is used for (very) dense graphs. Basically, you keep an NxN matrix of values (weights/costs, or yes/no booleans). Then, distances[i][j] represents the cost of travelling to j from i. An unavailable arc has a cost of INF (or some error value).
The problem itself is generally solved by Dijkstra's Algorithm.

The King's Maze

I'm practicing up for a programming competition, and i'm going over some difficult problems that I wasn't able to answer in the past. One of them was the King's Maze. Essentially you are given an NxN array of numbers -50<x<50 that represent "tokens". You have to start at position 1,1(i assume that's 0,0 in array indexes) and finish at N,N. You have to pick up tokens on cells you visit, and you can't step on a cell with no token (represented by a 0). If you get surrounded by 0's you lose. If there is no solution to a maze you output "No solution". Else, you output the highest possible number you can get from adding up the tokens you pick up.
I have no idea how to solve this problem. I figured you could write a maze algorithm to solve it, but that takes time, and in programming contests you are only given two hours to solve multiple problems. I'm guessing there's some sort of pattern i'm missing. Anyone know how I should approach this?
Also, it might help to mention that this problem is meant for high school students.
This type of problem is typically solved using dynamic programming or memoization.
Basically you formulate a recursive solution, and solve it bottom up while remembering and reusing previously computed results.
The simple approach (i.e. simplest to code) is try all the possible paths - try each first step; for each first step try each second step; for each first step/second step combination try each third step; and so on. However depending on how big the maze is this may take too long to run (or it may not).
Your next step is to think about how you can do this faster. The first step is usually to eliminate moves that you know can't lead to a finish, or can't lead to a finish with higher points than the one you already have. Since this is practice for a competition we'll leave you to do this work yourself.
Think "graph" algorithms: The Algorithm Design Manual

Categories

Resources