petgraph icon indicating copy to clipboard operation
petgraph copied to clipboard

Add trait for edge_endpoints(EdgeIndex)

Open MForster opened this issue 4 years ago • 1 comments

Summary

Add a trait (and implement it for Graph, etc.) that allows access to the source and target NodeIndex from an EdgeIndex.

Motivation

Currently it does not seem to be possible to efficiently get the source and target of a node if all you have is an EdgeIndex and a GraphBase or GraphRef. There doesn't seem to be a more specific trait that exposes the edge_endpoints(EdgeIndex) trait method.

This happens for example when dealing with a Reversed graph or similar. This means that you have to iterate the graph to find the node, which of course has linear running time instead of constant.

Details

I suggest adding a new trait EdgeEndpoints, or similar that contains the edge_endpoints(EdgeIndex) method that Graph already has. Then graph implementations and adapters should implement this trait where possible.

An alternative could be to add a method that creates an EdgeRef from an EdgeIndex.

I would be willing to implement this if there is consensus that it's useful.

MForster avatar Feb 03 '22 07:02 MForster

See https://github.com/MForster/petgraph/commit/8da7d91d5ae2b3627e526a7600def203f8cbaf5a for a minimal implementation of what I mean.

I'm happy to expand on this and send a pull request.

MForster avatar Feb 03 '22 09:02 MForster