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.
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};
A
:el = EdgeList[g, DirectedEdge[Alternatives @@ A, _]]
{1 -> 2, 3 -> 2}
HighlightGraph[g, {A, EdgeList[g, DirectedEdge[Alternatives @@ A, _]]}]
A
:PropertyValue[{g, #}, EdgeWeight] & /@ EdgeList[g, DirectedEdge[Alternatives @@ A, _]]
{0.3, 0.4}
Answered by kglr on February 12, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP