Hi, I am working on a project and I have to implement a graph and to look for the shortest path. As I read so far the best way is with Dijkstra's algorithm. I am trying to do it but i have some problem. Can you please help me with it.
I am reading data from a file and my datastructure is like that:
The hh is file is like that :
class Datastructure {
public:
Datastructure();
~Datastructure();
// Adds one attraction with id id and name name to the data
// structure.
bool add(unsigned int id, std::string name);
// Tries to find path between from and to. If path is found prints
// it and return true.
bool path(unsigned int from, unsigned int to);
// Adds route between two nodes with distance distance.
bool route(unsigned int id1, unsigned int distance, unsigned int id2);
private:
enum COLOURS{WHITE,GRAY,BLACK};
struct Node;
struct Edge
{
// Node* startNode;
Node* destination_Node;
unsigned int distance;
Edge():distance(0){}
Edge(Node* destination, unsigned int distance_):
destination_Node(destination),distance(distance_){}
};
struct Node
{
unsigned int id;
std::string name;
unsigned int priority;
enum COLOURS colour;
std::vector<Edge> path_list;
Node(unsigned int id_,std::string name_):id(id_),name(name_),priority(0),colour(WHITE){}
~Node()
{
path_list.clear();
}
};
std::map<unsigned int,Node*> graph_list; //master container
};
I think in the structure "Node" i need also to have an int variable "cost" that will be 0 by default
Now when I call the function
bool path(unsigned int from, unsigned int to);
It need to return true or false because i have a given main() that i need to follow and also to print the that path.
My idea is to have a function:
bool path(unsigned int from, unsigned int to)
{
vector<int> path_ids;
reset();
if(bool Dijkstra(not sure what i need))
{
//print path
return true;
}
return false;
}
Reset() should be a function that reset the graph before every search.Also i think that there might be vector that will be passed by reference and will store the all the ids from the shortest path, so after that it will be easy to print them.
Can you please help me with ideas how to implement that.
Thank you in advance for any suggestions or help .