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,

Easy c̵̰͠r̵̛̠ö̴̪s̶̩̒s̵̭̀-t̶̲͝h̶̯̚r̵̺͐e̷̖̽ḁ̴̍d̶̖̔ ȓ̵͙ė̶͎ḟ̴͙e̸̖͛r̶̖͗ë̶̱́ṉ̵̒ĉ̷̥e̷͚̍ s̷̹͌h̷̲̉a̵̭͋r̷̫̊ḭ̵̊n̷̬͂g̵̦̃ f̶̻̊ơ̵̜ṟ̸̈́ R̵̞̋ù̵̺s̷̖̅ţ̸͗!̸̼͋

Rust S̵̓i̸̓n̵̉ I̴n̴f̶e̸r̵n̷a̴l mutability! Howdy, friendly Rust developer! Ever had a value get m̵̯̅ð̶͊v̴̮̾ê̴̼͘d away right under your nose just when

Hypergraph is a data structure library to generate directed hypergraphs.

Hypergraph is data structure library to create a directed hypergraph in which a hyperedge can join any number of vertices.

Hypergraph is data structure library to create a directed hypergraph in which a hyperedge can join any number of vertices.

Hypergraph is data structure library to create a directed hypergraph in which a hyperedge can join any number of vertices.

A keyboard layout optimizer for layouts of the

Keyboard Layout Optimizer Neo variant layout optimizer written in rust. The optimizer is based on the "evolve-keyboard-layout" scripts by ArneBab. It

Create dynamic grid-based layouts for egui

egui_grid Create dynamic grid layouts for egui. Grids are flexible, easy to create, with behavior similar to egui_extra's strip creation. They're comp

WIP: A program for switching Dygma layouts based on window name or exe name.

Dygma Layer Switcher Config On the first run of dygma-layer-switcher the config.yml will be generated. --- # Toggle logging. logging: false # Port of

Rust crate for embedding, manipulating and retrieving data embedded in binaries using linker sections

linkstore is a library that allows you to define global variables in your final compiled binary that can be modified post-compilation.

rbdt is a python library (written in rust) for parsing robots.txt files for large scale batch processing.

rbdt 🚨 🚨 🚨 🚨 rbdt is a work in progress, currently being extracted out of another (private) project for the purpose of open sourcing and better so

Tiny Rust library to draw pretty line graphs using ascii characters.

rasciigraph Tiny Rust library to draw pretty line graphs using ascii characters. Usage Add this to your Cargo.toml [dependencies] rasciigraph = "0.1.1

A simple 2D plotting library that outputs graphs to SVG that can be styled using CSS.

Poloto graphs can be stylized using css either directly in the SVG, or from inside of html with an embedded svg. The latter allows the user to dynamically match the svg to their website's theme.

A simple space shooter game. Runs in the terminal using characters-based UI. Fully written in Rust, using the
A simple space shooter game. Runs in the terminal using characters-based UI. Fully written in Rust, using the "ruscii" library.

Thrust - a terminal shooter game Originally created as a project for the "Missing Semester" course at JKU Linz (338.006). The game is entirely written

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