%% This is the Matlab code used to measure the betweenness of sectors.
% More details about the betweenness method can be found in our ES&T paper:
% Sai Liang, Shen Qu, and Ming Xu. Betweenness-Based Method to Identify
% Critical Transmission Sectors for Supply Chain Environmental Pressure
% Mitigation. Environmental Science & Technology 2016 50 (3), 1330-1337. DOI: 10.1021/acs.est.5b04855

%% INPUT variables:
% intermediate economic flow matrix Z
% sectoral monetary total outputs X, a column vector
% the quantity of sectoral resource uses or sectoral emissions E, a column vector
% sectoral final demand y, a column vector

%% OUTPUT variable
% b_node: the betweenness of each sector

%% codes

n = length(Z);  % n is the number of sectors
I = eye(n);

A = zeros(n);  % A is the direct requirement coefficient matrix
for i = 1:n
   for j = 1:n
       if X(j,1) ~= 0
           A(i,j) = Z(i,j)/X(j,1);              
       else
           A(i,j) = 0;
       end 
   end
end

F = zeros(n,1); % F is the intensity vector
for i = 1:n    
    if X(i,1) ~= 0      
        F(i,1) = E(i,1)/X(i,1);
    else
        F(i,1) = 0;
    end    
end

L = inv(I-A); % L is the Leontief inverse matrix
T = L*A;

for i=1:n
    temp = zeros(n,n);
    temp(i,i) = 1;
    J{i} = temp;
    clear temp;
end

b_node = zeros(n,1); % b_node indicates the betweenness of each sector
for i=1:n
        b_node(i,1)= F'*T*J{i}*T*y; % betweenness of sector i
end
