Hypergraph triads#
In this tutorial you count the triads of a hypergraph (three hyperedges that pairwise share a vertex) by their h-motif, keep the counts exact while hyperedges are deleted and inserted, and read what changed. You do it twice, in C++ and in Python, with the same input and the same output. It takes about 15 minutes.
You need dynG 0.3 or newer installed (Install): for C++ a compiler and
CMake, for Python pip install "dyng>=0.3" (the hypergraph and triad_count are new in 0.3;
this upgrades an installed 0.2.0, 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]",
Install). You should know what a hypergraph is
(Hypergraphs and hyperedge ids).
The algorithm is triad_count: hypergraph h-motif triad counts; this page shows how to use it.
The code is two example programs, examples/cpp/triad_count_update.cpp and
examples/python/triad_count.py, and their input examples/data/triad_count.hg and
examples/data/triad_count.dgt. CI builds the C++ program against the installed package and runs
both programs on the CPU backends (the local GPU gate, ci/gpu_local.sh, also on cuda), and
compares what they print with examples/data/triad_count_update.expected.txt; a test rebuilds
that file by a brute force over every triple of hyperedges after every batch
(python/tests/test_examples.py), and another runs the command of section 6 and compares its
output with this page (python/tests/test_triad_count_cli.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).
The programs and their input are in the repository, not in the installed package: clone it and
run every command of this page from its root (the tag of your version keeps the files as this
page quotes them; they are unchanged since v0.3.0, so with 0.3.0 installed clone that tag
instead):
git clone --branch v0.4.0 https://github.com/dyng-dev/dyng.git
cd dyng
1. The input: a .hg file and a batch file#
A .hg file has one hyperedge per line, its vertex ids separated by spaces; # starts a
comment. The example’s file has 12 lines over the vertices 1-10, and line 10 repeats line 1:
1 2 3
2 3 4
3 4 5
1 3 5
5 6 7
6 7 8
1 5 7 9
2 4 6 8
7 8 9 10
1 2 3
3 9 10
4 5 6
The batches are a .dgt file (File formats): %batch <n> starts a batch, -h id
deletes the hyperedge with that id and +h v1 v2 ... inserts one. The ids and the members are
dynG’s 0-based ones (the vertex of file id 1 is vertex 0):
%dgt 1 hypergraph
%batch 0
-h 1
-h 6
+h 0 4 8
+h 1 2 5 9
%batch 1
-h 3
2. Read the hypergraph (C++)#
dyng::io::hypergraph_read_options ro;
ro.dedupe_hyperedges = true; // repeated hyperedges dropped; file ids 1..n become 0..n-1
const auto hes = dyng::io::read_hypergraph<std::int32_t, dyng::unweighted>(argv[1], ro);
dyng::hypergraph_properties hp;
hp.line_graph = dyng::line_graph_policy::sorted_merge; // h_motif reads the line graph
hypergraph_t hg = hypergraph_t::from_hyperedges(res, hes.view(), hp);
read_hypergraphreads the file with ESCHER-GPU’s conventions: ids 1..n become vertices 0..n-1, anddedupe_hyperedgeskeeps the first of every repeated member set (ESCHER-GPU’s preprocessing of coauth-DBLP). The repeated line is dropped, so the hypergraph has 11 hyperedges, ids 0-10 in file order.line_graph_policy::sorted_mergekeeps the line graph (which hyperedges share a vertex) inside the hypergraph and maintains it under batches. The h-motifs and StatHyper’s types 2 and 3 read it;triad_count::compute()names the property if it is missing. StatHyper’s type 1 needs no line graph.
3. Count (C++)#
tc::result motifs = tc::compute(res, hg); // kind = pattern::h_motif: 30 bins
tc::options o;
o.kind = tc::pattern::stathyper_type1; // one bin: the sum of C(|e|, 3)
tc::result type1 = tc::compute(res, hg, o);
Each pattern is its own result: motifs holds 30 bins (options::kind defaults to
pattern::h_motif), type1 one bin. A result’s counts are a host array on every backend:
counts(), count(bin) and total(). The program prints the total and the non-zero bins as
bin:count:
11 hyperedges: 45 triads, type1 20
bins 3:2 9:9 14:19 15:6 28:1 29:8
Reading the bins. Bin k is ESCHER-GPU’s h-motif k + 1, the triads whose Venn diagram has a
given set of non-empty regions (the table is in section 1 of triad_count: hypergraph h-motif triad counts).
For example the 19 triads of bin 14 have a vertex in all three hyperedges (region ABC), a vertex in
two of the pairs only (AB, BC) and a vertex only in two of the hyperedges (A, C); the 8 of bin 29
share no vertex among all three, but every pair overlaps outside the third, and each hyperedge has
a vertex of its own. Bins 20-25 are always 0: they are the open h-motifs, where two of the
hyperedges are disjoint, and a triad is by definition closed. type1 is 20: the sum of C(|e|, 3)
over the 11 hyperedges (eight of 3 vertices, three of 4).
4. Apply the batches to both results at once (C++)#
auto [st, st1] = dyng::update(res, hg, batches[i].view(), motifs, type1); // one commit
std::cout << "batch " << i << ": " << st.deleted_frontier << " deleted, inserted ids";
for (const std::int64_t id : st.batch.inserted_ids) std::cout << ' ' << id;
std::cout << "\n h_motif -" << st.triads_removed << " +" << st.triads_added << " = "
<< motifs.total() << ", type1 -" << st1.triads_removed << " +" << st1.triads_added
<< " = " << type1.total() << '\n';
print_bins(" delta", st.delta, motifs.num_bins());
dyng::update(res, hg, batch, motifs, type1)applies the batch to the hypergraph once and brings both results up to date: each subtracts the triads through the deleted hyperedges on the hypergraph before the batch, then adds those through the inserted hyperedges on the hypergraph after it. It returns onetriad_count::statsper result. (One result alone istriad_count::update(res, hg, batch, motifs); updatingmotifsthat way and thentype1would fail, becausetype1would no longer match the hypergraph:stale_result_error.)st.batch.inserted_idsare the ids the inserted hyperedges received. The ESCHER store reuses the slots of deleted hyperedges by best fit, so the ids are its choice: read them here, never predict them. Batch 0 deletes 1 and 6 and its two insertions get 1 and 6 back.st.triads_removedandst.triads_addedare the two phases summed over the bins, andst.delta[k]the change of bin k (after minus before;std::array<std::int64_t, 30>).
The whole output of the program:
11 hyperedges: 45 triads, type1 20
bins 3:2 9:9 14:19 15:6 28:1 29:8
batch 0: 2 deleted, inserted ids 1 6
h_motif -27 +35 = 53, type1 -5 +5 = 20
delta 3:1 9:7 14:-6 15:-1 27:2 29:5
batch 1: 1 deleted, inserted ids 3
h_motif -16 +21 = 58, type1 -1 +1 = 20
delta 3:2 6:7 9:-5 14:-3 29:4
Batch 0 destroys 27 triads and creates 35, so the total goes from 45 to 53; its type-1 sum stays
20, because the deleted and the inserted hyperedges have the same sizes. After every update the
counts equal a fresh compute() on the new hypergraph exactly, on every backend.
Build and run it against your installed dynG (the CMake block of Install,
with add_executable(triad_count_update triad_count_update.cpp)):
$ ./triad_count_update examples/data/triad_count.hg examples/data/triad_count.dgt openmp
The last argument is the backend: sequential (the default), openmp or cuda (the GPU of
CUDA_VISIBLE_DEVICES; the program exits with 77 if there is none). The output is the same on
all three. A wrong number of arguments or another backend name prints the usage, and a file that
cannot be read the library’s message (the file, line and column); both exit with status 1.
5. The same in Python#
hg = dyng.io.read_hypergraph(
hg_path, dedupe_hyperedges=True, line_graph="sorted_merge", resources=res
) # file ids 1..n become 0..n-1
motifs = dyng.triad_count.compute(hg) # kind="h_motif": 30 bins
type1 = dyng.triad_count.compute(hg, kind="stathyper_type1") # one bin: the sum of C(|e|, 3)
st, st1 = dyng.update(hg, batch, motifs, type1) # one commit, both results updated
ids = "".join(f" {x}" for x in st.batch.inserted_ids.tolist())
print(f"batch {i}: {st.deleted_frontier} deleted, inserted ids{ids}")
print(
f" h_motif -{st.triads_removed} +{st.triads_added} = {motifs.total}, "
f"type1 -{st1.triads_removed} +{st1.triads_added} = {type1.total}"
)
print_bins(" delta", st.delta)
The Python names are the C++ ones: kind is a string (or a dyng.triad_count.Pattern),
Result.counts is a read-only uint64 NumPy array, Stats.delta an int64 array of the result’s
num_bins values (30 here) and st.batch.inserted_ids an int64 array. dyng.update() takes
several results of the same hypergraph and returns their stats as a tuple. Run it:
$ python examples/python/triad_count.py examples/data/triad_count.hg examples/data/triad_count.dgt
It prints the eight lines above, byte for byte (python/tests/test_examples.py).
6. From the command line#
dyng triad_count does the same without a program; --format escher prints ESCHER-GPU’s
output lines (all 30 bins, then the deltas of each batch) on standard output, and the update’s
time update_seconds= on standard error:
$ dyng triad_count update --input examples/data/triad_count.hg --dedupe-hyperedges \
--batches examples/data/triad_count.dgt --format escher
update_seconds=...
Hypergraph: 11 hyperedges, max vertex id 10, 36 pins
Motif counts (30 bins): 0 0 0 2 0 0 0 0 0 9 0 0 0 0 19 6 0 0 0 0 0 0 0 0 0 0 0 0 1 8
Batch 1 (2 deletions, 2 insertions)
Motif delta counts (30 bins): 0 0 0 1 0 0 0 0 0 7 0 0 0 0 -6 -1 0 0 0 0 0 0 0 0 0 0 0 2 0 5
Batch 2 (1 deletions, 1 insertions)
Motif delta counts (30 bins): 0 0 0 2 0 0 7 0 0 -5 0 0 0 0 -3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4
and --format json (the default) the triad-bins JSON that dyng.io.read_triad_bins reads back
(Command-line interface).
Next#
Count a new hypergraph pattern (an Op): count a pattern that is not one of the four, as an Op of
experimental::count_local_patterns, with the same engine and the same exact updates.triad_count: hypergraph h-motif triad counts: the 30-bin table, the requirements of each pattern, the performance gates against ESCHER-GPU and what differs from the papers.