rustworkx icon indicating copy to clipboard operation
rustworkx copied to clipboard

Ignore self-loops and parallel edges when reading edge list

Open SILIZ4 opened this issue 2 weeks ago • 2 comments

What is the expected enhancement?

I want to read an edge list (e.g. using PyGraph.read_edge_list) and make sure that the returned graph contains no self-loops and no parallel edges. Since PyGraph.read_edge_list is static, PyGraph(multigraph=False).read_edge_list cannot work.

I'm willing to work on a PR for this. Before writing code however, I wanted to check that I didn't miss something functionality of the library. I see three ways this could be handled:

  1. Add flags to the read_edge_list method.
  2. Make read_edge_list not static and use _add_edge. This doesn't currently address self-loops, but a flag similar to multigraph could be added to _add_edge and the constructor of PyGraph.
  3. Create new methods remove_self_loops and to_graph (need to find better name) that remove self-loops and parallel edges, respectively.

Personally, I don't like option 1. I'm not sure if option 1 or 2 (or both?) is best: they both provide functionality useful outside this specific use case.

SILIZ4 avatar Dec 04 '25 19:12 SILIZ4

Optional named arguments from Option 1 are the best solution. If I had to pick an option it would be that one, I would approve a PR if you submitted that

IvanIsCoding avatar Dec 05 '25 22:12 IvanIsCoding

With that being said remove_self_loops sounds like a potentially useful method. The to_graph method should already exist?

IvanIsCoding avatar Dec 05 '25 22:12 IvanIsCoding