Questions tagged [petgraph]

`petgraph` is a graph data structure library for Rust

petgraph is a graph data structure library for Rust. It provides graph types and graph algorithms.

Documentation

Repository

crates.io

26 questions
6
votes
1 answer

Which algorithm from petgraph will find the shortest path from A to B?

I have a directional graph and want to find the shortest path from node A to node B. I searched on crates.io and found petgraph which looks like the most popular crate. It implements a number of algorithms, but none of them solve my task. Did I miss…
user1244932
  • 7,352
  • 5
  • 46
  • 103
4
votes
1 answer

How do I serialize and deserialize a graph using Serde with Petgraph?

The Petgraph documentation hints at Serde support. Under "current features": serde-1 - Defaults off. Enables serialization for Graph, StableGraph using serde 1.0. May require a more recent version of Rust than petgraph alone. I can see the file…
Rich Apodaca
  • 28,316
  • 16
  • 103
  • 129
3
votes
1 answer

How to wrap up functions using NodeIndex in petgraph

Newbie question: I want to use the petgraph rust crate. The graph should be a part of the struct MyGraph. For this, I need to wrap some functions to make them acessible from outside: use petgraph::graph::DiGraph; use…
3
votes
1 answer

In Rust's petgraph how can I test whether a node is part of a cycle?

I'm using Rust's petgraph library and am wondering how I can check if a node is part of a cycle. The petgraph::algo::is_cyclic_directed function will tell me whether there is any cycle in the graph, but after looking all through the docs I couldn't…
curiousdannii
  • 1,658
  • 1
  • 25
  • 40
3
votes
1 answer

Rust Petgraph WASM Project, trying to use struct with Graph property, but requires 2 parameters

I am very new to Rust, so this might be a very rookie question. I am trying to create a web app with rust -> wasm. Tried to follow the tutorial https://rustwasm.github.io/docs/book/introduction.html and tried to use the crate package "petgraph" I…
bbofrk
  • 33
  • 3
3
votes
1 answer

How can I store a type directly in a struct when all the traits I need are implemented for references to that type?

Petgraph implements all its traits for references to it's internal graph type. How can I store and use a Graph rather than an &Graph in a struct? This works but stores a reference to the graph: extern crate petgraph; use…
timthelion
  • 2,636
  • 2
  • 21
  • 30
3
votes
1 answer

writing petgraph Dot to a file

I may be missing something very basic -- I am new to Rust. I'm attempting to write a petgraph::dot::Dot representation to a file. The following small code example doesn't compile: use petgraph::Graph; use petgraph::dot::{Dot, Config}; use…
Ultrasaurus
  • 3,031
  • 2
  • 33
  • 52
3
votes
1 answer

Create a Petgraph graph from a Vec<(String, String)> loaded from JSON

I'm trying to create a petgraph Graph from JSON data. The JSON contains the edges of the graph, the key represents the starting vertex and the value is a list of adjacent vertices. It's possible to generate a graph with a vector of edges. I managed…
L. Meyer
  • 2,863
  • 1
  • 23
  • 26
2
votes
1 answer

What type annotations are required for Petgraph's all_simple_paths() function?

I'm trying to use Petgraph's all_simple_paths(), but I'm not sure what type annotations are required, the the docs don't give an example. It returns a impl Iterator where TargetColl: FromIterator, but I don't know what…
curiousdannii
  • 1,658
  • 1
  • 25
  • 40
2
votes
1 answer

How can I get a deterministic topological sort in Petgraph?

I'm using Petgraph's toposort function to get a sorted list of the graph's nodes. toposort does not however guarantee that all nodes at the same level will be returned in a consistent deterministic order. Is there some other option within Petgraph…
curiousdannii
  • 1,658
  • 1
  • 25
  • 40
2
votes
1 answer

How can I iterate a Petgraph node's edges with the nodes they connect?

The edges function of a Petgraph Graph returns an iterator of edges. Each iteration then returns an EdgeReference which handily stores both nodes and the edge weight, which you can see if you debug print one. But unfortunately the EdgeReference…
curiousdannii
  • 1,658
  • 1
  • 25
  • 40
2
votes
1 answer

How do you perform a filtered search with Rust's petgraph?

Rust's Petgraph library has a bunch of filter 'adaptors', but I haven't been able to find any examples or tutorials for how to use them. Some (but not all) have constructors, such as EdgeFiltered.from_fn() which takes a graph and a function, but…
curiousdannii
  • 1,658
  • 1
  • 25
  • 40
2
votes
1 answer

How can I solve the travelling salesman problem using rust and petgraph?

I have a parser from my raw input to a petgraph::UnGraph structure. I need to find the shortest path that visits all nodes. I found algo::dijkstra, but from what I understood, Dijkstra would only give me the shortest path connecting two specific…
Dincio
  • 1,010
  • 1
  • 13
  • 25
2
votes
1 answer

In rust; what types are included in a type's Namespace?

I'm studying the source to the petgraph library, and I cannot find out where the type Graph::NodeId comes from. I can see that the function astar accepts a type G (which can be a Graph). astar expects there to be a type NodeId in G's namespace. pub…
timthelion
  • 2,636
  • 2
  • 21
  • 30
2
votes
1 answer

Using the Bellman-Ford algorithm from petgraph

I would like to use the Bellman-Ford algorithm from the petgraph crate. Here is a very simple sample program which does not compile: extern crate petgraph; use petgraph::prelude::*; use petgraph::dot::{Dot, Config}; use…
adrian
  • 266
  • 1
  • 10
1
2