Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm for finding the best route from single source to multiple destinations in a matrix

Tags:

algorithm

Problem statement: We have an m * n matrix. The starting point is the top left cell. We can only go down or right in the matrix. Destinations are randomly chosen in the matrix. Now we need to find the best routine with the following constraints:

  1. Each node on the route could only have one parent node but could have at most two child nodes.
  2. minimize duplication routes. For example, if we have destinations look like below:

first example

Instead of using the routine on the left-hand side, we should reduce it to the right-hand side one.

  1. prefer going right then down unless going down is shorter than right.

In the example below, instead of choosing the left hand side solution, we should prefer right hand side one as it branches to (2, 1) down by move down from (2, 0) by 1 instead of going right by 2 from (0, 1).

second example

Other examples look like below, which are all best routines.

7 more examples

I'm working on this for a while. I've looked into some algorithms like transitive reduction and Dijkstra, but didn't figure them out. If you would like to give me some hint on algorithms that I could look into that would be great.

Thanks!


Edit 2:

I've received some ideas about Dijkstra algorithm and using dynamic programming. I think for Dijkstra algorithm if you could provide hints for converting this problem to a graph problem that would be great. I was looking into the algorithm and think the primary issue of it is that cells do not have to be visited. In the example below, if we remove one of the destinations, the whole routine would have a significant change comparing to the left-hand side map.

fourth example

For dynamic programming, I have a thought on how a node should join to the path. The priority should look like:

priority graph

But the problem is it fails to consider the problem with a dynamic view, which will output a wrong result.

like image 397
Dawen Shi Avatar asked Sep 04 '18 11:09

Dawen Shi


People also ask

Which algorithm might you use to find the shortest route between two destinations?

If the selected heuristic is optimal the computational complexity is reduced to O(n). That is why the A* algorithm is widely used for shortest path search.

What is the fastest path finding algorithm?

Dijkstra's algorithm is used for our fastest path algorithm because it can find the shortest path between vertices in the graph. The coordinates on the arena are considered as the vertices in the graph.

How BFS and DFS are helpful to find the shortest path between cities of a country?

BFS can be used to find a single source shortest path in an unweighted graph because, in BFS, we reach a vertex with a minimum number of edges from a source vertex. In DFS, we might traverse through more edges to reach a destination vertex from a source.


1 Answers

I think your examples could all fit with the following general algorithm: traversing left and up simultaneously — starting with the ends of the first row and column, followed by the ends of the second row and column, etc. — extend routes from each D encountered to the closest route, D or S (Manhattan distance in the N-NW-W arc, of course).

Example 7:

  1 2 3 4
1 S     D

2     D

3   D

4 D     D

  1 2 3 4
1 S-----D<
  |
2 |   D
  |
3 | D
  |
4 D     D
  ^

  1 2 3 4
1 S-----D
  |   |
2 |   D  <
  |
3 |-D
  |
4 D     D
    ^

  1 2 3 4
1 S-----D
  |   |
2 |   D
  |
3 |-D
  |
4 D-----D<
        ^

Example 5:

  1 2 3 4
1 S

2       D

3     D

4 D     D

  1 2 3 4
1 S      <
  |
2 |     D
  |
3 |   D
  |
4 D     D
  ^

  1 2 3 4
1 S
  |
2 |-----D<
  |
3 |   D
  |
4 D     D
    ^

  1 2 3 4
1 S
  |
2 |-----D
  |   |
3 |   D  <
  |
4 D     D
      ^

  1 2 3 4
1 S
  |
2 |-----D
  |   |
3 |   D--
  |     |
4 D     D<
        ^

Example 1:

  1 2 3 4
1 S

2     D

3   D   D

4     D

  1 2 3 4
1 S--
    |
2   --D  <
    |
3   D   D

4     D
    ^

  1 2 3 4
1 S--
    |
2   --D
    |
3   D---D<
      |
4     D
      ^
like image 158
גלעד ברקן Avatar answered Nov 09 '22 21:11

גלעד ברקן