-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dijkstra.h
74 lines (65 loc) · 2.09 KB
/
dijkstra.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
#ifndef CPP_ALGORITHM_DIJKSTRA_H
#define CPP_ALGORITHM_DIJKSTRA_H
#include <map>
#include <queue>
#include <set>
#include <vector>
/**
* @brief Dijkstra algorithm
* @details Dijkstra algorithm is a single source shortest path algorithm that handle non-negative edge weights.
* @note
* - class representation: @ref Dijkstra::Graph, @ref Dijkstra::Vertex, @ref Dijkstra::MinComparator
*/
namespace Dijkstra
{
/**
* \brief Vertex of Dijkstra algorithm.
* \details Each vertex has a unique id, a set of neighbors, a predecessor and a distance.
*/
struct Vertex
{
explicit Vertex(const char id)
: id(id), neighbors(std::set<Vertex*>()), predecessor(nullptr), distance(std::numeric_limits<int>::max())
{
}
char id;
std::set<Vertex*> neighbors;
Vertex* predecessor;
int distance;
};
/**
* \brief Comparator for priority queue.
*/
class MinComparator
{
public:
auto operator()(const Vertex* l, const Vertex* r) const -> bool { return (l->distance > r->distance); }
};
/**
* \brief Graph of Dijkstra algorithm.
*/
class Graph
{
public:
/**
* \brief Dijkstra algorithm.
* \details A single source shortest path algorithm that handle non-negative edge weights.
* It find the shortest path between two vertices in a graph.
* Relaxation is the process of updating the distance of a vertex, when a shorter path is found.
* \param source source vertex
*/
void DijkstraAlgorithm(Vertex& source);
/**
* \brief Reordering elements of the queue.
* \param min_queue minimum priority queue
*/
void ReorderQueue(std::priority_queue<Vertex*, std::vector<Vertex*>, MinComparator>& min_queue);
void AddVertex(Vertex& v);
void AddEdge(Vertex& u, Vertex& v, int weight);
private:
std::vector<Vertex*> vertices;
std::vector<std::tuple<Vertex*, Vertex*>> adjacency_list;
std::map<std::pair<char, char>, int> weight_list;
};
}
#endif