%% Color Nodes of Digraph Using Hitting Probabilities
% Consider this theoretical, right-stochastic transition matrix of a stochastic process.
% P = \left[ {\begin{array}{*{20}{c}}
% 1/2&0&1/2&0&0&0&0\\
% 0&{1/3}&0&{2/3}&0&0&0\\
% 1/4&0&3/4&0&0&0&0\\
% 0&2/3&0&1/3&0&0&0\\
% 1/4&1/8&1/8&1/8&1/4&1/8&0\\
% 1/2&0&0&0&0&0&1/2
% \end{array}} \right].
% Create the Markov chain that is characterized by the transition matrix P.

P = [1/2 0   1/2 0   0   0   0
     0   1/3 0   2/3 0   0   0
     1/4 0   3/4 0   0   0   0
     0   2/3 0   1/3 0   0   0
     1/4 1/8 1/8 1/8 1/4 1/8 0
     1/6 1/6 1/6 1/6 1/6 1/6 0
     1/2 0   0   0   0   0   1/2];
mc = dtmc(P);

% Plot a digraph of the Markov chain mc. Specify node colors representing the hitting probabilities for state 1, beginning from each state in the Markov chain.

hitprob(mc,1,'Graph',true);

% Plot another digraph. Include state 3 as a target state.

hitprob(mc,[1 3],'Graph',true);

% The probability of hitting states 1 or 3 from state 6 is approximately 0.5.
%
% Create a 20-state Markov chain from a random transition matrix containing 375 randomly placed infeasible transitions. An infeasible transition is a transition whose probability of occurring is zero.

rng(4) % For reproducibility
mc = mcmix(20,'Zeros',375);

% Plot a digraph showing, for each state, the probability of transitioning to the subclass containing states 1 and 2.

target = [1 2];
hitprob(mc,target,'Graph',true);

%
% Copyright 2019 The MathWorks, Inc.
%