-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGraph.h
75 lines (61 loc) · 1.63 KB
/
Graph.h
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
73
74
75
#ifndef GRAPH_H
#define GRAPH_H
#include <unordered_map>
#include <queue>
#include <functional>
#include "Vertex.h"
using namespace std;
class Graph
{
unordered_map<int, Vertex> _vertices;
public:
void addVertex(Vertex vertex)
{
_vertices[vertex.getId()] = vertex;
}
//MA #12 TODO: IMPLEMENT!
unordered_map<Vertex, int> *computeShortestPath(Vertex *start)
{
//holds known distances
unordered_map<Vertex, int> distances;
//underlying heap
priority_queue<Vertex, vector<Vertex>, PathWeightComparer> dijkstra_queue{};
//reset start's path weight
start->setPathWeight(0);
//make sure that the starting vertex is in the graph
if (_vertices.find(start->getId()) != _vertices.end())
{
//push on starting vertex
dijkstra_queue.push(*start);
//while queue not empty
while (dijkstra_queue.empty() == false)
{
//push on outgoing edges that haven't been discovered
Vertex top = dijkstra_queue.top();
dijkstra_queue.pop();
//Top of heap not known (in distances)?
if (distances.find(top) == distances.end())
{
//make known
//***Factor in getload here as well to decide time to take path
int current_path_weight = top.getPathWeight()*top.getload();
distances[top] = current_path_weight;
//push on outgoing edges
for (auto item : top.getEdges())
{
Vertex *next = item.first;
int weight = item.second;
next->setPathWeight(weight + current_path_weight);
//not known? add to heap
if (distances.find(*next) == distances.end())
{
dijkstra_queue.push(*next);
}
}
}
}
return &distances;
}
}
};
#endif