In a (possibly directed) graph, is there a simple way to find all nodes reachable for a given node?
Show older comments
I've recently started working with MATLAB graphs. In two recent problems, I have graphs which are not known to be connected every node to every other node. Is there a way to determine in one or a known number of steps, the set of nodes which can (or cannot would work) be reached from a specified node? I've encountered this question both with undirected (small, and I powered through) and now undirected nodes.
Just in case I'm in asking an XY question, my current directed graph is entirely unidirectional; there are no closed loops. I could trivially assign a value to each node, and know that every edge leads to a node with a strictly larger value than the node it leads from. To be even more specific if it helps, though I definitely don't know the proper math language: the graph represents a double elimination playoff bracket, and I want to put together the possible routes for each seed on a plot to guide teams through the process, pruning out things that don't matter to that team. Fortunately in this case, the graph is deterministic; no re-seeding occurs due to match outcomes, making such a plot useful.
My previous issue, which was small so I powered through, had an undirected graph, and I really just wanted to know whether or not the graph was continuous; that is, whether every node pair was connected by some path.
1 Comment
GeeTwo
on 7 Apr 2025
Edited: Walter Roberson
on 7 Apr 2025
Accepted Answer
More Answers (1)
The quickest way to find all nodes reachable from a given node is using the nearest function with Inf as the distance:
g = digraph([1 1 1 2 2], [2 5 6 3 4]);
plot(g)
node = 1;
distance = Inf;
nearest(g, node, distance)
This returns the nodes sorted by the distance from the original node.
Also, the allpaths method returns all paths between two nodes.
2 Comments
GeeTwo
on 7 Apr 2025
John D'Errico
on 7 Apr 2025
Please answer more questions. Whenever you do, I ALAYS learn something useful!
Categories
Find more on Graph and Network Algorithms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


