Python library for embedding large graphs in 2D space, using force-directed layouts.

Overview

Graph Force

A python/rust library for embedding graphs in 2D space, using force-directed layouts.

Installation

pip install graph_force

Usage

The first parameter defines the number of nodes in graph. The second parameter is an iterable of edges, where each edge is a tuple of two integers representing the nodes it connects. Node ids start at 0.

import graph_force

edges = [(0, 1), (1, 2), (2, 3), (3, 0)]
pos = graph_force.layout_from_edge_list(4, edges)

Example with networkx

This library does not have a function to consume a networkx graph directly, but it is easy to convert it to an edge list.

import networkx as nx
import graph_force

G = nx.grid_2d_graph(10, 10)
# we have to map the names to integers
# as graph_force only supports integers as node ids at the moment
edges = []
mapping = {n: i for i, n in enumerate(G.nodes)}
i = 0
for edge in G.edges:
    edges.append((mapping[edge[0]], mapping[edge[1]]))

pos = graph_force.layout_from_edge_list(len(G.nodes), edges, iter=1000)
nx.draw(G, {n: pos[i] for n, i in mapping.items()}, node_size=2, width=0.1)

Example with edge file

This methods can be used with large graphs, where the edge list does not fit into memory.

Format of the file:

  • Little endian
  • 4 bytes: number of nodes(int)
  • 12 bytes: nodeA(int), nodeB(int), weight(float)
import graph_force
import struct

with open("edges.bin", "rb") as f:
    n = 10
    f.write(struct.pack("i", n))
    for x in range(n-1):
        f.write(struct.pack("iif", x, x+1, 1))

pos = graph_force.layout_from_edge_file("edges.bin", iter=50)

Options

iter, threads and model, initial_pos are optional parameters, supported by layout_from_edge_list and layout_from_edge_file.

pos = graph_force.layout_from_edge_list(
    number_of_nodes,
    edges,
    iter=500,  # number of iterations, default 500
    threads=0,  # number of threads, default 0 (all available)
    model="spring_model", # model to use, default "spring_model", other option is "networkx_model"
    initial_pos=[(0.4, 0.7), (0.7, 0.2), ...], # initial positions, default None (random)
)

Available models

Contributing

You might also like...
A graph library for Rust.

Gamma A graph library for Rust. Gamma provides primitives and traversals for working with graphs. It is based on ideas presented in A Minimal Graph AP

Simple but powerful graph library for Rust

Graphlib Graphlib is a simple and powerful Rust graph library. This library attempts to provide a generic api for building, mutating and iterating ove

Library for Rubik's cube applications.

Rubik Master cube-demo3.mov Do you like to solve Rubik's cube? I do. As a cuber and programmer, I want to build a toolset to build applications like S

The library provides basic functions to work with Graphviz dot lang from rust code.

Description The library provides the basic access to the graphs in graphviz format with ability to import into or export from it. Base examples: Parse

Sub-pixel precision light spot rendering library for astronomy and video tracking applications.

Planetarium Sub-pixel precision light spot rendering library for astronomy and video tracking applications. Example usage use planetarium::{Canvas, Sp

Rust library for of graph ensembles

Rust library for random graph ensembles Minimal Rust version: 1.55.0 Implements simple sampling and monte carlo (or rather markov-) steps, that can be

Generic framebuffer implementation in Rust for use with embedded-graphics library

Fraramebuffer implementation for Rust's Embedded-graphics Framebuffer approach helps to deal with display flickering when you update multiple parts of

Small, lightweight and fast library for rendering text with wgpu.

wgpu-text wgpu-text is a wrapper over glyph-brush for fast and easy text rendering in wgpu. This project was inspired by and is similar to wgpu_glyph,

A 2D vector graphics library optimized for GUIs, written in Rust and wgpu
A 2D vector graphics library optimized for GUIs, written in Rust and wgpu

RootVG A 2D vector graphics library optimized for GUIs, written in Rust and wgpu How it Works Unlike other 2D vector graphics libraries which have a s

Comments
  • suggestion/question: edge_file_from_layout functionality as counterpart of layout_from_edge_file

    suggestion/question: edge_file_from_layout functionality as counterpart of layout_from_edge_file

    https://github.com/H4kor/graph-force/blob/14a5c610f332ed3049a9c85f7ddb9337869cf349/src/lib.rs#L48

    I really like the idea of supporting large graphs via file, is it possible to implement an opposite action for saving graphs to a file? This can be a nice building block for implementing a graph storage

    opened by yetkinozturk 0
  • Add weights to edges

    Add weights to edges

    • The binary format already has weights but ignores them
    • layout_from_edge_list has to be extended to allow weights
    • Models might have to be adjusted to used weights properly.
    enhancement 
    opened by H4kor 0
Owner
Niko Abeler
Teaching computers to do things with arguable efficiency.
Niko Abeler
A cargo subcommand for creating GraphViz DOT files and dependency graphs

cargo-graph Linux: A cargo subcommand for building GraphViz DOT files of dependency graphs. This subcommand was originally based off and inspired by t

Kevin K. 213 Nov 24, 2022
GraphScope: A One-Stop Large-Scale Graph Computing System from Alibaba

A One-Stop Large-Scale Graph Computing System from Alibaba GraphScope is a unified distributed graph computing platform that provides a one-stop envir

Alibaba 2.2k Jan 1, 2023
picom-xrdesktop-companion is a program that runs alongside picom, that mirrors your desktop windows to the VR/XR space.

picom-xrdesktop-companion picom-xrdesktop-companion What is this How to use Installation Dependencies Building Limitations Bugs Window stacking Questi

yshui 15 Dec 9, 2022
A vector graphics renderer using OpenGL with a Rust & C API.

bufro A vector graphics renderer using OpenGL with a Rust & C API. A Rust example can be found in examples/quickstart.rs (using glutin). A C example c

Aspect 9 Dec 15, 2022
ASCII 3D-renderer using Ray Marching technique written in Rust with NCurses

pistol ASCII renderer using Ray Marching technique written in Rust ?? with NCurses. This project is a giga-chad compared to my previous attempt to wri

Eugene Sokolov 5 Dec 10, 2022
A Graph implemented using nothing but `Vec`s in rust

VecGraph A Graph implemented using nothing but Vecs in rust. Details The graph is implemented using two Vecs: nodes and edges. nodes stores "nodes". w

null 1 Jan 27, 2022
A universe that consists of meta knowledge domains from Stack Exchange, made using Bevy with

Stack of Knowledge Universe A universe that consists of meta knowledge domains from Stack Exchange, made using Bevy with ❤️ . Setup Remove .placeholde

dirtsea 13 Mar 1, 2022
Real-time 3D orientation visualization of a BNO055 IMU using Bissel and Bevy

orientation This is a demonstration of real-time visualization of the attitude of a BNO055 IMU across a wireless network to a Bevy app using the Bisse

chris m 4 Dec 10, 2022
An SVG rendering library.

resvg resvg is an SVG rendering library. Purpose resvg can be used as a Rust library, a C library and as a CLI application to render SVG files based o

Evgeniy Reizner 1.8k Jan 7, 2023
Graph data structure library for Rust.

petgraph Graph data structure library. Supports Rust 1.41 and later. Please read the API documentation here Crate feature flags: graphmap (default) en

null 2k Jan 9, 2023