%% Calculate Reachability Matrix
% Create and plot a directed graph.

s = [1 1 2 2 3 4 4 5];
t = [2 4 3 4 5 5 6 6];
G = digraph(s,t);
plot(G,'Layout','subspace')

%
% Calculate the adjacency matrix of the transitive closure of G. The result is a reachability matrix, which has nonzero values to indicate which nodes are reachable from each node.

D = transclosure(G);
R = full(adjacency(D))

% For example, to answer the question "Which nodes are reachable from node 3?", you can look at the third row in the matrix. That row indicates only nodes 5 and 6 are reachable from node 3:

find(R(3,:))

% Copyright 2019 The MathWorks, Inc.