Search code examples
c++multidimensional-arraydijkstrashortest-pathadjacency-matrix

Is it possible to create an adjacency matrix from a 2D array of nodes in C++?


So I have a raw file that is 250x200px that I've read into a 2D array like rawFile[250][200] with each pixel (each array index) acting as a node and each pixel value representing an elevation (think: topographical map). I want to find the shortest path using Dijkstra's algorithm from rawFile[0][0] to rawFile[250][200] with the distance cost being the absolute value of the difference of going from node1 to node2 plus the shortest path distance to the currently visited node. Each node can move in the four cardinal directions (N, E, S, W) assuming there is an adjacent node in the respective direction. I've read the pseudocode and various implementations that all require an adjacency matrix or adjacency list, which in this case would be adjMatrix[50000][50000]. However, I am struggling to figure out how I can populate the adjacency matrix from just the raw file.

Do any of you have any suggestions on solving this problem? Thanks!


Solution

  • Converting an image into an adjacency matrix is completely unnecessary; the only thing you need is the "neighbor-ness" information which is already implicitly present.

    The neighbors of any pixel i,j are represented by the pixels at (i-1,j), (i+1,j), (i,j-1), (i,j+1) subject to image boundaries. You don't need a matrix to encode that; any time the algorithm says to look at neighbors, just supply the neighboring pixels.