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

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.

• Can you post an example of the type of graph in question? – Ben Kalziqi May 3 '18 at 17:46
• I find it difficult to understand what you are trying to say. – Szabolcs May 3 '18 at 17:56
• Also, what does this have to do with visualization? Why did you use that tag? – Szabolcs May 3 '18 at 17:57
• I add an example. I hope it is clearer now. – Francesco Ciardiello May 3 '18 at 17:58
• Correct grammar would help. I can't make any sense of twisted sentences like "Being the graph a tree then for each vertex starts a unique edge.". – Szabolcs May 3 '18 at 18:07

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, _]]}]


### Weights of the edges incident to vertices in A:

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


{0.3, 0.4}

• Thanks. I mean this. – Francesco Ciardiello May 3 '18 at 19:43
• I have used this. there is a small problem with this answer. IncidenceList[g, A] returns the set of edges which are incident to a vertex in A. Actually in my case, i should have the set of edges which start from a vertex in A. In the above example the two things coincide because the set A={1,3} contains vertices from which edges start but they do not end in them. The answer to this post is partial. – Francesco Ciardiello May 13 '18 at 1:04
• @FrancescoCiardiello, updated method returns the set of edges that start in a vertex in A. – kglr May 15 '18 at 0:51

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}