I have a 2D grid, somtehing like this, but a little bit bigger
A 1 2 2 2
2 1 X 2 1
1 X X 1 2
1 2 X 1 1
2 2 1 B 2
I want to find the shortest path from A to B. Where is X, there is not possible to go. From 1 to 2 is cost 2, from 2 to 1 is cost 1. I can move horizontally and vertically.
I imagined it like graph with evaluated vertexes and then I tried transform it to graph with evaluated edges. Then i wanted to apply dijkstra algorithm on it. In one article I read, that it is possible to apply dijkstra directly to grid.
My question is if it is possible to apply dijkstra algorithm directly to weighted grid? If no, how can I make a graph from grid or adjacency matrix? But adjacency matrix is probably too large for my inputs.
Thank you very much for your answers :)
Related
Given n by n points and the distance d between these points, I need to find the corresponding undirected weighted graph that would result in these distances. I tried using Prim's algorithm to find the MST, however this set is of size n-1 and does not include the n needed edges. E.g. given n by n distances
0 3 5
3 0 4
5 4 0
I need to find the corresponding edges:
1 - 2 = 3
1 - 3 = 5
2 - 3 = 4
Which results in the graph:
3
1 --------- 2
\ /
\5 /4
\ /
\ /
3
However Prim's would return only the first 2 edges since a MST doesn't contain any cycles.
One graph that would result in these distances is the graph that has an edge from every node to every other node and the length of that edge is the distance according to the matrix. (I'm not sure what you mean by unweighted directed because the example you give appears to be undirected and I'm not sure what the difference is between weights and lengths here).
Another option would be to consider the distances in increasing order, as you have done with Prim's algorithm, and, as well as checking to see if the edge is required to connect its two ends, check to see if the minimum distance between those ends in the graph reconstructed so far is the same as the distance in the matrix. If it is not, add the edge even if the ends are connected in the graph so far.
I'm currently planning a game, but I have a hard time figuring the path finding algorithm and I was wondering if a generous soul would be able to help me with that.
Here's the scenario, I need an algorithm that calculate if it would be better to walk around obstacle or destroy them. I gave a "difficulty" to each tile of my game. For exemple a basic tile is 1 and obstacle can be between 5-100.
Here is some exemples. I must move from Red Square to Blue square. If I put an obstacle on the way I should get something like this :
Explanation : Left or Right path is only 3 difficulty and the obstacle is 5. So it's better to walk around.
Second example :
Explanation : The algorithm as 3 choice, break the obstacle or walk around by left or right because it's the same difficulty level.
Last example :
Explanation : The algorithm must be able to find a "weak spot" and be able to walk to it and destroy it.
I'll continue to work on it. Hope you can give me some guidance.
You can use Dijkstra's algorithm. To use the algorithm we'll define the following graph. Each square will be a node in the graph. Between two adjacent squares we'll define 2 directed edges. The weight of an edge will be the number written on the pointed node (e.g. for an edge (a,b) the weight will be the number written on square b).
Then, by running Dijkstra's algorithm on the graph defined above we'll get the shortest path.
You can do this with a very minor variation (actually no variation at all, you just interpret differently what is an arc cost) of Dijkstra's algorithm.
Basically, you can reach the "inside" of the obstacled square at the cost of its destruction, so you have a graph where all nodes are connected to all their neighbours (i.e., the obstacle does not "disconnect" the intervening nodes).
So in this case, going from A to B,
1 1 A 1 1 1
1 1 1 1 1 1
2 5 5 5 2 1
1 1 B 1 1 1
the costs would be
2 1 A 1 2 3
3 2 1 2 3 4
5 7 6 7 5 5
6 7 B 7 6 6
and you would go straight downwards.
In most tree or graph problems i tried to solve,the input is generally the entire tree or graph structure in a node1->leafs or node1->adjacent nodes format.
Is there any list of commonly used structures to save this data in memory which later helps for the intended algorithm.For example:
Say i have a list of graph nodes like:
1 3 8 2 4.....# 1 is connected to 3 8 2 4...nodes
2 5 1 3... # 2 is connected to 5 1 3...nodes
3 1 2... #likewise
. ...
8 ......
so if i want to use the random contraction algorithm (in which i will have to contract edges say i contract 1 and 8..i use a multi-linked list structure in which each node on the adjacency list points to its corresponding row i.e.8 in the first line points to the 8th node.
Now the question,why i chose this structure to store data?
contracting is effectively making 1 and 8 one single entity,
so i read 1's adjacency list starting from 3 and go to 3rds adjacency list change 1 to 8 and next 8's row make 1 to 8 now go to 2's list change 1 to 8....and finally i append 1s list to 8 and remove duplicates..Yep,so finally 1 is deleted from graph after contracting 1 and 8
I want to know all the usually or rarely used structures for storing trees and graphs,if associated with algos the algo name as well?Thank You
One common way to store graphs is to use an n-by-n matrix, where n is the number of vertices in the graph. If you simply wanted to store the adjacency, if X is the matrix, then X[i][j] = 1 if vertex j is reachable from vertex i, and 0 otherwise. You could also store edge costs or edge capacities in this manner. The disadvantage is of course the amount of memory being used, O(n^2) instead of O(n+m) where m is the number of edges, but the advantage is O(1) lookup for every possible vertex pair.
Floyd's algorithm for solving the All Pairs Shortest Paths problem can naturally make use of such a matrix, as well as more complex sub-cubic algorithms for solving various graph paths problems that utilize faster matrix multiplication over a ring.
I have different numbers which needs to be sorted in ascending order by exchanging each number with 0.
The resultant number should be obtained only from exchanging each number with 0.
for eg, I have 3x3 matrix,
3 4 1
2 5 0
6 8 7
In above matrix, the number should be exchanged with 0 to make an ascending order. From above, only 5,7 and 1 are exchanged with 0 at first step. The final output should be like this
1 2 3
4 5 6
7 8 0
What is an optimal solution to achieve this.
Thanks
This is an easier version of the famous 15-puzzle.
The general way to approach such a problem is to model it as states graph, and run a shortest-path algorithm in order to find a path from the source (given board) to the target (sorted board).
The states graph is G=(V,E) where: V= { all possible boards } and E={(u,v) | can change board u to v with a single swap }`.
You can run BFS or bi-directional BFS (since you have one source and one target), or even A* Algorithm with an appropriate admissible heuristic function to find the path on the states graph, which represents a series of swaps that yield a solution.
I'm practicing solving programming problems in free time. This problem I spotted some time ago and still don't know how to solve it:
For a given undirected graph with n vertices and m edges (both less than 2 × 106)
I need to split its vertices into as many groups as possible, but with one
condition: each pair of vertices from different groups are connected by edge.
Each vertex is in exactly one group. At the end I need to know the size of
each group.
I was proud when I came up with this solution: consider complemented graph of the original graph and use Disjoint-set data structure for it. It gives us the right answer (not difficult to prove). But it's only theoretical solution. With given constraints it's very very bad, not optimal. But I believe this approach can be somehow smartly fixed. But how?
Can anyone help?
EDIT: for a graph with vertices from 1 to 7 and 16 edges:
1 3
1 4
1 5
2 3
3 4
4 5
4 7
4 6
5 6
6 7
2 4
2 7
2 5
3 5
3 7
1 7
we have 3 groups with sizes: 1, 2 and 4.
These groups are: {4}, {5,7}, {1,2,3,6} respectively. There are edges connecting each pair of vertices from different groups and we can't create more groups.
I think the only ingredient you're missing is how to deal with sparse graphs.
Let's think about this in terms of finding the biggest possible complete graph where the only operation I can do is group a set of nodes (say v_1, ..., v_k) together and give the new supernode edges only to those nodes u that were connected to all of v_1, ..., v_k.
If your graph has fewer than n^2/4 edges, randomly sample n node pairs, noting which pairs are not joined by an edge. Union-find is an easy way to code this up. Now rebuild the graph using as groups the sets you found by this random sampling. Recurse on this reduced graph. (I'm not quite sure how to analyse this step, but I believe each sample-rebuild cycle reduces the graph size by at least a constant factor with high probability, so this whole process takes near-linear time.)
Once you have a fairly dense graph (at least n^2/4 edges), you can convert to an adjacency matrix representation and do exactly what you were suggesting --- check all node pairs, do a union whenever you see that two nodes aren't joined by an edge, and read off the sets.