MATLAB: How to find all the downstream nodes from a node in a graph

graphgraph theoryplotting

Hello All,
Hope you are staying safe and doing well.
I am trying to solve a problem where I would need to figure out all the nodes going downstream from a specified node in a MatLab graph.
For example:
I have this sample code which gives me the following figure
G = graph([1,2,3,4,2,6,7,1,9],[2,3,4,5,6,7,8,9,10]);
plot(G)
Now, I need to know the number/IDs/indexes of nodes going out of node 1 towads 2 and beyond. Like, it should give me that Node 2,3,4,5,6,7,and 8 are connected to a tree branch groing out of 1. Please let me know if there is a way to figure that out.
I have tried the command OUTEDGES and successors. But they only give nodes directly connected to 1.
I would appreciate your help and ideas in this problem. Thank you.

Best Answer

Gc = G;
[eid, nid] = outedges(Gc,1);
Gc = rmedge(Gc, eid(nid ~= 2));
downstream_nodes = setdiff(unique(minspantree(Gc,'Root',1).Edges.EndNodes), 1);