I have to create a graph, and I used vectors as the underlying structure for my map. I have to implement Dijkstra’s Algorithm.
I have an idea of what I should do, but I have a problem implementing it. This is what I have so far.
1 2 3 4 5 6 7 8
|
struct Path{
Path(float num1, float num2): direction(num1), weight(num2), taken(false) { }
// This boolean will determine if that vertex is taken
bool taken;
float direction;
float weight;
};
|
I have a file that has something like this:
1 2 2.0 4 1.0
2 4 3.0 5 10.0
3 1 4.0 6 5.0
4 3 2.0 6 8.0 7 4.0 5 2.0
5 7 6.0
6
7 6 1.0
|
I created the vector with
vector < vector <Path> > vector
I have written the code to place the path into the vector.
So what this means is vertex 1 (vector[0]) contains direction 2, with weight 2.0 and direction 4 with weight 1.0.
Now for Dijkstra's Algorithm is pretty much going from point A to point B taking the shortest path.
I have to implement a way where they start at the beginning index, and finds a path to the end index. For example if the start index is '1' and the end index is '7' it goes through 1, 4, 7 and the total is 5.
So with that example I want to start at vector[0], go to the first element which is 2, save the weight of the edge, change the bool to true so it doesn't go back, then go to vector[1], go to the first element which is 4, add the weight to the total, go to vector[3] etc. Pretty much finding all the different combinations I can get from point A to point B. And if the bool is true, I have to skip and and go to the next element. I will then compare different totals to find the shortest length. I'm having problem trying to find a way to go from vector[0] to vector[1] when it reads that vertex direction. If there's another way I'm open to suggestions how to find a better algorithm, since my algorithm is maybe like O(n^n)