2
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ What relevance do the weights have to the question? $\endgroup$ – Szabolcs May 3 '18 at 12:51
2
$\begingroup$
ClearAll[vcIn, vcOut]
vcIn = Rest @ VertexInComponent @ ## &;
vcOut = Rest @ VertexOutComponent @ ## &;

Examples:

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

enter image description here

vcIn[g, 3]

{2, 1}

vcOut[g, 2]

{3, 4}

$\endgroup$
1
$\begingroup$

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}}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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