%% Identify Communicating Classes in Digraph
% Consider this theoretical, right-stochastic transition matrix of a stochastic process.
% P = \left[ {\begin{array}{*{20}{c}}
% {0.5}&{0.5}&0&0\\
% {0.5}&0&{0.5}&0\\
% 0&0&0&1\\
% 0&0&1&0
% \end{array}} \right].
% Create the Markov chain that is characterized by the transition matrix P. Name the states Regime 1 through Regime 4.

P = [0.5 0.5 0 0; 0.5 0 0.5 0; 0 0 0 1; 0 0 1 0];
mc = dtmc(P,'StateNames',["Regime 1" "Regime 2" "Regime 3" "Regime 4"]);

%
% Plot a directed graph of the Markov chain. Identify the communicating classes in the digraph and color the edges according to the probability of transition.

figure;
graphplot(mc,'ColorNodes',true,'ColorEdges',true)

% States 3 and 4 compose a communicating class with period 2. States 1 and 2 are transient.
% Copyright 2012 The MathWorks, Inc.