for the past four days I am trying to understand the dijkstra’s algorithm. But I can’t. I have a vector of points. From that I created a cost matrix. But I don’t know how to make the dijkstra’s algorithm. Sources are available in net, but I am not from Computer science background, So I can’t understand them. I am trying to make a function like this
vector<int> dijkstra(costMatrix[][])
{
....
....
return vector<int>pathPointindex
}
main()
{
vector<Point> availablePoints;
costMatrix[][]=createCostMatrix();
vector<int> indexes=dijikstra(costMatrix)
for(int i=0;i<indexes.size();i++)
cout << "path points are " << availablePoints[indexes[i]] << endl;
}
If anybody, can you please post the code. I am not lazy. But my project already crossed the deadline one day ago. Now I lost my hope to understand the logic. Now Just I want the function. “A man in need is the angel indeed” .
EDIT: Special thanks to “Loki astari” for his excellent answer
I advise you to look at TopCoder tutorial that have very pragmatic apporach.
You will need to find out how STL priority queue works and make sure you don’t have any
negative edge weightsin your graph.Decent full implementation can be found here. You will have to add Path vector to it and implement
RecoverPathmethod in order to get nodes on path from source to sink. To use this solution you will also need to convert youradjacency matrixtoadjacency listin following way:EDIT: If your graph is dense I would advise you to use Ford Bellman algorithm is much simpler and shouldn’t be much slower.
EDIT2: To calculate path you should add in the header
Then you have to add RecoverPath method (it works only when path exists)