-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SpanningTreeIterator fails for MultiGraphs #7451
Comments
I don't see anything in the docs indicating that it works for MultiGraphs. Looking through the code, there is no handling of multiedges and their edge keys.
|
In the documentation of the In anyway it would seem more consistent with the other spanning tree functions if it could also handle MultiGraphs. If there are n edges between two vertices then it seems natural to me to return each spanning tree with each choice of one of these edges once (respecting the ordering of the weight) as two edges between the same vertices can have different weights. |
A couple of points on this:
I might take a crack at this later, but looking at what I've got going on this summer, I don't think it will happen before the end of June at the earliest. |
@mjschwenne @jlportner I can work on this. Can you please provide me with a headstart? |
I tried fixing it myself and changing the
|
You might try creating the edges iterator before the for loop. Something like: partition_dict = partition.partition_dict
partition_key = self.partition_key
G = self.G
edges = G.edges(keys=True, data=True) if G.is_multigraph() else G.edges(data=True)
for *e, d in edges:
e = tuple(e)
d[partition_key] = partition_dict[e] if e in partition_dict else EdgePartition.OPEN
def clear_partition(self, G):
edges = G.edges(keys=True, data=True) if G.is_multigraph() else G.edges(data=True)
for *e, d in edges:
if self.partition_key in d:
del d[self.partition_key] |
I have tried the following approach. def _write_partition(self, partition):`
partition_dict = partition.partition_dict
partition_key = self.partition_key
G = self.G
#Handling multi-graphs
if G.is_multigraph():
for u, v, k, d in G.edges(keys= True, data = True):
edge = (u, v, k)
d[partition_key] = partition_dict.get(edge, EdgePartition.OPEN)
else:
for u, v, d in G.edges(data=True):
edge = (u, v)
d[partition_key] = partition_dict.get(edge, EdgePartition.OPEN)
def _clear_partition(self, G):
"""
Removes partition data from the graph
"""
partition_key = self.partition_key
edges = G.edges(keys=True, data=True) if G.is_multigraph() else G.edges(data=True)
for *e, d in edges:
if partition_key in d:
del d[partition_key] This worked fine for some test cases I tried. I have opened a PR. Can you please review it? |
Now that #7454 has been merged, I believe this issue can be closed. |
From my layman understanding it seems as if the SpanningTreeIterator was written with MultiGraphs in mind.
However, trying it with a MultiGraph yields repeated output of the same spanning tree.
Current Behavior
Using the SpanningTreeIterator with a MultiGraph outputs many times the same spanning tree and not all possible spanning trees.
Consider the three cycle graph given by edges [(0,1),(1,2),(0,2)]. Then currently for this viewed as a multigraph it gives four times the spanning tree [(0,1), (0,2)].
Expected Behavior
Get all different spanning trees on the graph.
For the above example it should give the following 3 spanning trees:
[(0,1),(0,2)], [(0,2),(1,2)], [(0,1),(1,2)].
Steps to Reproduce
gives the wrong result whereas
gives the expected result.
Environment
Python version: 3.12.3
NetworkX version: 3.3
The text was updated successfully, but these errors were encountered: