5

In my assignment I have a MxN grid

e.g.

M = 5, N = 8

KKKK....
.###...X
.XX#...X
...#....
........

K - start point, X - finish point, # - obstacle

I need to find the smallest number of moves to carry packages from start points to finish points. We can only carry 1 package at a time and we can't move diagonally.

The answer for this example is 20.

My suggestion would be to implement A* algorithm and launch it for every possibility (calculate the smallest number of moves from each of the start points to each of the end points) and the pick the smallest one, considering the fact that 1 package covers 1 end point.

Is there a better way to implement that?

c0der
  • 18,467
  • 6
  • 33
  • 65
MarJan
  • 51
  • 4
  • can you show what you `have already tried` ? – Rafael Herscovici Jan 13 '19 at 00:29
  • @Dementic, I watched several videos, and started to implement it in C++, you can find my progress here - https://github.com/Martin11M/AAL_pathfinder, it is 1:34 AM at my place, so I am going to sleep. In my local repo I have a buggy implementation of A* and I don.t want to push it yet. Tomorrow I may start over using Java – MarJan Jan 13 '19 at 00:33
  • 3
    @Dementic This is language-agnostic, and OP suggests using a A* and launching it from every possibility which is a concrete solution proposal. – ggorlen Jan 13 '19 at 00:34
  • Can you use each start and end point only once? – Richard Jan 13 '19 at 00:39
  • @Richard, yes. In the example - we have 4 packages placed at the start points. After the task is finished, packages need to be on 4 target fields, one on each. The answer for this example is 20. I think that when we move package from the start field, we no longer consider it a start field and we can use it to move other packages – MarJan Jan 13 '19 at 00:41
  • if its language agnostic, maybe it belongs here: https://softwareengineering.stackexchange.com/ ? – Rafael Herscovici Jan 13 '19 at 05:15
  • 3
    @Dementic No, language-agnostic algorithm questions are [very much on topic](https://stackoverflow.com/help/on-topic) for SO. – ggorlen Jan 13 '19 at 06:25
  • @MarJan Do the packages block any other? In other words, are the middle `K` start positions unable to move until their neighbors vacate their start positions? – ggorlen Jan 13 '19 at 06:48
  • 1
    @ggorlen in this particular example the packages block each other. We can't move diagonally, and we don't have any free neighbours in this case – MarJan Jan 13 '19 at 09:46
  • @marjan: Are your boards always so small with so few sources and targets? – Richard Jan 13 '19 at 09:59
  • I think that you don't need to calculate explicitly the shortest distance for each K/X pair. If you run Dijkstra or A* for given point K, it calculates in principle shortest distance to all other vertices, so if you reach during the graph traversal (the phase where you pick the vertex with the least distance) an X point, you have found the closest X for given K so you can terminate the search algorithm. You could then repeat this for all K points... – ewcz Jan 13 '19 at 10:16
  • @Richard they can be any rational size with any amount of start and end points – MarJan Jan 13 '19 at 10:42
  • @ewcz is it considered in that solution, that we may not have any possibilities to move the package? as in my example. When we run it for one start point, our assumption would be that we can easily move it in every direction and other packages aren't in our way – MarJan Jan 13 '19 at 10:42
  • @MarJan: "Any size" includes unsolvable large problems for any algorithm. But I suppose you man that there are instances significantly larger than what you show here. If it's possible to limit the sizes your chances of finding a nice solution increase. – Richard Jan 13 '19 at 10:44
  • 1
    @Richard, my guess is that it will be tested for sth like 100x100 with 10-20 start spots max – MarJan Jan 13 '19 at 12:05
  • Do all finish points have to be reached or could more than one package go to a single finish point? – גלעד ברקן Jan 13 '19 at 12:36
  • @גלעדברקן each finish spot needs to be populated. In other words 1 package covers 1 field, we can't stack them one on another – MarJan Jan 13 '19 at 12:37

1 Answers1

1

Although my practical understanding of it is limited, I'll attempt to formulate the problem as a minimum-cost flow problem. Consider each starting point a source and each finish point a sink. The cost of sending a flow, f, over a particular edge is f * a, where a is the edge's cost. A customary way to handle multiple sources and sinks is to connect each group to another single instance.

Tentative formulation:

Call n the number of starting points or finish points.

  1. connect all starting points to a single source with flow n, where each edge has capacity and flow 1 and cost 0.

  2. connect all finish points to a sink with each edge having capacity 1 and cost 0.

  3. all other edges have infinite capacity (at least n seems to cover that) and cost 1.

  4. find min cost for achieving flow n through the network.

Diagram:

imaginary source
 with edge to each
  S with capacity 1
 / /|\
S1S-S1S2.2.2.2.
2       | | | 2   imaginary sink
. # # # .-.-.-T -- with edge to
2       | | | 1     each T with
.2T1T # .-.-.-T --  capacity 1
                      |   |
. . . # . . . .

. . . . . . . .

(Numbers in the diagram are the optimal flow through each edge, they add up to 20.)

גלעד ברקן
  • 23,602
  • 3
  • 25
  • 61
  • could you give me a practical example? since I can't really understand your diagram correctly. Pipes connect the start point and sinks, but what are "2" for? – MarJan Jan 13 '19 at 14:09
  • 1
    @MarJan the numbers in the diagram are the optimal flow through each edge (they add up to 20). An edge in the problem is any single allowed move. (I drew edges as lines where there was no expected flow.) Does that make sense? – גלעד ברקן Jan 13 '19 at 14:14
  • it does make sense, thanks for the clarification. Will try to implement it that way vs A*. Will compare them and then choose the better one. Thank You :) – MarJan Jan 13 '19 at 14:15
  • so my Maximum Flow should be equal n, where n is the number of Start/End Points? – MarJan Jan 23 '19 at 00:39
  • @MarJan I think find min cost for achieving flow `n` through the network (as suggested in step 4.). I don't think it's a "max flow" problem; rather, a [minimum-cost flow problem](https://en.wikipedia.org/wiki/Minimum-cost_flow_problem) – גלעד ברקן Jan 23 '19 at 00:59
  • 1
    @MarJan in the example, it seems to me the flow that's achieved is `n = 4` (number of starting or ending points), since 4 gets from the source to the sink. – גלעד ברקן Jan 23 '19 at 01:05