GraphRicciCurvature

repository·master·Indexed 43 days ago

https://github.com/saibalmars/graphriccicurvature

A Python library for computing discrete Ricci curvatures (Ollivier-Ricci and Forman-Ricci), Ricci flow, and Ricci community detection on NetworkX graphs. It supports various transportation distance methods including Optimal Transportation Distance (OTD), Average Transportation Distance (ATD), and Sinkhorn distance. The library provides tools to uniformize edge curvature via Ricci flow and identify community structures by analyzing modularity drops during edge removal.

Tokens
4.7K
Snippets
17
Records
24
Agent score
54%

What's inside GraphRicciCurvature

  1. Install GraphRicciCurvature via pip

    master

    You can install the library using pip3. Note that from version 0.4.0 onwards, NetworKit is a required dependency for computing shortest paths for density distribution. If NetworKit installation fails, you may need to follow its specific installation instructions.

    pip3 install [--user] GraphRicciCurvature
  2. How to improve community detection results

    master

    If ricci_community or standard Ricci flow does not yield high-quality community detection (e.g., low ARI or Modularity), consider the following tuning strategies:

    1. Increase Ricci flow iterations: Some graphs require more than the default 10 iterations to capture community structure. Try 20 to 50 iterations.
    2. Tune exp_power: Increasing exp_power (e.g., to 1 or 2) puts more focus on neighbors with larger weights during optimal transportation. Setting exp_power=2 is often effective for real-world graphs.
    3. Manual Surgery: Instead of relying on automatic cutoffs, manually inspect the edge weight histogram and remove edges with weights above a specific threshold to see how it affects Modularity and the Adjusted Rand Index (ARI).
  3. Compute Ollivier-Ricci curvature

    master

    To compute the Ollivier-Ricci curvature, use the OllivierRicci class. The curvature values are stored as edge attributes in the NetworkX graph under the key "ricciCurvature".

    Parameters:

    • G: The NetworkX graph.
    • alpha: Curvature parameter (e.g., 0.5).
    • method: Set to "OTD" to compute the Ricci flow metric via Optimal Transportation Distance.
    • verbose: Logging level (e.g., "INFO").
    import networkx as nx
    from GraphRicciCurvature.OllivierRicci import OllivierRicci
    
    G = nx.karate_club_graph()
    # Initialize and compute
    orc = OllivierRicci(G, alpha=0.5, verbose="INFO")
    orc.compute_ricci_curvature()
    
    # Access the curvature of a specific edge
    curvature = orc.G[0][1]["ricciCurvature"]
  4. Compute Ricci flow and Ricci communities

    master

    Ricci flow uniformizes edge Ricci curvature. You can compute the Ricci flow metric (using Optimal Transportation Distance) and subsequently detect communities using the ricci_community() method.

    Steps:

    1. Initialize OllivierRicci with method="OTD".
    2. Call compute_ricci_flow(iterations=...).
    3. Call ricci_community() to get the clustering results.
    import networkx as nx
    from GraphRicciCurvature.OllivierRicci import OllivierRicci
    
    G = nx.karate_club_graph()
    # Initialize with OTD method
    orc_OTD = OllivierRicci(G, alpha=0.5, method="OTD", verbose="INFO")
    
    # Compute Ricci flow
    orc_OTD.compute_ricci_flow(iterations=10)
    
    # Detect communities
    clustering = orc_OTD.ricci_community()
  5. Configure OllivierRicci initialization parameters

    master

    When initializing OllivierRicci(G, ...) or using the underlying _compute_ricci_curvature_edges, you can tune the following parameters:

    • weight: The edge attribute name used for curvature (default: 'weight').
    • alpha: Mass share remaining on the original node (range 0-1, default: 0.5).
    • method: Transportation distance method:
      • 'OTD': Optimal Transportation Distance.
      • 'ATD': Average Transportation Distance.
      • 'Sinkhorn': Approximate Sinkhorn distance.
      • 'OTDSinkhornMix': Uses OTD for small neighborhoods and Sinkhorn for large ones (default).
    • base: Base for weight distribution (default: math.e).
    • exp_power: Exponential power for weight distribution (default: 2).
    • proc: Number of CPU processes for multiprocessing (default: mp.cpu_count()).
    • shortest_path: 'all_pairs' (precomputes APSP matrix) or 'pairwise' (on-the-fly search) (default: 'all_pairs').
    • nbr_topk: Number of top edge-weight neighbors to consider (default: 3000).
    • verbose: Logging level ('INFO', 'TRACE', 'DEBUG', 'ERROR').
  6. Configure Ollivier-Ricci probability distribution parameters

    master

    When using OllivierRicci, you can tune the probability distribution used for optimal transportation to improve community detection results. This is done via the following parameters:

    • alpha: The share of mass to leave on the original node. Range: [0, 1]. Default: 0.5.
    • base: Base variable for weight distribution. Default: math.e.
    • exp_power: Exponential power for weight distribution. Higher values focus more on neighbors with larger weights. Default: 0.
    • method: Transportation method. Options: "OTD" (Optimal Transportation Distance), "ATD" (Average Transportation Distance), or "Sinkhorn" (Approximated Sinkhorn distance, faster). Default: "Sinkhorn".
  7. Package Requirements

    master

    The following packages are required for GraphRicciCurvature to function:

    • NetworkX >= 2.0 (Base graph library)
    • NetworKit >= 6.1 (Required for shortest path algorithms in version 0.4.0+)
    • NumPy (POT support)
    • POT (For optimal transportation distance)
    • python-louvain (For faster modularity computation)