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

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

% Plot a digraph of the Markov chain mc. Display the transition probabilities.

graphplot(mc,'ColorEdges',true)

% Compute the expected first hitting time for state 1, beginning from each state in the Markov chain.

ht = hittime(mc,1)

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

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

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

hittime(mc,[1 4],'Graph',true);

%
% Create the Markov chain characterized by this transition matrix:
% 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/6&1/6&1/6&1/6&1/6&1/6&0\\
% 1/2&0&0&0&0&0&1/2
% \end{array}} \right].

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);

% Compute the expected first hitting times for state 1, beginning from each state in the Markov chain mc. Also, plot a digraph and specify node colors representing the expected first hitting times for state 1.

ht = hittime(mc,1,'Graph',true)

% States 2 and 4 form an absorbing class.  Therefore, state 1 is unreachable from these states. The absorbing class is remote with respect to  state 1, with an expected first hitting time of Inf.
% State 1 is reachable from states 5 and 6, but the probability of transitioning into the absorbing class from states 5 and 6 is nonzero. Therefore, states 5 and 6 are remote-reachable with respect to state 1, with an expected first hitting time of Inf.
% The expected first hitting time for state 1 beginning from state 7 is 2 time steps. The expected first hitting time for state 1 beginning from state 3 is 4 time steps.
%
% Copyright 2019 The MathWorks, Inc.