Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

rust

petgraph

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 members are all private, so you can't access them in your code.

So how can I iterate both the edges and nodes connected to a node? This seems like something that should be simple, but I haven't been able to find any example code out there.

like image 488
curiousdannii Avatar asked Sep 04 '25 03:09

curiousdannii


1 Answers

edges returns a iterator for EdgeReference which implements the EdgeRef trait which has a source and target that return NodeIndex which can be used to get the node from the graph.

like image 155
loganfsmyth Avatar answered Sep 07 '25 04:09

loganfsmyth