1
$\begingroup$

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.

$\endgroup$
5
  • $\begingroup$ Can you post an example of the type of graph in question? $\endgroup$ May 3, 2018 at 17:46
  • $\begingroup$ I find it difficult to understand what you are trying to say. $\endgroup$
    – Szabolcs
    May 3, 2018 at 17:56
  • $\begingroup$ Also, what does this have to do with visualization? Why did you use that tag? $\endgroup$
    – Szabolcs
    May 3, 2018 at 17:57
  • $\begingroup$ I add an example. I hope it is clearer now. $\endgroup$ May 3, 2018 at 17:58
  • 1
    $\begingroup$ 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.". $\endgroup$
    – Szabolcs
    May 3, 2018 at 18:07

2 Answers 2

4
$\begingroup$
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}

$\endgroup$
3
  • $\begingroup$ Thanks. I mean this. $\endgroup$ May 3, 2018 at 19:43
  • $\begingroup$ 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. $\endgroup$ May 13, 2018 at 1:04
  • $\begingroup$ @FrancescoCiardiello, updated method returns the set of edges that start in a vertex in A. $\endgroup$
    – kglr
    May 15, 2018 at 0:51
1
$\begingroup$

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}
$\endgroup$

Your Answer

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

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