4
$\begingroup$

I wonder, if it's possible to delete graph edges on the base of their weights. In other words, can patterns in the function EdgeDelete[g,patt] include weights of edges to delete? WM Documentation doesn't describe these options.

$\endgroup$

1 Answer 1

6
$\begingroup$

You can use the pattern

_?(yourCondition @ PropertyValue[{g, #}, EdgeWeight] &)

or the pattern

e_ /; (yourCondition @ PropertyValue[{g, e}, EdgeWeight]

Examples:

g1 = CompleteGraph[4, EdgeWeight -> Range[6], 
  VertexLabels -> Automatic, EdgeLabels -> "EdgeWeight"]

enter image description here

g2 = EdgeDelete[g1, _?(OddQ @ PropertyValue[{g1, #}, EdgeWeight] &)];

(* or g2 = EdgeDelete[g1, e_ /; (OddQ @ PropertyValue[{g1, e}, EdgeWeight])]; *)

Graph[g2, VertexCoordinates -> GraphEmbedding[g1]]

enter image description here

forbiddenweights = {1, 3, 4};

g3 = EdgeDelete[g1, _?(MatchQ[Alternatives @@ forbiddenweights]@
      PropertyValue[{g1, #}, EdgeWeight] &)];

Graph[g3, VertexCoordinates -> GraphEmbedding[g1]]

enter image description here

$\endgroup$
1
  • $\begingroup$ Thanks, this should help! $\endgroup$
    – Konstantin
    Commented Jan 5, 2022 at 10:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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