%% Compute Absorption Probabilities
% Create a 20-state Markov chain from a random transition matrix containing 375 randomly placed infeasible transitions.  Plot a digraph of the Markov chain.

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

%
% Find a recurrent class in the Markov chain mc by following this procedure:
% Classify the states by passing mc to classify. Return the array of class memberships ClassStates and the logical vector specifying whether the classes are recurrent ClassRecurrence.
% Extract the recurrent classes from the array of classes by indexing into the array using the logical vector.

[~,ClassStates,ClassRecurrence] = classify(mc);
s = ClassStates{ClassRecurrence}

% States 4 and 15 form a recurrent class.
% Plot two digraphs of the Markov chain mc. For the first digraph, use node colors to identify the state classification. For the second digraph, show the probability of absorption into the recurrent class for each state.

subplot(2,1,1)
graphplot(mc,'ColorNodes',true)
legend off
subplot(2,1,2)
hitprob(mc,s,'Graph',true);

% The states to the left of states 2, 11, 18, and 13 are remote with respect to the recurrent class.  Therefore, their absorption probability is 0.
% Copyright 2019 The MathWorks, Inc.