Group Abstract Group Abstract

Message Boards Message Boards

6
|
5.1K Views
|
2 Replies
|
8 Total Likes
View groups...
Share
Share this post:

[FEATURE] Test if a graph is edge-weighted

Posted 9 years ago
POSTED BY: Szabolcs Horvát
2 Replies

The solution above suffers from bad performance, unfortunately. Here's a faster, but complicated version:

https://mathematica.stackexchange.com/a/163247/12

edgeWeightedQ[g_?EmptyGraphQ] := False (* avoid error with First if graph has no edges but is vertex weighted *)
edgeWeightedQ[g_] :=
    WeightedGraphQ[g] &&
    With[{weights = Developer`ToPackedArray@GraphComputation`WeightValues[g]},
      If[First[weights] === 1 && MinMax[weights] === {1, 1},
        PropertyValue[g, EdgeWeight] =!= Automatic,
        True
      ]
    ]

A similar implementation is included in IGraph/M as IGEdgeWeightedQ.

POSTED BY: Szabolcs Horvát

Originally I wrote that the best I could come up with was

 PropertyValue[wg, EdgeWeight] =!= Automatic

but I was not confident that this would work in every situation.

Perpahs some thought that this was nitpicking. It wasn't:

g = Graph[{}, {}];

PropertyValue[g, EdgeWeight]
(* $Failed *)

It does not work on the null graph.

So in case someone else wants to test if a graph is edge-weighted, let me rewise that to

WeightedGraphQ[wg] &&  PropertyValue[wg, EdgeWeight] =!= Automatic

(keeping in mind that && is short circuiting)

If anyone finds a problem with this variant, let me know.

POSTED BY: Szabolcs Horvát
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard