Is 2 times Manhattan distance still an admissible in N puzzle searching problem using A star? - algorithm

I know Manhattan distance is an admissible heuristic function since it doesn't overestimate the cost of moving a tile to the correct location. But my question is
If I double h, say scale up each of the Manhattan distances by a factor of 2, or the factor of the corresponding tile value (e.g., h'=9*h if we are moving tile 9, 2*h if we move 2).
Is it still admissible? My feeling is it's not since it overestimate the true cost. So for this specific problem, is Manhattan distance the most dominant heuristic function possible?
taking this as an example, starting with
1 2 3
4 5 6
7 0 8
we want
1 2 3
4 5 6
7 8 0
Apparently we only need one move (swap 0 and 8) and guaranteed to find a solution, so admissible or non admissible at this point doesn't matter. But in general, does 2*MD an admissible heuristic function? How do we justify this?

Related

Minimum number of flips to get adjacent 1's in a matrix

Given a binary matrix (values of 0 or 1), adjacent entries of 1 denote “hills”. Also, given some number k, find the minimum number of 0's you need to “flip” to 1 in order to form a hill of at least size k.
Edit: For clarification, adjacent means left-right-up-down neighborhoods. Diagonals do not count as adjacent. For example,
[0 1
0 1]
is one hill of size 2,
[0 1
1 0]
defines 2 hills of size 1,
[0 1
1 1]
defines 1 hill of size 3, and
[1 1
1 1]
defines 1 hill of size 4.
Also for clarification, size is defined by the area formed by the adjacent blob of 1's.
My initial solution has to do with transforming each existing hill into nodes of a graph, and the cost to be the minimal path to each other node. Then, performing a DFS (or similar algorithm) to find the minimum cost.
This fails in cases where choosing some path reduces the cost for another edge, and solutions to combat this (that I can think of) are too close to a brute force solution.
Your problem is closely related to the rectilinear Steiner tree problem.
A Steiner tree connects a set of points together using line segments, minimising the total length of the line segments. The line segments can meet in arbitrary places, not necessarily at points in the set (so it is not the same thing as a minimum spanning tree). For example, given three points at the corners of an equilateral triangle, the Euclidean Steiner tree connects them by meeting in the middle:
A rectilinear Steiner tree is the same, except you minimise the total Manhattan distance instead of the total Euclidean distance.
In your problem, instead of joining your hills with line segments whose length is measured by Euclidean distance, you are joining your hills by adding pixels. The total number of 0s you need to flip to join two cells in your array is equal to the Manhattan distance between those two cells, minus 1.
The rectilinear Steiner tree problem is known to be NP-complete, even when restricted to points with integer coordinates. Your problem is a generalisation, except for two differences:
The "minus 1" part when measuring the Manhattan distance. I doubt that this subtle difference is enough to bring the problem into a lower complexity class, though I don't have a proof for you.
The coordinates of your integer points are bounded by the size of the matrix (as pointed out by Albert Hendriks in the comments). This does matter — it means that pseudo-polynomial time for the rectilinear Steiner tree problem would be polynomial time for your problem.
This means that your problem may or may not be NP-hard, depending on whether the rectilinear Steiner tree problem is weakly NP-complete or strongly NP-complete. I wasn't able to find a definitive answer to this in the literature, and there isn't much information about the problem other than in academic literature. It does at least appear that there isn't a known pseudo-polynomial time algorithm, as far as I can tell.
Given that, your most likely options are some kind of backtracking search for an exact solution, or applying a heuristic to get a "good enough" solution. One possible heuristic as described by Wikipedia is to compute a rectilinear minimum spanning tree and then try to improve on the RMST using an iterative improvement method. The RMST itself gives a solution within a constant factor of 1.5 of the true optimum.
A hill is composed by four sequences of 1's:
The right sequence is composed of r 'bits', the up sequence has u bits, and so on.
A hill of size k is k= 1 + r + l + u + d (1 central + sequences), where each value is 0 <= v < k.
The problem is combinatorial. For each cell all possible combinations of {r,l,u,d} that satisfy the former relation should be tested.
When testing a combination in a cell, you must count the number of the existing 1 in each value of the combination, they don't "flip". This will also skip early some other combinations.

Find shortest path in 2D weighted grid

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 :)

Pathfinding with destructible obstacles

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.

Admissible Heuristic Manhattan Distance

I recently started an introductory course to Artificial Intelligence and I have been given an assignment to implement an admissible heuristic function in Python that solves the 15-Puzzle with A* search.
I implemented the Manhattan Distance along with some other heuristics. The Python code worked just fine and the algorithm actually solves the problem but I have some doubts as to whether the Manhattan distance heuristic is an admissible for this particular problem.
According to theory a heuristic is admissible if it never overestimates the cost to reach the goal. That means that the heuristic is optimistic and the cost it returns is never greater than the actual one.
When the initial state is the following (0 signifies the empty slot):
1 2 3 4
0 6 7 8
5 9 10 12
13 14 11 15
my program solves the problem with 5 moves, but the sum of the Manhattan Distances of every misplaced tile is equal to 10 which is double the value of the actual cost. So the real cost is much less than the estimated one. Does this mean that the heuristic is not admissible or is there something wrong with my logic?
I thought about counting just the empty block's Manhattan distance but that would lead to states with zero estimated costs when the empty block is in its correct place but other tiles are misplaced.
The Manhattan Distance heuristic is admissible since it considers each tile independently (while in fact tiles interfere with each other). So it's optimistic.
In your example the sum of the distance from the goal position of all tiles is 5 (tiles 5, 9, 10, 11, 15 need one move each).

Algorithm to sort numbers

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.

Resources