Delete folder annotations/annotation_144 with huggingface_hub
Browse files- annotations/annotation_144/8386_Efficient_Streaming_Algor.pdf +0 -3
- annotations/annotation_144/EfficientStreamingAlgor/DD_order.bin +0 -3
- annotations/annotation_144/EfficientStreamingAlgor/README.md +0 -130
- annotations/annotation_144/EfficientStreamingAlgor/config.yml +0 -7
- annotations/annotation_144/EfficientStreamingAlgor/gen_edge.cpp +0 -36
- annotations/annotation_144/EfficientStreamingAlgor/generation.py +0 -708
- annotations/annotation_144/EfficientStreamingAlgor/logs.log +0 -56
- annotations/annotation_144/EfficientStreamingAlgor/memory_monitor.py +0 -31
- annotations/annotation_144/EfficientStreamingAlgor/probability.py +0 -20
- annotations/annotation_144/EfficientStreamingAlgor/reformat.py +0 -51
- annotations/annotation_144/EfficientStreamingAlgor/samples.bin +0 -3
- annotations/annotation_144/EfficientStreamingAlgor/tiny_edges.csv +0 -6
- annotations/annotation_144/canonical.py +0 -708
- annotations/annotation_144/function_info.json +0 -688
- annotations/annotation_144/instruction.txt +0 -53
- annotations/annotation_144/latex.txt +0 -90
- annotations/annotation_144/requirements.txt +0 -15
- annotations/annotation_144/test.py +0 -97
- annotations/annotation_144/test.sh +0 -4
annotations/annotation_144/8386_Efficient_Streaming_Algor.pdf
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:47ce13bbced61982159402f010283b108db7ec4a1889b7a6667ace6ecf7978ae
|
| 3 |
-
size 549386
|
|
|
|
|
|
|
|
|
|
|
|
annotations/annotation_144/EfficientStreamingAlgor/DD_order.bin
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:def7a6395e04d31c572eec504dd7ac6b6ff5efa0815c74876693bfba356d7c44
|
| 3 |
-
size 24
|
|
|
|
|
|
|
|
|
|
|
|
annotations/annotation_144/EfficientStreamingAlgor/README.md
DELETED
|
@@ -1,130 +0,0 @@
|
|
| 1 |
-
## streaming.py
|
| 2 |
-
|
| 3 |
-
This is the code for sampling a graphlet of size $k$ uniformly in a large graph of $n$ vertices in a streaming manner.
|
| 4 |
-
|
| 5 |
-
To run the code, first install the following packages:
|
| 6 |
-
|
| 7 |
-
- pyspark (Only necessary for the `DataFrame` mode)
|
| 8 |
-
|
| 9 |
-
- pyyaml
|
| 10 |
-
|
| 11 |
-
- numpy
|
| 12 |
-
|
| 13 |
-
Then, prepare for the edge list file and the configuration `config.yml`, and put them in the same directory with the code. The edge list file should be in the following format (a header followed by all edges) and not contain loops or duplicated edges:
|
| 14 |
-
|
| 15 |
-
```
|
| 16 |
-
u,v
|
| 17 |
-
x1,y1
|
| 18 |
-
x2,y2
|
| 19 |
-
x3,y3
|
| 20 |
-
...
|
| 21 |
-
xm,ym
|
| 22 |
-
```
|
| 23 |
-
|
| 24 |
-
The parameters in `config.yml` are:
|
| 25 |
-
|
| 26 |
-
- `n`: the number of vertices,
|
| 27 |
-
|
| 28 |
-
- `edgelistfile`: the name of the edge list file,
|
| 29 |
-
|
| 30 |
-
- `edgelistmode`: the mode of storing the edge list which has four choices:
|
| 31 |
-
|
| 32 |
-
- `local`: The code reads the edges from the local file regarding it as a streaming source. Everything except the edge list is stored in the main memory. We assume the main memory is $O(nk^2)$.
|
| 33 |
-
|
| 34 |
-
- `DataFrame`: The code iterates the edges from Spark DataFrame regarding it as a streaming source. We assume that the driver node has $O(nk^2)$ memory. Large data (e.g., edge list of size $O(n^2)$) is stored in the form of Spark DataFrame so that it is distributed, while small data (e.g., the vertex list of size $O(n)$ or $O(nk)$) is stored in the driver's memory.
|
| 35 |
-
|
| 36 |
-
- `StreamingDataFrame` (TODO): The code iterates the edges from Spark StreamingDataFrame.
|
| 37 |
-
|
| 38 |
-
- `memory`: The code stores everything (including the edge list) in main memory.
|
| 39 |
-
|
| 40 |
-
- `epsilon`: the parameter for DD-order,
|
| 41 |
-
|
| 42 |
-
- `k`: the parameter for graphlet sampling,
|
| 43 |
-
|
| 44 |
-
- `target`: we do sampling in batches until there are `target` successful trials.
|
| 45 |
-
|
| 46 |
-
- `MAX_EDGES`: the maximum number of edges we store during the computation of DD-order and sampling. We will perform sampling with a batch size `MAX_EDGES/(k*k)`.
|
| 47 |
-
|
| 48 |
-
- (optional) `ddorderfile`: a binary file containing a list representing the DD-order, so that we do not compute it again.
|
| 49 |
-
|
| 50 |
-
Finally, run:
|
| 51 |
-
|
| 52 |
-
```
|
| 53 |
-
python streaming.py
|
| 54 |
-
```
|
| 55 |
-
|
| 56 |
-
If `edgelistmode` is `DataFrame`, you may also run the code by:
|
| 57 |
-
|
| 58 |
-
```
|
| 59 |
-
spark-submit streaming.py
|
| 60 |
-
```
|
| 61 |
-
|
| 62 |
-
It will print the logs on the terminal and produce the following three files:
|
| 63 |
-
|
| 64 |
-
- `logs.log`: the logs.
|
| 65 |
-
|
| 66 |
-
- `DD_order.bin`: the DD-order which is a list of size `n`. Use `pickle.load()` to recover it. (This file is produced only when `ddorderfile` is NOT specified in the config.)
|
| 67 |
-
|
| 68 |
-
- `samples.bin`: the sampling results which is a list of size `target*k`. Use `pickle.load()` to recover it.
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
## gen_edge.cpp
|
| 73 |
-
|
| 74 |
-
This program is to generate the edge list of a random undirected graph. It takes 2 arguments `n` and `p`, indicating the number of vertices and the probability of each edge.
|
| 75 |
-
|
| 76 |
-
G++ is the recommended compiler for this code.
|
| 77 |
-
|
| 78 |
-
```
|
| 79 |
-
g++ gen_edge.cpp -o gen_edge
|
| 80 |
-
```
|
| 81 |
-
|
| 82 |
-
It produces a file `edge_list.csv` in the same directory. Then, for example, run
|
| 83 |
-
|
| 84 |
-
```
|
| 85 |
-
./gen_edge 10 0.5
|
| 86 |
-
```
|
| 87 |
-
|
| 88 |
-
## reformat.py
|
| 89 |
-
|
| 90 |
-
This is to reformat the dataset from http://konect.cc/networks/ , removing duplicated edges and self-loops.
|
| 91 |
-
|
| 92 |
-
The usage is
|
| 93 |
-
|
| 94 |
-
```
|
| 95 |
-
python reformat.py <INPUT_FILE> <OUTPUT_FILE>
|
| 96 |
-
```
|
| 97 |
-
|
| 98 |
-
For example,
|
| 99 |
-
|
| 100 |
-
```
|
| 101 |
-
python reformat.py out.edit-biwikibooks edge_list.csv
|
| 102 |
-
```
|
| 103 |
-
|
| 104 |
-
## probability.py
|
| 105 |
-
|
| 106 |
-
This is to print the probability of each $k$-graphlet in a log file.
|
| 107 |
-
|
| 108 |
-
The code takes one argument as the binary file containing the sampling result (for example, `samples.bin`). Then, run
|
| 109 |
-
|
| 110 |
-
```
|
| 111 |
-
python probability.py samples.bin
|
| 112 |
-
```
|
| 113 |
-
|
| 114 |
-
It will produce a file `probability.txt` showing the probability of each $k$-graphlet in the log file.
|
| 115 |
-
|
| 116 |
-
## memory_monitor.py
|
| 117 |
-
|
| 118 |
-
This is to record the memory consumption of the code.
|
| 119 |
-
|
| 120 |
-
```
|
| 121 |
-
python memory_monitor.py '<command>' <id string>
|
| 122 |
-
```
|
| 123 |
-
|
| 124 |
-
`command` is the task we need to run, and `id string` is to identify the task so that you can run memory monitor for multiple tasks. It records the virtual memory of the task every 10 seconds by looking at `ps aux` and prints the records to `memory<id number>.bin` every 100 seconds (use `pickle.load()` to recover it). Finally it prints the maximum memory usage on the terminal.
|
| 125 |
-
|
| 126 |
-
For example,
|
| 127 |
-
|
| 128 |
-
```
|
| 129 |
-
python memory_monitor.py 'python streaming.py' 123
|
| 130 |
-
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
annotations/annotation_144/EfficientStreamingAlgor/config.yml
DELETED
|
@@ -1,7 +0,0 @@
|
|
| 1 |
-
n: 4 # 顶点数
|
| 2 |
-
edgelistmode: local # 读取模式:local 用 csv.reader
|
| 3 |
-
edgelistfile: EfficientStreamingAlgor/tiny_edges.csv # 刚才保存的小图
|
| 4 |
-
MAX_EDGES: 100 # 最大边数
|
| 5 |
-
k: 3 # 采样子图大小
|
| 6 |
-
epsilon: 0.5 # 近似参数
|
| 7 |
-
target: 5 # 采样子图数量
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
annotations/annotation_144/EfficientStreamingAlgor/gen_edge.cpp
DELETED
|
@@ -1,36 +0,0 @@
|
|
| 1 |
-
#include<bits/stdc++.h>
|
| 2 |
-
using namespace std;
|
| 3 |
-
|
| 4 |
-
const int MAXR=1e5;
|
| 5 |
-
|
| 6 |
-
int ran(int a,int b) {
|
| 7 |
-
uniform_int_distribution<int> u(a,b);
|
| 8 |
-
static default_random_engine e(time(0));
|
| 9 |
-
return u(e);
|
| 10 |
-
}
|
| 11 |
-
|
| 12 |
-
int main(int argc,char *argv[]) {
|
| 13 |
-
int n=atoi(argv[1]);
|
| 14 |
-
double p=atof(argv[2]);
|
| 15 |
-
|
| 16 |
-
// initialize the random generator
|
| 17 |
-
ran(1,100);
|
| 18 |
-
ran(1,100);
|
| 19 |
-
ran(1,100);
|
| 20 |
-
|
| 21 |
-
// generate the edge list
|
| 22 |
-
int degree_of_2=0;
|
| 23 |
-
FILE *f=fopen("edge_list.csv","w");
|
| 24 |
-
fprintf(f,"u,v\n");
|
| 25 |
-
for(int i=1; i<=n; i++)
|
| 26 |
-
for(int j=i+1; j<=n; j++) {
|
| 27 |
-
double x=ran(0,MAXR)*1.0/MAXR;
|
| 28 |
-
if (x<=p) {
|
| 29 |
-
fprintf(f,"%d,%d\n",i,j);
|
| 30 |
-
degree_of_2+=(i==2 || j==2);
|
| 31 |
-
}
|
| 32 |
-
}
|
| 33 |
-
fclose(f);
|
| 34 |
-
|
| 35 |
-
printf("For you to check: the degree of vertex 2 is %d\n",degree_of_2);
|
| 36 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
annotations/annotation_144/EfficientStreamingAlgor/generation.py
DELETED
|
@@ -1,708 +0,0 @@
|
|
| 1 |
-
# --------------------------------------------------------------------------------------
|
| 2 |
-
# Efficient Streaming Algorithms for Graphlet Sampling — Annotated Canonical Code
|
| 3 |
-
# --------------------------------------------------------------------------------------
|
| 4 |
-
# This file implements a practical version of STREAM-UGS (preprocessing + sampling)
|
| 5 |
-
# aligned with the NeurIPS 2024 paper “Efficient Streaming Algorithms for Graphlet
|
| 6 |
-
# Sampling” by Bourreau et al. In particular, it mirrors:
|
| 7 |
-
# • APPROX-DD (Algorithm 1) to compute an approximate DD order (ϑ-DD order),
|
| 8 |
-
# • COMPUTEDISTRIB (Algorithm 2) to build the bucket distribution p and Γ,
|
| 9 |
-
# • SAMPLE (Algorithm 3) with RAND-GROW and PROB for rejection sampling.
|
| 10 |
-
#
|
| 11 |
-
# Where this implementation differs:
|
| 12 |
-
# (D1) Heuristic switch for PEEL vs. PEEL_approx:
|
| 13 |
-
# The code auto-chooses between a heuristic peeling (PEEL_heuristic) and the
|
| 14 |
-
# full APPROX-DD-style peeling (PEEL_approx) based on MAX_EDGES and degree
|
| 15 |
-
# thresholds, following the paper’s Appendix E heuristic spirit (APPROX-DD-HEURISTIC),
|
| 16 |
-
# not the exact pseudocode of Algorithm 1. (Paper §3.1, Alg. 1; Appendix E.)
|
| 17 |
-
#
|
| 18 |
-
# (D2) Bucket non-emptiness (1_v) test:
|
| 19 |
-
# The paper’s COMPUTEDISTRIB suggests a truncated BFS to determine whether
|
| 20 |
-
# G(v) contains a k-graphlet and to compute b_v = 1_v·d(v|G(v))^{k−1} in one pass
|
| 21 |
-
# with M = Ω(kn) (Paper §3.2, Alg. 2; Lemma 3.3). This code uses a “light/heavy”
|
| 22 |
-
# bookkeeping plus a DSU pass over the DD order (get_degree_dvGv / get_b) to
|
| 23 |
-
# cheaply approximate the same condition without explicitly running parallel BFSs.
|
| 24 |
-
# This is an implementation choice consistent with the paper’s goal: compute p(v)
|
| 25 |
-
# and Γ efficiently; correctness relies on capturing whether a size-k connected
|
| 26 |
-
# subgraph exists in G(v).
|
| 27 |
-
#
|
| 28 |
-
# (D3) Parallel edge sampling and degree queries:
|
| 29 |
-
# The paper proposes shared, batched routines (SUBDEGREES/CHECKEDGES/
|
| 30 |
-
# SAMPLEEDGES) to serve many trials (Theorem 3.4; Appendix C). Here,
|
| 31 |
-
# get_degree_duGv + RAND_GROW_fast embody the same idea: prepare batched
|
| 32 |
-
# queries per u and per anchor v, push them through one or a few full passes,
|
| 33 |
-
# then consume “answers” to draw uniform edges from cuts E_i. The names differ
|
| 34 |
-
# but the intent matches (Paper §3.3; Algorithm 3; Appendix C).
|
| 35 |
-
#
|
| 36 |
-
# (D4) Edge-list backends:
|
| 37 |
-
# The paper assumes a semi-streaming model (sequential passes). This file supports
|
| 38 |
-
# several backends:
|
| 39 |
-
# - Edge_list_local: plain CSV iteration (fits paper’s pass model),
|
| 40 |
-
# - Edge_list_DataFrame: Spark DataFrame iterator (convenience backend),
|
| 41 |
-
# - Edge_list_memory: pre-loads the CSV in RAM (not semi-streaming strict).
|
| 42 |
-
# Use the ‘local’ mode to match streaming behavior most closely. (Paper §3.)
|
| 43 |
-
#
|
| 44 |
-
# (D5) Acceptance constant Γ and p(v):
|
| 45 |
-
# Γ = 1 / ((k−1)!·Z·(1+ε)^{k−1}) and p(v) ∝ 1_v·d(v|G(v))^{k−1} directly follow
|
| 46 |
-
# the paper (Algorithm 2 lines 4–7 and Algorithm 3 acceptance rule). (Paper §3.2,
|
| 47 |
-
# Alg. 2; §3.3, Alg. 3; Lemma C.1.)
|
| 48 |
-
#
|
| 49 |
-
# For quick paper mapping:
|
| 50 |
-
# • get_DD_order / PEEL_approx ≈ APPROX-DD (Paper §3.1, Alg. 1)
|
| 51 |
-
# • sampling_preprocess (build p and Γ) ≈ COMPUTEDISTRIB (Paper §3.2, Alg. 2)
|
| 52 |
-
# • RAND_GROW / RAND_GROW_fast and PROB ≈ SAMPLE (Paper §3.3, Alg. 3)
|
| 53 |
-
#
|
| 54 |
-
# References in comments: sections/algorithms point to the paper’s main text and appendices.
|
| 55 |
-
# --------------------------------------------------------------------------------------
|
| 56 |
-
# Paper: “Efficient Streaming Algorithms for Graphlet Sampling” (NeurIPS 2024)
|
| 57 |
-
# • APPROX-DD (Algorithm 1), COMPUTEDISTRIB (Algorithm 2), SAMPLE (Algorithm 3),
|
| 58 |
-
# §1–§3; Lemma 3.3; Theorem 3.4; Appendix C (shared routines).
|
| 59 |
-
# • Heuristic (Appendix E).
|
| 60 |
-
# :contentReference[oaicite:1]{index=1}
|
| 61 |
-
# --------------------------------------------------------------------------------------
|
| 62 |
-
|
| 63 |
-
# (Optional) Spark imports are commented; enable only if using DataFrame backend
|
| 64 |
-
# from pyspark.sql import SparkSession
|
| 65 |
-
# from pyspark.sql import functions as sf
|
| 66 |
-
# from pyspark.sql.types import *
|
| 67 |
-
|
| 68 |
-
import math, random, heapq
|
| 69 |
-
from bisect import bisect_left, bisect
|
| 70 |
-
import yaml, csv, logging, sys, pickle
|
| 71 |
-
import numpy as np
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
class DSU() :
|
| 75 |
-
"""
|
| 76 |
-
Disjoint Set Union over vertices in DD order to support COMPUTEDISTRIB-like logic.
|
| 77 |
-
|
| 78 |
-
Purpose in this code:
|
| 79 |
-
• Used in get_b() to determine 1_v (i.e., whether G(v) contains any size-k graphlet).
|
| 80 |
-
The union structure merges “light” neighbors discovered in get_degree_dvGv().
|
| 81 |
-
It also tracks, per set, the largest “last_heavy” rank seen upstream.
|
| 82 |
-
• This approximates the one-pass bucket non-emptiness check described in the paper’s
|
| 83 |
-
Lemma 3.3 (Paper §3.2; Alg. 2), without running explicit BFS for each v.
|
| 84 |
-
|
| 85 |
-
Fields per vertex:
|
| 86 |
-
- root: DSU parent/root
|
| 87 |
-
- size: component size of “light” adjacency seen so far
|
| 88 |
-
- last_heavy: max DD rank of a heavy neighbor discovered for nodes in the set
|
| 89 |
-
"""
|
| 90 |
-
def __init__(self, n, last_heavy) :
|
| 91 |
-
self.n = n
|
| 92 |
-
# a[i] = (root=i, size=1, last_heavy[i]) for i in [0..n]
|
| 93 |
-
# NOTE: vertices are expected to be numbered 1..n; index 0 is present but unused.
|
| 94 |
-
self.a = np.array([(i, 1, last_heavy[i]) for i in range(n+1)], dtype=[('root',int), ('size',int), ('last_heavy',int)])
|
| 95 |
-
|
| 96 |
-
def get_root(self, x) :
|
| 97 |
-
# Standard path-compressed find
|
| 98 |
-
if self.a[x]['root']==x :
|
| 99 |
-
return x
|
| 100 |
-
else :
|
| 101 |
-
self.a[x]['root'] = self.get_root(self.a[x]['root'])
|
| 102 |
-
return self.a[x]['root']
|
| 103 |
-
|
| 104 |
-
def merge(self, x, y) :
|
| 105 |
-
# Union by attaching root y under root x; we only need correctness, not rank heuristic
|
| 106 |
-
x_root = self.get_root(x)
|
| 107 |
-
y_root = self.get_root(y)
|
| 108 |
-
if x_root!=y_root :
|
| 109 |
-
self.a[y_root]['root'] = x_root
|
| 110 |
-
self.a[x_root]['size'] += self.a[y_root]['size']
|
| 111 |
-
# “last_heavy” of the component is the max of the two sub-components.
|
| 112 |
-
self.a[x_root]['last_heavy'] = max(self.a[x_root]['last_heavy'], self.a[y_root]['last_heavy'])
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
def next_bit(S, size) :
|
| 116 |
-
"""
|
| 117 |
-
Iterate set bits of S in [0..size-1].
|
| 118 |
-
Used by PROB() to enumerate subsets; mirrors Algorithm 3 lines 18–22
|
| 119 |
-
(Paper §3.3) where subsets S_i are built via bitmasks.
|
| 120 |
-
"""
|
| 121 |
-
for i in range(size) :
|
| 122 |
-
if ((S>>i)&1) :
|
| 123 |
-
yield i
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
class Query() :
|
| 127 |
-
"""
|
| 128 |
-
Helper to batch degree queries per u (candidate expansion vertex) over many anchors v.
|
| 129 |
-
|
| 130 |
-
Used in:
|
| 131 |
-
- get_degree_duGv(): to accumulate, per u, how many neighbors it has in G(v) for multiple v.
|
| 132 |
-
- RAND_GROW_fast(): to relocate queries to the correct anchor v and answer them in one pass.
|
| 133 |
-
|
| 134 |
-
This corresponds to the batched SUBDEGREES/CHECKEDGES idea in Appendix C
|
| 135 |
-
(Paper Theorem 3.4 and §3.3), enabling shared passes across many trials.
|
| 136 |
-
"""
|
| 137 |
-
def __init__(self, u) :
|
| 138 |
-
self.u = u
|
| 139 |
-
self.v_list, self.queries = set(), set()
|
| 140 |
-
|
| 141 |
-
def build_cnt(self) :
|
| 142 |
-
# Prepare prefix-sum container aligned to sorted v_list (descending by DD rank)
|
| 143 |
-
self.v_list = sorted(list(self.v_list))
|
| 144 |
-
self.cnt = [0]*(len(self.v_list)+1)
|
| 145 |
-
|
| 146 |
-
def build_queries_for_v(self, degree_duGv, DD_order) :
|
| 147 |
-
"""
|
| 148 |
-
Transform absolute query ids into per-v groups, arranged so that a single streaming pass
|
| 149 |
-
can satisfy the “j-th incident edge” queries (Algorithm SAMPLEEDGES in Appendix C).
|
| 150 |
-
"""
|
| 151 |
-
self.num_index = [0]+[degree_duGv[(DD_order[v], self.u)] for v in reversed(self.v_list)]
|
| 152 |
-
self.queries_for_v = [[] for v in self.v_list]
|
| 153 |
-
vi = 0
|
| 154 |
-
for q in sorted(list(self.queries), reverse=True) :
|
| 155 |
-
while vi+1<len(self.v_list) and q<=self.num_index[-(vi+2)] :
|
| 156 |
-
vi += 1
|
| 157 |
-
self.queries_for_v[vi].append(q)
|
| 158 |
-
self.queries = None
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
class Edge_list_local() :
|
| 162 |
-
"""
|
| 163 |
-
Streaming-friendly edge list: sequential CSV scan per pass.
|
| 164 |
-
Matches the semi-streaming pass model assumed by the paper (§3).
|
| 165 |
-
CSV columns: header row then rows of "u,v".
|
| 166 |
-
"""
|
| 167 |
-
def __init__(self, edge_list_file) :
|
| 168 |
-
self.edge_list_file = edge_list_file
|
| 169 |
-
self.passes = 0
|
| 170 |
-
|
| 171 |
-
def get_edge(self) :
|
| 172 |
-
self.passes += 1
|
| 173 |
-
with open(self.edge_list_file, 'r') as f :
|
| 174 |
-
edge_list = csv.reader(f, delimiter=',')
|
| 175 |
-
next(edge_list)
|
| 176 |
-
for row in edge_list :
|
| 177 |
-
yield int(row[0]), int(row[1])
|
| 178 |
-
|
| 179 |
-
def end(self) :
|
| 180 |
-
x = 0
|
| 181 |
-
|
| 182 |
-
class Edge_list_DataFrame() :
|
| 183 |
-
"""
|
| 184 |
-
Spark DataFrame backend (convenience for large files).
|
| 185 |
-
Not strictly required by the streaming model; ensure pyspark is available and
|
| 186 |
-
uncomment imports at the top if you use this mode.
|
| 187 |
-
"""
|
| 188 |
-
def __init__(self, edge_list_file) :
|
| 189 |
-
self.spark = SparkSession.builder.getOrCreate()
|
| 190 |
-
self.spark.sparkContext.setLogLevel("WARN")
|
| 191 |
-
self.edge_list = self.spark.read.csv(edge_list_file, header=True, inferSchema=True)
|
| 192 |
-
self.passes = 0
|
| 193 |
-
|
| 194 |
-
def get_edge(self) :
|
| 195 |
-
self.passes += 1
|
| 196 |
-
for e in self.edge_list.toLocalIterator() :
|
| 197 |
-
yield e['u'], e['v']
|
| 198 |
-
|
| 199 |
-
def end(self) :
|
| 200 |
-
self.spark.stop()
|
| 201 |
-
|
| 202 |
-
class Edge_list_StreamingDataFrame() :
|
| 203 |
-
# Placeholder for a fully streaming DF backend (not implemented)
|
| 204 |
-
# TODO: implement if needed; otherwise use 'local' for paper-faithful passes.
|
| 205 |
-
def __init__(self, edge_list_file) :
|
| 206 |
-
x = 0
|
| 207 |
-
|
| 208 |
-
class Edge_list_memory() :
|
| 209 |
-
"""
|
| 210 |
-
In-memory edge list. Deviates from strict semi-streaming assumptions (the paper’s
|
| 211 |
-
model expects sublinear memory). Useful for debugging or small graphs.
|
| 212 |
-
"""
|
| 213 |
-
def __init__(self, edge_list_file) :
|
| 214 |
-
with open(edge_list_file, 'r') as f :
|
| 215 |
-
edge_list = csv.reader(f, delimiter=',')
|
| 216 |
-
next(edge_list)
|
| 217 |
-
self.edge_list = list(edge_list)
|
| 218 |
-
self.passes = 0
|
| 219 |
-
|
| 220 |
-
def get_edge(self) :
|
| 221 |
-
self.passes += 1
|
| 222 |
-
for e in self.edge_list :
|
| 223 |
-
yield int(e[0]), int(e[1])
|
| 224 |
-
|
| 225 |
-
def end(self) :
|
| 226 |
-
x = 0
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
class Graph() :
|
| 230 |
-
"""
|
| 231 |
-
Core class implementing:
|
| 232 |
-
• APPROX-DD-like preprocessing (get_DD_order / PEEL_approx / PEEL_heuristic),
|
| 233 |
-
• COMPUTEDISTRIB-like distribution build (sampling_preprocess),
|
| 234 |
-
• SAMPLE-like sampling (RAND_GROW_fast + PROB + sample loop).
|
| 235 |
-
|
| 236 |
-
Key paper references:
|
| 237 |
-
- Approx DD order (Paper §3.1, Alg. 1; Lemma A.1 etc.),
|
| 238 |
-
- Distribution p and Γ (Paper §3.2, Alg. 2),
|
| 239 |
-
- Sampling (Paper §3.3, Alg. 3; Appendix C for shared routines).
|
| 240 |
-
"""
|
| 241 |
-
def __init__(self, n, edgelistmode, edge_list_file, MAX_EDGES) :
|
| 242 |
-
self.n = n
|
| 243 |
-
edge_list_class = {'local': Edge_list_local,
|
| 244 |
-
'DataFrame': Edge_list_DataFrame,
|
| 245 |
-
'StreamingDataFrame': Edge_list_StreamingDataFrame,
|
| 246 |
-
'memory': Edge_list_memory}
|
| 247 |
-
self.edge_list = edge_list_class[edgelistmode](edge_list_file)
|
| 248 |
-
self.MAX_EDGES = MAX_EDGES
|
| 249 |
-
|
| 250 |
-
def get_degree_G(self, vertices) :
|
| 251 |
-
"""
|
| 252 |
-
Compute degrees in the induced subgraph G[vertices] by one pass (streaming).
|
| 253 |
-
This is the primitive the paper denotes as COMPUTEALLDEG (Paper §3.1),
|
| 254 |
-
incurring a single pass whenever recomputation is needed in APPROX-DD.
|
| 255 |
-
"""
|
| 256 |
-
if (len(vertices)<self.n/10) :
|
| 257 |
-
degree = {x: 0 for x in vertices}
|
| 258 |
-
else :
|
| 259 |
-
degree = np.zeros(self.n+1, dtype=int)
|
| 260 |
-
for x, y in self.edge_list.get_edge() :
|
| 261 |
-
if (x in vertices and y in vertices) :
|
| 262 |
-
degree[x] += 1
|
| 263 |
-
degree[y] += 1
|
| 264 |
-
return degree
|
| 265 |
-
|
| 266 |
-
def get_degree_H(self, partition) :
|
| 267 |
-
"""
|
| 268 |
-
Compute, for H partitioned into ℓ random buckets H_i, the internal degree
|
| 269 |
-
d(v | G[H_i]) used by SHAVE (Paper §3.1, Alg. 1 lines 15–19).
|
| 270 |
-
"""
|
| 271 |
-
if (len(partition)<self.n/10) :
|
| 272 |
-
degree = {x: 0 for x in partition}
|
| 273 |
-
else :
|
| 274 |
-
degree = np.zeros(self.n+1, dtype=int)
|
| 275 |
-
for x, y in self.edge_list.get_edge() :
|
| 276 |
-
if (partition.get(x,-1)==partition.get(y,-2)) :
|
| 277 |
-
degree[x] += 1
|
| 278 |
-
degree[y] += 1
|
| 279 |
-
return degree
|
| 280 |
-
|
| 281 |
-
def PEEL_approx(self, vertices, degree_G, epsilon, alpha, max_degree, DD_order) :
|
| 282 |
-
"""
|
| 283 |
-
APPROX-DD (Algorithm 1) core: SHAVE loop with random partitioning.
|
| 284 |
-
• β and ℓ = ⌈2(1+β)⌉ as in Alg. 1 line 14; threshold_ub = Δ/(1+β).
|
| 285 |
-
• H = {v : d(v|G) ≥ Δ/(1+α)} (Alg. 1 line 9).
|
| 286 |
-
• Each SHAVE iteration: partition H into ℓ parts; remove R_i ⊂ H_i with
|
| 287 |
-
d(v|G[H_i]) ≤ Δ/(1+β), but only those still meeting d(v|G) ≥ Δ/(1+α)
|
| 288 |
-
(Alg. 1 lines 16–21). (Paper §3.1; Lemma 3.2; Appendix A.)
|
| 289 |
-
"""
|
| 290 |
-
beta = 2/epsilon+2+epsilon
|
| 291 |
-
l = math.ceil(2*(1+beta))
|
| 292 |
-
threshold_lb, threshold_ub = max_degree/(1+alpha), max_degree/(1+beta)
|
| 293 |
-
H = {x for x in vertices if degree_G[x]>=threshold_lb}
|
| 294 |
-
while H :
|
| 295 |
-
# SHAVE: uniform random partition H -> l buckets
|
| 296 |
-
partition = {x: random.randint(0,l-1) for x in H}
|
| 297 |
-
degree_H = self.get_degree_H(partition)
|
| 298 |
-
R = {x for x in H if degree_H[x]<=threshold_ub}
|
| 299 |
-
H -= R
|
| 300 |
-
group = [[] for i in range(l)]
|
| 301 |
-
for x in R :
|
| 302 |
-
group[partition[x]].append(x)
|
| 303 |
-
partition, degree_H = None, None
|
| 304 |
-
for i in range(l) :
|
| 305 |
-
# Remove only those still above the global lower bound, align with Alg. 1 line 18
|
| 306 |
-
Si = set(x for x in group[i] if degree_G[x]>=threshold_lb)
|
| 307 |
-
if (not Si) :
|
| 308 |
-
continue
|
| 309 |
-
# Appending Si to DD_order matches Alg. 1 line 19 (“append ... in arbitrary order”)
|
| 310 |
-
DD_order.extend(Si)
|
| 311 |
-
vertices -= Si
|
| 312 |
-
# Recompute degrees after deletions: another pass, consistent with paper comments
|
| 313 |
-
degree_G = self.get_degree_G(vertices)
|
| 314 |
-
return degree_G
|
| 315 |
-
|
| 316 |
-
def PEEL_heuristic(self, vertices, heap, degree_G, threshold_lb, DD_order) :
|
| 317 |
-
"""
|
| 318 |
-
Heuristic peeling: greedily remove current max-degree vertices under a pass budget.
|
| 319 |
-
This mirrors Appendix E’s APPROX-DD-HEURISTIC: when subgraphs fit in
|
| 320 |
-
memory (or under MAX_EDGES), process a large chunk with fewer passes.
|
| 321 |
-
(Paper Appendix E; Fig. 1 shows the practical benefit on sparse graphs.)
|
| 322 |
-
"""
|
| 323 |
-
if (len(heap)<self.n/10) :
|
| 324 |
-
edges = {x: [] for d,x in heap}
|
| 325 |
-
else :
|
| 326 |
-
edges = [[] for x in range(self.n+1)]
|
| 327 |
-
# Build adjacency limited to candidate range (one pass)
|
| 328 |
-
for x, y in self.edge_list.get_edge() :
|
| 329 |
-
if (x in vertices and y in vertices and (-degree_G[x],x)<=heap[-1] and (-degree_G[y],y)<=heap[-1]) :
|
| 330 |
-
edges[x].append(y)
|
| 331 |
-
edges[y].append(x)
|
| 332 |
-
# Greedy removal, pushing updates via a heap (local relaxation of Alg. 1)
|
| 333 |
-
# NOTE: Saves passes vs. recomputing full degrees after every deletion.
|
| 334 |
-
# Stop when degree falls below threshold_lb for the remaining candidates.
|
| 335 |
-
# (This threshold is Δ/(1+α) derived from current Δ.)
|
| 336 |
-
while heap and -heap[0][0]>=threshold_lb:
|
| 337 |
-
top = heapq.heappop(heap)
|
| 338 |
-
if (-top[0]!=degree_G[top[1]]) :
|
| 339 |
-
continue
|
| 340 |
-
DD_order.append(top[1])
|
| 341 |
-
vertices.remove(top[1])
|
| 342 |
-
for y in edges[top[1]] :
|
| 343 |
-
if y in vertices :
|
| 344 |
-
degree_G[y] -= 1
|
| 345 |
-
heapq.heappush(heap,(-degree_G[y], y))
|
| 346 |
-
|
| 347 |
-
def get_DD_order(self, epsilon, enable_heuristic = True) :
|
| 348 |
-
"""
|
| 349 |
-
Compute an approximate DD ordering (ϑ = 1/(1+ε) DD order).
|
| 350 |
-
This function chooses between:
|
| 351 |
-
- PEEL_heuristic (Appendix E) when a large prefix can be handled with fewer passes,
|
| 352 |
-
- PEEL_approx (Algorithm 1) otherwise.
|
| 353 |
-
Both produce an ordering consistent with Lemma A.1. (Paper §3.1, Alg. 1; App. E.)
|
| 354 |
-
"""
|
| 355 |
-
logging.info('Computing approx DD-ordering...')
|
| 356 |
-
|
| 357 |
-
vertices = set(range(1, self.n+1))
|
| 358 |
-
|
| 359 |
-
DD_order = []
|
| 360 |
-
alpha = epsilon/2
|
| 361 |
-
degree_G = self.get_degree_G(vertices)
|
| 362 |
-
while vertices :
|
| 363 |
-
if enable_heuristic :
|
| 364 |
-
# Decide which PEEL to run: estimate how many high-degree vertices we can
|
| 365 |
-
# safely process (bounded by MAX_EDGES to control per-iteration work).
|
| 366 |
-
vertex_list = sorted([(-degree_G[x],x) for x in vertices])
|
| 367 |
-
if (len(vertices)<self.n/10) :
|
| 368 |
-
degree_dvGRv = {x: 0 for x in vertices}
|
| 369 |
-
else :
|
| 370 |
-
degree_dvGRv = np.zeros(self.n+1, dtype=int)
|
| 371 |
-
for x, y in self.edge_list.get_edge() :
|
| 372 |
-
if (x in vertices and y in vertices) :
|
| 373 |
-
degree_dvGRv[x if (-degree_G[y],y)<(-degree_G[x],x) else y] += 1
|
| 374 |
-
prefix_sum_degree, prefix_ind = 0, len(vertex_list)
|
| 375 |
-
for i, (d,x) in enumerate(vertex_list) :
|
| 376 |
-
prefix_sum_degree+=degree_dvGRv[x]
|
| 377 |
-
if (prefix_sum_degree>self.MAX_EDGES) :
|
| 378 |
-
prefix_ind = i
|
| 379 |
-
break
|
| 380 |
-
degree_dvGRv = None
|
| 381 |
-
|
| 382 |
-
max_degree = -vertex_list[0][0]
|
| 383 |
-
threshold_lb = max_degree/(1+alpha)
|
| 384 |
-
if (prefix_ind==len(vertex_list) or (-vertex_list[prefix_ind][0])<=threshold_lb) :
|
| 385 |
-
# Heuristic block: process a big chunk greedily (Appendix E)
|
| 386 |
-
logging.info('** PEEL_heuristic for %d vertices, max degree %d, select %d vertices', len(vertices), max_degree, prefix_ind)
|
| 387 |
-
self.PEEL_heuristic(vertices, vertex_list[0:prefix_ind], degree_G,
|
| 388 |
-
((-vertex_list[prefix_ind][0])/(1+epsilon) if prefix_ind<len(vertex_list) else 0),
|
| 389 |
-
DD_order)
|
| 390 |
-
degree_G = self.get_degree_G(vertices)
|
| 391 |
-
else :
|
| 392 |
-
# Full APPROX-DD style SHAVE (Algorithm 1)
|
| 393 |
-
logging.info('** PEEL_approx for %d vertices, max degree %d', len(vertices), max_degree)
|
| 394 |
-
degree_G = self.PEEL_approx(vertices, degree_G, epsilon, alpha, max_degree, DD_order)
|
| 395 |
-
else :
|
| 396 |
-
# Force the exact APPROX-DD approach (Algorithm 1)
|
| 397 |
-
max_degree = max(degree_G.values()) if type(degree_G) is dict else degree_G.max()
|
| 398 |
-
logging.info('** PEEL_approx for %d vertices, max degree %d', len(vertices), max_degree)
|
| 399 |
-
degree_G = self.PEEL_approx(vertices, degree_G, epsilon, alpha, max_degree, DD_order)
|
| 400 |
-
|
| 401 |
-
return DD_order
|
| 402 |
-
|
| 403 |
-
def get_degree_dvGv(self, k) :
|
| 404 |
-
"""
|
| 405 |
-
Build oriented adjacency summaries wrt DD order to support “light/heavy” tests.
|
| 406 |
-
For each x, we track:
|
| 407 |
-
- degree[x] = d(x|G(x)) as vertices with rank ≥ rank(x) arrive in the scan,
|
| 408 |
-
- light_edge_list[x]: early neighbors kept while degree[x] ≤ (k−2),
|
| 409 |
-
- last_heavy[x]: max DD rank at which x (or its light neighbors) see a heavy push.
|
| 410 |
-
This enables computing b_x = 1_x · d(x|G(x))^{k−1} without per-v BFS
|
| 411 |
-
(Paper §3.2, Alg. 2; Lemma 3.3). It’s an implementation detail not explicitly
|
| 412 |
-
prescribed, but consistent with detecting 1_v.
|
| 413 |
-
"""
|
| 414 |
-
degree, light_edge_list, last_heavy = np.zeros(self.n+1, dtype=int), [[] for x in range(self.n+1)], [-1]*(self.n+1)
|
| 415 |
-
for x, y in self.edge_list.get_edge() :
|
| 416 |
-
# Orient edge from lower DD rank to higher DD rank to respect G(v) subgraph definition
|
| 417 |
-
if (self.DD_ranking[x]>self.DD_ranking[y]) :
|
| 418 |
-
x, y = y, x
|
| 419 |
-
degree[x] += 1
|
| 420 |
-
if degree[x]<=k-2 :
|
| 421 |
-
light_edge_list[x].append(y)
|
| 422 |
-
else :
|
| 423 |
-
# When x becomes heavy, propagate last_heavy to neighbors seen while light
|
| 424 |
-
last_heavy[x] = self.DD_ranking[x]
|
| 425 |
-
if not (light_edge_list[x] is None) :
|
| 426 |
-
for z in light_edge_list[x] :
|
| 427 |
-
last_heavy[z]=max(last_heavy[z], self.DD_ranking[x])
|
| 428 |
-
light_edge_list[x] = None
|
| 429 |
-
last_heavy[y]=max(last_heavy[y], self.DD_ranking[x])
|
| 430 |
-
return degree, light_edge_list, last_heavy
|
| 431 |
-
|
| 432 |
-
def get_b(self, degree_dvGv, light_edge_list, last_heavy, k) :
|
| 433 |
-
"""
|
| 434 |
-
Compute b[x] = 1_x · d(x|G(x))^{k−1} used to form p(x) and Z = Σ b[x].
|
| 435 |
-
1_x is set by:
|
| 436 |
-
- If degree[x] ≥ k−1: bucket non-empty (trivially x has ≥k−1 forward neighbors).
|
| 437 |
-
- Otherwise: union the “light” edges via DSU; if DSU component size ≥ k or
|
| 438 |
-
there exists a “last_heavy” rank ≥ current rank, then non-empty.
|
| 439 |
-
This tracks the paper’s intuition (Paper §3.2): 1_v detects existence of any size-k
|
| 440 |
-
connected induced subgraph in G(v).
|
| 441 |
-
"""
|
| 442 |
-
b = [0]*(self.n+1)
|
| 443 |
-
dsu = DSU(self.n, last_heavy)
|
| 444 |
-
for i in range(self.n-1, -1, -1) :
|
| 445 |
-
x = self.DD_order[i]
|
| 446 |
-
if degree_dvGv[x]<=k-2 :
|
| 447 |
-
# Merge light edges of x; if comp is big enough or touches a heavy layer, set 1_x=1
|
| 448 |
-
for y in light_edge_list[x] :
|
| 449 |
-
dsu.merge(x,y)
|
| 450 |
-
b[x] = pow(int(degree_dvGv[x]),k-1) if (dsu.a[x]['size']>=k or dsu.a[x]['last_heavy']>=i) else 0
|
| 451 |
-
else :
|
| 452 |
-
b[x] = pow(int(degree_dvGv[x]),k-1)
|
| 453 |
-
return b
|
| 454 |
-
|
| 455 |
-
def sampling_preprocess(self, k, epsilon, ddorder = None, enable_heuristic = True) :
|
| 456 |
-
"""
|
| 457 |
-
COMPUTEDISTRIB (Algorithm 2): compute DD order (if needed), then build:
|
| 458 |
-
- DD_ranking, d(v|G(v)) surrogates via get_degree_dvGv,
|
| 459 |
-
- b[v], Z = Σ b, p[v] = b[v]/Z,
|
| 460 |
-
- Γ = 1 / ((k−1)! · Z · (1+ε)^{k−1}).
|
| 461 |
-
This matches lines 2–8 of Algorithm 2. (Paper §3.2; Lemma 3.3 for 1_v test.)
|
| 462 |
-
"""
|
| 463 |
-
if ddorder is None :
|
| 464 |
-
self.DD_order = self.get_DD_order(epsilon, enable_heuristic)
|
| 465 |
-
logging.info('Number of passes for DD-ordering = %s', self.edge_list.passes)
|
| 466 |
-
else :
|
| 467 |
-
self.DD_order = ddorder
|
| 468 |
-
logging.info('Sampling preprocessing...')
|
| 469 |
-
self.DD_ranking = np.empty(self.n+1, dtype=int)
|
| 470 |
-
self.DD_ranking[self.DD_order] = np.arange(self.n)
|
| 471 |
-
degree_dvGv, light_edge_list, last_heavy = self.get_degree_dvGv(k)
|
| 472 |
-
b = self.get_b(degree_dvGv, light_edge_list, last_heavy, k)
|
| 473 |
-
degree_dvGv, light_edge_list, last_heavy = None, None, None
|
| 474 |
-
Z = sum(b)
|
| 475 |
-
# p[v] is over indices 0..n; vertex 0 remains 0-probability (IDs expected 1..n)
|
| 476 |
-
self.p = [b[x]/Z for x in range(self.n+1)]
|
| 477 |
-
# Γ per Algorithm 2, line 7; (1+ε)^{k−1} comes from using a ϑ-DD order (Paper §2 Def. 2.1; Lemma C.1)
|
| 478 |
-
self.Gamma = 1/(math.factorial(k-1)*Z*pow(1+epsilon,k-1))
|
| 479 |
-
|
| 480 |
-
def get_degree_duGv(self, v, samples) :
|
| 481 |
-
"""
|
| 482 |
-
Batched degree queries for many instances:
|
| 483 |
-
• For each trial j anchored at v[j], and current sample S_j, compute
|
| 484 |
-
d(u | G(v[j])) for u ∈ S_j, while also marking which (x,y) edges in S_j exist.
|
| 485 |
-
Mirrors SUBDEGREES + CHECKEDGES combo (Paper Appendix C, Lemma C.2–C.3)
|
| 486 |
-
but fused into one routine for efficiency.
|
| 487 |
-
Returns:
|
| 488 |
-
- degree_duGv: dict keyed by (v_anchor, u) -> d(u | G(v_anchor))
|
| 489 |
-
- S: dict for sampled_edges_tot[(x,y)] == True if edge found in S_j
|
| 490 |
-
"""
|
| 491 |
-
L, S = {}, {}
|
| 492 |
-
batch_size = len(samples)
|
| 493 |
-
for j in range(batch_size) :
|
| 494 |
-
for u in samples[j] :
|
| 495 |
-
if not (u in L) :
|
| 496 |
-
L[u] = Query(u)
|
| 497 |
-
L[u].v_list.add(self.DD_ranking[v[j]])
|
| 498 |
-
S.update({(u,y): False for y in samples[j]})
|
| 499 |
-
for u, Lu in L.items() :
|
| 500 |
-
Lu.build_cnt()
|
| 501 |
-
# Stream once: update counts per (u, v) bucket and mark edges inside S_j
|
| 502 |
-
for x, y in self.edge_list.get_edge() :
|
| 503 |
-
if x in L: L[x].cnt[bisect(L[x].v_list, self.DD_ranking[y])-1] += 1
|
| 504 |
-
if y in L: L[y].cnt[bisect(L[y].v_list, self.DD_ranking[x])-1] += 1
|
| 505 |
-
if (x,y) in S :
|
| 506 |
-
S[(x,y)] = S[(y,x)] = True
|
| 507 |
-
# Convert cumulative counts to suffix sums (degrees in G(v))
|
| 508 |
-
for u, Lu in L.items() :
|
| 509 |
-
for l in range(len(Lu.v_list)-2, -1, -1) :
|
| 510 |
-
Lu.cnt[l] += Lu.cnt[l+1]
|
| 511 |
-
degree_duGv = {(self.DD_order[v], u): Lu.cnt[i] for u, Lu in L.items() for i,v in enumerate(Lu.v_list)}
|
| 512 |
-
return degree_duGv, S
|
| 513 |
-
|
| 514 |
-
def RAND_GROW(self, v, k, batch_size) :
|
| 515 |
-
"""
|
| 516 |
-
Baseline RAND-GROW (Algorithm 3 lines 8–13): grow S by sampling one edge
|
| 517 |
-
uniformly at random from the cut E_i each round. This version streams edges
|
| 518 |
-
and does reservoir sampling per instance (k−1 passes overall).
|
| 519 |
-
Kept here for reference; the fast version below is used by default. (Paper §3.3.)
|
| 520 |
-
"""
|
| 521 |
-
samples = [{v[i]} for i in range(batch_size)]
|
| 522 |
-
for i in range(k-1) :
|
| 523 |
-
cnt, sampled_edge = [0]*batch_size, [None]*batch_size
|
| 524 |
-
for x, y in self.edge_list.get_edge() :
|
| 525 |
-
for j in range(batch_size) :
|
| 526 |
-
if (x in samples[j]) and (not (y in samples[j])) and (self.DD_ranking[y]>=self.DD_ranking[v[j]]) \
|
| 527 |
-
or (y in samples[j]) and (not (x in samples[j])) and (self.DD_ranking[x]>=self.DD_ranking[v[j]]) :
|
| 528 |
-
cnt[j] += 1
|
| 529 |
-
if random.randint(1,cnt[j])==1 :
|
| 530 |
-
sampled_edge[j] = [x,y]
|
| 531 |
-
for j in range(batch_size) :
|
| 532 |
-
samples[j].update(sampled_edge[j])
|
| 533 |
-
samples = [list(x) for x in samples]
|
| 534 |
-
return samples
|
| 535 |
-
|
| 536 |
-
def RAND_GROW_fast(self, v, k, batch_size) :
|
| 537 |
-
"""
|
| 538 |
-
Fast RAND-GROW with batched subdegree/edge answering (Appendix C).
|
| 539 |
-
For each round i=2..k:
|
| 540 |
-
• Choose u in S_j with prob ∝ |cut(u, G(v[j]) \ S_j)| using degree_duGv
|
| 541 |
-
minus already-internal edges (sampled_edges_tot).
|
| 542 |
-
• Draw a random edge from that cut uniformly by issuing “j-th edge” queries
|
| 543 |
-
that can be satisfied in one streamed pass (the ‘answers’ dict).
|
| 544 |
-
This compresses per-instance work and shares passes across Q trials (Paper §3.3,
|
| 545 |
-
Theorem 3.4; Appendix C SAMPLEEDGES idea).
|
| 546 |
-
"""
|
| 547 |
-
samples = [[v[i]] for i in range(batch_size)]
|
| 548 |
-
for i in range(2,k+1) :
|
| 549 |
-
degree_duGv, sampled_edges_tot = self.get_degree_duGv(v, samples)
|
| 550 |
-
L, original_query, answers, up = {}, [None]*batch_size, {}, np.zeros(batch_size, dtype=int)
|
| 551 |
-
# --- set up weighted-choices for u and edge-order queries per (u, v) ---
|
| 552 |
-
for j in range(batch_size) :
|
| 553 |
-
# degree_duS[u] = edges from u to current S_j already present
|
| 554 |
-
degree_duS = {x: sum(1 for y in samples[j] if sampled_edges_tot.get((x,y),False)==True) for x in samples[j]}
|
| 555 |
-
# Choose u ∈ S_j with weight d(u|G(v)) − d(u|G[S_j])
|
| 556 |
-
u = random.choices(samples[j], weights=[degree_duGv[(v[j], u)]-degree_duS[u] for u in samples[j]])[0]
|
| 557 |
-
up[j] = u
|
| 558 |
-
# Sample “edge indices” uniformly from 1..d(u|G(v)), request degree_duS[u]+1 of them
|
| 559 |
-
original_query[j] = random.sample(range(1, degree_duGv[(v[j], u)]+1), degree_duS[u]+1)
|
| 560 |
-
if not (u in L) :
|
| 561 |
-
L[u] = Query(u)
|
| 562 |
-
L[u].v_list.add(self.DD_ranking[v[j]])
|
| 563 |
-
L[u].queries.update(original_query[j])
|
| 564 |
-
sampled_edges_tot = None
|
| 565 |
-
# --- reorganize per-anchor v buckets, now we know which (u, v) each query belongs to ---
|
| 566 |
-
for u, Lu in L.items() :
|
| 567 |
-
Lu.build_cnt()
|
| 568 |
-
Lu.build_queries_for_v(degree_duGv, self.DD_order)
|
| 569 |
-
degree_duGv = None
|
| 570 |
-
# --- single stream pass: fulfill “j-th incident edge” queries with prefix counts ---
|
| 571 |
-
for xx, yy in self.edge_list.get_edge() :
|
| 572 |
-
for x, y in [(xx,yy), (yy,xx)] :
|
| 573 |
-
if x in L :
|
| 574 |
-
it = bisect(L[x].v_list, self.DD_ranking[y])-1
|
| 575 |
-
if it>-1 :
|
| 576 |
-
L[x].cnt[it] += 1
|
| 577 |
-
q = L[x].queries_for_v[it]
|
| 578 |
-
while len(q)>0 and q[-1]==L[x].cnt[it]+L[x].num_index[-(it+2)] :
|
| 579 |
-
answers[(x,q[-1])] = y
|
| 580 |
-
q.pop()
|
| 581 |
-
L = None
|
| 582 |
-
# --- finalize this round: pick the first answered edge not already inside S_j ---
|
| 583 |
-
for j in range(batch_size) :
|
| 584 |
-
for eid in original_query[j] :
|
| 585 |
-
u = answers.get((up[j], eid),-1)
|
| 586 |
-
if u!=-1 and not (u in samples[j]) :
|
| 587 |
-
samples[j].append(u)
|
| 588 |
-
break
|
| 589 |
-
|
| 590 |
-
return samples
|
| 591 |
-
|
| 592 |
-
def PROB(self, v, samples, k, batch_size) :
|
| 593 |
-
"""
|
| 594 |
-
Compute q_S, the probability that RAND-GROW(v, ≺) generates set S, as in
|
| 595 |
-
Algorithm 3 PROB (Paper §3.3, lines 15–23). Uses DP over subsets (bitmasks).
|
| 596 |
-
For each trial j:
|
| 597 |
-
- samples_j[1..k-1] = picked nodes beyond v[j],
|
| 598 |
-
- q accumulates path probabilities q_i(S_i), with transitions:
|
| 599 |
-
q[S ∪ {w}] += q[S] * (n_i / c_i)
|
| 600 |
-
where n_i = #edges from w to S, and
|
| 601 |
-
c_i = sum_{z∈S} [ d(z|G(v)) − d(z| G[S \ {w}] ) ].
|
| 602 |
-
The acceptance test uses Γ / (p(v)·q_S) (Algorithm 3 line 5).
|
| 603 |
-
"""
|
| 604 |
-
degree_duGv, sampled_edges_tot = self.get_degree_duGv(v, samples)
|
| 605 |
-
for j in range(batch_size) :
|
| 606 |
-
sampled_edge_list = {(x,y) for x in samples[j] for y in samples[j] if (x<y and sampled_edges_tot.get((x,y),False)==True)}
|
| 607 |
-
samples_j = samples[j][1:k]
|
| 608 |
-
q = [1]+[0]*((1<<(k-1))-1)
|
| 609 |
-
for S in range(1<<(k-1)) :
|
| 610 |
-
Sset = {samples_j[x] for x in next_bit(S,k)}.union({v[j]})
|
| 611 |
-
for x in next_bit(S^(-1),k-1) :
|
| 612 |
-
# c_i: denominator uses degrees in G(v) minus edges inside current S without {w}
|
| 613 |
-
ci = sum(degree_duGv[(v[j],u)] for u in Sset)-2*sum(1 for e in sampled_edge_list if e[0] in Sset and e[1] in Sset)
|
| 614 |
-
# n_i: number of edges linking the candidate w to Sset
|
| 615 |
-
ni = sum(1 for e in sampled_edge_list if e[0]==samples_j[x] and e[1] in Sset or e[1]==samples_j[x] and e[0] in Sset)
|
| 616 |
-
q[S|(1<<x)] += q[S]*ni/ci
|
| 617 |
-
|
| 618 |
-
# Rejection step: accept with prob Γ / (p(v)·q_S) (Paper §3.3; Lemma C.1)
|
| 619 |
-
if random.random()>self.Gamma/(self.p[v[j]]*q[-1]) :
|
| 620 |
-
samples[j] = None
|
| 621 |
-
|
| 622 |
-
def sample_one_batch(self, k, batch_size) :
|
| 623 |
-
"""
|
| 624 |
-
Run many trials in one batch:
|
| 625 |
-
1) Sample anchors v ~ p,
|
| 626 |
-
2) Grow with RAND_GROW_fast,
|
| 627 |
-
3) Filter via PROB (accept/reject).
|
| 628 |
-
Batches realize Theorem 3.4’s parallelism with shared passes. (Paper §3.3.)
|
| 629 |
-
"""
|
| 630 |
-
# Sample anchor vertices v by distribution p (Algorithm 3 line 2)
|
| 631 |
-
v = random.choices(range(self.n+1), weights=self.p, k=batch_size)
|
| 632 |
-
# Grow candidates (Algorithm 3 line 3); fast batched variant
|
| 633 |
-
# samples = self.RAND_GROW(v, k, batch_size)
|
| 634 |
-
samples = self.RAND_GROW_fast(v, k, batch_size)
|
| 635 |
-
# Compute q_S and accept (Algorithm 3 lines 4–6)
|
| 636 |
-
self.PROB(v, samples, k, batch_size)
|
| 637 |
-
return samples
|
| 638 |
-
|
| 639 |
-
def sample(self, k, target) :
|
| 640 |
-
"""
|
| 641 |
-
Keep sampling batches until 'target' uniform k-graphlets accepted.
|
| 642 |
-
Expected success probability per trial is k^{-O(k)} (scaled by (1+ε)^{k−1})
|
| 643 |
-
per Lemma C.1; batching amortizes passes/time (Paper §3.3; Theorem 3.4).
|
| 644 |
-
"""
|
| 645 |
-
# Choose batch size ~ MAX_EDGES / k^2 to cap per-batch edge work
|
| 646 |
-
batch_size, batch_count = self.MAX_EDGES//(k*k), 0
|
| 647 |
-
samples = []
|
| 648 |
-
logging.info('Start sampling with batch size %d ...',batch_size)
|
| 649 |
-
while True:
|
| 650 |
-
samples_batch = self.sample_one_batch(k, batch_size)
|
| 651 |
-
for i, s in enumerate(samples_batch):
|
| 652 |
-
if not (s is None):
|
| 653 |
-
samples.append(s)
|
| 654 |
-
if len(samples) == target :
|
| 655 |
-
logging.info('Sampled successfully after %d trials', batch_count*batch_size+i+1)
|
| 656 |
-
return samples
|
| 657 |
-
batch_count += 1
|
| 658 |
-
logging.info('The %d-th batch. Totally %d trials, %d successful trials.', batch_count, batch_count*batch_size, len(samples))
|
| 659 |
-
|
| 660 |
-
|
| 661 |
-
def load_yaml():
|
| 662 |
-
"""
|
| 663 |
-
Load experiment/config parameters. Typical keys:
|
| 664 |
-
- n: #vertices (assumes vertices labeled 1..n),
|
| 665 |
-
- edgelistmode: 'local' (streaming), 'DataFrame', or 'memory',
|
| 666 |
-
- edgelistfile: CSV with header and columns 'u,v',
|
| 667 |
-
- MAX_EDGES: per-pass work cap to steer heuristic decisions,
|
| 668 |
-
- k: graphlet size,
|
| 669 |
-
- epsilon: ε for DD-order approximation,
|
| 670 |
-
- ddorderfile (optional): load a precomputed DD order,
|
| 671 |
-
- target: #uniform samples to output.
|
| 672 |
-
"""
|
| 673 |
-
with open("EfficientStreamingAlgor/config.yml", "r", encoding="utf-8") as f:
|
| 674 |
-
re = yaml.load(f, Loader=yaml.FullLoader)
|
| 675 |
-
return re
|
| 676 |
-
|
| 677 |
-
|
| 678 |
-
if __name__ == '__main__' :
|
| 679 |
-
|
| 680 |
-
args = load_yaml()
|
| 681 |
-
logging.basicConfig(level=logging.INFO,
|
| 682 |
-
handlers=[logging.FileHandler('logs.log'), logging.StreamHandler(sys.stdout)],
|
| 683 |
-
format="[%(asctime)s %(levelname)s] %(message)s")
|
| 684 |
-
|
| 685 |
-
# initialize: choose an edge list backend (use 'local' for semi-streaming faithfulness)
|
| 686 |
-
graph = Graph(args['n'], args['edgelistmode'], args['edgelistfile'], args['MAX_EDGES'])
|
| 687 |
-
|
| 688 |
-
# preprocess (Paper §3: PREPROCESS calls APPROX-DD then COMPUTEDISTRIB; Alg. 4 in App. A)
|
| 689 |
-
if 'ddorderfile' in args :
|
| 690 |
-
# preprocess with a provided DD order (useful to amortize cost across runs)
|
| 691 |
-
with open(args['ddorderfile'],'rb') as f:
|
| 692 |
-
ddorder = pickle.load(f)
|
| 693 |
-
graph.sampling_preprocess(args['k'], args['epsilon'], ddorder)
|
| 694 |
-
else :
|
| 695 |
-
# compute DD order then compute p and Γ
|
| 696 |
-
graph.sampling_preprocess(args['k'], args['epsilon'])
|
| 697 |
-
with open('DD_order.bin','wb') as f:
|
| 698 |
-
pickle.dump(graph.DD_order, f)
|
| 699 |
-
passes_for_preprocess = graph.edge_list.passes
|
| 700 |
-
|
| 701 |
-
# sample (Paper §3.3: SAMPLE, batched; success count grows linearly with batch trials)
|
| 702 |
-
samples = graph.sample(args['k'], args['target'])
|
| 703 |
-
with open('samples.bin','wb') as f:
|
| 704 |
-
pickle.dump(samples, f)
|
| 705 |
-
logging.info('Number of passes in total = %s', graph.edge_list.passes)
|
| 706 |
-
logging.info('Number of passes for preprocessing = %s', passes_for_preprocess)
|
| 707 |
-
logging.info('Number of passes for sampling = %s', graph.edge_list.passes - passes_for_preprocess)
|
| 708 |
-
graph.edge_list.end()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
annotations/annotation_144/EfficientStreamingAlgor/logs.log
DELETED
|
@@ -1,56 +0,0 @@
|
|
| 1 |
-
[2025-08-21 02:07:43,346 INFO] Computing approx DD-ordering...
|
| 2 |
-
[2025-08-21 02:07:43,352 INFO] ** PEEL_heuristic for 4 vertices, max degree 3, select 4 vertices
|
| 3 |
-
[2025-08-21 02:07:43,352 INFO] Number of passes for DD-ordering = 4
|
| 4 |
-
[2025-08-21 02:07:43,352 INFO] Sampling preprocessing...
|
| 5 |
-
[2025-08-21 02:07:43,352 INFO] Start sampling with batch size 11 ...
|
| 6 |
-
[2025-08-21 02:07:43,357 INFO] The 1-th batch. Totally 11 trials, 1 successful trials.
|
| 7 |
-
[2025-08-21 02:07:43,357 INFO] The 2-th batch. Totally 22 trials, 1 successful trials.
|
| 8 |
-
[2025-08-21 02:07:43,358 INFO] The 3-th batch. Totally 33 trials, 1 successful trials.
|
| 9 |
-
[2025-08-21 02:07:43,358 INFO] The 4-th batch. Totally 44 trials, 1 successful trials.
|
| 10 |
-
[2025-08-21 02:07:43,358 INFO] The 5-th batch. Totally 55 trials, 3 successful trials.
|
| 11 |
-
[2025-08-21 02:07:43,360 INFO] The 6-th batch. Totally 66 trials, 3 successful trials.
|
| 12 |
-
[2025-08-21 02:07:43,361 INFO] The 7-th batch. Totally 77 trials, 4 successful trials.
|
| 13 |
-
[2025-08-21 02:07:43,362 INFO] Sampled successfully after 78 trials
|
| 14 |
-
[2025-08-21 02:07:43,362 INFO] Number of passes in total = 45
|
| 15 |
-
[2025-08-21 02:07:43,362 INFO] Number of passes for preprocessing = 5
|
| 16 |
-
[2025-08-21 02:07:43,362 INFO] Number of passes for sampling = 40
|
| 17 |
-
[2025-08-21 02:08:17,890 INFO] Computing approx DD-ordering...
|
| 18 |
-
[2025-08-21 02:08:17,890 INFO] ** PEEL_heuristic for 4 vertices, max degree 3, select 4 vertices
|
| 19 |
-
[2025-08-21 02:08:17,892 INFO] Number of passes for DD-ordering = 4
|
| 20 |
-
[2025-08-21 02:08:17,892 INFO] Sampling preprocessing...
|
| 21 |
-
[2025-08-21 02:08:17,892 INFO] Start sampling with batch size 11 ...
|
| 22 |
-
[2025-08-21 02:08:17,892 INFO] The 1-th batch. Totally 11 trials, 2 successful trials.
|
| 23 |
-
[2025-08-21 02:08:17,892 INFO] The 2-th batch. Totally 22 trials, 3 successful trials.
|
| 24 |
-
[2025-08-21 02:08:17,894 INFO] Sampled successfully after 31 trials
|
| 25 |
-
[2025-08-21 02:08:17,894 INFO] Number of passes in total = 20
|
| 26 |
-
[2025-08-21 02:08:17,894 INFO] Number of passes for preprocessing = 5
|
| 27 |
-
[2025-08-21 02:08:17,894 INFO] Number of passes for sampling = 15
|
| 28 |
-
[2025-08-21 02:09:19,781 INFO] Computing approx DD-ordering...
|
| 29 |
-
[2025-08-21 02:09:19,781 INFO] ** PEEL_heuristic for 4 vertices, max degree 3, select 4 vertices
|
| 30 |
-
[2025-08-21 02:09:19,782 INFO] Number of passes for DD-ordering = 4
|
| 31 |
-
[2025-08-21 02:09:19,782 INFO] Sampling preprocessing...
|
| 32 |
-
[2025-08-21 02:09:19,782 INFO] Start sampling with batch size 11 ...
|
| 33 |
-
[2025-08-21 02:09:19,782 INFO] The 1-th batch. Totally 11 trials, 1 successful trials.
|
| 34 |
-
[2025-08-21 02:09:19,782 INFO] The 2-th batch. Totally 22 trials, 1 successful trials.
|
| 35 |
-
[2025-08-21 02:09:19,785 INFO] The 3-th batch. Totally 33 trials, 3 successful trials.
|
| 36 |
-
[2025-08-21 02:09:19,785 INFO] Sampled successfully after 44 trials
|
| 37 |
-
[2025-08-21 02:09:19,785 INFO] Number of passes in total = 25
|
| 38 |
-
[2025-08-21 02:09:19,785 INFO] Number of passes for preprocessing = 5
|
| 39 |
-
[2025-08-21 02:09:19,785 INFO] Number of passes for sampling = 20
|
| 40 |
-
[2025-08-21 02:09:37,619 INFO] Computing approx DD-ordering...
|
| 41 |
-
[2025-08-21 02:09:37,619 INFO] ** PEEL_heuristic for 4 vertices, max degree 3, select 4 vertices
|
| 42 |
-
[2025-08-21 02:09:37,619 INFO] Number of passes for DD-ordering = 4
|
| 43 |
-
[2025-08-21 02:09:37,619 INFO] Sampling preprocessing...
|
| 44 |
-
[2025-08-21 02:09:37,619 INFO] Start sampling with batch size 11 ...
|
| 45 |
-
[2025-08-21 02:09:37,622 INFO] The 1-th batch. Totally 11 trials, 0 successful trials.
|
| 46 |
-
[2025-08-21 02:09:37,622 INFO] The 2-th batch. Totally 22 trials, 0 successful trials.
|
| 47 |
-
[2025-08-21 02:09:37,622 INFO] The 3-th batch. Totally 33 trials, 0 successful trials.
|
| 48 |
-
[2025-08-21 02:09:37,624 INFO] The 4-th batch. Totally 44 trials, 0 successful trials.
|
| 49 |
-
[2025-08-21 02:09:37,624 INFO] The 5-th batch. Totally 55 trials, 0 successful trials.
|
| 50 |
-
[2025-08-21 02:09:37,624 INFO] The 6-th batch. Totally 66 trials, 1 successful trials.
|
| 51 |
-
[2025-08-21 02:09:37,624 INFO] The 7-th batch. Totally 77 trials, 3 successful trials.
|
| 52 |
-
[2025-08-21 02:09:37,627 INFO] The 8-th batch. Totally 88 trials, 3 successful trials.
|
| 53 |
-
[2025-08-21 02:09:37,627 INFO] Sampled successfully after 98 trials
|
| 54 |
-
[2025-08-21 02:09:37,627 INFO] Number of passes in total = 50
|
| 55 |
-
[2025-08-21 02:09:37,627 INFO] Number of passes for preprocessing = 5
|
| 56 |
-
[2025-08-21 02:09:37,627 INFO] Number of passes for sampling = 45
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
annotations/annotation_144/EfficientStreamingAlgor/memory_monitor.py
DELETED
|
@@ -1,31 +0,0 @@
|
|
| 1 |
-
import os, time, pickle, random, sys
|
| 2 |
-
|
| 3 |
-
def write_m(m) :
|
| 4 |
-
with open(f'memory{sys.argv[2]}.bin', 'wb') as f:
|
| 5 |
-
pickle.dump(m,f)
|
| 6 |
-
|
| 7 |
-
command_with_rn = sys.argv[1]+' streamrn'+sys.argv[2]
|
| 8 |
-
|
| 9 |
-
os.system(command_with_rn+' &')
|
| 10 |
-
|
| 11 |
-
m = []
|
| 12 |
-
cnt = 0
|
| 13 |
-
while True :
|
| 14 |
-
ls = os.popen(f'ps aux | grep streamrn{sys.argv[2]}').read()
|
| 15 |
-
if not (command_with_rn in ls) :
|
| 16 |
-
break
|
| 17 |
-
ls = ls.split('\n')
|
| 18 |
-
for l in ls :
|
| 19 |
-
if (command_with_rn in l and not ('memory_monitor' in l)) :
|
| 20 |
-
l = l.split()
|
| 21 |
-
current_m = int(l[4])
|
| 22 |
-
m.append(current_m)
|
| 23 |
-
break
|
| 24 |
-
cnt += 1
|
| 25 |
-
if (cnt%10==0) :
|
| 26 |
-
write_m(m)
|
| 27 |
-
time.sleep(10)
|
| 28 |
-
|
| 29 |
-
print('Maximum memory usage:', max(m))
|
| 30 |
-
|
| 31 |
-
write_m(m)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
annotations/annotation_144/EfficientStreamingAlgor/probability.py
DELETED
|
@@ -1,20 +0,0 @@
|
|
| 1 |
-
import sys, pickle
|
| 2 |
-
|
| 3 |
-
with open(sys.argv[1],'rb') as fr:
|
| 4 |
-
samples = pickle.load(fr)
|
| 5 |
-
|
| 6 |
-
target = len(samples)
|
| 7 |
-
for i in range(target) :
|
| 8 |
-
samples[i].sort()
|
| 9 |
-
samples.sort()
|
| 10 |
-
|
| 11 |
-
fw = open('probability.txt','w')
|
| 12 |
-
|
| 13 |
-
cnt = 0
|
| 14 |
-
for i in range(target) :
|
| 15 |
-
if i==0 or samples[i]==samples[i-1] :
|
| 16 |
-
cnt += 1
|
| 17 |
-
else :
|
| 18 |
-
fw.write(f'{samples[i-1]}: {cnt}/{target}\n')
|
| 19 |
-
cnt = 1
|
| 20 |
-
fw.write(f'{samples[-1]}: {cnt}/{target}')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
annotations/annotation_144/EfficientStreamingAlgor/reformat.py
DELETED
|
@@ -1,51 +0,0 @@
|
|
| 1 |
-
import sys, os
|
| 2 |
-
|
| 3 |
-
fr = open(sys.argv[1], 'r')
|
| 4 |
-
TMPNUM = 31
|
| 5 |
-
tmpw = [open(f'tmp{i}', 'w') for i in range(TMPNUM)]
|
| 6 |
-
|
| 7 |
-
print('***** Phase 1')
|
| 8 |
-
|
| 9 |
-
cnt = 0
|
| 10 |
-
for line in fr :
|
| 11 |
-
cnt += 1
|
| 12 |
-
if (cnt%10000000 == 0) :
|
| 13 |
-
print(cnt)
|
| 14 |
-
if line[0]=='%' :
|
| 15 |
-
continue
|
| 16 |
-
l = line.split()
|
| 17 |
-
if len(l)<2 :
|
| 18 |
-
continue
|
| 19 |
-
x, y = int(l[0]), int(l[1])
|
| 20 |
-
if (x==y) :
|
| 21 |
-
continue
|
| 22 |
-
if x>y :
|
| 23 |
-
x, y = y, x
|
| 24 |
-
h = ((x*998244353)^(y*1000000007))%TMPNUM
|
| 25 |
-
tmpw[h].write(f'{x} {y}\n')
|
| 26 |
-
|
| 27 |
-
print('***** Phase 2')
|
| 28 |
-
|
| 29 |
-
fw = open(sys.argv[2], 'w')
|
| 30 |
-
fw.write('u,v\n')
|
| 31 |
-
|
| 32 |
-
cnt = 0
|
| 33 |
-
for i in range(TMPNUM) :
|
| 34 |
-
print('** file', i)
|
| 35 |
-
tmpw[i].close()
|
| 36 |
-
tmpr = open(f'tmp{i}', 'r')
|
| 37 |
-
S = set()
|
| 38 |
-
for line in tmpr :
|
| 39 |
-
cnt += 1
|
| 40 |
-
if (cnt%10000000 == 0) :
|
| 41 |
-
print(cnt)
|
| 42 |
-
l = line.split()
|
| 43 |
-
x, y = int(l[0]), int(l[1])
|
| 44 |
-
if (x,y) in S :
|
| 45 |
-
continue
|
| 46 |
-
S.add((x,y))
|
| 47 |
-
fw.write(f'{x},{y}\n')
|
| 48 |
-
tmpr.close()
|
| 49 |
-
os.remove(f'tmp{i}')
|
| 50 |
-
|
| 51 |
-
fw.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
annotations/annotation_144/EfficientStreamingAlgor/samples.bin
DELETED
|
@@ -1,3 +0,0 @@
|
|
| 1 |
-
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:365ea209560b0e8dfec7c5d6c8d1bbe0b074dafdfe1617671720375ac6920404
|
| 3 |
-
size 66
|
|
|
|
|
|
|
|
|
|
|
|
annotations/annotation_144/EfficientStreamingAlgor/tiny_edges.csv
DELETED
|
@@ -1,6 +0,0 @@
|
|
| 1 |
-
u,v
|
| 2 |
-
1,2
|
| 3 |
-
2,3
|
| 4 |
-
3,4
|
| 5 |
-
4,1
|
| 6 |
-
2,4
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
annotations/annotation_144/canonical.py
DELETED
|
@@ -1,708 +0,0 @@
|
|
| 1 |
-
# --------------------------------------------------------------------------------------
|
| 2 |
-
# Efficient Streaming Algorithms for Graphlet Sampling — Annotated Canonical Code
|
| 3 |
-
# --------------------------------------------------------------------------------------
|
| 4 |
-
# This file implements a practical version of STREAM-UGS (preprocessing + sampling)
|
| 5 |
-
# aligned with the NeurIPS 2024 paper “Efficient Streaming Algorithms for Graphlet
|
| 6 |
-
# Sampling” by Bourreau et al. In particular, it mirrors:
|
| 7 |
-
# • APPROX-DD (Algorithm 1) to compute an approximate DD order (ϑ-DD order),
|
| 8 |
-
# • COMPUTEDISTRIB (Algorithm 2) to build the bucket distribution p and Γ,
|
| 9 |
-
# • SAMPLE (Algorithm 3) with RAND-GROW and PROB for rejection sampling.
|
| 10 |
-
#
|
| 11 |
-
# Where this implementation differs:
|
| 12 |
-
# (D1) Heuristic switch for PEEL vs. PEEL_approx:
|
| 13 |
-
# The code auto-chooses between a heuristic peeling (PEEL_heuristic) and the
|
| 14 |
-
# full APPROX-DD-style peeling (PEEL_approx) based on MAX_EDGES and degree
|
| 15 |
-
# thresholds, following the paper’s Appendix E heuristic spirit (APPROX-DD-HEURISTIC),
|
| 16 |
-
# not the exact pseudocode of Algorithm 1. (Paper §3.1, Alg. 1; Appendix E.)
|
| 17 |
-
#
|
| 18 |
-
# (D2) Bucket non-emptiness (1_v) test:
|
| 19 |
-
# The paper’s COMPUTEDISTRIB suggests a truncated BFS to determine whether
|
| 20 |
-
# G(v) contains a k-graphlet and to compute b_v = 1_v·d(v|G(v))^{k−1} in one pass
|
| 21 |
-
# with M = Ω(kn) (Paper §3.2, Alg. 2; Lemma 3.3). This code uses a “light/heavy”
|
| 22 |
-
# bookkeeping plus a DSU pass over the DD order (get_degree_dvGv / get_b) to
|
| 23 |
-
# cheaply approximate the same condition without explicitly running parallel BFSs.
|
| 24 |
-
# This is an implementation choice consistent with the paper’s goal: compute p(v)
|
| 25 |
-
# and Γ efficiently; correctness relies on capturing whether a size-k connected
|
| 26 |
-
# subgraph exists in G(v).
|
| 27 |
-
#
|
| 28 |
-
# (D3) Parallel edge sampling and degree queries:
|
| 29 |
-
# The paper proposes shared, batched routines (SUBDEGREES/CHECKEDGES/
|
| 30 |
-
# SAMPLEEDGES) to serve many trials (Theorem 3.4; Appendix C). Here,
|
| 31 |
-
# get_degree_duGv + RAND_GROW_fast embody the same idea: prepare batched
|
| 32 |
-
# queries per u and per anchor v, push them through one or a few full passes,
|
| 33 |
-
# then consume “answers” to draw uniform edges from cuts E_i. The names differ
|
| 34 |
-
# but the intent matches (Paper §3.3; Algorithm 3; Appendix C).
|
| 35 |
-
#
|
| 36 |
-
# (D4) Edge-list backends:
|
| 37 |
-
# The paper assumes a semi-streaming model (sequential passes). This file supports
|
| 38 |
-
# several backends:
|
| 39 |
-
# - Edge_list_local: plain CSV iteration (fits paper’s pass model),
|
| 40 |
-
# - Edge_list_DataFrame: Spark DataFrame iterator (convenience backend),
|
| 41 |
-
# - Edge_list_memory: pre-loads the CSV in RAM (not semi-streaming strict).
|
| 42 |
-
# Use the ‘local’ mode to match streaming behavior most closely. (Paper §3.)
|
| 43 |
-
#
|
| 44 |
-
# (D5) Acceptance constant Γ and p(v):
|
| 45 |
-
# Γ = 1 / ((k−1)!·Z·(1+ε)^{k−1}) and p(v) ∝ 1_v·d(v|G(v))^{k−1} directly follow
|
| 46 |
-
# the paper (Algorithm 2 lines 4–7 and Algorithm 3 acceptance rule). (Paper §3.2,
|
| 47 |
-
# Alg. 2; §3.3, Alg. 3; Lemma C.1.)
|
| 48 |
-
#
|
| 49 |
-
# For quick paper mapping:
|
| 50 |
-
# • get_DD_order / PEEL_approx ≈ APPROX-DD (Paper §3.1, Alg. 1)
|
| 51 |
-
# • sampling_preprocess (build p and Γ) ≈ COMPUTEDISTRIB (Paper §3.2, Alg. 2)
|
| 52 |
-
# • RAND_GROW / RAND_GROW_fast and PROB ≈ SAMPLE (Paper §3.3, Alg. 3)
|
| 53 |
-
#
|
| 54 |
-
# References in comments: sections/algorithms point to the paper’s main text and appendices.
|
| 55 |
-
# --------------------------------------------------------------------------------------
|
| 56 |
-
# Paper: “Efficient Streaming Algorithms for Graphlet Sampling” (NeurIPS 2024)
|
| 57 |
-
# • APPROX-DD (Algorithm 1), COMPUTEDISTRIB (Algorithm 2), SAMPLE (Algorithm 3),
|
| 58 |
-
# §1–§3; Lemma 3.3; Theorem 3.4; Appendix C (shared routines).
|
| 59 |
-
# • Heuristic (Appendix E).
|
| 60 |
-
# :contentReference[oaicite:1]{index=1}
|
| 61 |
-
# --------------------------------------------------------------------------------------
|
| 62 |
-
|
| 63 |
-
# (Optional) Spark imports are commented; enable only if using DataFrame backend
|
| 64 |
-
# from pyspark.sql import SparkSession
|
| 65 |
-
# from pyspark.sql import functions as sf
|
| 66 |
-
# from pyspark.sql.types import *
|
| 67 |
-
|
| 68 |
-
import math, random, heapq
|
| 69 |
-
from bisect import bisect_left, bisect
|
| 70 |
-
import yaml, csv, logging, sys, pickle
|
| 71 |
-
import numpy as np
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
class DSU() :
|
| 75 |
-
"""
|
| 76 |
-
Disjoint Set Union over vertices in DD order to support COMPUTEDISTRIB-like logic.
|
| 77 |
-
|
| 78 |
-
Purpose in this code:
|
| 79 |
-
• Used in get_b() to determine 1_v (i.e., whether G(v) contains any size-k graphlet).
|
| 80 |
-
The union structure merges “light” neighbors discovered in get_degree_dvGv().
|
| 81 |
-
It also tracks, per set, the largest “last_heavy” rank seen upstream.
|
| 82 |
-
• This approximates the one-pass bucket non-emptiness check described in the paper’s
|
| 83 |
-
Lemma 3.3 (Paper §3.2; Alg. 2), without running explicit BFS for each v.
|
| 84 |
-
|
| 85 |
-
Fields per vertex:
|
| 86 |
-
- root: DSU parent/root
|
| 87 |
-
- size: component size of “light” adjacency seen so far
|
| 88 |
-
- last_heavy: max DD rank of a heavy neighbor discovered for nodes in the set
|
| 89 |
-
"""
|
| 90 |
-
def __init__(self, n, last_heavy) :
|
| 91 |
-
self.n = n
|
| 92 |
-
# a[i] = (root=i, size=1, last_heavy[i]) for i in [0..n]
|
| 93 |
-
# NOTE: vertices are expected to be numbered 1..n; index 0 is present but unused.
|
| 94 |
-
self.a = np.array([(i, 1, last_heavy[i]) for i in range(n+1)], dtype=[('root',int), ('size',int), ('last_heavy',int)])
|
| 95 |
-
|
| 96 |
-
def get_root(self, x) :
|
| 97 |
-
# Standard path-compressed find
|
| 98 |
-
if self.a[x]['root']==x :
|
| 99 |
-
return x
|
| 100 |
-
else :
|
| 101 |
-
self.a[x]['root'] = self.get_root(self.a[x]['root'])
|
| 102 |
-
return self.a[x]['root']
|
| 103 |
-
|
| 104 |
-
def merge(self, x, y) :
|
| 105 |
-
# Union by attaching root y under root x; we only need correctness, not rank heuristic
|
| 106 |
-
x_root = self.get_root(x)
|
| 107 |
-
y_root = self.get_root(y)
|
| 108 |
-
if x_root!=y_root :
|
| 109 |
-
self.a[y_root]['root'] = x_root
|
| 110 |
-
self.a[x_root]['size'] += self.a[y_root]['size']
|
| 111 |
-
# “last_heavy” of the component is the max of the two sub-components.
|
| 112 |
-
self.a[x_root]['last_heavy'] = max(self.a[x_root]['last_heavy'], self.a[y_root]['last_heavy'])
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
def next_bit(S, size) :
|
| 116 |
-
"""
|
| 117 |
-
Iterate set bits of S in [0..size-1].
|
| 118 |
-
Used by PROB() to enumerate subsets; mirrors Algorithm 3 lines 18–22
|
| 119 |
-
(Paper §3.3) where subsets S_i are built via bitmasks.
|
| 120 |
-
"""
|
| 121 |
-
for i in range(size) :
|
| 122 |
-
if ((S>>i)&1) :
|
| 123 |
-
yield i
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
class Query() :
|
| 127 |
-
"""
|
| 128 |
-
Helper to batch degree queries per u (candidate expansion vertex) over many anchors v.
|
| 129 |
-
|
| 130 |
-
Used in:
|
| 131 |
-
- get_degree_duGv(): to accumulate, per u, how many neighbors it has in G(v) for multiple v.
|
| 132 |
-
- RAND_GROW_fast(): to relocate queries to the correct anchor v and answer them in one pass.
|
| 133 |
-
|
| 134 |
-
This corresponds to the batched SUBDEGREES/CHECKEDGES idea in Appendix C
|
| 135 |
-
(Paper Theorem 3.4 and §3.3), enabling shared passes across many trials.
|
| 136 |
-
"""
|
| 137 |
-
def __init__(self, u) :
|
| 138 |
-
self.u = u
|
| 139 |
-
self.v_list, self.queries = set(), set()
|
| 140 |
-
|
| 141 |
-
def build_cnt(self) :
|
| 142 |
-
# Prepare prefix-sum container aligned to sorted v_list (descending by DD rank)
|
| 143 |
-
self.v_list = sorted(list(self.v_list))
|
| 144 |
-
self.cnt = [0]*(len(self.v_list)+1)
|
| 145 |
-
|
| 146 |
-
def build_queries_for_v(self, degree_duGv, DD_order) :
|
| 147 |
-
"""
|
| 148 |
-
Transform absolute query ids into per-v groups, arranged so that a single streaming pass
|
| 149 |
-
can satisfy the “j-th incident edge” queries (Algorithm SAMPLEEDGES in Appendix C).
|
| 150 |
-
"""
|
| 151 |
-
self.num_index = [0]+[degree_duGv[(DD_order[v], self.u)] for v in reversed(self.v_list)]
|
| 152 |
-
self.queries_for_v = [[] for v in self.v_list]
|
| 153 |
-
vi = 0
|
| 154 |
-
for q in sorted(list(self.queries), reverse=True) :
|
| 155 |
-
while vi+1<len(self.v_list) and q<=self.num_index[-(vi+2)] :
|
| 156 |
-
vi += 1
|
| 157 |
-
self.queries_for_v[vi].append(q)
|
| 158 |
-
self.queries = None
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
class Edge_list_local() :
|
| 162 |
-
"""
|
| 163 |
-
Streaming-friendly edge list: sequential CSV scan per pass.
|
| 164 |
-
Matches the semi-streaming pass model assumed by the paper (§3).
|
| 165 |
-
CSV columns: header row then rows of "u,v".
|
| 166 |
-
"""
|
| 167 |
-
def __init__(self, edge_list_file) :
|
| 168 |
-
self.edge_list_file = edge_list_file
|
| 169 |
-
self.passes = 0
|
| 170 |
-
|
| 171 |
-
def get_edge(self) :
|
| 172 |
-
self.passes += 1
|
| 173 |
-
with open(self.edge_list_file, 'r') as f :
|
| 174 |
-
edge_list = csv.reader(f, delimiter=',')
|
| 175 |
-
next(edge_list)
|
| 176 |
-
for row in edge_list :
|
| 177 |
-
yield int(row[0]), int(row[1])
|
| 178 |
-
|
| 179 |
-
def end(self) :
|
| 180 |
-
x = 0
|
| 181 |
-
|
| 182 |
-
class Edge_list_DataFrame() :
|
| 183 |
-
"""
|
| 184 |
-
Spark DataFrame backend (convenience for large files).
|
| 185 |
-
Not strictly required by the streaming model; ensure pyspark is available and
|
| 186 |
-
uncomment imports at the top if you use this mode.
|
| 187 |
-
"""
|
| 188 |
-
def __init__(self, edge_list_file) :
|
| 189 |
-
self.spark = SparkSession.builder.getOrCreate()
|
| 190 |
-
self.spark.sparkContext.setLogLevel("WARN")
|
| 191 |
-
self.edge_list = self.spark.read.csv(edge_list_file, header=True, inferSchema=True)
|
| 192 |
-
self.passes = 0
|
| 193 |
-
|
| 194 |
-
def get_edge(self) :
|
| 195 |
-
self.passes += 1
|
| 196 |
-
for e in self.edge_list.toLocalIterator() :
|
| 197 |
-
yield e['u'], e['v']
|
| 198 |
-
|
| 199 |
-
def end(self) :
|
| 200 |
-
self.spark.stop()
|
| 201 |
-
|
| 202 |
-
class Edge_list_StreamingDataFrame() :
|
| 203 |
-
# Placeholder for a fully streaming DF backend (not implemented)
|
| 204 |
-
# TODO: implement if needed; otherwise use 'local' for paper-faithful passes.
|
| 205 |
-
def __init__(self, edge_list_file) :
|
| 206 |
-
x = 0
|
| 207 |
-
|
| 208 |
-
class Edge_list_memory() :
|
| 209 |
-
"""
|
| 210 |
-
In-memory edge list. Deviates from strict semi-streaming assumptions (the paper’s
|
| 211 |
-
model expects sublinear memory). Useful for debugging or small graphs.
|
| 212 |
-
"""
|
| 213 |
-
def __init__(self, edge_list_file) :
|
| 214 |
-
with open(edge_list_file, 'r') as f :
|
| 215 |
-
edge_list = csv.reader(f, delimiter=',')
|
| 216 |
-
next(edge_list)
|
| 217 |
-
self.edge_list = list(edge_list)
|
| 218 |
-
self.passes = 0
|
| 219 |
-
|
| 220 |
-
def get_edge(self) :
|
| 221 |
-
self.passes += 1
|
| 222 |
-
for e in self.edge_list :
|
| 223 |
-
yield int(e[0]), int(e[1])
|
| 224 |
-
|
| 225 |
-
def end(self) :
|
| 226 |
-
x = 0
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
class Graph() :
|
| 230 |
-
"""
|
| 231 |
-
Core class implementing:
|
| 232 |
-
• APPROX-DD-like preprocessing (get_DD_order / PEEL_approx / PEEL_heuristic),
|
| 233 |
-
• COMPUTEDISTRIB-like distribution build (sampling_preprocess),
|
| 234 |
-
• SAMPLE-like sampling (RAND_GROW_fast + PROB + sample loop).
|
| 235 |
-
|
| 236 |
-
Key paper references:
|
| 237 |
-
- Approx DD order (Paper §3.1, Alg. 1; Lemma A.1 etc.),
|
| 238 |
-
- Distribution p and Γ (Paper §3.2, Alg. 2),
|
| 239 |
-
- Sampling (Paper §3.3, Alg. 3; Appendix C for shared routines).
|
| 240 |
-
"""
|
| 241 |
-
def __init__(self, n, edgelistmode, edge_list_file, MAX_EDGES) :
|
| 242 |
-
self.n = n
|
| 243 |
-
edge_list_class = {'local': Edge_list_local,
|
| 244 |
-
'DataFrame': Edge_list_DataFrame,
|
| 245 |
-
'StreamingDataFrame': Edge_list_StreamingDataFrame,
|
| 246 |
-
'memory': Edge_list_memory}
|
| 247 |
-
self.edge_list = edge_list_class[edgelistmode](edge_list_file)
|
| 248 |
-
self.MAX_EDGES = MAX_EDGES
|
| 249 |
-
|
| 250 |
-
def get_degree_G(self, vertices) :
|
| 251 |
-
"""
|
| 252 |
-
Compute degrees in the induced subgraph G[vertices] by one pass (streaming).
|
| 253 |
-
This is the primitive the paper denotes as COMPUTEALLDEG (Paper §3.1),
|
| 254 |
-
incurring a single pass whenever recomputation is needed in APPROX-DD.
|
| 255 |
-
"""
|
| 256 |
-
if (len(vertices)<self.n/10) :
|
| 257 |
-
degree = {x: 0 for x in vertices}
|
| 258 |
-
else :
|
| 259 |
-
degree = np.zeros(self.n+1, dtype=int)
|
| 260 |
-
for x, y in self.edge_list.get_edge() :
|
| 261 |
-
if (x in vertices and y in vertices) :
|
| 262 |
-
degree[x] += 1
|
| 263 |
-
degree[y] += 1
|
| 264 |
-
return degree
|
| 265 |
-
|
| 266 |
-
def get_degree_H(self, partition) :
|
| 267 |
-
"""
|
| 268 |
-
Compute, for H partitioned into ℓ random buckets H_i, the internal degree
|
| 269 |
-
d(v | G[H_i]) used by SHAVE (Paper §3.1, Alg. 1 lines 15–19).
|
| 270 |
-
"""
|
| 271 |
-
if (len(partition)<self.n/10) :
|
| 272 |
-
degree = {x: 0 for x in partition}
|
| 273 |
-
else :
|
| 274 |
-
degree = np.zeros(self.n+1, dtype=int)
|
| 275 |
-
for x, y in self.edge_list.get_edge() :
|
| 276 |
-
if (partition.get(x,-1)==partition.get(y,-2)) :
|
| 277 |
-
degree[x] += 1
|
| 278 |
-
degree[y] += 1
|
| 279 |
-
return degree
|
| 280 |
-
|
| 281 |
-
def PEEL_approx(self, vertices, degree_G, epsilon, alpha, max_degree, DD_order) :
|
| 282 |
-
"""
|
| 283 |
-
APPROX-DD (Algorithm 1) core: SHAVE loop with random partitioning.
|
| 284 |
-
• β and ℓ = ⌈2(1+β)⌉ as in Alg. 1 line 14; threshold_ub = Δ/(1+β).
|
| 285 |
-
• H = {v : d(v|G) ≥ Δ/(1+α)} (Alg. 1 line 9).
|
| 286 |
-
• Each SHAVE iteration: partition H into ℓ parts; remove R_i ⊂ H_i with
|
| 287 |
-
d(v|G[H_i]) ≤ Δ/(1+β), but only those still meeting d(v|G) ≥ Δ/(1+α)
|
| 288 |
-
(Alg. 1 lines 16–21). (Paper §3.1; Lemma 3.2; Appendix A.)
|
| 289 |
-
"""
|
| 290 |
-
beta = 2/epsilon+2+epsilon
|
| 291 |
-
l = math.ceil(2*(1+beta))
|
| 292 |
-
threshold_lb, threshold_ub = max_degree/(1+alpha), max_degree/(1+beta)
|
| 293 |
-
H = {x for x in vertices if degree_G[x]>=threshold_lb}
|
| 294 |
-
while H :
|
| 295 |
-
# SHAVE: uniform random partition H -> l buckets
|
| 296 |
-
partition = {x: random.randint(0,l-1) for x in H}
|
| 297 |
-
degree_H = self.get_degree_H(partition)
|
| 298 |
-
R = {x for x in H if degree_H[x]<=threshold_ub}
|
| 299 |
-
H -= R
|
| 300 |
-
group = [[] for i in range(l)]
|
| 301 |
-
for x in R :
|
| 302 |
-
group[partition[x]].append(x)
|
| 303 |
-
partition, degree_H = None, None
|
| 304 |
-
for i in range(l) :
|
| 305 |
-
# Remove only those still above the global lower bound, align with Alg. 1 line 18
|
| 306 |
-
Si = set(x for x in group[i] if degree_G[x]>=threshold_lb)
|
| 307 |
-
if (not Si) :
|
| 308 |
-
continue
|
| 309 |
-
# Appending Si to DD_order matches Alg. 1 line 19 (“append ... in arbitrary order”)
|
| 310 |
-
DD_order.extend(Si)
|
| 311 |
-
vertices -= Si
|
| 312 |
-
# Recompute degrees after deletions: another pass, consistent with paper comments
|
| 313 |
-
degree_G = self.get_degree_G(vertices)
|
| 314 |
-
return degree_G
|
| 315 |
-
|
| 316 |
-
def PEEL_heuristic(self, vertices, heap, degree_G, threshold_lb, DD_order) :
|
| 317 |
-
"""
|
| 318 |
-
Heuristic peeling: greedily remove current max-degree vertices under a pass budget.
|
| 319 |
-
This mirrors Appendix E’s APPROX-DD-HEURISTIC: when subgraphs fit in
|
| 320 |
-
memory (or under MAX_EDGES), process a large chunk with fewer passes.
|
| 321 |
-
(Paper Appendix E; Fig. 1 shows the practical benefit on sparse graphs.)
|
| 322 |
-
"""
|
| 323 |
-
if (len(heap)<self.n/10) :
|
| 324 |
-
edges = {x: [] for d,x in heap}
|
| 325 |
-
else :
|
| 326 |
-
edges = [[] for x in range(self.n+1)]
|
| 327 |
-
# Build adjacency limited to candidate range (one pass)
|
| 328 |
-
for x, y in self.edge_list.get_edge() :
|
| 329 |
-
if (x in vertices and y in vertices and (-degree_G[x],x)<=heap[-1] and (-degree_G[y],y)<=heap[-1]) :
|
| 330 |
-
edges[x].append(y)
|
| 331 |
-
edges[y].append(x)
|
| 332 |
-
# Greedy removal, pushing updates via a heap (local relaxation of Alg. 1)
|
| 333 |
-
# NOTE: Saves passes vs. recomputing full degrees after every deletion.
|
| 334 |
-
# Stop when degree falls below threshold_lb for the remaining candidates.
|
| 335 |
-
# (This threshold is Δ/(1+α) derived from current Δ.)
|
| 336 |
-
while heap and -heap[0][0]>=threshold_lb:
|
| 337 |
-
top = heapq.heappop(heap)
|
| 338 |
-
if (-top[0]!=degree_G[top[1]]) :
|
| 339 |
-
continue
|
| 340 |
-
DD_order.append(top[1])
|
| 341 |
-
vertices.remove(top[1])
|
| 342 |
-
for y in edges[top[1]] :
|
| 343 |
-
if y in vertices :
|
| 344 |
-
degree_G[y] -= 1
|
| 345 |
-
heapq.heappush(heap,(-degree_G[y], y))
|
| 346 |
-
|
| 347 |
-
def get_DD_order(self, epsilon, enable_heuristic = True) :
|
| 348 |
-
"""
|
| 349 |
-
Compute an approximate DD ordering (ϑ = 1/(1+ε) DD order).
|
| 350 |
-
This function chooses between:
|
| 351 |
-
- PEEL_heuristic (Appendix E) when a large prefix can be handled with fewer passes,
|
| 352 |
-
- PEEL_approx (Algorithm 1) otherwise.
|
| 353 |
-
Both produce an ordering consistent with Lemma A.1. (Paper §3.1, Alg. 1; App. E.)
|
| 354 |
-
"""
|
| 355 |
-
logging.info('Computing approx DD-ordering...')
|
| 356 |
-
|
| 357 |
-
vertices = set(range(1, self.n+1))
|
| 358 |
-
|
| 359 |
-
DD_order = []
|
| 360 |
-
alpha = epsilon/2
|
| 361 |
-
degree_G = self.get_degree_G(vertices)
|
| 362 |
-
while vertices :
|
| 363 |
-
if enable_heuristic :
|
| 364 |
-
# Decide which PEEL to run: estimate how many high-degree vertices we can
|
| 365 |
-
# safely process (bounded by MAX_EDGES to control per-iteration work).
|
| 366 |
-
vertex_list = sorted([(-degree_G[x],x) for x in vertices])
|
| 367 |
-
if (len(vertices)<self.n/10) :
|
| 368 |
-
degree_dvGRv = {x: 0 for x in vertices}
|
| 369 |
-
else :
|
| 370 |
-
degree_dvGRv = np.zeros(self.n+1, dtype=int)
|
| 371 |
-
for x, y in self.edge_list.get_edge() :
|
| 372 |
-
if (x in vertices and y in vertices) :
|
| 373 |
-
degree_dvGRv[x if (-degree_G[y],y)<(-degree_G[x],x) else y] += 1
|
| 374 |
-
prefix_sum_degree, prefix_ind = 0, len(vertex_list)
|
| 375 |
-
for i, (d,x) in enumerate(vertex_list) :
|
| 376 |
-
prefix_sum_degree+=degree_dvGRv[x]
|
| 377 |
-
if (prefix_sum_degree>self.MAX_EDGES) :
|
| 378 |
-
prefix_ind = i
|
| 379 |
-
break
|
| 380 |
-
degree_dvGRv = None
|
| 381 |
-
|
| 382 |
-
max_degree = -vertex_list[0][0]
|
| 383 |
-
threshold_lb = max_degree/(1+alpha)
|
| 384 |
-
if (prefix_ind==len(vertex_list) or (-vertex_list[prefix_ind][0])<=threshold_lb) :
|
| 385 |
-
# Heuristic block: process a big chunk greedily (Appendix E)
|
| 386 |
-
logging.info('** PEEL_heuristic for %d vertices, max degree %d, select %d vertices', len(vertices), max_degree, prefix_ind)
|
| 387 |
-
self.PEEL_heuristic(vertices, vertex_list[0:prefix_ind], degree_G,
|
| 388 |
-
((-vertex_list[prefix_ind][0])/(1+epsilon) if prefix_ind<len(vertex_list) else 0),
|
| 389 |
-
DD_order)
|
| 390 |
-
degree_G = self.get_degree_G(vertices)
|
| 391 |
-
else :
|
| 392 |
-
# Full APPROX-DD style SHAVE (Algorithm 1)
|
| 393 |
-
logging.info('** PEEL_approx for %d vertices, max degree %d', len(vertices), max_degree)
|
| 394 |
-
degree_G = self.PEEL_approx(vertices, degree_G, epsilon, alpha, max_degree, DD_order)
|
| 395 |
-
else :
|
| 396 |
-
# Force the exact APPROX-DD approach (Algorithm 1)
|
| 397 |
-
max_degree = max(degree_G.values()) if type(degree_G) is dict else degree_G.max()
|
| 398 |
-
logging.info('** PEEL_approx for %d vertices, max degree %d', len(vertices), max_degree)
|
| 399 |
-
degree_G = self.PEEL_approx(vertices, degree_G, epsilon, alpha, max_degree, DD_order)
|
| 400 |
-
|
| 401 |
-
return DD_order
|
| 402 |
-
|
| 403 |
-
def get_degree_dvGv(self, k) :
|
| 404 |
-
"""
|
| 405 |
-
Build oriented adjacency summaries wrt DD order to support “light/heavy” tests.
|
| 406 |
-
For each x, we track:
|
| 407 |
-
- degree[x] = d(x|G(x)) as vertices with rank ≥ rank(x) arrive in the scan,
|
| 408 |
-
- light_edge_list[x]: early neighbors kept while degree[x] ≤ (k−2),
|
| 409 |
-
- last_heavy[x]: max DD rank at which x (or its light neighbors) see a heavy push.
|
| 410 |
-
This enables computing b_x = 1_x · d(x|G(x))^{k−1} without per-v BFS
|
| 411 |
-
(Paper §3.2, Alg. 2; Lemma 3.3). It’s an implementation detail not explicitly
|
| 412 |
-
prescribed, but consistent with detecting 1_v.
|
| 413 |
-
"""
|
| 414 |
-
degree, light_edge_list, last_heavy = np.zeros(self.n+1, dtype=int), [[] for x in range(self.n+1)], [-1]*(self.n+1)
|
| 415 |
-
for x, y in self.edge_list.get_edge() :
|
| 416 |
-
# Orient edge from lower DD rank to higher DD rank to respect G(v) subgraph definition
|
| 417 |
-
if (self.DD_ranking[x]>self.DD_ranking[y]) :
|
| 418 |
-
x, y = y, x
|
| 419 |
-
degree[x] += 1
|
| 420 |
-
if degree[x]<=k-2 :
|
| 421 |
-
light_edge_list[x].append(y)
|
| 422 |
-
else :
|
| 423 |
-
# When x becomes heavy, propagate last_heavy to neighbors seen while light
|
| 424 |
-
last_heavy[x] = self.DD_ranking[x]
|
| 425 |
-
if not (light_edge_list[x] is None) :
|
| 426 |
-
for z in light_edge_list[x] :
|
| 427 |
-
last_heavy[z]=max(last_heavy[z], self.DD_ranking[x])
|
| 428 |
-
light_edge_list[x] = None
|
| 429 |
-
last_heavy[y]=max(last_heavy[y], self.DD_ranking[x])
|
| 430 |
-
return degree, light_edge_list, last_heavy
|
| 431 |
-
|
| 432 |
-
def get_b(self, degree_dvGv, light_edge_list, last_heavy, k) :
|
| 433 |
-
"""
|
| 434 |
-
Compute b[x] = 1_x · d(x|G(x))^{k−1} used to form p(x) and Z = Σ b[x].
|
| 435 |
-
1_x is set by:
|
| 436 |
-
- If degree[x] ≥ k−1: bucket non-empty (trivially x has ≥k−1 forward neighbors).
|
| 437 |
-
- Otherwise: union the “light” edges via DSU; if DSU component size ≥ k or
|
| 438 |
-
there exists a “last_heavy” rank ≥ current rank, then non-empty.
|
| 439 |
-
This tracks the paper’s intuition (Paper §3.2): 1_v detects existence of any size-k
|
| 440 |
-
connected induced subgraph in G(v).
|
| 441 |
-
"""
|
| 442 |
-
b = [0]*(self.n+1)
|
| 443 |
-
dsu = DSU(self.n, last_heavy)
|
| 444 |
-
for i in range(self.n-1, -1, -1) :
|
| 445 |
-
x = self.DD_order[i]
|
| 446 |
-
if degree_dvGv[x]<=k-2 :
|
| 447 |
-
# Merge light edges of x; if comp is big enough or touches a heavy layer, set 1_x=1
|
| 448 |
-
for y in light_edge_list[x] :
|
| 449 |
-
dsu.merge(x,y)
|
| 450 |
-
b[x] = pow(int(degree_dvGv[x]),k-1) if (dsu.a[x]['size']>=k or dsu.a[x]['last_heavy']>=i) else 0
|
| 451 |
-
else :
|
| 452 |
-
b[x] = pow(int(degree_dvGv[x]),k-1)
|
| 453 |
-
return b
|
| 454 |
-
|
| 455 |
-
def sampling_preprocess(self, k, epsilon, ddorder = None, enable_heuristic = True) :
|
| 456 |
-
"""
|
| 457 |
-
COMPUTEDISTRIB (Algorithm 2): compute DD order (if needed), then build:
|
| 458 |
-
- DD_ranking, d(v|G(v)) surrogates via get_degree_dvGv,
|
| 459 |
-
- b[v], Z = Σ b, p[v] = b[v]/Z,
|
| 460 |
-
- Γ = 1 / ((k−1)! · Z · (1+ε)^{k−1}).
|
| 461 |
-
This matches lines 2–8 of Algorithm 2. (Paper §3.2; Lemma 3.3 for 1_v test.)
|
| 462 |
-
"""
|
| 463 |
-
if ddorder is None :
|
| 464 |
-
self.DD_order = self.get_DD_order(epsilon, enable_heuristic)
|
| 465 |
-
logging.info('Number of passes for DD-ordering = %s', self.edge_list.passes)
|
| 466 |
-
else :
|
| 467 |
-
self.DD_order = ddorder
|
| 468 |
-
logging.info('Sampling preprocessing...')
|
| 469 |
-
self.DD_ranking = np.empty(self.n+1, dtype=int)
|
| 470 |
-
self.DD_ranking[self.DD_order] = np.arange(self.n)
|
| 471 |
-
degree_dvGv, light_edge_list, last_heavy = self.get_degree_dvGv(k)
|
| 472 |
-
b = self.get_b(degree_dvGv, light_edge_list, last_heavy, k)
|
| 473 |
-
degree_dvGv, light_edge_list, last_heavy = None, None, None
|
| 474 |
-
Z = sum(b)
|
| 475 |
-
# p[v] is over indices 0..n; vertex 0 remains 0-probability (IDs expected 1..n)
|
| 476 |
-
self.p = [b[x]/Z for x in range(self.n+1)]
|
| 477 |
-
# Γ per Algorithm 2, line 7; (1+ε)^{k−1} comes from using a ϑ-DD order (Paper §2 Def. 2.1; Lemma C.1)
|
| 478 |
-
self.Gamma = 1/(math.factorial(k-1)*Z*pow(1+epsilon,k-1))
|
| 479 |
-
|
| 480 |
-
def get_degree_duGv(self, v, samples) :
|
| 481 |
-
"""
|
| 482 |
-
Batched degree queries for many instances:
|
| 483 |
-
• For each trial j anchored at v[j], and current sample S_j, compute
|
| 484 |
-
d(u | G(v[j])) for u ∈ S_j, while also marking which (x,y) edges in S_j exist.
|
| 485 |
-
Mirrors SUBDEGREES + CHECKEDGES combo (Paper Appendix C, Lemma C.2–C.3)
|
| 486 |
-
but fused into one routine for efficiency.
|
| 487 |
-
Returns:
|
| 488 |
-
- degree_duGv: dict keyed by (v_anchor, u) -> d(u | G(v_anchor))
|
| 489 |
-
- S: dict for sampled_edges_tot[(x,y)] == True if edge found in S_j
|
| 490 |
-
"""
|
| 491 |
-
L, S = {}, {}
|
| 492 |
-
batch_size = len(samples)
|
| 493 |
-
for j in range(batch_size) :
|
| 494 |
-
for u in samples[j] :
|
| 495 |
-
if not (u in L) :
|
| 496 |
-
L[u] = Query(u)
|
| 497 |
-
L[u].v_list.add(self.DD_ranking[v[j]])
|
| 498 |
-
S.update({(u,y): False for y in samples[j]})
|
| 499 |
-
for u, Lu in L.items() :
|
| 500 |
-
Lu.build_cnt()
|
| 501 |
-
# Stream once: update counts per (u, v) bucket and mark edges inside S_j
|
| 502 |
-
for x, y in self.edge_list.get_edge() :
|
| 503 |
-
if x in L: L[x].cnt[bisect(L[x].v_list, self.DD_ranking[y])-1] += 1
|
| 504 |
-
if y in L: L[y].cnt[bisect(L[y].v_list, self.DD_ranking[x])-1] += 1
|
| 505 |
-
if (x,y) in S :
|
| 506 |
-
S[(x,y)] = S[(y,x)] = True
|
| 507 |
-
# Convert cumulative counts to suffix sums (degrees in G(v))
|
| 508 |
-
for u, Lu in L.items() :
|
| 509 |
-
for l in range(len(Lu.v_list)-2, -1, -1) :
|
| 510 |
-
Lu.cnt[l] += Lu.cnt[l+1]
|
| 511 |
-
degree_duGv = {(self.DD_order[v], u): Lu.cnt[i] for u, Lu in L.items() for i,v in enumerate(Lu.v_list)}
|
| 512 |
-
return degree_duGv, S
|
| 513 |
-
|
| 514 |
-
def RAND_GROW(self, v, k, batch_size) :
|
| 515 |
-
"""
|
| 516 |
-
Baseline RAND-GROW (Algorithm 3 lines 8–13): grow S by sampling one edge
|
| 517 |
-
uniformly at random from the cut E_i each round. This version streams edges
|
| 518 |
-
and does reservoir sampling per instance (k−1 passes overall).
|
| 519 |
-
Kept here for reference; the fast version below is used by default. (Paper §3.3.)
|
| 520 |
-
"""
|
| 521 |
-
samples = [{v[i]} for i in range(batch_size)]
|
| 522 |
-
for i in range(k-1) :
|
| 523 |
-
cnt, sampled_edge = [0]*batch_size, [None]*batch_size
|
| 524 |
-
for x, y in self.edge_list.get_edge() :
|
| 525 |
-
for j in range(batch_size) :
|
| 526 |
-
if (x in samples[j]) and (not (y in samples[j])) and (self.DD_ranking[y]>=self.DD_ranking[v[j]]) \
|
| 527 |
-
or (y in samples[j]) and (not (x in samples[j])) and (self.DD_ranking[x]>=self.DD_ranking[v[j]]) :
|
| 528 |
-
cnt[j] += 1
|
| 529 |
-
if random.randint(1,cnt[j])==1 :
|
| 530 |
-
sampled_edge[j] = [x,y]
|
| 531 |
-
for j in range(batch_size) :
|
| 532 |
-
samples[j].update(sampled_edge[j])
|
| 533 |
-
samples = [list(x) for x in samples]
|
| 534 |
-
return samples
|
| 535 |
-
|
| 536 |
-
def RAND_GROW_fast(self, v, k, batch_size) :
|
| 537 |
-
"""
|
| 538 |
-
Fast RAND-GROW with batched subdegree/edge answering (Appendix C).
|
| 539 |
-
For each round i=2..k:
|
| 540 |
-
• Choose u in S_j with prob ∝ |cut(u, G(v[j]) \ S_j)| using degree_duGv
|
| 541 |
-
minus already-internal edges (sampled_edges_tot).
|
| 542 |
-
• Draw a random edge from that cut uniformly by issuing “j-th edge” queries
|
| 543 |
-
that can be satisfied in one streamed pass (the ‘answers’ dict).
|
| 544 |
-
This compresses per-instance work and shares passes across Q trials (Paper §3.3,
|
| 545 |
-
Theorem 3.4; Appendix C SAMPLEEDGES idea).
|
| 546 |
-
"""
|
| 547 |
-
samples = [[v[i]] for i in range(batch_size)]
|
| 548 |
-
for i in range(2,k+1) :
|
| 549 |
-
degree_duGv, sampled_edges_tot = self.get_degree_duGv(v, samples)
|
| 550 |
-
L, original_query, answers, up = {}, [None]*batch_size, {}, np.zeros(batch_size, dtype=int)
|
| 551 |
-
# --- set up weighted-choices for u and edge-order queries per (u, v) ---
|
| 552 |
-
for j in range(batch_size) :
|
| 553 |
-
# degree_duS[u] = edges from u to current S_j already present
|
| 554 |
-
degree_duS = {x: sum(1 for y in samples[j] if sampled_edges_tot.get((x,y),False)==True) for x in samples[j]}
|
| 555 |
-
# Choose u ∈ S_j with weight d(u|G(v)) − d(u|G[S_j])
|
| 556 |
-
u = random.choices(samples[j], weights=[degree_duGv[(v[j], u)]-degree_duS[u] for u in samples[j]])[0]
|
| 557 |
-
up[j] = u
|
| 558 |
-
# Sample “edge indices” uniformly from 1..d(u|G(v)), request degree_duS[u]+1 of them
|
| 559 |
-
original_query[j] = random.sample(range(1, degree_duGv[(v[j], u)]+1), degree_duS[u]+1)
|
| 560 |
-
if not (u in L) :
|
| 561 |
-
L[u] = Query(u)
|
| 562 |
-
L[u].v_list.add(self.DD_ranking[v[j]])
|
| 563 |
-
L[u].queries.update(original_query[j])
|
| 564 |
-
sampled_edges_tot = None
|
| 565 |
-
# --- reorganize per-anchor v buckets, now we know which (u, v) each query belongs to ---
|
| 566 |
-
for u, Lu in L.items() :
|
| 567 |
-
Lu.build_cnt()
|
| 568 |
-
Lu.build_queries_for_v(degree_duGv, self.DD_order)
|
| 569 |
-
degree_duGv = None
|
| 570 |
-
# --- single stream pass: fulfill “j-th incident edge” queries with prefix counts ---
|
| 571 |
-
for xx, yy in self.edge_list.get_edge() :
|
| 572 |
-
for x, y in [(xx,yy), (yy,xx)] :
|
| 573 |
-
if x in L :
|
| 574 |
-
it = bisect(L[x].v_list, self.DD_ranking[y])-1
|
| 575 |
-
if it>-1 :
|
| 576 |
-
L[x].cnt[it] += 1
|
| 577 |
-
q = L[x].queries_for_v[it]
|
| 578 |
-
while len(q)>0 and q[-1]==L[x].cnt[it]+L[x].num_index[-(it+2)] :
|
| 579 |
-
answers[(x,q[-1])] = y
|
| 580 |
-
q.pop()
|
| 581 |
-
L = None
|
| 582 |
-
# --- finalize this round: pick the first answered edge not already inside S_j ---
|
| 583 |
-
for j in range(batch_size) :
|
| 584 |
-
for eid in original_query[j] :
|
| 585 |
-
u = answers.get((up[j], eid),-1)
|
| 586 |
-
if u!=-1 and not (u in samples[j]) :
|
| 587 |
-
samples[j].append(u)
|
| 588 |
-
break
|
| 589 |
-
|
| 590 |
-
return samples
|
| 591 |
-
|
| 592 |
-
def PROB(self, v, samples, k, batch_size) :
|
| 593 |
-
"""
|
| 594 |
-
Compute q_S, the probability that RAND-GROW(v, ≺) generates set S, as in
|
| 595 |
-
Algorithm 3 PROB (Paper §3.3, lines 15–23). Uses DP over subsets (bitmasks).
|
| 596 |
-
For each trial j:
|
| 597 |
-
- samples_j[1..k-1] = picked nodes beyond v[j],
|
| 598 |
-
- q accumulates path probabilities q_i(S_i), with transitions:
|
| 599 |
-
q[S ∪ {w}] += q[S] * (n_i / c_i)
|
| 600 |
-
where n_i = #edges from w to S, and
|
| 601 |
-
c_i = sum_{z∈S} [ d(z|G(v)) − d(z| G[S \ {w}] ) ].
|
| 602 |
-
The acceptance test uses Γ / (p(v)·q_S) (Algorithm 3 line 5).
|
| 603 |
-
"""
|
| 604 |
-
degree_duGv, sampled_edges_tot = self.get_degree_duGv(v, samples)
|
| 605 |
-
for j in range(batch_size) :
|
| 606 |
-
sampled_edge_list = {(x,y) for x in samples[j] for y in samples[j] if (x<y and sampled_edges_tot.get((x,y),False)==True)}
|
| 607 |
-
samples_j = samples[j][1:k]
|
| 608 |
-
q = [1]+[0]*((1<<(k-1))-1)
|
| 609 |
-
for S in range(1<<(k-1)) :
|
| 610 |
-
Sset = {samples_j[x] for x in next_bit(S,k)}.union({v[j]})
|
| 611 |
-
for x in next_bit(S^(-1),k-1) :
|
| 612 |
-
# c_i: denominator uses degrees in G(v) minus edges inside current S without {w}
|
| 613 |
-
ci = sum(degree_duGv[(v[j],u)] for u in Sset)-2*sum(1 for e in sampled_edge_list if e[0] in Sset and e[1] in Sset)
|
| 614 |
-
# n_i: number of edges linking the candidate w to Sset
|
| 615 |
-
ni = sum(1 for e in sampled_edge_list if e[0]==samples_j[x] and e[1] in Sset or e[1]==samples_j[x] and e[0] in Sset)
|
| 616 |
-
q[S|(1<<x)] += q[S]*ni/ci
|
| 617 |
-
|
| 618 |
-
# Rejection step: accept with prob Γ / (p(v)·q_S) (Paper §3.3; Lemma C.1)
|
| 619 |
-
if random.random()>self.Gamma/(self.p[v[j]]*q[-1]) :
|
| 620 |
-
samples[j] = None
|
| 621 |
-
|
| 622 |
-
def sample_one_batch(self, k, batch_size) :
|
| 623 |
-
"""
|
| 624 |
-
Run many trials in one batch:
|
| 625 |
-
1) Sample anchors v ~ p,
|
| 626 |
-
2) Grow with RAND_GROW_fast,
|
| 627 |
-
3) Filter via PROB (accept/reject).
|
| 628 |
-
Batches realize Theorem 3.4’s parallelism with shared passes. (Paper §3.3.)
|
| 629 |
-
"""
|
| 630 |
-
# Sample anchor vertices v by distribution p (Algorithm 3 line 2)
|
| 631 |
-
v = random.choices(range(self.n+1), weights=self.p, k=batch_size)
|
| 632 |
-
# Grow candidates (Algorithm 3 line 3); fast batched variant
|
| 633 |
-
# samples = self.RAND_GROW(v, k, batch_size)
|
| 634 |
-
samples = self.RAND_GROW_fast(v, k, batch_size)
|
| 635 |
-
# Compute q_S and accept (Algorithm 3 lines 4–6)
|
| 636 |
-
self.PROB(v, samples, k, batch_size)
|
| 637 |
-
return samples
|
| 638 |
-
|
| 639 |
-
def sample(self, k, target) :
|
| 640 |
-
"""
|
| 641 |
-
Keep sampling batches until 'target' uniform k-graphlets accepted.
|
| 642 |
-
Expected success probability per trial is k^{-O(k)} (scaled by (1+ε)^{k−1})
|
| 643 |
-
per Lemma C.1; batching amortizes passes/time (Paper §3.3; Theorem 3.4).
|
| 644 |
-
"""
|
| 645 |
-
# Choose batch size ~ MAX_EDGES / k^2 to cap per-batch edge work
|
| 646 |
-
batch_size, batch_count = self.MAX_EDGES//(k*k), 0
|
| 647 |
-
samples = []
|
| 648 |
-
logging.info('Start sampling with batch size %d ...',batch_size)
|
| 649 |
-
while True:
|
| 650 |
-
samples_batch = self.sample_one_batch(k, batch_size)
|
| 651 |
-
for i, s in enumerate(samples_batch):
|
| 652 |
-
if not (s is None):
|
| 653 |
-
samples.append(s)
|
| 654 |
-
if len(samples) == target :
|
| 655 |
-
logging.info('Sampled successfully after %d trials', batch_count*batch_size+i+1)
|
| 656 |
-
return samples
|
| 657 |
-
batch_count += 1
|
| 658 |
-
logging.info('The %d-th batch. Totally %d trials, %d successful trials.', batch_count, batch_count*batch_size, len(samples))
|
| 659 |
-
|
| 660 |
-
|
| 661 |
-
def load_yaml():
|
| 662 |
-
"""
|
| 663 |
-
Load experiment/config parameters. Typical keys:
|
| 664 |
-
- n: #vertices (assumes vertices labeled 1..n),
|
| 665 |
-
- edgelistmode: 'local' (streaming), 'DataFrame', or 'memory',
|
| 666 |
-
- edgelistfile: CSV with header and columns 'u,v',
|
| 667 |
-
- MAX_EDGES: per-pass work cap to steer heuristic decisions,
|
| 668 |
-
- k: graphlet size,
|
| 669 |
-
- epsilon: ε for DD-order approximation,
|
| 670 |
-
- ddorderfile (optional): load a precomputed DD order,
|
| 671 |
-
- target: #uniform samples to output.
|
| 672 |
-
"""
|
| 673 |
-
with open("EfficientStreamingAlgor/config.yml", "r", encoding="utf-8") as f:
|
| 674 |
-
re = yaml.load(f, Loader=yaml.FullLoader)
|
| 675 |
-
return re
|
| 676 |
-
|
| 677 |
-
|
| 678 |
-
if __name__ == '__main__' :
|
| 679 |
-
|
| 680 |
-
args = load_yaml()
|
| 681 |
-
logging.basicConfig(level=logging.INFO,
|
| 682 |
-
handlers=[logging.FileHandler('logs.log'), logging.StreamHandler(sys.stdout)],
|
| 683 |
-
format="[%(asctime)s %(levelname)s] %(message)s")
|
| 684 |
-
|
| 685 |
-
# initialize: choose an edge list backend (use 'local' for semi-streaming faithfulness)
|
| 686 |
-
graph = Graph(args['n'], args['edgelistmode'], args['edgelistfile'], args['MAX_EDGES'])
|
| 687 |
-
|
| 688 |
-
# preprocess (Paper §3: PREPROCESS calls APPROX-DD then COMPUTEDISTRIB; Alg. 4 in App. A)
|
| 689 |
-
if 'ddorderfile' in args :
|
| 690 |
-
# preprocess with a provided DD order (useful to amortize cost across runs)
|
| 691 |
-
with open(args['ddorderfile'],'rb') as f:
|
| 692 |
-
ddorder = pickle.load(f)
|
| 693 |
-
graph.sampling_preprocess(args['k'], args['epsilon'], ddorder)
|
| 694 |
-
else :
|
| 695 |
-
# compute DD order then compute p and Γ
|
| 696 |
-
graph.sampling_preprocess(args['k'], args['epsilon'])
|
| 697 |
-
with open('DD_order.bin','wb') as f:
|
| 698 |
-
pickle.dump(graph.DD_order, f)
|
| 699 |
-
passes_for_preprocess = graph.edge_list.passes
|
| 700 |
-
|
| 701 |
-
# sample (Paper §3.3: SAMPLE, batched; success count grows linearly with batch trials)
|
| 702 |
-
samples = graph.sample(args['k'], args['target'])
|
| 703 |
-
with open('samples.bin','wb') as f:
|
| 704 |
-
pickle.dump(samples, f)
|
| 705 |
-
logging.info('Number of passes in total = %s', graph.edge_list.passes)
|
| 706 |
-
logging.info('Number of passes for preprocessing = %s', passes_for_preprocess)
|
| 707 |
-
logging.info('Number of passes for sampling = %s', graph.edge_list.passes - passes_for_preproc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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
annotations/annotation_144/function_info.json
DELETED
|
@@ -1,688 +0,0 @@
|
|
| 1 |
-
[
|
| 2 |
-
{
|
| 3 |
-
"function_name": "DSU.__init__",
|
| 4 |
-
"file_path": "stream_ugs.py",
|
| 5 |
-
"function_calls": [
|
| 6 |
-
{
|
| 7 |
-
"function_call_name": "numpy.array",
|
| 8 |
-
"functionality": "Create an ndarray from an iterable with a given dtype.",
|
| 9 |
-
"input": "iterable=[(i, 1, last_heavy[i]) for i in range(n+1)], dtype=[('root', int), ('size', int), ('last_heavy', int)]",
|
| 10 |
-
"output": "ndarray",
|
| 11 |
-
"location_or_namespace": "numpy"
|
| 12 |
-
}
|
| 13 |
-
]
|
| 14 |
-
},
|
| 15 |
-
{
|
| 16 |
-
"function_name": "DSU.get_root",
|
| 17 |
-
"file_path": "stream_ugs.py",
|
| 18 |
-
"function_calls": [
|
| 19 |
-
{
|
| 20 |
-
"function_call_name": "DSU.get_root",
|
| 21 |
-
"functionality": "",
|
| 22 |
-
"input": "self.a[x]['root']",
|
| 23 |
-
"output": "int (root representative)",
|
| 24 |
-
"location_or_namespace": "stream_ugs.py"
|
| 25 |
-
}
|
| 26 |
-
]
|
| 27 |
-
},
|
| 28 |
-
{
|
| 29 |
-
"function_name": "DSU.merge",
|
| 30 |
-
"file_path": "stream_ugs.py",
|
| 31 |
-
"function_calls": [
|
| 32 |
-
{
|
| 33 |
-
"function_call_name": "DSU.get_root",
|
| 34 |
-
"functionality": "",
|
| 35 |
-
"input": "x or y",
|
| 36 |
-
"output": "int (root representative)",
|
| 37 |
-
"location_or_namespace": "stream_ugs.py"
|
| 38 |
-
}
|
| 39 |
-
]
|
| 40 |
-
},
|
| 41 |
-
{
|
| 42 |
-
"function_name": "next_bit",
|
| 43 |
-
"file_path": "stream_ugs.py",
|
| 44 |
-
"function_calls": []
|
| 45 |
-
},
|
| 46 |
-
{
|
| 47 |
-
"function_name": "Query.__init__",
|
| 48 |
-
"file_path": "stream_ugs.py",
|
| 49 |
-
"function_calls": []
|
| 50 |
-
},
|
| 51 |
-
{
|
| 52 |
-
"function_name": "Query.build_cnt",
|
| 53 |
-
"file_path": "stream_ugs.py",
|
| 54 |
-
"function_calls": []
|
| 55 |
-
},
|
| 56 |
-
{
|
| 57 |
-
"function_name": "Query.build_queries_for_v",
|
| 58 |
-
"file_path": "stream_ugs.py",
|
| 59 |
-
"function_calls": []
|
| 60 |
-
},
|
| 61 |
-
{
|
| 62 |
-
"function_name": "Edge_list_local.__init__",
|
| 63 |
-
"file_path": "stream_ugs.py",
|
| 64 |
-
"function_calls": []
|
| 65 |
-
},
|
| 66 |
-
{
|
| 67 |
-
"function_name": "Edge_list_local.get_edge",
|
| 68 |
-
"file_path": "stream_ugs.py",
|
| 69 |
-
"function_calls": [
|
| 70 |
-
{
|
| 71 |
-
"function_call_name": "open",
|
| 72 |
-
"functionality": "Open a file path and return a file object.",
|
| 73 |
-
"input": "edge_list_file, mode='r'",
|
| 74 |
-
"output": "file object",
|
| 75 |
-
"location_or_namespace": "builtins"
|
| 76 |
-
},
|
| 77 |
-
{
|
| 78 |
-
"function_call_name": "csv.reader",
|
| 79 |
-
"functionality": "Create an iterator that reads lines from a CSV file.",
|
| 80 |
-
"input": "file_object, delimiter=','",
|
| 81 |
-
"output": "csv reader iterator",
|
| 82 |
-
"location_or_namespace": "csv"
|
| 83 |
-
}
|
| 84 |
-
]
|
| 85 |
-
},
|
| 86 |
-
{
|
| 87 |
-
"function_name": "Edge_list_local.end",
|
| 88 |
-
"file_path": "stream_ugs.py",
|
| 89 |
-
"function_calls": []
|
| 90 |
-
},
|
| 91 |
-
{
|
| 92 |
-
"function_name": "Edge_list_DataFrame.__init__",
|
| 93 |
-
"file_path": "stream_ugs.py",
|
| 94 |
-
"function_calls": [
|
| 95 |
-
{
|
| 96 |
-
"function_call_name": "pyspark.sql.SparkSession.builder.getOrCreate",
|
| 97 |
-
"functionality": "Create or retrieve a SparkSession.",
|
| 98 |
-
"input": "",
|
| 99 |
-
"output": "SparkSession",
|
| 100 |
-
"location_or_namespace": "pyspark.sql"
|
| 101 |
-
},
|
| 102 |
-
{
|
| 103 |
-
"function_call_name": "SparkContext.setLogLevel",
|
| 104 |
-
"functionality": "Set logging verbosity for the SparkContext.",
|
| 105 |
-
"input": "level='WARN'",
|
| 106 |
-
"output": "None",
|
| 107 |
-
"location_or_namespace": "pyspark"
|
| 108 |
-
},
|
| 109 |
-
{
|
| 110 |
-
"function_call_name": "pyspark.sql.DataFrameReader.csv",
|
| 111 |
-
"functionality": "Load a CSV file into a Spark DataFrame.",
|
| 112 |
-
"input": "path=edge_list_file, header=True, inferSchema=True",
|
| 113 |
-
"output": "DataFrame",
|
| 114 |
-
"location_or_namespace": "pyspark.sql"
|
| 115 |
-
}
|
| 116 |
-
]
|
| 117 |
-
},
|
| 118 |
-
{
|
| 119 |
-
"function_name": "Edge_list_DataFrame.get_edge",
|
| 120 |
-
"file_path": "stream_ugs.py",
|
| 121 |
-
"function_calls": [
|
| 122 |
-
{
|
| 123 |
-
"function_call_name": "pyspark.sql.DataFrame.toLocalIterator",
|
| 124 |
-
"functionality": "Return an iterator that pulls partitions into the driver.",
|
| 125 |
-
"input": "",
|
| 126 |
-
"output": "iterator over Row",
|
| 127 |
-
"location_or_namespace": "pyspark.sql"
|
| 128 |
-
}
|
| 129 |
-
]
|
| 130 |
-
},
|
| 131 |
-
{
|
| 132 |
-
"function_name": "Edge_list_DataFrame.end",
|
| 133 |
-
"file_path": "stream_ugs.py",
|
| 134 |
-
"function_calls": [
|
| 135 |
-
{
|
| 136 |
-
"function_call_name": "pyspark.sql.SparkSession.stop",
|
| 137 |
-
"functionality": "Stop the underlying SparkSession.",
|
| 138 |
-
"input": "",
|
| 139 |
-
"output": "None",
|
| 140 |
-
"location_or_namespace": "pyspark.sql"
|
| 141 |
-
}
|
| 142 |
-
]
|
| 143 |
-
},
|
| 144 |
-
{
|
| 145 |
-
"function_name": "Edge_list_StreamingDataFrame.__init__",
|
| 146 |
-
"file_path": "stream_ugs.py",
|
| 147 |
-
"function_calls": []
|
| 148 |
-
},
|
| 149 |
-
{
|
| 150 |
-
"function_name": "Edge_list_memory.__init__",
|
| 151 |
-
"file_path": "stream_ugs.py",
|
| 152 |
-
"function_calls": [
|
| 153 |
-
{
|
| 154 |
-
"function_call_name": "open",
|
| 155 |
-
"functionality": "Open a file path and return a file object.",
|
| 156 |
-
"input": "edge_list_file, mode='r'",
|
| 157 |
-
"output": "file object",
|
| 158 |
-
"location_or_namespace": "builtins"
|
| 159 |
-
},
|
| 160 |
-
{
|
| 161 |
-
"function_call_name": "csv.reader",
|
| 162 |
-
"functionality": "Create an iterator that reads lines from a CSV file.",
|
| 163 |
-
"input": "file_object, delimiter=','",
|
| 164 |
-
"output": "csv reader iterator",
|
| 165 |
-
"location_or_namespace": "csv"
|
| 166 |
-
}
|
| 167 |
-
]
|
| 168 |
-
},
|
| 169 |
-
{
|
| 170 |
-
"function_name": "Edge_list_memory.get_edge",
|
| 171 |
-
"file_path": "stream_ugs.py",
|
| 172 |
-
"function_calls": []
|
| 173 |
-
},
|
| 174 |
-
{
|
| 175 |
-
"function_name": "Edge_list_memory.end",
|
| 176 |
-
"file_path": "stream_ugs.py",
|
| 177 |
-
"function_calls": []
|
| 178 |
-
},
|
| 179 |
-
{
|
| 180 |
-
"function_name": "Graph.__init__",
|
| 181 |
-
"file_path": "stream_ugs.py",
|
| 182 |
-
"function_calls": [
|
| 183 |
-
{
|
| 184 |
-
"function_call_name": "Edge_list_local.__init__",
|
| 185 |
-
"functionality": "",
|
| 186 |
-
"input": "edge_list_file",
|
| 187 |
-
"output": "Edge_list_local instance",
|
| 188 |
-
"location_or_namespace": "stream_ugs.py"
|
| 189 |
-
},
|
| 190 |
-
{
|
| 191 |
-
"function_call_name": "Edge_list_DataFrame.__init__",
|
| 192 |
-
"functionality": "",
|
| 193 |
-
"input": "edge_list_file",
|
| 194 |
-
"output": "Edge_list_DataFrame instance",
|
| 195 |
-
"location_or_namespace": "stream_ugs.py"
|
| 196 |
-
},
|
| 197 |
-
{
|
| 198 |
-
"function_call_name": "Edge_list_StreamingDataFrame.__init__",
|
| 199 |
-
"functionality": "",
|
| 200 |
-
"input": "edge_list_file",
|
| 201 |
-
"output": "Edge_list_StreamingDataFrame instance",
|
| 202 |
-
"location_or_namespace": "stream_ugs.py"
|
| 203 |
-
},
|
| 204 |
-
{
|
| 205 |
-
"function_call_name": "Edge_list_memory.__init__",
|
| 206 |
-
"functionality": "",
|
| 207 |
-
"input": "edge_list_file",
|
| 208 |
-
"output": "Edge_list_memory instance",
|
| 209 |
-
"location_or_namespace": "stream_ugs.py"
|
| 210 |
-
}
|
| 211 |
-
]
|
| 212 |
-
},
|
| 213 |
-
{
|
| 214 |
-
"function_name": "Graph.get_degree_G",
|
| 215 |
-
"file_path": "stream_ugs.py",
|
| 216 |
-
"function_calls": [
|
| 217 |
-
{
|
| 218 |
-
"function_call_name": "numpy.zeros",
|
| 219 |
-
"functionality": "Create a zero-initialized ndarray.",
|
| 220 |
-
"input": "shape=self.n+1, dtype=int",
|
| 221 |
-
"output": "ndarray",
|
| 222 |
-
"location_or_namespace": "numpy"
|
| 223 |
-
},
|
| 224 |
-
{
|
| 225 |
-
"function_call_name": "Edge_list_backend.get_edge",
|
| 226 |
-
"functionality": "",
|
| 227 |
-
"input": "",
|
| 228 |
-
"output": "iterator of (u, v)",
|
| 229 |
-
"location_or_namespace": "stream_ugs.py"
|
| 230 |
-
}
|
| 231 |
-
]
|
| 232 |
-
},
|
| 233 |
-
{
|
| 234 |
-
"function_name": "Graph.get_degree_H",
|
| 235 |
-
"file_path": "stream_ugs.py",
|
| 236 |
-
"function_calls": [
|
| 237 |
-
{
|
| 238 |
-
"function_call_name": "numpy.zeros",
|
| 239 |
-
"functionality": "Create a zero-initialized ndarray.",
|
| 240 |
-
"input": "shape=self.n+1, dtype=int",
|
| 241 |
-
"output": "ndarray",
|
| 242 |
-
"location_or_namespace": "numpy"
|
| 243 |
-
},
|
| 244 |
-
{
|
| 245 |
-
"function_call_name": "Edge_list_backend.get_edge",
|
| 246 |
-
"functionality": "",
|
| 247 |
-
"input": "",
|
| 248 |
-
"output": "iterator of (u, v)",
|
| 249 |
-
"location_or_namespace": "stream_ugs.py"
|
| 250 |
-
}
|
| 251 |
-
]
|
| 252 |
-
},
|
| 253 |
-
{
|
| 254 |
-
"function_name": "Graph.PEEL_approx",
|
| 255 |
-
"file_path": "stream_ugs.py",
|
| 256 |
-
"function_calls": [
|
| 257 |
-
{
|
| 258 |
-
"function_call_name": "math.ceil",
|
| 259 |
-
"functionality": "Ceiling function.",
|
| 260 |
-
"input": "x",
|
| 261 |
-
"output": "int",
|
| 262 |
-
"location_or_namespace": "math"
|
| 263 |
-
},
|
| 264 |
-
{
|
| 265 |
-
"function_call_name": "Graph.get_degree_H",
|
| 266 |
-
"functionality": "",
|
| 267 |
-
"input": "partition",
|
| 268 |
-
"output": "degree mapping",
|
| 269 |
-
"location_or_namespace": "stream_ugs.py"
|
| 270 |
-
},
|
| 271 |
-
{
|
| 272 |
-
"function_call_name": "random.randint",
|
| 273 |
-
"functionality": "Return a random integer N such that a <= N <= b.",
|
| 274 |
-
"input": "a=0, b=l-1",
|
| 275 |
-
"output": "int",
|
| 276 |
-
"location_or_namespace": "random"
|
| 277 |
-
},
|
| 278 |
-
{
|
| 279 |
-
"function_call_name": "Graph.get_degree_G",
|
| 280 |
-
"functionality": "",
|
| 281 |
-
"input": "vertices",
|
| 282 |
-
"output": "degree mapping",
|
| 283 |
-
"location_or_namespace": "stream_ugs.py"
|
| 284 |
-
}
|
| 285 |
-
]
|
| 286 |
-
},
|
| 287 |
-
{
|
| 288 |
-
"function_name": "Graph.PEEL_heuristic",
|
| 289 |
-
"file_path": "stream_ugs.py",
|
| 290 |
-
"function_calls": [
|
| 291 |
-
{
|
| 292 |
-
"function_call_name": "Edge_list_backend.get_edge",
|
| 293 |
-
"functionality": "",
|
| 294 |
-
"input": "",
|
| 295 |
-
"output": "iterator of (u, v)",
|
| 296 |
-
"location_or_namespace": "stream_ugs.py"
|
| 297 |
-
},
|
| 298 |
-
{
|
| 299 |
-
"function_call_name": "heapq.heappop",
|
| 300 |
-
"functionality": "Pop the smallest item off the heap.",
|
| 301 |
-
"input": "heap",
|
| 302 |
-
"output": "item",
|
| 303 |
-
"location_or_namespace": "heapq"
|
| 304 |
-
},
|
| 305 |
-
{
|
| 306 |
-
"function_call_name": "heapq.heappush",
|
| 307 |
-
"functionality": "Push item onto heap, maintaining the heap invariant.",
|
| 308 |
-
"input": "heap, item",
|
| 309 |
-
"output": "None",
|
| 310 |
-
"location_or_namespace": "heapq"
|
| 311 |
-
}
|
| 312 |
-
]
|
| 313 |
-
},
|
| 314 |
-
{
|
| 315 |
-
"function_name": "Graph.get_DD_order",
|
| 316 |
-
"file_path": "stream_ugs.py",
|
| 317 |
-
"function_calls": [
|
| 318 |
-
{
|
| 319 |
-
"function_call_name": "logging.info",
|
| 320 |
-
"functionality": "Log a message with INFO level.",
|
| 321 |
-
"input": "msg, *args",
|
| 322 |
-
"output": "None",
|
| 323 |
-
"location_or_namespace": "logging"
|
| 324 |
-
},
|
| 325 |
-
{
|
| 326 |
-
"function_call_name": "Graph.get_degree_G",
|
| 327 |
-
"functionality": "",
|
| 328 |
-
"input": "vertices",
|
| 329 |
-
"output": "degree mapping",
|
| 330 |
-
"location_or_namespace": "stream_ugs.py"
|
| 331 |
-
},
|
| 332 |
-
{
|
| 333 |
-
"function_call_name": "numpy.zeros",
|
| 334 |
-
"functionality": "Create a zero-initialized ndarray.",
|
| 335 |
-
"input": "shape=self.n+1, dtype=int",
|
| 336 |
-
"output": "ndarray",
|
| 337 |
-
"location_or_namespace": "numpy"
|
| 338 |
-
},
|
| 339 |
-
{
|
| 340 |
-
"function_call_name": "Edge_list_backend.get_edge",
|
| 341 |
-
"functionality": "",
|
| 342 |
-
"input": "",
|
| 343 |
-
"output": "iterator of (u, v)",
|
| 344 |
-
"location_or_namespace": "stream_ugs.py"
|
| 345 |
-
},
|
| 346 |
-
{
|
| 347 |
-
"function_call_name": "Graph.PEEL_heuristic",
|
| 348 |
-
"functionality": "",
|
| 349 |
-
"input": "vertices, heap_slice, degree_G, threshold_lb, DD_order",
|
| 350 |
-
"output": "None",
|
| 351 |
-
"location_or_namespace": "stream_ugs.py"
|
| 352 |
-
},
|
| 353 |
-
{
|
| 354 |
-
"function_call_name": "Graph.PEEL_approx",
|
| 355 |
-
"functionality": "",
|
| 356 |
-
"input": "vertices, degree_G, epsilon, alpha, max_degree, DD_order",
|
| 357 |
-
"output": "degree mapping",
|
| 358 |
-
"location_or_namespace": "stream_ugs.py"
|
| 359 |
-
},
|
| 360 |
-
{
|
| 361 |
-
"function_call_name": "numpy.ndarray.max",
|
| 362 |
-
"functionality": "Return the maximum of an array.",
|
| 363 |
-
"input": "self (ndarray)",
|
| 364 |
-
"output": "int",
|
| 365 |
-
"location_or_namespace": "numpy"
|
| 366 |
-
}
|
| 367 |
-
]
|
| 368 |
-
},
|
| 369 |
-
{
|
| 370 |
-
"function_name": "Graph.get_degree_dvGv",
|
| 371 |
-
"file_path": "stream_ugs.py",
|
| 372 |
-
"function_calls": [
|
| 373 |
-
{
|
| 374 |
-
"function_call_name": "numpy.zeros",
|
| 375 |
-
"functionality": "Create a zero-initialized ndarray.",
|
| 376 |
-
"input": "shape=self.n+1, dtype=int",
|
| 377 |
-
"output": "ndarray",
|
| 378 |
-
"location_or_namespace": "numpy"
|
| 379 |
-
},
|
| 380 |
-
{
|
| 381 |
-
"function_call_name": "Edge_list_backend.get_edge",
|
| 382 |
-
"functionality": "",
|
| 383 |
-
"input": "",
|
| 384 |
-
"output": "iterator of (u, v)",
|
| 385 |
-
"location_or_namespace": "stream_ugs.py"
|
| 386 |
-
}
|
| 387 |
-
]
|
| 388 |
-
},
|
| 389 |
-
{
|
| 390 |
-
"function_name": "Graph.get_b",
|
| 391 |
-
"file_path": "stream_ugs.py",
|
| 392 |
-
"function_calls": [
|
| 393 |
-
{
|
| 394 |
-
"function_call_name": "DSU.__init__",
|
| 395 |
-
"functionality": "",
|
| 396 |
-
"input": "n=self.n, last_heavy=last_heavy",
|
| 397 |
-
"output": "DSU instance",
|
| 398 |
-
"location_or_namespace": "stream_ugs.py"
|
| 399 |
-
},
|
| 400 |
-
{
|
| 401 |
-
"function_call_name": "DSU.merge",
|
| 402 |
-
"functionality": "",
|
| 403 |
-
"input": "x, y",
|
| 404 |
-
"output": "None",
|
| 405 |
-
"location_or_namespace": "stream_ugs.py"
|
| 406 |
-
}
|
| 407 |
-
]
|
| 408 |
-
},
|
| 409 |
-
{
|
| 410 |
-
"function_name": "Graph.sampling_preprocess",
|
| 411 |
-
"file_path": "stream_ugs.py",
|
| 412 |
-
"function_calls": [
|
| 413 |
-
{
|
| 414 |
-
"function_call_name": "Graph.get_DD_order",
|
| 415 |
-
"functionality": "",
|
| 416 |
-
"input": "epsilon, enable_heuristic",
|
| 417 |
-
"output": "list[int] (DD order)",
|
| 418 |
-
"location_or_namespace": "stream_ugs.py"
|
| 419 |
-
},
|
| 420 |
-
{
|
| 421 |
-
"function_call_name": "logging.info",
|
| 422 |
-
"functionality": "Log a message with INFO level.",
|
| 423 |
-
"input": "msg, *args",
|
| 424 |
-
"output": "None",
|
| 425 |
-
"location_or_namespace": "logging"
|
| 426 |
-
},
|
| 427 |
-
{
|
| 428 |
-
"function_call_name": "numpy.empty",
|
| 429 |
-
"functionality": "Create an uninitialized ndarray.",
|
| 430 |
-
"input": "shape=self.n+1, dtype=int",
|
| 431 |
-
"output": "ndarray",
|
| 432 |
-
"location_or_namespace": "numpy"
|
| 433 |
-
},
|
| 434 |
-
{
|
| 435 |
-
"function_call_name": "numpy.arange",
|
| 436 |
-
"functionality": "Create evenly spaced values within a given interval.",
|
| 437 |
-
"input": "stop=self.n",
|
| 438 |
-
"output": "ndarray",
|
| 439 |
-
"location_or_namespace": "numpy"
|
| 440 |
-
},
|
| 441 |
-
{
|
| 442 |
-
"function_call_name": "Graph.get_degree_dvGv",
|
| 443 |
-
"functionality": "",
|
| 444 |
-
"input": "k",
|
| 445 |
-
"output": "tuple (degree, light_edge_list, last_heavy)",
|
| 446 |
-
"location_or_namespace": "stream_ugs.py"
|
| 447 |
-
},
|
| 448 |
-
{
|
| 449 |
-
"function_call_name": "Graph.get_b",
|
| 450 |
-
"functionality": "",
|
| 451 |
-
"input": "degree_dvGv, light_edge_list, last_heavy, k",
|
| 452 |
-
"output": "list[int] b",
|
| 453 |
-
"location_or_namespace": "stream_ugs.py"
|
| 454 |
-
},
|
| 455 |
-
{
|
| 456 |
-
"function_call_name": "math.factorial",
|
| 457 |
-
"functionality": "Compute n! (factorial).",
|
| 458 |
-
"input": "n=k-1",
|
| 459 |
-
"output": "int",
|
| 460 |
-
"location_or_namespace": "math"
|
| 461 |
-
}
|
| 462 |
-
]
|
| 463 |
-
},
|
| 464 |
-
{
|
| 465 |
-
"function_name": "Graph.get_degree_duGv",
|
| 466 |
-
"file_path": "stream_ugs.py",
|
| 467 |
-
"function_calls": [
|
| 468 |
-
{
|
| 469 |
-
"function_call_name": "Query.__init__",
|
| 470 |
-
"functionality": "",
|
| 471 |
-
"input": "u",
|
| 472 |
-
"output": "Query instance",
|
| 473 |
-
"location_or_namespace": "stream_ugs.py"
|
| 474 |
-
},
|
| 475 |
-
{
|
| 476 |
-
"function_call_name": "Query.build_cnt",
|
| 477 |
-
"functionality": "",
|
| 478 |
-
"input": "",
|
| 479 |
-
"output": "None",
|
| 480 |
-
"location_or_namespace": "stream_ugs.py"
|
| 481 |
-
},
|
| 482 |
-
{
|
| 483 |
-
"function_call_name": "Edge_list_backend.get_edge",
|
| 484 |
-
"functionality": "",
|
| 485 |
-
"input": "",
|
| 486 |
-
"output": "iterator of (u, v)",
|
| 487 |
-
"location_or_namespace": "stream_ugs.py"
|
| 488 |
-
},
|
| 489 |
-
{
|
| 490 |
-
"function_call_name": "bisect.bisect",
|
| 491 |
-
"functionality": "Locate the insertion point for x in a sorted list.",
|
| 492 |
-
"input": "a=Lu.v_list, x=rank_other",
|
| 493 |
-
"output": "int (index)",
|
| 494 |
-
"location_or_namespace": "bisect"
|
| 495 |
-
},
|
| 496 |
-
{
|
| 497 |
-
"function_call_name": "Query.build_queries_for_v",
|
| 498 |
-
"functionality": "",
|
| 499 |
-
"input": "degree_duGv, DD_order",
|
| 500 |
-
"output": "None",
|
| 501 |
-
"location_or_namespace": "stream_ugs.py"
|
| 502 |
-
}
|
| 503 |
-
]
|
| 504 |
-
},
|
| 505 |
-
{
|
| 506 |
-
"function_name": "Graph.RAND_GROW",
|
| 507 |
-
"file_path": "stream_ugs.py",
|
| 508 |
-
"function_calls": [
|
| 509 |
-
{
|
| 510 |
-
"function_call_name": "Edge_list_backend.get_edge",
|
| 511 |
-
"functionality": "",
|
| 512 |
-
"input": "",
|
| 513 |
-
"output": "iterator of (u, v)",
|
| 514 |
-
"location_or_namespace": "stream_ugs.py"
|
| 515 |
-
},
|
| 516 |
-
{
|
| 517 |
-
"function_call_name": "random.randint",
|
| 518 |
-
"functionality": "Return a random integer N such that a <= N <= b.",
|
| 519 |
-
"input": "a=1, b=cnt[j]",
|
| 520 |
-
"output": "int",
|
| 521 |
-
"location_or_namespace": "random"
|
| 522 |
-
}
|
| 523 |
-
]
|
| 524 |
-
},
|
| 525 |
-
{
|
| 526 |
-
"function_name": "Graph.RAND_GROW_fast",
|
| 527 |
-
"file_path": "stream_ugs.py",
|
| 528 |
-
"function_calls": [
|
| 529 |
-
{
|
| 530 |
-
"function_call_name": "Graph.get_degree_duGv",
|
| 531 |
-
"functionality": "",
|
| 532 |
-
"input": "v, samples",
|
| 533 |
-
"output": "degree_duGv dict, sampled_edges_tot dict",
|
| 534 |
-
"location_or_namespace": "stream_ugs.py"
|
| 535 |
-
},
|
| 536 |
-
{
|
| 537 |
-
"function_call_name": "numpy.zeros",
|
| 538 |
-
"functionality": "Create a zero-initialized ndarray.",
|
| 539 |
-
"input": "shape=batch_size, dtype=int",
|
| 540 |
-
"output": "ndarray",
|
| 541 |
-
"location_or_namespace": "numpy"
|
| 542 |
-
},
|
| 543 |
-
{
|
| 544 |
-
"function_call_name": "random.choices",
|
| 545 |
-
"functionality": "Choose k elements with replacement, weighted.",
|
| 546 |
-
"input": "population=samples[j], weights=[...], k=1 (implicit)",
|
| 547 |
-
"output": "list (selected elements)",
|
| 548 |
-
"location_or_namespace": "random"
|
| 549 |
-
},
|
| 550 |
-
{
|
| 551 |
-
"function_call_name": "random.sample",
|
| 552 |
-
"functionality": "Choose k unique elements from a population.",
|
| 553 |
-
"input": "population=range(1, degree_duGv[(v[j], u)]+1), k=degree_duS[u]+1",
|
| 554 |
-
"output": "list[int]",
|
| 555 |
-
"location_or_namespace": "random"
|
| 556 |
-
},
|
| 557 |
-
{
|
| 558 |
-
"function_call_name": "Query.__init__",
|
| 559 |
-
"functionality": "",
|
| 560 |
-
"input": "u",
|
| 561 |
-
"output": "Query instance",
|
| 562 |
-
"location_or_namespace": "stream_ugs.py"
|
| 563 |
-
},
|
| 564 |
-
{
|
| 565 |
-
"function_call_name": "Query.build_cnt",
|
| 566 |
-
"functionality": "",
|
| 567 |
-
"input": "",
|
| 568 |
-
"output": "None",
|
| 569 |
-
"location_or_namespace": "stream_ugs.py"
|
| 570 |
-
},
|
| 571 |
-
{
|
| 572 |
-
"function_call_name": "Query.build_queries_for_v",
|
| 573 |
-
"functionality": "",
|
| 574 |
-
"input": "degree_duGv, DD_order",
|
| 575 |
-
"output": "None",
|
| 576 |
-
"location_or_namespace": "stream_ugs.py"
|
| 577 |
-
},
|
| 578 |
-
{
|
| 579 |
-
"function_call_name": "Edge_list_backend.get_edge",
|
| 580 |
-
"functionality": "",
|
| 581 |
-
"input": "",
|
| 582 |
-
"output": "iterator of (u, v)",
|
| 583 |
-
"location_or_namespace": "stream_ugs.py"
|
| 584 |
-
},
|
| 585 |
-
{
|
| 586 |
-
"function_call_name": "bisect.bisect",
|
| 587 |
-
"functionality": "Locate the insertion point for x in a sorted list.",
|
| 588 |
-
"input": "a=Lu.v_list, x=rank_y",
|
| 589 |
-
"output": "int (index)",
|
| 590 |
-
"location_or_namespace": "bisect"
|
| 591 |
-
}
|
| 592 |
-
]
|
| 593 |
-
},
|
| 594 |
-
{
|
| 595 |
-
"function_name": "Graph.PROB",
|
| 596 |
-
"file_path": "stream_ugs.py",
|
| 597 |
-
"function_calls": [
|
| 598 |
-
{
|
| 599 |
-
"function_call_name": "Graph.get_degree_duGv",
|
| 600 |
-
"functionality": "",
|
| 601 |
-
"input": "v, samples",
|
| 602 |
-
"output": "degree_duGv dict, sampled_edges_tot dict",
|
| 603 |
-
"location_or_namespace": "stream_ugs.py"
|
| 604 |
-
},
|
| 605 |
-
{
|
| 606 |
-
"function_call_name": "next_bit",
|
| 607 |
-
"functionality": "",
|
| 608 |
-
"input": "S, size",
|
| 609 |
-
"output": "iterator of bit positions",
|
| 610 |
-
"location_or_namespace": "stream_ugs.py"
|
| 611 |
-
},
|
| 612 |
-
{
|
| 613 |
-
"function_call_name": "random.random",
|
| 614 |
-
"functionality": "Return the next random floating point number in [0.0, 1.0).",
|
| 615 |
-
"input": "",
|
| 616 |
-
"output": "float",
|
| 617 |
-
"location_or_namespace": "random"
|
| 618 |
-
}
|
| 619 |
-
]
|
| 620 |
-
},
|
| 621 |
-
{
|
| 622 |
-
"function_name": "Graph.sample_one_batch",
|
| 623 |
-
"file_path": "stream_ugs.py",
|
| 624 |
-
"function_calls": [
|
| 625 |
-
{
|
| 626 |
-
"function_call_name": "random.choices",
|
| 627 |
-
"functionality": "Choose k elements with replacement, weighted.",
|
| 628 |
-
"input": "population=range(self.n+1), weights=self.p, k=batch_size",
|
| 629 |
-
"output": "list[int]",
|
| 630 |
-
"location_or_namespace": "random"
|
| 631 |
-
},
|
| 632 |
-
{
|
| 633 |
-
"function_call_name": "Graph.RAND_GROW_fast",
|
| 634 |
-
"functionality": "",
|
| 635 |
-
"input": "v, k, batch_size",
|
| 636 |
-
"output": "list[list[int]] (samples or None)",
|
| 637 |
-
"location_or_namespace": "stream_ugs.py"
|
| 638 |
-
},
|
| 639 |
-
{
|
| 640 |
-
"function_call_name": "Graph.PROB",
|
| 641 |
-
"functionality": "",
|
| 642 |
-
"input": "v, samples, k, batch_size",
|
| 643 |
-
"output": "None (in-place filtering)",
|
| 644 |
-
"location_or_namespace": "stream_ugs.py"
|
| 645 |
-
}
|
| 646 |
-
]
|
| 647 |
-
},
|
| 648 |
-
{
|
| 649 |
-
"function_name": "Graph.sample",
|
| 650 |
-
"file_path": "stream_ugs.py",
|
| 651 |
-
"function_calls": [
|
| 652 |
-
{
|
| 653 |
-
"function_call_name": "logging.info",
|
| 654 |
-
"functionality": "Log a message with INFO level.",
|
| 655 |
-
"input": "msg, *args",
|
| 656 |
-
"output": "None",
|
| 657 |
-
"location_or_namespace": "logging"
|
| 658 |
-
},
|
| 659 |
-
{
|
| 660 |
-
"function_call_name": "Graph.sample_one_batch",
|
| 661 |
-
"functionality": "",
|
| 662 |
-
"input": "k, batch_size",
|
| 663 |
-
"output": "list[list[int] | None]",
|
| 664 |
-
"location_or_namespace": "stream_ugs.py"
|
| 665 |
-
}
|
| 666 |
-
]
|
| 667 |
-
},
|
| 668 |
-
{
|
| 669 |
-
"function_name": "load_yaml",
|
| 670 |
-
"file_path": "stream_ugs.py",
|
| 671 |
-
"function_calls": [
|
| 672 |
-
{
|
| 673 |
-
"function_call_name": "open",
|
| 674 |
-
"functionality": "Open a file path and return a file object.",
|
| 675 |
-
"input": "\"codes/config.yml\", mode='r', encoding='utf-8'",
|
| 676 |
-
"output": "file object",
|
| 677 |
-
"location_or_namespace": "builtins"
|
| 678 |
-
},
|
| 679 |
-
{
|
| 680 |
-
"function_call_name": "yaml.load",
|
| 681 |
-
"functionality": "Parse a YAML stream and return the corresponding Python object.",
|
| 682 |
-
"input": "file_object, Loader=yaml.FullLoader",
|
| 683 |
-
"output": "dict",
|
| 684 |
-
"location_or_namespace": "yaml"
|
| 685 |
-
}
|
| 686 |
-
]
|
| 687 |
-
}
|
| 688 |
-
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
annotations/annotation_144/instruction.txt
DELETED
|
@@ -1,53 +0,0 @@
|
|
| 1 |
-
# Code Generation Instruction: STREAM-UGS — Uniform *k*-Graphlet Sampling (Semi-Streaming)
|
| 2 |
-
|
| 3 |
-
## Goal
|
| 4 |
-
|
| 5 |
-
Implement the **main interface function** for the STREAM-UGS algorithm that (i) preprocesses a graph to compute an approximate DD order and the bucket distribution, and (ii) draws **uniform random connected induced subgraphs of size *k*** using rejection sampling. Follow the algorithmic description in **Section 3** — Algorithms **APPROX-DD (Alg. 1)**, **COMPUTEDISTRIB (Alg. 2)**, and **SAMPLE (Alg. 3)** — of *“Efficient Streaming Algorithms for Graphlet Sampling.”* 
|
| 6 |
-
|
| 7 |
-
---
|
| 8 |
-
|
| 9 |
-
### **Functionality Description**
|
| 10 |
-
|
| 11 |
-
**Implement the core logic described in Section 3 (Algorithms 1–3) of the paper**:
|
| 12 |
-
given an undirected simple graph provided as an edge list, compute a ϑ-DD order (preprocessing), build the sampling distribution $p(v)$ and normalization constant $\Gamma$, and then repeatedly run the randomized grow-and-accept procedure to return **uniform *k*-graphlets**. The function should **encapsulate the whole pipeline** (preprocessing → sampling) behind a single interface, while allowing callers to optionally supply a precomputed vertex order to skip recomputation. (Source: §3, Algs. 1–3.) 
|
| 13 |
-
|
| 14 |
-
---
|
| 15 |
-
|
| 16 |
-
### **Expected Function Name**
|
| 17 |
-
|
| 18 |
-
`stream_ugs_sample`
|
| 19 |
-
|
| 20 |
-
---
|
| 21 |
-
|
| 22 |
-
### **Input**
|
| 23 |
-
|
| 24 |
-
* `n` (int): Number of vertices; vertices are identified by integers in `[1, n]`.
|
| 25 |
-
* `edge_list_path` (str): Path to a CSV (or text) file containing the undirected edge list as pairs of vertex IDs; order in the file may be arbitrary (the implementation may scan it sequentially).
|
| 26 |
-
* `k` (int): Target graphlet size (number of vertices in each sampled connected induced subgraph).
|
| 27 |
-
* `epsilon` (float): Accuracy parameter for the approximate DD order (ϑ = 1/(1+ε)).
|
| 28 |
-
* `target` (int): Desired number of successful uniform samples to return.
|
| 29 |
-
* `dd_order` (list\[int], optional): A total order of the vertices (length `n`); if provided, use it in place of computing an order.
|
| 30 |
-
* `memory_budget` (int, optional): A coarse budget indicator (e.g., maximum number of edges or pairs the algorithm may materialize per pass); used only for internal scheduling/heuristics.
|
| 31 |
-
* `random_seed` (int, optional): Seed to make the sampling procedure reproducible.
|
| 32 |
-
|
| 33 |
-
---
|
| 34 |
-
|
| 35 |
-
### **Output**
|
| 36 |
-
|
| 37 |
-
* `samples` (list\[list\[int]]): A list of up to `target` vertex sets, each of length `k`.
|
| 38 |
-
Each inner list represents a **uniformly drawn connected induced *k*-subgraph** (graphlet) of the input graph, expressed by its vertex IDs (order within each list is arbitrary).
|
| 39 |
-
|
| 40 |
-
---
|
| 41 |
-
|
| 42 |
-
### **Key Constraints & Considerations**
|
| 43 |
-
|
| 44 |
-
* **Abstraction over implementation details:** Do **not** prescribe specific programming language features, filesystems, or libraries. Keep the design open to any internal data structures or pass-sharing strategies that realize the paper’s routines.
|
| 45 |
-
* **Streaming mindset (but flexible):** The algorithm is **defined** for semi-streaming (sequential passes over the edge list with sublinear memory), but your function signature must remain agnostic to how passes are implemented. The input is a file path to an edge list for simplicity; the implementation may internally choose its access pattern.
|
| 46 |
-
* **Uniformity guarantee:** Accepted outputs must be distributed **uniformly** over all connected induced *k*-vertex subgraphs, accomplished via the paper’s grow-and-accept scheme (RAND-GROW + PROB with acceptance probability proportional to $\Gamma/(p(v)\,q_S)$). Reference only the **concept**; do not hardcode any particular numeric constants from the paper in this prompt. 
|
| 47 |
-
* **Optional precomputed order:** If `dd_order` is supplied, **skip** computing the approximate DD order and proceed directly to building the distribution and sampling.
|
| 48 |
-
* **Graph assumptions:** Graph is undirected, loop-free, and may contain duplicate lines in the file; the implementation should treat duplicates as a single edge or ignore them without failing.
|
| 49 |
-
* **Determinism controls:** If `random_seed` is provided, internal randomness should be seeded to make results reproducible (to the extent permitted by the algorithm’s randomness).
|
| 50 |
-
|
| 51 |
-
---
|
| 52 |
-
|
| 53 |
-
**Interface function names:** `stream_ugs_sample`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
annotations/annotation_144/latex.txt
DELETED
|
@@ -1,90 +0,0 @@
|
|
| 1 |
-
\section{Computing a $\vartheta$-DD order}
|
| 2 |
-
|
| 3 |
-
We present an algorithm, \textsc{APPROX-DD} (Algorithm~\ref{alg:approx-dd}),
|
| 4 |
-
that, given a graph $G$ and a parameter $0<\varepsilon=O(1)$, computes a
|
| 5 |
-
$\vartheta$-DD order of $G$, where $\vartheta=\frac{1}{1+\varepsilon}$.
|
| 6 |
-
Our main result is:
|
| 7 |
-
|
| 8 |
-
\begin{theorem}[Semi-streaming passes for \textsc{APPROX-DD}]
|
| 9 |
-
\label{thm:approx-dd-passes}
|
| 10 |
-
In the semi-streaming model, \textsc{APPROX-DD} can be implemented so that,
|
| 11 |
-
with probability at least $1-n^{-\Theta(1)}$, it makes
|
| 12 |
-
\[
|
| 13 |
-
q = O\!\Big(\tfrac{\log n}{\varepsilon^{2}}\log \tfrac{\log n}{\varepsilon}\Big)
|
| 14 |
-
\quad\text{passes if } M=\Omega(n),
|
| 15 |
-
\]
|
| 16 |
-
and
|
| 17 |
-
\[
|
| 18 |
-
q = O\!\Big(\tfrac{\log n}{\varepsilon^{2}}\Big)
|
| 19 |
-
\quad\text{passes if } M=\Omega\!\Big(\tfrac{n\log n}{\varepsilon}\Big).
|
| 20 |
-
\]
|
| 21 |
-
In both cases the total running time is $O(q\cdot m)$.
|
| 22 |
-
\end{theorem}
|
| 23 |
-
|
| 24 |
-
The complete proof of Theorem~\ref{thm:approx-dd-passes} combines several
|
| 25 |
-
technical lemmas and can be found in Appendix~A. The algorithm proceeds in
|
| 26 |
-
\emph{peeling} rounds; every round deletes from $G$ all vertices of degree at
|
| 27 |
-
least $\frac{\Delta}{1+\alpha}$, for a suitable $\alpha$, and places them in a
|
| 28 |
-
list $L$ in the right order. This order is computed by a procedure called
|
| 29 |
-
\emph{shaving}, which uses random partitioning. Pseudocode is given below.
|
| 30 |
-
|
| 31 |
-
\begin{algorithm}[t]
|
| 32 |
-
\caption{Approximate DD Order}
|
| 33 |
-
\label{alg:approx-dd}
|
| 34 |
-
\begin{algorithmic}[1]
|
| 35 |
-
\Function{APPROX-DD}{$G,\varepsilon$}
|
| 36 |
-
\State $L \gets$ empty list
|
| 37 |
-
\While{$V(G)\neq \emptyset$}
|
| 38 |
-
\State $(G,L) \gets \textsc{PEEL}(G,\varepsilon,L)$
|
| 39 |
-
\EndWhile
|
| 40 |
-
\State \Return $L$
|
| 41 |
-
\EndFunction
|
| 42 |
-
\Statex
|
| 43 |
-
|
| 44 |
-
\Function{PEEL}{$G,\varepsilon,L$}
|
| 45 |
-
\State $\Delta \gets \max_{u\in V(G)} d(u\,|\,G)$ \Comment{$\triangleright$ recompute degrees; one streaming pass}
|
| 46 |
-
\State $\alpha \gets \varepsilon/2$
|
| 47 |
-
\State $H \gets \{\,v\in V(G): d(v\,|\,G)\ge \Delta/(1+\alpha)\,\}$
|
| 48 |
-
\While{$H \neq \emptyset$}
|
| 49 |
-
\State $(G,L,H) \gets \textsc{SHAVE}(G,\varepsilon,\Delta,L,H)$ \Comment{$\triangleright$ degree updates}
|
| 50 |
-
\EndWhile
|
| 51 |
-
\State \Return $(G,L)$
|
| 52 |
-
\EndFunction
|
| 53 |
-
\Statex
|
| 54 |
-
|
| 55 |
-
\Function{SHAVE}{$G,\varepsilon,\Delta,L,H$}
|
| 56 |
-
\State $\beta \gets \dfrac{2+2\varepsilon+\varepsilon^{2}}{\varepsilon}$,\qquad
|
| 57 |
-
$\ell \gets \left\lceil 2(1+\beta)\right\rceil$
|
| 58 |
-
\State Let $H_{1},\dots,H_{\ell}$ be a uniform random partition of $H$
|
| 59 |
-
\For{$i=1,\dots,\ell$}
|
| 60 |
-
\State $R_i \gets \{\,v\in H_i : d(v\,|\,G[H_i]) \le \Delta/(1+\beta)\,\}$ \Comment{$\triangleright$ recompute degrees}
|
| 61 |
-
\State $S_i \gets \{\,v\in R_i : d(v\,|\,G) \ge \Delta/(1+\alpha)\,\}$
|
| 62 |
-
\State append the vertices of $S_i$ to $L$ in arbitrary order
|
| 63 |
-
\State $G \gets G \setminus S_i$ \Comment{$\triangleright$ degree updates}
|
| 64 |
-
\EndFor
|
| 65 |
-
\State $H \gets H \setminus \bigcup_{i\in[\ell]} R_i$
|
| 66 |
-
\State \Return $(G,L,H)$
|
| 67 |
-
\EndFunction
|
| 68 |
-
\end{algorithmic}
|
| 69 |
-
\end{algorithm}
|
| 70 |
-
|
| 71 |
-
\paragraph{Intuition for \textsc{PEEL} and \textsc{SHAVE}.}
|
| 72 |
-
Let $H$ be the subset of vertices with degree at least $\frac{\Delta}{1+\alpha}$
|
| 73 |
-
in $G$. If every vertex in $H$ had all of its neighbors after itself in $L$,
|
| 74 |
-
we could remove all of $H$ in a single batch. In general this does not happen.
|
| 75 |
-
Instead, we randomly partition $H$ into $\ell=\lceil 2(1+\beta)\rceil$ batches
|
| 76 |
-
$H_1,\dots,H_\ell$. By Markov's inequality, each vertex has at most
|
| 77 |
-
$\Delta/(1+\beta)$ neighbors within its batch with good probability; then, in
|
| 78 |
-
phase $i$, every such vertex whose current degree is at least
|
| 79 |
-
$\Delta/(1+\alpha)$ is removed together in one batch and appended to $L$ in
|
| 80 |
-
arbitrary order. Degrees of the remaining vertices are updated, and the process
|
| 81 |
-
continues. \textsc{SHAVE} by itself does not necessarily empty $H$; \textsc{PEEL}
|
| 82 |
-
iterates \textsc{SHAVE} until $H$ becomes empty.
|
| 83 |
-
|
| 84 |
-
\begin{lemma}
|
| 85 |
-
\label{lem:peel-calls}
|
| 86 |
-
Let $p\in(0,1)$. With probability at least $1-p$, one execution of
|
| 87 |
-
\textsc{PEEL} (Algorithm~\ref{alg:approx-dd}) calls \textsc{SHAVE}
|
| 88 |
-
$O\!\big(\tfrac{\log n}{p}\big)$ times, and at most once if
|
| 89 |
-
$\Delta \ge 12(1+\alpha)(1+\beta)\ln \tfrac{n}{p}$.
|
| 90 |
-
\end{lemma}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
annotations/annotation_144/requirements.txt
DELETED
|
@@ -1,15 +0,0 @@
|
|
| 1 |
-
# 基本 Python 标准库(无需写)
|
| 2 |
-
# import os, sys, csv, logging, pickle, random, math, heapq, tempfile, unittest 等
|
| 3 |
-
# 都是标准库,不需要加
|
| 4 |
-
|
| 5 |
-
numpy>=1.21
|
| 6 |
-
PyYAML>=6.0
|
| 7 |
-
|
| 8 |
-
# 如果你要用 DataFrame backend,需要安装 PySpark
|
| 9 |
-
pyspark>=3.3.0
|
| 10 |
-
|
| 11 |
-
# (可选)如果你在 set_seed 用到了 torch 的随机种子,需要 PyTorch
|
| 12 |
-
torch>=2.0.0
|
| 13 |
-
|
| 14 |
-
# 测试用例依赖
|
| 15 |
-
pytest>=7.0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
annotations/annotation_144/test.py
DELETED
|
@@ -1,97 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import csv
|
| 3 |
-
import random
|
| 4 |
-
import tempfile
|
| 5 |
-
import unittest
|
| 6 |
-
import numpy as np
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
def set_seed(seed: int = 2025):
|
| 10 |
-
random.seed(seed)
|
| 11 |
-
np.random.seed(seed)
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
def canonicalize(samples):
|
| 15 |
-
"""Sort nodes inside each sample and then sort the whole list."""
|
| 16 |
-
return sorted([sorted(s) for s in samples])
|
| 17 |
-
|
| 18 |
-
def write_bigger_graph():
|
| 19 |
-
n = 10
|
| 20 |
-
edges = [(i, i+1) for i in range(1, n)] + [(n, 1)]
|
| 21 |
-
tmpfile = tempfile.NamedTemporaryFile(delete=False, suffix=".csv")
|
| 22 |
-
with open(tmpfile.name, "w", newline="") as f:
|
| 23 |
-
wr = csv.writer(f)
|
| 24 |
-
wr.writerow(["u", "v"])
|
| 25 |
-
wr.writerows(edges)
|
| 26 |
-
return tmpfile.name # 注意:只返回路径,不要返回 n
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
class TestGraphEquivalence(unittest.TestCase):
|
| 32 |
-
@classmethod
|
| 33 |
-
def setUpClass(cls):
|
| 34 |
-
# 导入两个实现的 Graph 类
|
| 35 |
-
from EfficientStreamingAlgor import generation
|
| 36 |
-
import canonical
|
| 37 |
-
cls.CGraph = canonical.Graph
|
| 38 |
-
cls.GGraph = generation.Graph
|
| 39 |
-
|
| 40 |
-
# 公共参数
|
| 41 |
-
cls.k = 3
|
| 42 |
-
cls.epsilon = 0.5
|
| 43 |
-
cls.target = 3
|
| 44 |
-
cls.max_edges = 500
|
| 45 |
-
cls.edge_list_path = write_bigger_graph()
|
| 46 |
-
cls.n = 10
|
| 47 |
-
|
| 48 |
-
@classmethod
|
| 49 |
-
def tearDownClass(cls):
|
| 50 |
-
try:
|
| 51 |
-
os.unlink(cls.edge_list_path)
|
| 52 |
-
except Exception:
|
| 53 |
-
pass
|
| 54 |
-
|
| 55 |
-
def test_equivalence_with_seed(self):
|
| 56 |
-
set_seed(2025)
|
| 57 |
-
g1 = self.CGraph(self.n, "local", self.edge_list_path, self.max_edges)
|
| 58 |
-
g1.sampling_preprocess(self.k, self.epsilon)
|
| 59 |
-
out1 = g1.sample(self.k, self.target)
|
| 60 |
-
|
| 61 |
-
set_seed(2025)
|
| 62 |
-
g2 = self.GGraph(self.n, "local", self.edge_list_path, self.max_edges)
|
| 63 |
-
g2.sampling_preprocess(self.k, self.epsilon)
|
| 64 |
-
out2 = g2.sample(self.k, self.target)
|
| 65 |
-
|
| 66 |
-
self.assertEqual(canonicalize(out1), canonicalize(out2))
|
| 67 |
-
|
| 68 |
-
def test_equivalence_with_dd_order(self):
|
| 69 |
-
set_seed(7)
|
| 70 |
-
g1 = self.CGraph(self.n, "local", self.edge_list_path, self.max_edges)
|
| 71 |
-
g1.sampling_preprocess(self.k, self.epsilon)
|
| 72 |
-
dd_order = list(g1.DD_order)
|
| 73 |
-
out1 = g1.sample(self.k, self.target)
|
| 74 |
-
|
| 75 |
-
set_seed(7)
|
| 76 |
-
g2 = self.GGraph(self.n, "local", self.edge_list_path, self.max_edges)
|
| 77 |
-
g2.sampling_preprocess(self.k, self.epsilon, ddorder=dd_order)
|
| 78 |
-
out2 = g2.sample(self.k, self.target)
|
| 79 |
-
|
| 80 |
-
self.assertEqual(canonicalize(out1), canonicalize(out2))
|
| 81 |
-
|
| 82 |
-
def test_single_sample(self):
|
| 83 |
-
set_seed(123)
|
| 84 |
-
g1 = self.CGraph(self.n, "local", self.edge_list_path, self.max_edges)
|
| 85 |
-
g1.sampling_preprocess(self.k, self.epsilon)
|
| 86 |
-
out1 = g1.sample(self.k, 1)
|
| 87 |
-
|
| 88 |
-
set_seed(123)
|
| 89 |
-
g2 = self.GGraph(self.n, "local", self.edge_list_path, self.max_edges)
|
| 90 |
-
g2.sampling_preprocess(self.k, self.epsilon)
|
| 91 |
-
out2 = g2.sample(self.k, 1)
|
| 92 |
-
|
| 93 |
-
self.assertEqual(canonicalize(out1), canonicalize(out2))
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
if __name__ == "__main__":
|
| 97 |
-
unittest.main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
annotations/annotation_144/test.sh
DELETED
|
@@ -1,4 +0,0 @@
|
|
| 1 |
-
python test.py
|
| 2 |
-
rm samples.bin
|
| 3 |
-
rm DD_order.bin
|
| 4 |
-
rm logs.log
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|