I have an undirected and unweighted graph with about a million nodes - visualized in a matrix format.
Representation of a sample graph:
The red cells are blocked.
My problem is to find the shortest distance between any two cells. The source and destination cells will be given one by one, and I need to return the shortest paths for all given cell pairs. The traversal is manhattan only (up,down,left,right -no diagonal).No path can go through the blocked cells.
I looked at the following:
- the Floyd-warshall algorithm: but it takes too much memory.
- Dijkstra's algorithm for all nodes, but it is too slow.
- A* could work, but would still be slow - if the shortest path needs to be found for all pairs.
Is there any other approach for it?
Since it's just a uniform matrix, I would think that there might be some pattern I am unaware of.
Can a BFS traversal be cached somehow, so that a search can resume at an already visited node rather than starting from scratch again?

If your graph is a 1000x1000 grid, then let every cell (x,y) be a "border cell" if x%100 = 0 or y%100 = 0.
This divides your graph into 100 regions separated by borders.
If you need to find a shortest path between points in the same region, then you can just use BFS within the region, and you will search at most 10K nodes.
When you need to find a path between different regions, then it will always consist of a path to a border node, maybe a path between border nodes, and then a path from a border node to the target.
You can accelerate the search by using Floyd-Warshall to precompute the shortest path distance between every pair of border cells. There are only about 20000 of those, so this will require storing about 200 million lengths.
Your BFS search will then be limited to edges within the source region, F-W paths between the source and target regions, and edges within the target region.
This will provide a speed-up of 20-50x for worst-case searches.