-
Notifications
You must be signed in to change notification settings - Fork 0
/
dijkstra.cpp
72 lines (68 loc) · 2.46 KB
/
dijkstra.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "dijkstra.h"
#include "graph.h"
#include <queue>
#include <map>
#include <cfloat>
#include <algorithm>
vector<int> dijkstra::dijkstraPath(graph Sanfran, int src, int dest){
std::priority_queue<Node*, std::vector<Node*>, Compare> priority;
map<int, Node*> nodeMap;
vector<int> Verticies = Sanfran.getVertices();
vector<Node*> allWeights;
for (size_t i = 0; i < Verticies.size(); i++)
{
Node *newNode = new Node;
newNode->nodeId = Verticies.at(i);
newNode->visited = false;
newNode->distance = DBL_MAX;
allWeights.push_back(newNode);
nodeMap[Verticies.at(i)] = newNode;
if (Verticies.at(i) == src)
{
newNode->distance = 0;
priority.push(newNode);
}
}
//dijkstras using priority queue
while (dest != priority.top()->nodeId)
{
Node *current = priority.top();
priority.pop();
if(current->visited == false)
{
vector<int> neighbors = Sanfran.getAdjacentNodes(current->nodeId); //Find adjacent Nodes
for (size_t i = 0; i < neighbors.size(); i++)
{
Node *neighbor = nodeMap[neighbors.at(i)];
if (neighbor->visited == false)
{
double neighborWeight = Sanfran.getEdgeWeight(current->nodeId, neighbor->nodeId); //find weight from node id
if (current->distance + neighborWeight < neighbor-> distance)
{
neighbor-> previous = current -> nodeId;
neighbor-> distance = current->distance + neighborWeight;
}
priority.push(neighbor);
}
}
current->visited = true;
}
}
vector<int> nodeShortestPath;
vector<int> edgeShortestPath;
Node *finalNode = nodeMap[dest];
nodeShortestPath.push_back(finalNode->nodeId);
int i = 0;
while (nodeShortestPath.back() != src)
{
nodeShortestPath.push_back(nodeMap[nodeShortestPath.back()]->previous);
edgeShortestPath.push_back(Sanfran.getEdge(nodeShortestPath[i], nodeShortestPath[i+1]));
i++;
}
reverse(edgeShortestPath.begin(), edgeShortestPath.end());
for (size_t i = 0; i < allWeights.size(); i++)
{
delete allWeights.at(i);
}
return edgeShortestPath;
}