Skip to main content

Fast gSpan (frequent subgraph mining) powered by gBolt C++ backend

Project description

fast-gspan

CI PyPI Python 3.10+ License: BSD-2-Clause

A Python wrapper for frequent subgraph mining powered by the gBolt C++ backend.

Provides a simple API to mine frequent subgraph patterns from NetworkX graphs, with significant speedups over pure-Python gSpan implementations.

Installation

Pre-built wheels are available for Linux (x86_64) and macOS (arm64, x86_64):

pip install fast-gspan

Building from source

If a pre-built wheel is not available for your platform, install from source:

pip install git+https://github.com/Masatsugar/fast-gspan.git
python -m fast_gspan build   # compile the C++ backend

Source builds require:

  • CMake >= 3.10
  • C++ compiler with C++11 support (GCC, Clang)
  • OpenMP (optional, for parallel mining)
# Ubuntu/Debian
sudo apt-get install cmake g++ make

# macOS
brew install cmake libomp

Quick start

import networkx as nx
from fast_gspan import FastgSpan

# Prepare your graph database
graphs = [...]  # list of NetworkX graphs with 'label' attributes on nodes/edges

# Mine frequent subgraphs
fgs = FastgSpan(min_support=10, max_num_vertices=8)
df = fgs.run_from_graphs(graphs)

print(df[["support", "num_vert", "description"]])

From a gSpan-format file

from fast_gspan import FastgSpan

df = FastgSpan(min_support=10, max_num_vertices=8).run_from_file("graphs.txt")

Parallel mining & progress

fgs = FastgSpan(
    min_support=10,
    max_num_vertices=8,
    num_threads=4,       # 0 = all cores (default)
    show_progress=True,  # show real-time pattern count
)
df = fgs.run_from_graphs(graphs)

API

FastgSpan

High-level interface. Constructor parameters:

Parameter Type Default Description
gbolt_path str | None None Path to gBolt executable. Auto-detected if None.
min_support int 2 Minimum absolute support threshold.
min_num_vertices int 1 Minimum vertices in a pattern.
max_num_vertices int 10 Maximum vertices in a pattern.
num_threads int 0 Number of OpenMP threads (0 = all cores).
show_progress bool False Show progress during mining.
verbose bool False Print debug information.

Methods:

  • run_from_graphs(graphs) -- Mine from a list of nx.Graph. Returns pd.DataFrame.
  • run_from_file(filepath) -- Read a gSpan-format file and mine. Returns pd.DataFrame.

GBoltWrapper

Low-level wrapper around the gBolt binary. Use this if you need direct access to raw pattern dicts.

  • mine_frequent_subgraphs(graphs) -- Returns list[dict] with keys: pattern_id, support, vertices, edges, dfs_codes, graph_data.

Output format

The returned DataFrame has the following columns:

Column Description
support Number of graphs containing this pattern
description DFS-code representation: (from, to, from_label, edge_label, to_label) per edge
num_vert Number of vertices in the pattern
pattern_id Pattern ID assigned by gBolt
vertices List of (vertex_id, label) tuples
edges List of (from, to, edge_label) tuples

gSpan-format file

Input files follow the standard gSpan text format:

t # 0
v 0 1
v 1 2
e 0 1 3
t # 1
v 0 1
v 1 1
v 2 2
e 0 1 3
e 1 2 4
t # -1

Changes from upstream gBolt

This package bundles a modified fork of gBolt with the following changes:

New features

  • -x, --max-vertices option -- Limits the maximum number of vertices in mined patterns. Allows early pruning during DFS exploration, reducing both runtime and memory usage.
  • Projection size guard (MAX_PROJECTION_SIZE) -- Skips projections exceeding 500,000 entries to prevent memory explosion on dense graphs.

Output format change

  • The DFS-code output (-d flag) now emits the full tuple:
    e <from> <to> <from_label> <edge_label> <to_label>
    
    The upstream format only emitted e <from> <to> <edge_label>. The extended format enables exact reconstruction of canonical DFS codes in the Python wrapper.

Build system

  • CMake minimum version raised from 2.6 to 3.10.
  • Added macOS (Apple Clang) fallback for OpenMP via Homebrew libomp.

Bug fixes

  • Added const qualifier to three operator() methods in include/graph.h to fix compiler warnings and ensure correctness with modern C++ standards.

License

The bundled gBolt C++ source is licensed under the BSD 2-Clause License (Copyright (c) 2017, Keren Zhou). See fast_gspan/vendor/gbolt/LICENSE for details.

The Python wrapper code in this repository is also released under the BSD 2-Clause License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fast_gspan-0.1.1.tar.gz (38.8 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

fast_gspan-0.1.1-py3-none-manylinux_2_17_x86_64.whl (173.2 kB view details)

Uploaded Python 3manylinux: glibc 2.17+ x86-64

fast_gspan-0.1.1-py3-none-macosx_11_0_arm64.whl (373.4 kB view details)

Uploaded Python 3macOS 11.0+ ARM64

File details

Details for the file fast_gspan-0.1.1.tar.gz.

File metadata

  • Download URL: fast_gspan-0.1.1.tar.gz
  • Upload date:
  • Size: 38.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for fast_gspan-0.1.1.tar.gz
Algorithm Hash digest
SHA256 4783b90b9c29f74826e06c0d3fd3f6e85b5df3465261e43cbdfbf31a2e957009
MD5 c8f194176233ac624a6bc589a1dbd216
BLAKE2b-256 684073ed29dc36c2e7b66d9fad7d042c6032c47577328336202bd2bd58dea38c

See more details on using hashes here.

Provenance

The following attestation bundles were made for fast_gspan-0.1.1.tar.gz:

Publisher: publish.yml on Masatsugar/fast-gspan

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fast_gspan-0.1.1-py3-none-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for fast_gspan-0.1.1-py3-none-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 a5b50ab2087c262a163b7498e14c64e5003336497e80113ea329b8746874d75d
MD5 2aa0ca4df5885f3dbb18773c7a0096e2
BLAKE2b-256 480ad50c4990491e8ac53fc7e3428e6170b42b629cf35b60df736c8616552db8

See more details on using hashes here.

Provenance

The following attestation bundles were made for fast_gspan-0.1.1-py3-none-manylinux_2_17_x86_64.whl:

Publisher: publish.yml on Masatsugar/fast-gspan

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fast_gspan-0.1.1-py3-none-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for fast_gspan-0.1.1-py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d4c57fd3df0daabac11e92f7e7d0da167e9202a8516719afc7efdbb1ae0481b9
MD5 83055c62413c5430973e30b4feb4d922
BLAKE2b-256 e1a71536981702f0811a8946e2cad0bed6c15005401f1818cd639ec545f64b94

See more details on using hashes here.

Provenance

The following attestation bundles were made for fast_gspan-0.1.1-py3-none-macosx_11_0_arm64.whl:

Publisher: publish.yml on Masatsugar/fast-gspan

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page