VeloGraphX
High-performance dynamic graph analytics in C++20
Loading...
Searching...
No Matches
sssp.hpp
Go to the documentation of this file.
1#pragma once
2#include <cstdint>
3#include <functional>
4#include <queue>
5#include <utility>
6#include <vector>
7
11
12namespace velographx {
13
14template <class Graph>
16 public:
17 BasicIncrementalSSSP(Graph& g, VertexId source) : g_(g), source_(source) { recompute(); }
18
19 [[nodiscard]] const std::vector<std::uint64_t>& distances() const noexcept { return dist_; }
20
21 void apply(const UpdateBatch& batch) {
22 bool deletion = false;
23 for (const auto& e : batch.updates) deletion |= !e.add;
24 apply_updates(g_, batch);
25 if (deletion) recompute();
26 else relax_from_updates(batch);
27 }
28
29 void recompute() {
31 vertex_count(g_), source_, dist_,
32 [&](VertexId u, auto&& relax) {
33 for_each_neighbor(g_, u, [&](VertexId v) { relax(v, 1); });
34 });
35 }
36
37 private:
38 void relax_from_updates(const UpdateBatch& batch) {
39 if (dist_.size() < vertex_count(g_)) {
41 }
42 using Item = std::pair<std::uint64_t, VertexId>;
43 std::priority_queue<Item, std::vector<Item>, std::greater<Item>> queue;
44 for (const auto& e : batch.updates) {
45 if (!e.add || e.src >= dist_.size() || e.dst >= dist_.size()) continue;
46 if (dist_[e.src] != incremental_detail::kDijkstraInf &&
47 dist_[e.src] + 1 < dist_[e.dst]) {
48 dist_[e.dst] = dist_[e.src] + 1;
49 queue.push({dist_[e.dst], e.dst});
50 }
51 if (!is_directed(g_) && dist_[e.dst] != incremental_detail::kDijkstraInf &&
52 dist_[e.dst] + 1 < dist_[e.src]) {
53 dist_[e.src] = dist_[e.dst] + 1;
54 queue.push({dist_[e.src], e.src});
55 }
56 }
58 dist_, queue,
59 [&](VertexId u, auto&& relax) {
60 for_each_neighbor(g_, u, [&](VertexId v) { relax(v, 1); });
61 });
62 }
63
64 Graph& g_;
65 VertexId source_;
66 std::vector<std::uint64_t> dist_;
67};
68
70
71} // namespace velographx
void apply(const UpdateBatch &batch)
Definition sssp.hpp:21
BasicIncrementalSSSP(Graph &g, VertexId source)
Definition sssp.hpp:17
const std::vector< std::uint64_t > & distances() const noexcept
Definition sssp.hpp:19
void propagate_dijkstra(std::vector< std::uint64_t > &dist, std::priority_queue< std::pair< std::uint64_t, VertexId >, std::vector< std::pair< std::uint64_t, VertexId > >, std::greater< std::pair< std::uint64_t, VertexId > > > &queue, NeighborEnumerator &&enumerate)
Definition dijkstra.hpp:17
constexpr std::uint64_t kDijkstraInf
Definition dijkstra.hpp:14
void recompute_dijkstra(std::size_t vertex_count, VertexId source, std::vector< std::uint64_t > &dist, NeighborEnumerator &&enumerate)
Definition dijkstra.hpp:38
void apply_updates(Graph &graph, const Batch &batch)
void for_each_neighbor(const Graph &graph, VertexId u, Fn &&fn)
constexpr bool is_directed(const Graph &graph)
std::uint32_t VertexId
Definition frontier.hpp:6
constexpr std::size_t vertex_count(const Graph &graph)
std::vector< EdgeUpdate > updates