Message Boards Message Boards

[FEATURE] Remove edge weights (or other builtin property) from a graph


It would be very useful to have a function to remove EdgeWeight (or any other builtin property like EdgeCapacity) from a graph, without touching any other properties. I do not currently see a simple way to do this.


Please upvote if you ran into the same problem and feel a need for this feature.

If you do have a simple way to solve this simple and common problem, let me know. If you have a complicated, but reliable way, please still do let me know. I am interested in the shortest fully reliable solution.

My current best solution is

removeWeights[wg_?GraphQ] := Graph[VertexList[wg], EdgeList[wg], FilterRules[Options[wg], Except[EdgeWeight]]]

But I am not fully confident that this will not break in some edge case (or in some older version), and that it truly preserves all graph properties. As far as I can tell, this use of Options with Graph is not documented.

Is it also non-ideal because it changes the internal graph representation to "Incidence" (even if it was "Simple"), which I guess may have performance consequences. This can be checked using GraphComputation`GraphRepresentation

A confirmation that this is the current "right way" from a Wolfram developer would be most welcome!

POSTED BY: Szabolcs Horvát
14 days ago

I am still looking for answers to this. Is there any documented way at all to remove the EdgeWeight property from a Graph? This is such a basic task that there surely must be a way.

Furthermore, yet another property handling bug:

g = Graph[{1 <-> 2, 2 <-> 3}, EdgeWeight -> {5, 6}];

g2 = RemoveProperty[{g, 1 <-> 2}, EdgeWeight];

This corrupts the internal state of the graph, and makes it unusable with several functions:

In[83]:= InputForm[g2]

Graph[{1, 2, 3}, {UndirectedEdge[1, 2], UndirectedEdge[2, 3]}, {EdgeWeight -> {6}}]

As you can see, the EdgeWeight vector has the wrong length.

Now WeightedAdjacencyMatrix[g2] or EdgeBetweennessCentrality[g2] simply stay unevaluated, even though GraphQ[g2] and WeightedGraphQ[g2] both return True.

People have been complaining about property bugs similar to this for years. I keep wondering if we should consider the Graph-functionality of Mathematica abandoned, and no longer developed.

There are even functions which have been broken since their introduction 3 years ago and are still broken in 11.1.0 despite multiple reports.

POSTED BY: Szabolcs Horvát
7 days ago

This is a known issue to internal teams, and it is being looked at. Here I am just passing an idea from a friend:

g = RandomGraph[{10, 20}, EdgeLabels -> RandomReal[1, 20]]
PropertyValue[g, EdgeLabels] =.
POSTED BY: Vitaliy Kaurov
6 days ago

Thank you Vitaliy! This works for EdgeLabels. The problem seems to be specific to EdgeWeight, EdgeCost and EdgeCapacity.

I have the impression that there are at least three types of graph properties:

  1. those that show up as a separate option in Options[g] with a list value
  2. those that show up as a separate option in Options[g] with a rule-list value
  3. those that show up in Properties in Options[g]

The problem seems to affect only category 1.

I tried to explore these here, but I did not get any feedback, so I am not certain that my hypothesis about them is correct (there are exceptions, e.g. VertexCoordiates seems to have its own special behaviour).

There are more differences between these than how they are stored. Some quirks and bugs (like this property removal problem) affect only some of them.

  • Category 1 works with multigraphs, the rest doesn't (but instead of giving errors, they tend to misbehave).
  • Category 1 has a default value, and all items (all edges or all vertices) must have a value assigned.
  • Category 2 allows assigning values to only some of them.
  • Category 2 seems to be accessible at the graph level, i.e. PropertyValue[g, prop] instead of PropertyValue[{g, 1<->2}, prop]. Category 3 isn't.
  • Category 2 properties seem to always be present in a sense, even if it doesn't show up in Options[g]. They are always listed in PropertyList[g].
  • There seems to be no easy way to query which category 3 properties a graph has (unless it's a graph-level property). They would need to be queries separately for each edge/vertex.
  • Both category 2 and category 3 properties are settable as SetProperty[g, Properties -> {UndirectedEdge[1,2] -> {prop -> value}}]. Category 1 isn't. However, category 1 is also settable as SetProperty[g, prop -> ruleList], not only as SetProperty[g, prop -> valueList].

As you can see, it's pretty complicated.

Note: Since EdgeLabels wants a rule list, your example should be:

g = RandomGraph[{10, 20}];
g2 = SetProperty[g, 
  EdgeLabels -> Thread[EdgeList[g] -> RandomReal[1, 20]]]

Otherwise all but the first value in RandomReal[1,20] will be ignored (and this happens completely silently, which is a bit confusing).

But PropertyValue[g2, EdgeLabels] =. works fine for EdgeLabels (as you say), and so does RemoveProperty[g2, EdgeLabels]. The former is imperative and changes the graph, the latter is functional, and returns a new graph.

POSTED BY: Szabolcs Horvát
6 days ago

Group Abstract Group Abstract