#graph #dijkstra #path-finding #graph-node #bellman-ford #easy-to-use

simple_graph_algorithms

A library with the goal of making running graph algorithms as easy as possible

1 stable release

1.0.0 Jun 19, 2023

#1969 in Algorithms

Download history 8/week @ 2024-02-18 9/week @ 2024-02-25 36/week @ 2024-03-10 2/week @ 2024-03-17 13/week @ 2024-03-31

51 downloads per month

MIT license

70KB
896 lines

simple_graph_algorithms

This library aims to provide simple to use implementations for various algorithms run on graphs.

Algorithms

The following algorithms are currently implemented in this library:

Documentation

The documentation will be hosted on docs.rs once the first version has been released to crates.io.

Performance

Algorithm Mean time over 100 runs on a graph with 10,000 nodes and 39,600 edges
Bellman-Ford 2.1883 s
Dijkstra 52.3155 ms

These tests where performed on a Ryzen 5 7600x. Performance might be slower on older hardware.

To run these tests yourself type cargo bench, a full run will take a few minutes.


lib.rs:

Overview

All algorithms in this crate are run using the Graph struct. It is used to organize nodes that are connected to each other using weighted edges and to provide a simple to use interface.

Click here to see a list of implemented algorithms.

Minimal working example

use simple_graph_algorithms::{Graph, algorithms::{dijkstra, RunAlgorithmError}};

fn main() -> Result<(), RunAlgorithmError> {
    // Create empty graph
    let mut graph = Graph::new();

    // Add new nodes to the graph
    graph.add_node("a");
    graph.add_node("b");
    graph.add_node("c");
    graph.add_node("d");
    graph.add_node("e");

    // Add edges to the graph
    graph.add_edge(1, &"a", &"b"); // Adds an edge that leads from a to b with weight 1
    graph.add_edge(2, &"a", &"c");
    graph.add_edge(5, &"b", &"d");
    graph.add_edge(3, &"c", &"a");
    graph.add_edge(4, &"d", &"a");
    graph.add_edge(2, &"d", &"c");
    graph.add_edge(3, &"c", &"e");
    
    // Calculate the shortest path tree starting at node "a" using Dijkstra's algorithm
    let spt = dijkstra(&mut graph, &"a")?;

    // Get the shortest distance from "a" to other nodes
    assert_eq!(spt.shortest_distance(&"d"), Some(6));
    assert_eq!(spt.shortest_distance(&"c"), Some(2));
    assert_eq!(spt.shortest_distance(&"e"), Some(5));

    Ok(())
}

Features

Feature Description
from_instruction Enables functionality that allows graphs to be parsed from a list of instructions.
serde Serde serialization and deserialization support for some objects.

Dependencies

~175KB