Crates.io | mgraph |
lib.rs | mgraph |
version | 0.1.5 |
source | src |
created_at | 2024-03-26 18:23:35.303234 |
updated_at | 2024-03-30 16:33:20.519654 |
description | A simple, performance-oriented graph library for Rust |
homepage | |
repository | https://github.com/makstamoian/graph/ |
max_upload_size | |
id | 1186785 |
size | 18,668 |
This is a simple graph library packed up into a crate (https://crates.io/crates/mgraph). You can add it to your project by running:
cargo add mgraph
Docs: https://docs.rs/mgraph/latest/mgraph/
Let's assume you want to find the shortest path in a graph using Dijkstra algorithm.
let mut graph = mgraph::Graph::new()
graph.add_node(0);
graph.add_node(1);
graph.add_node(2);
graph.add_node(3);
graph.add_edge(0, 1, 6);
graph.add_edge(0, 2, 16);
graph.add_edge(1, 2, 7);
graph.add_edge(2, 3, 8);
Arguments of the add_edge()
function are source node, target node and edge weight. If you want to create an edge going only from source to target, but not vice-versa, you can use add_edge_directed()
instead.
let result = graph.shortest_path(0, 2);
let parents = result.parents.unwrap();
let cost = result.cost.unwrap();
println!("{:#?}\n\n{:?}", cost, parents);
The shortest_path()
function returns two values: cost
and parents
. cost
is the cost of the shortest path from node A to node B (if one exists), while parents
is a HashMap which represents a node and its predecessor (parent). We need parents
to be able to restore the full shortest path from node A to node B, once again, if one exists.
resore_path()
function:let shortest_path = graph.restore_path(0, 2, parents);
shortest_path()
function receives source
, target
and parents
as arguments.
This was only one of many use cases of this library, however, feel free to contribute to README.md and improve the library and the docs, I will highly appreciate it