function [can_form,new_state_order,fund_mat,abs_time,abs_prob] = Absorbing_Chain( abs_mark_chain )
%ABSORBING_CHAIN returns fundamental quantities of Absorbing Markov Chains

[size_mark_chain,~]=size(abs_mark_chain);

new_state_order=zeros(size_mark_chain,1);

can_form=zeros(size_mark_chain);

%abs_states=zeros(size_mark_chain,1);
abs_state_counter=0;
for i=1:size_mark_chain
    if size(find(abs_mark_chain(i,:)))==1 & find(abs_mark_chain(i,:))==i
        can_form(:,size_mark_chain-abs_state_counter)=abs_mark_chain(:,i);
        new_state_order(size_mark_chain-abs_state_counter)=i;
        abs_state_counter=abs_state_counter+1;        
    else
        can_form(:,i-abs_state_counter)=...
            abs_mark_chain(:,i);
        new_state_order(i-abs_state_counter)=i;
    end
end
work_can_form=can_form;
can_form=zeros(size_mark_chain);

for i=1:size_mark_chain
    can_form(i,:)=work_can_form(new_state_order(i),:);
end


fund_mat=eye(size_mark_chain-abs_state_counter)-...
    can_form(1:size_mark_chain-abs_state_counter,...
    1:size_mark_chain-abs_state_counter);
fund_mat=inv(fund_mat);

[size_fund_mat,~]=size(fund_mat);

abs_time=fund_mat*ones(size_fund_mat,1);

abs_prob=fund_mat*can_form(1:1:size_mark_chain-abs_state_counter,...
    size_mark_chain-abs_state_counter+1:size_mark_chain);


end

