# paths in a weighted directed trees

Given an weighted directed tree, I have to write this:

Given a vertex v, I should list all the vertex c such that there exist a path whose origin is in c and whose end is in v

Given a vertex v, I should list all the vertex c such that there exist a path whose origin is in v and whose end is in c

What is the easiest way to do this in Mathematica?

Thanks very much.

• What relevance do the weights have to the question? – Szabolcs May 3 '18 at 12:51

ClearAll[vcIn, vcOut]
vcIn = Rest @ VertexInComponent @ ## &;
vcOut = Rest @ VertexOutComponent @ ## &;


Examples:

g = Graph[{1 -> 2, 2 -> 3, 3 -> 4}, VertexShapeFunction -> "Name"]


vcIn[g, 3]


{2, 1}

vcOut[g, 2]


{3, 4}

Like with all Graphs, you can use FindPath.

g = Graph[{1 -> 2, 2 -> 3, 3 -> 4}, VertexShapeFunction -> "Name"]
c = 1;
v = 3;
FindPath[g, c, v, ∞, 1]


{{1, 2, 3}}