From Wikipedia:
Dijkstra's algorithm to find the shortest path between a and b. It picks the unvisited vertex with the lowest distance, calculates the distance through it to each unvisited neighbor, and updates the neighbor's distance if smaller. Mark visited (set to red) when done with neighbors. |
What's in the 2D array pre-loaded from the textfile? Since you've said the program isn't complete yet, its difficult to infer with certainty what kind of data your working with.
It sounds like the original algo could have used a struct containing:
* each points x & y & maybe z coordinates (from the need to calculate distance at all)
* the total distance from start to that point (initially set to an arbitrarily high number if your program does not have an exception for distance=0)
* a bool for if the point has been "visited"
* another array containing the id's of the points connected to
But they didn't do structs back then. As arrays, it would look like:
float points[100][3];//3 dimensions worth of coordinates
float totaldistance[100];//total distance from start
bool visited[100]; // once set, point does not need calculating again
int connectedpoints[100][100]; //list of connected points, possibly complete.
you also need:
int arrivedfrom[100]; //set when a point's total distance is altered. indicates what node was being visited when the distance to that unvisited node was either set or lowered.
Take care with 0's and the use of point zero if you chose to use the entire array.
after every point has been visited (detected when you look for the unvisited point with the shortest distance, but don't find any unvisited points), you exit the calculation phase.
The total distance will be in the end point.
The path can be determined by looking at arrivedfrom[] at the end point, and then working your way back to the orig. That's why that value was saved. Its the breadcrumb that makes reporting an answer possible at all. :-)