distributed-samplingGitHub

Apache Spark Edition

Distributed graph sampling, one function call away.

Graph sampling methods built natively on Apache Spark GraphFrames. Select existing strategies or plug in your own without touching the core.

pip install graph-distributed-sampling

Quick start

Attach GraphFrames, then sample.

The library runs inside any Spark environment where pyspark is available and the GraphFrames package is attached to the session.

session.py
from pyspark.sql import SparkSession spark = (    SparkSession.builder    .appName("sampling")    .config("spark.jars.packages",            "graphframes:graphframes:0.8.3-spark3.5-s_2.12")    .getOrCreate())
sample.py
from graphframes import GraphFramefrom distributed_sampling import sample_graph # gf: a GraphFrame with columns id, src, dstsampled = sample_graph(gf, "random_node", fraction=0.1, seed=42) # Switching strategy is a one-string changesampled = sample_graph(gf, "nuvsn", fraction=0.1, seed=42)sampled = sample_graph(gf, "forest_fire", p=0.35, max_nodes=50_000)
  • Distributed by default

    Samplers are pure DataFrame transformations — joins, aggregations, anti-joins. The graph is never collected to the driver.

  • One uniform interface

    Every sampler consumes and returns a GraphFrame and shares induced-subgraph construction, so methods are interchangeable.

  • Lightweight packaging

    Pure Python with no hard Spark dependency: your cluster provides pyspark and GraphFrames, avoiding version conflicts.

14 samplers · 4 families

Pick a strategy. Keep the interface.

Every method sits behind the same sample_graph call — switch strategies by changing a single string.

  • Random node

    Node

    Uniform vertex sample with its induced subgraph.

  • Uniform vertex + neighborhood

    Node

    Uniform seed vertices augmented with their one-hop neighborhood.

  • Degree-weighted vertex + neighborhood

    Node

    Gumbel-top-k degree-weighted seeds plus neighborhoods, computed without the driver.

  • Random edge

    Edge

    Uniform edge sample; endpoints of retained edges are kept.

  • Top-K by degree

    Edge

    Keeps the K vertices with the highest in, out, or total degree.

  • Snowball

    Traversal

    Layer-by-layer frontier expansion from seeds, stopping early when nothing new appears.

  • Breadth-first

    Traversal

    Breadth-first expansion using bidirectional frontier joins and anti-joins.

  • Depth-first (approx.)

    Traversal

    Greedy per-frontier choice of one unvisited successor per step.

  • Forest fire

    Traversal

    Each active vertex burns a random subset of its outgoing neighbors with probability p.

  • Random walk

    Walk

    Thousands of unbiased walkers advanced in parallel within one set of transformations.

  • Multiple independent walkers

    Walk

    Dead-end walkers respawn from fresh random vertices to sustain coverage.

  • Metropolis–Hastings walk

    Walk

    Accepts moves with min(1, deg(u)/deg(v)) to correct degree bias.

  • Random walk with escaping

    Walk

    Restarts with probability α to escape dense regions and improve coverage.

  • Multidimensional walk

    Walk

    Successor choice biased by a vertex attribute for property-aware exploration.

Evaluation

Built for the graphs that don't fit in memory.

Evaluated on seven SNAP graphs spanning four orders of magnitude in edge count, from ca-GrQc to the 68.9M-edge soc-LiveJournal1. Scaling runs use an Amazon EMR cluster with four m5.2xlarge workers.

Faster than Gradoop on Apache Flink

Matched parallelism, three samplers both systems implement. Faster on every graph; the margin narrows with scale.

  • ca-GrQc4.6×
  • ego-Facebook7.0×
  • ca-AstroPh3.5×

Scaling from 1 to 8 executors

web-Google on Amazon EMR. Runtime falls monotonically; compute-bound walks scale best.

  • random_walk3.8×
  • mhrw2.4×
  • uvsn2.0×

Plugin architecture

Bring your own sampler.

The built-in samplers are registered through exactly the same mechanism, so first- and third-party methods are treated identically — no modification or recompilation of the core.

  1. 1Subclass GraphSampler; configuration goes in the constructor.
  2. 2Implement sample(graph) and return a GraphFrame.
  3. 3Declare an entry point in your package — the registry discovers it on first use.
new_pkg/new_module.py
from distributed_sampling.samplers \    import GraphSampler class NewSampler(GraphSampler):    name = "new_method"     def __init__(self, new_param=0.5):        self.new_param = new_param     def sample(self, graph):        ...  # return a GraphFrame
pyproject.toml
[project.entry-points."distributed_sampling.samplers"]new_method = "new_pkg.new_module:NewSampler"
usage
sample_graph(gf, "new_method", new_param=0.8)