I have to make a program that finds the shortest path in graph using dijkstra's algorithm. I found the following to implement a graph but I don't know how to realise the dijkstra's algoritm.
<code>#ifndef GRAPH_H_
#define GRAPH_H_
/*
* This file has declarations for classes used to represent the graph
*/
//enum for the status of a node
enum Status {
NOT_VISITED,
VISITED
};
//forward declaration
class Node;
//An object of this class represents an edge in the graph.
class Edge
{
private:
Node *orgNode;//the originating vertex
Node *dstNode;//the destination vertex
unsigned cost;//cost of the edge
//An object of this class holds a vertex of the graph
class Node
{
private:
string name;
vector<Edge> adjNodeList;//list of outgoing edges for this vertex
enum Status status;//used in dfs to mark the node visited
public:
Node(string id)
{
name = id;
status = NOT_VISITED;
}
//do not del the adj nodes here...they will be deleted by graph destructor
~Node()
{
adjNodeList.clear();
}
enum Status getStatus()
{
return status;
}
void setStatus(enum Status st)
{
status = st;
}
string getName()
{
return name;
}
void addAdjNode(Node *adj, unsigned cost)
{
//create an edge with 'this' as the originating node and adj as the destination node
Edge newEdge(this, adj, cost);
adjNodeList.push_back(newEdge);
}
//displays all adjacent verticies of this vertex
void displayList()
{
string edgeOp = " -> " ;
for(int i=0 ; i < adjNodeList.size() ; i++)
{
Edge edg = adjNodeList[i];
cout << name << " -> " << edg.getDstNode()->getName() << endl ;
}
}
};
//An object of class graph holds a directed graph
class Graph
{
private:
vector<Node*> nodeList;//list of verticies
bool foundCycle;//true if a cycle is found, false otherwise
int desiredCycSize;
void clearVisited()
{
for(int i = 0; i < nodeList.size() && !foundCycle ; i++)
{
nodeList[i]->setStatus(NOT_VISITED);
}
}
unsigned numOfCities, numOfFlights, cycSize;
//read in number of cities(TODO:in current implementation..not reqd), number of edges and the desired tour size
cin >> numOfCities >> numOfFlights >> cycSize;
cin >> fromCity >> toCity >> cost;
//find if a vertex for the city already exists, if so get that
Node *u = findNodeByName(fromCity);
if(u == NULL)
{
u = new Node(fromCity);
addNewNode(u);
}
//find if a vertex for the city already exists, if so get that
Node *v = findNodeByName(toCity);
if(v == NULL)
{
v = new Node(toCity);
addNewNode(v);
}