Problem
So, let's say one is a 2-dimensional array of integer values representing a map with a grid, for example: +-----+------+-----+-----+-----+ | 10 | 2 | 2 | 4 | 656 | +-----+------+-----+-----+-----+ | 234 | 165 | 724 | 759 | 230 | +-----+------+-----+-----+-----+ | 843 | 734 | 999 | 143 | 213 | +-----+------+-----+-----+-----+ | 242 | 2135 | 131 | 24 | 374 | +-----+------+-----+-----+-----+ | 159 | 464 | 155 | 124 | 151 | +-----+------+-----+-----+-----+ +-----+------+-----+-----+-----+ | 10 | 2 | 2 | 4 | 656 | +-----+------+-----+-----+-----+ | 234 | 165 | 724 | 759 | 230 | +-----+------+-----+-----+-----+ | 843 | 734 | 999 | 143 | 213 | +-----+------+-----+-----+-----+ | 242 | 2135 | 131 | 24 | 374 | +-----+------+-----+-----+-----+ | 159 | 464 | 155 | 124 | 151 | +-----+------+-----+-----+-----+
2d indices represent the coordinates of a cell on the map, and the values in the array represent the relative difficulty of crossing the terrain of this cell - for example, 999 can be thick blackberries, while 2,3,4 can be a slightly inclined path ... or something like that.
Now we want to find the easiest way from [x, y] on the grid to [q, r] on the grid (where the sum of the steps is the smallest possible, in other words)
Problem area
This needs to be launched in a modern browser, where a rather spartan map is displayed, and we will draw a line from [x, y] to [q, r] through all the alternating vertices after the user entered [Q, R]. Conveniently, [X, Y] is always the same (say [0,0] for simplicity)
So use the Jikstra algorithm or *!
So, my first instinct was modeling the array in the form of a graph, applying the Dijkstra algorithm and working from there. And in the case above, with a 5x5 grid, this works fine. I move each index of the array and use the value and adjacent values to generate a node with weighted edges for all its neighbors. This creates a graph in which I can apply the Jikstra algorithm to.
However, in practice, I will work with arrays up to 50,000 x 50,000 in size! This is 250 million!
Obviously, creating an on-the-fly graph to run the Dijkstrass algorithm is not applicable. My next idea was to pre-compute the paths (the dataset has been fixed), save them on the server and call back when we get the destination [q, r] ... but this is 250,000,000 paths ... even if I ran it in less than a second (which I don’t think it would be), it will take several years to calculate all the paths ...
I think I might need a different approach, but I'm not sure how I can do this?