TransWikia.com

How can I extract weights labelling edges starting from a fixed set of vertices

Mathematica Asked by Francesco Ciardiello on February 12, 2021

Let g be a directed weighted graph (in particular is tree). Given a set of vertices A, how can I extract the weights in a set B having the following feature

Each weight in B labels an edge which starts from a vertex in A.

Being the graph a tree, then a unique edge starts from a vertex. Clearly for a tree, the extraction is conceptually easier. However the extraction procedure seems to be a standard procedure.

For example the graph is

g = Graph[{1 -> 2, 3 -> 2, 2 -> 4, 4 -> 5}, EdgeWeight -> {0.3, 0.4, 0.5, 0.7}]

The subset of vertices is A={1,3}. The weight 0.3 is associated with node 1.
The weight 0.4 is associated with node 3.

Thanks very much.

2 Answers

Maybe you mean this.

Function[edge,
  First[edge] -> PropertyValue[{g, edge}, EdgeWeight]
] /@ EdgeList[g]

{1 -> 0.3, 3 -> 0.4, 2 -> 0.5, 4 -> 0.7}

Answered by Szabolcs on February 12, 2021

g = Graph[{1 -> 2, 3 -> 2, 2 -> 4, 4 -> 5}, 
   EdgeWeight -> {0.3, 0.4, 0.5, 0.7}, 
   VertexLabels -> Placed["Name", Center], VertexSize -> Medium, 
   EdgeLabels -> "EdgeWeight"];
A = {1, 3};

Edges starting at a vertex in A:

el = EdgeList[g, DirectedEdge[Alternatives @@ A, _]]

{1 -> 2, 3 -> 2}

HighlightGraph[g, {A, EdgeList[g, DirectedEdge[Alternatives @@ A, _]]}]

enter image description here

Weights of the edges incident to vertices in A:

PropertyValue[{g, #}, EdgeWeight] & /@ EdgeList[g, DirectedEdge[Alternatives @@ A, _]]

{0.3, 0.4}

Answered by kglr on February 12, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP