4
$\begingroup$

Apologies for what seems like a silly question. I'm sure I am missing something obvious, but this is driving me a bit crazy.

Does EdgeWeight work differently in Directed vs. Undirected graphs?

If I have a weighted undirected graph, I can assign and then extract the list of edge weights, or individual weights, as follows:

G = Graph[{1 <-> 2, 2 <-> 3, 1 <-> 3, 2 <-> 4, 3 <-> 4}, 
   EdgeWeight -> {1, 2, 3, 4, 5}];
PropertyValue[G, EdgeWeight]
PropertyValue[{G, 2 <-> 3}, EdgeWeight]

And I get the expected output

{1, 2, 3, 4, 5}
2

However, if I try the same with a directed graph:

H = DirectedGraph[{1 -> 2, 2 -> 3, 3 -> 4, 4 -> 2}, 
   EdgeWeight -> {6, 7, 8, 9}];
PropertyValue[H, EdgeWeight]
PropertyValue[{H, 1 -> 2}, EdgeWeight]

I get the following nonsense

Automatic
$Failed

As I said, I'm sure I'm doing something stupid, but I'm not sure what. Can anyone help?

$\endgroup$
10
  • 1
    $\begingroup$ cannot test right now, but do you get the same error if you use \[DirectedEdge] instead of ->? $\endgroup$ Jul 15, 2019 at 0:42
  • 1
    $\begingroup$ For the record, I get the same error in 10.4.1 for Microsoft Windows (64-bit). $\endgroup$ Jul 15, 2019 at 1:24
  • 2
    $\begingroup$ this looks like an issue that is fixed in version 12 (we get the same error in v9 / windows 10). $\endgroup$
    – kglr
    Jul 15, 2019 at 6:16
  • 2
    $\begingroup$ Don't use DirectedGraph. Use Graph. Graph is a constructor for the graph data structure, either directed or undirected. DirectedGraph converts undirected graphs to directed ones, an entirely different use. IMO there's no bug here, just a misuse of DirectedGraph. $\endgroup$
    – Szabolcs
    Jul 15, 2019 at 14:49
  • 2
    $\begingroup$ Furthermore, DirectedGraph discarded all properties (including edge weights) up to M11.3. Since M12.0, many graph functions, including DirectedGraph, preserve properties. Why this major wasn't documented by Wolfram is beyond me ... $\endgroup$
    – Szabolcs
    Jul 15, 2019 at 14:54

1 Answer 1

6
$\begingroup$

There is no bug here, just a misuse of DirectedGraph.

You are trying to use DirectedGraph as a constructor. This is incorrect. As written in the documentation, the purpose of DirectedGraph is to convert an undirected graph to a directed one, not to construct a graph.

To construct a graph from an edge list, use Graph.

Why does DirectedGraph appear to work as a constructor at first glance then? That's because since M10.4 most graph processing functions accept an edge list instead of a Graph expression.

Why do edge weights go missing? Because up to M11.3, almost all graph processing functions discard properties, including edge weights. What very likely happens is that first the edge list is converted to a Graph expression internally (as in all other cases when a graph function gets an edge list and not a Graph as input). Then DirectedGraph is applied to this Graph expression, resulting in all weights getting discarded.

Since M12.0, most graph processing functions finally preserve properties. Unfortunately, the only indication that this was changed is a short note on the "What's New in 12" page.


In either version, you should not use DirectedGraph as a constructor. Always use Graph for this purpose.

$\endgroup$
1
  • $\begingroup$ Okay, I think I see. Thank you for the clear explanation. $\endgroup$
    – user66564
    Jul 15, 2019 at 18:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.