Shortest hyperpaths#
In this tutorial you compute the shortest hyperpaths of a weighted hypergraph from a source vertex, keep them exact while hyperedges are deleted and inserted and vertices leave and join hyperedges, and read the distances of the hyperedges and the vertices, the paths and what each update did. You do it in C++ and in Python, with the same input and the same output. It takes about 20 minutes.
You need dynG 0.4 or newer installed (Install): for C++ a compiler and
CMake, for Python pip install "dyng>=0.4" (hyper_sssp and the slack_csr storage are new in
0.4; this upgrades an installed 0.3.0, which has neither, but not its CUDA plugin, which is
pinned to its own version: with the plugin, upgrade both with pip install -U "dyng[cu13]" or
"dyng[cu12]"). You should know what a hypergraph is
(Hypergraphs and hyperedge ids). The algorithm is hyper_sssp: shortest hyperpaths; this page shows
how to use it.
The code is two example programs, examples/cpp/hyper_sssp_update.cpp and
examples/python/hyper_sssp.py, their input examples/data/hyper_sssp.hg and
examples/data/hyper_sssp.dgt, and two tests that hold the rest of the page’s code,
cpp/tests/algorithms/hyper_sssp/hyper_sssp_tutorial_test.cpp and
python/tests/test_shortest_hyperpaths_tutorial.py. CI builds the C++ program against the
installed package and runs both programs on the sequential backend (the local GPU gate,
ci/gpu_local.sh, also on cuda), and compares what they print with
examples/data/hyper_sssp_update.expected.txt; a test rebuilds that file from the oracle,
MOSP_ESCHER’s test oracle dyng.testing.hyper_sssp_reference, after every batch
(python/tests/test_examples.py). The snippets below are quoted from those files, and the
documentation check fails when a quoted line changes without this page
(ci/tutorial_quotes.py). Clone the repository and run the commands from its root:
git clone --branch v0.4.0 https://github.com/dyng-dev/dyng.git
cd dyng
1. The problem in one paragraph#
The line graph of a hypergraph has one node per live hyperedge, and two hyperedges are adjacent when they share a vertex. Entering hyperedge h costs its weight w(h). From a source vertex s, the distance D(h) of a hyperedge is the cost of the cheapest walk of overlapping hyperedges h_1, …, h_k = h with s in h_1: D(h) = w(h_1) + … + w(h_k), so a hyperedge that holds s has D(h) = w(h). Its parent is the hyperedge before it on such a walk (the lowest id among equals), -1 when it holds s. The distance of a vertex is the smallest D(h) over the hyperedges that hold it, and 0 for s.
2. The input: a weighted .hg file and a batch file#
A .hg file has one hyperedge per line; ; w ends a line with its weight. The example’s 9
hyperedges over the vertices 1-12 (ids 1..n in the file become the vertices 0..n-1):
1 2 3 ; 4
3 4 ; 2
4 5 6 ; 7
2 7 ; 9
7 8 ; 3
6 8 9 ; 1
9 10 ; 5
10 11 12 ; 2
5 12 ; 8
The batches are a .dgt file (File formats), with dynG’s 0-based ids: -h id
deletes a hyperedge, +h v1 v2 ... ; w inserts one, and the incidence edits -i h v and
+i h v remove vertex v from, or add it to, the live hyperedge h:
%dgt 1 hypergraph
%batch 0
-h 3
-i 5 7
+i 1 6
+h 0 11 ; 6
%batch 1
-h 6
-i 0 1
+i 8 2
+h 3 9 ; 1
Inside a batch the order is fixed, whatever the order of the lines: the hyperedge deletions, then the incidence removals, the incidence additions, and the insertions last (Hypergraphs and hyperedge ids). An edit that cannot apply (a dead hyperedge, a vertex that is not a member, the last member of a hyperedge) is skipped and counted.
3. Build the hypergraph (C++)#
const auto hes = dyng::io::read_hypergraph<std::int32_t, std::int32_t>(argv[1]); // '; w'
dyng::hypergraph_properties hp;
hp.storage = dyng::hypergraph_storage::slack_csr; // incidence edits and hyper_sssp need it
hp.line_graph = dyng::line_graph_policy::slack_csr;
hypergraph_t hg = hypergraph_t::from_hyperedges(res, hes.view(), hp);
read_hypergraph<std::int32_t, std::int32_t>reads the weights (; w): hyper_sssp needs integer weights in [1, 2^28].hypergraph_storage::slack_csris the storage of incidence edits and of hyper_sssp (the ESCHER store,escher, keeps its rows sorted for the triad counts and refuses incidence edits), andline_graph_policy::slack_csrkeeps its line graph up to date under every batch.
4. Compute from two sources (C++)#
hs::result<std::int32_t> a = hs::compute(res, hg, 0); // from vertex 0
hs::options o;
o.work_budget = 0.5; // fall back to a recompute sooner (stats::relaxations shows the work)
hs::result<std::int32_t> c = hs::compute(res, hg, 9, o); // from vertex 9
A result belongs to one source vertex. hyperedge_distances() and hyperedge_parents() are its
arrays by hyperedge id (device memory on cuda: the program copies them with to_vector()), and
distance_to(v) is the distance of a vertex. The program prints every reached hyperedge as
id:D/parent and the distance of vertex 11:
9 hyperedges, 12 vertices
from 0: 0:4/-1 1:6/0 2:13/1 3:13/0 4:16/3 5:14/2 6:19/5 7:21/6 8:21/2 | vertex 11: 21
from 9: 0:19/1 1:15/2 2:13/5 3:18/4 4:9/5 5:6/6 6:5/-1 7:2/-1 8:10/7 | vertex 11: 2
From vertex 0, hyperedge 0 = {0, 1, 2} holds the source, so D = w = 4 and its parent is -1; hyperedge 1 = {2, 3} overlaps it, D = 4 + 2 = 6, parent 0; and so on. Vertex 11 is in hyperedges 7 (D = 21) and 8 (D = 21), so its distance is 21.
options::work_budget (here 0.5 for the second result) bounds the work of an update, as a
multiple of the line graph’s live entries: past it the update recomputes from scratch (exact
either way; section 7).
5. Apply the batches to both results at once (C++)#
auto [sa, sc] = dyng::update(res, hg, batches[i].view(), a, c); // one commit, both results
std::cout << "batch " << i << ": -" << sa.batch.deleted_hyperedges << " +"
<< sa.batch.inserted_hyperedges << " hyperedges, incidences -"
<< sa.batch.deleted_incidences << " +" << sa.batch.inserted_incidences
<< ", line graph -" << sa.line_graph_deletions << " +" << sa.line_graph_insertions
<< ", invalidated " << sa.invalidated << " and " << sc.invalidated << '\n';
dyng::update(res, hg, batch, a, c)applies the batch to the hypergraph once and brings both results up to date; it returns onehyper_sssp::statsper result. (One result alone ishyper_sssp::update(res, hg, batch, a); the other would then be stale.)stats::batchis what the batch did to the hypergraph (the same for both results):deleted_hyperedges,inserted_hyperedgesandinserted_ids,deleted_incidencesandinserted_incidences,skipped_ops.line_graph_deletions/line_graph_insertionsare the pairs of overlapping hyperedges the batch removed and added, andinvalidatedthe hyperedges whose path the update had to rebuild (each result its own).
The whole output:
9 hyperedges, 12 vertices
from 0: 0:4/-1 1:6/0 2:13/1 3:13/0 4:16/3 5:14/2 6:19/5 7:21/6 8:21/2 | vertex 11: 21
from 9: 0:19/1 1:15/2 2:13/5 3:18/4 4:9/5 5:6/6 6:5/-1 7:2/-1 8:10/7 | vertex 11: 2
batch 0: -1 +1 hyperedges, incidences -1 +1, line graph -2 +3, invalidated 2 and 2
from 0: 0:4/-1 1:6/0 2:13/1 3:6/-1 4:9/1 5:14/2 6:13/7 7:8/3 8:14/3 | vertex 11: 6
from 9: 0:12/3 1:14/0 2:13/5 3:8/7 4:17/1 5:6/6 6:5/-1 7:2/-1 8:10/7 | vertex 11: 2
batch 1: -1 +1 hyperedges, incidences -1 +1, line graph -1 +4, invalidated 1 and 3
from 0: 0:4/-1 1:6/0 2:13/1 3:6/-1 4:9/1 5:14/2 6:7/1 7:8/3 8:12/0 | vertex 11: 6
from 9: 0:7/1 1:3/6 2:8/6 3:8/7 4:6/1 5:9/2 6:1/-1 7:2/-1 8:10/7 | vertex 11: 2
Batch 0 deletes hyperedge 3, and its insertion {0, 11} gets id 3 back (the store reuses the slot;
read the ids from inserted_ids, never predict them). The new hyperedge 3 holds vertex 0, so from
vertex 0 the vertex 11 is now at distance 6. After every update the results equal a fresh
compute() on the new hypergraph, bit for bit, on every backend.
Build and run it against your installed dynG (the CMake block of Install,
with add_executable(hyper_sssp_update hyper_sssp_update.cpp)):
$ ./hyper_sssp_update examples/data/hyper_sssp.hg examples/data/hyper_sssp.dgt sequential
The last argument is the backend: sequential (the default) or cuda (the GPU of
CUDA_VISIBLE_DEVICES; the program exits with 77 if there is none); the output is the same.
hyper_sssp has no OpenMP backend in 0.4: OpenMP resources raise not_supported_error, whose
message names the fix.
6. A batch in code, vertex distances and a path (C++)#
The rest of the C++ code is in hyper_sssp_tutorial_test.cpp, on the same hypergraph. A batch
built in code, the first batch of the file:
dyng::hyperedge_batch<std::int32_t, std::int32_t> batch;
batch.delete_hyperedge(3); // -h 3
batch.delete_incidence(5, 7); // -i 5 7: vertex 7 leaves hyperedge 5
batch.insert_incidence(1, 6); // +i 1 6: vertex 6 joins hyperedge 1
batch.insert_hyperedge({0, 11}, /*weight=*/6); // +h 0 11 ; 6
hs::stats st = hs::update(res, hg, batch.view(), r);
The distance of every vertex is computed on request, once per hypergraph version (a host array):
const dyng::array_view<const std::int64_t> vd = r.vertex_distances(); // host memory
EXPECT_EQ(vd[0], 0); // the source
EXPECT_EQ(vd[11], r.distance_to(11)); // the cheapest hyperedge holding vertex 11: id 3
EXPECT_EQ(vd[11], 6);
A shortest hyperpath is the chain of parents back to a hyperedge that holds the source:
const auto d = r.hyperedge_distances(); // host memory on the sequential backend
const auto p = r.hyperedge_parents();
std::vector<std::int32_t> path; // the hyperedges from the source to hyperedge 6, reversed
for (std::int32_t h = 6; h != -1; h = p[static_cast<std::size_t>(h)]) path.push_back(h);
EXPECT_EQ(path, (std::vector<std::int32_t>{6, 7, 3})); // 3 holds vertex 0, then 7, then 6
EXPECT_EQ(d[6], 6 + 2 + 5); // w(3) + w(7) + w(6): every hyperedge entered costs its weight
7. Tune the work budget#
An update rebuilds the paths through what the batch changed; when a batch changes much of the
hypergraph, recomputing is cheaper. stats::relaxations counts the work an update did (the
neighbours its pulls and pushes read), and options::work_budget caps it: before each push round
(cuda) or expansion (sequential), an update whose relaxations exceed work_budget times the line
graph’s live entries recomputes from scratch (stats::fallback_used,
fallback_reason == work_budget), or raises convergence_error under
on_budget = budget_action::error. The work before the first check (the invalidation and the
seed’s pulls) is never refused, so even a budget of 0 lets an update whose work all lies in its
seed complete; 0 acts at the first push round or expansion. The result is exact either way:
hs::options o = r.get_options();
o.work_budget = 0.0; // recompute at the first push round or expansion
r.set_options(o);
dyng::hyperedge_batch<std::int32_t, std::int32_t> shortcut;
shortcut.insert_hyperedge({0, 9}, /*weight=*/1);
st = hs::update(res, hg, shortcut.view(), r);
EXPECT_TRUE(st.fallback_used);
EXPECT_EQ(st.fallback_reason, hs::fallback_reason::work_budget);
EXPECT_GT(st.relaxations, 0); // the work the budget counted: tune work_budget from it
To tune it, run your batches with work_budget = std::numeric_limits<double>::infinity() (never
fall back), time the updates and a compute(), and find the batch size where the update stops
being faster than the recompute. The budget is a multiple of the line graph’s live entries, not a
count of relaxations: set it near that batch’s stats::relaxations divided by the live entries,
which is the size of the line graph’s column array (hg.h2h().num_entries() in C++,
len(hg.h2h().col_ind) in Python). The default 1.0, MOSP_ESCHER’s, means about one pass over the
line graph. On cuda, a batch close to its budget may fall back in one run and not in another
(the relaxations of a parallel round depend on the schedule); the results are the same.
8. Check against the oracle#
dyng::testing::hyper_sssp_reference and check_hyper_sssp_tree are MOSP_ESCHER’s test oracle (a
Dijkstra over the line graph rebuilt from the hyperedges; it shares no code with hyper_sssp and
the container), for tests of your own:
const auto rows = hg.h2v(); // the hyperedges by id
const std::vector<std::int64_t> offsets = dyng::to_vector(res, rows.row_ptr);
const std::vector<std::int32_t> members = dyng::to_vector(res, rows.col_ind);
std::vector<std::int64_t> weights;
for (const std::int32_t w : hg.weights()) weights.push_back(w);
const auto [bad_distances, bad_parents] = dyng::testing::check_hyper_sssp_tree(
offsets, members, hg.num_vertices(), weights, r.source(), /*source_is_vertex=*/true,
dyng::to_vector(res, r.hyperedge_distances()), dyng::to_vector(res, r.hyperedge_parents()));
EXPECT_EQ(bad_distances, 0);
EXPECT_EQ(bad_parents, 0);
Link dyng::testing for it (target_link_libraries(... dyng::dyng dyng::testing)).
9. The same in Python#
The example program, with explicit resources (the CPU wheel’s default resources are OpenMP’s, which hyper_sssp does not run on in 0.4):
hg = dyng.io.read_hypergraph(
hg_path, weighted=True, storage="slack_csr", line_graph="slack_csr", resources=res
) # '; w' on every line; file ids 1..n become 0..n-1
a = dyng.hyper_sssp.compute(hg, 0) # from vertex 0
c = dyng.hyper_sssp.compute(hg, 9, work_budget=0.5) # from vertex 9, a smaller work budget
sa, sc = dyng.update(hg, batch, a, c) # one commit, both results updated
s = sa.batch
print(
f"batch {i}: -{s.deleted_hyperedges} +{s.inserted_hyperedges} hyperedges, incidences "
f"-{s.deleted_incidences} +{s.inserted_incidences}, line graph -{sa.line_graph_deletions} "
f"+{sa.line_graph_insertions}, invalidated {sa.invalidated} and {sc.invalidated}"
)
$ python examples/python/hyper_sssp.py examples/data/hyper_sssp.hg examples/data/hyper_sssp.dgt
prints the nine lines above, byte for byte. The rest, from
python/tests/test_shortest_hyperpaths_tutorial.py:
batch = dyng.HyperedgeBatch(
delete_hyperedges=[3], # -h 3
delete_incidences=([5], [7]), # -i 5 7: vertex 7 leaves hyperedge 5
insert_incidences=([1], [6]), # +i 1 6: vertex 6 joins hyperedge 1
insert_hyperedges=[[0, 11]], # +h 0 11 ; 6
insert_weights=[6],
)
st = dyng.hyper_sssp.update(hg, batch, sp)
vd = sp.vertex_distances() # a dyng.Array of host memory, one distance per vertex
assert vd[0] == 0 # the source
assert vd[11] == sp.distance_to(11) == 6 # the cheapest hyperedge holding vertex 11: id 3
d, p = sp.hyperedge_distances.to_numpy(), sp.hyperedge_parents.to_numpy()
path = [6] # the hyperedges from the source to hyperedge 6, reversed
while p[path[-1]] != -1:
path.append(int(p[path[-1]]))
assert path == [6, 7, 3] # 3 holds vertex 0, then 7, then 6
assert d[6] == 6 + 2 + 5 # w(3) + w(7) + w(6): every hyperedge entered costs its weight
sp.set_options(work_budget=0.0) # recompute at the first push round or expansion
shortcut = dyng.HyperedgeBatch(insert_hyperedges=[[0, 9]], insert_weights=[1])
st = dyng.hyper_sssp.update(hg, shortcut, sp)
assert st.fallback_used and st.fallback_reason == "work_budget"
assert st.relaxations > 0 # the work the budget counted: tune work_budget from it
d, p = dyng.testing.hyper_sssp_reference(hg, sp.source) # MOSP_ESCHER's test oracle
assert sp.hyperedge_distances.tolist() == d.tolist()
assert sp.hyperedge_parents.tolist() == p.tolist()
assert dyng.testing.check_hyper_sssp_tree(hg, sp) == (0, 0)
The Python names are the C++ ones: Options, Stats, Result; the arrays are read-only
dyng.Array views (.to_numpy() copies them), and dyng.update() takes several results and
returns their stats as a tuple.
10. From the command line#
dyng hyper_sssp does the same without a program and writes the distances and parents by
hyperedge id in sssp’s file formats (Command-line interface):
$ dyng hyper_sssp update --input examples/data/hyper_sssp.hg --batches examples/data/hyper_sssp.dgt \
--source 0 --out sp --vertex-distances --compare-recompute
writes sp/hyperedge_distances.txt, sp/hyperedge_parents.txt and sp/vertex_distances.txt, and
prints one line per batch.
Next#
hyper_sssp: shortest hyperpaths: the update step by step, the two CUDA engines, the gates against MOSP_ESCHER and what differs from the paper.
Hypergraphs and hyperedge ids: the storages, the incidence edits and the ids.