function a = A354017(n)
% Matlab function to generate OEIS sequence A354017 - Author Adam A. Arfaoui 
N = n-1;
P = Partitions(N);
Cmb = zeros(size(P));
for i = 1:size(P,1)
    ind = 1;
    for j = 1:size(P,2)
        if P(i,j)>0
            RM = repmat(j,1,P(i,j));
            Cmb(i,ind:ind+size(RM,2)-1) = RM;
            ind = ind+size(RM,2);
        end
    end
end
GP = int32(perms(1:N));
RCmb = int32(zeros(size(GP,1)*size(Cmb,1),size(Cmb,2)));
GM = int32(repmat(GP,size(Cmb,1),1));
np = size(GP,1);
ind = 1;
for i = 1:size(Cmb,1)
    RR = int32(repmat(Cmb(i,:),np,1));
    RCmb(ind:ind+np-1,:) = RR;
    ind = ind+np;
end

for i = 1:size(GM,1)
    GM(i,:) = RCmb(i,GM(i,:));
end
GM = unique(GM,'rows','stable');

GM = GM(GM(:,1)>0,:);
G = int32(zeros(N,2*size(GM,1)));
idx = 1;
for i = 1:size(GM,1)
    E = zeros(N,2);
    E(1:end,2) = (2:N+1)';
    ind = 1;
    for j = 1:N
        R = repmat(j,GM(i,j),1);
        E(ind:ind+size(R,1)-1,1) = R;
        ind = ind+size(R,1);
    end
    if any(E(:,1)-E(:,2)==0)
        continue
    else
        G(:,2*idx-1:2*idx) = E;
        idx = idx+1;
    end
end
G = G(:,sum(G,1)~=0);
NG = idx-1;

O = struct('g',[]);
for i = 1:NG
    O(i).g = graph(G(:,2*i-1),G(:,2*i));
    
end

I = zeros(NG,NG);
for i = 1:NG
    idx = 1;
    I(i,idx) = i;
    idx = 2;
    for j = 1:NG
        if i == j
            continue
        elseif isisomorphic(O(i).g,O(j).g)
            I(i,idx) = j;
            idx = idx+1;
        end
    end
    
end
I = sort(I,2);
[~, ia, ~] = unique(I,'rows','stable');

C = cell(size(ia,1),1);
for i = 1:size(ia,1)
    C{i,1} = FindSubGraphs(G(:,2*ia(i)-1:2*ia(i)));
end
a = sum(cell2mat(cellfun(@numel,C,'UniformOutput',false)));
end

function C = FindSubGraphs(A)
A = num2cell(A,2);
ni = size(A,1)+1;
B = A;
while size(A,1)>1
    A = subgraphs(A);
    B = [B;A];
end

C = cell(size(B,1),1);
n = size(B,1);
iter = 1;
for i = 1:n
    if B{i,:}==int32(0)
        continue
    else
        C{iter,1} = int32(B{i,:});
        iter = iter+1;
        for j = i+1:n
            if all(ismember(B{i,:},B{j,:})) && size(B{i,:},2)==size(B{j,:},2)
                B{j,1} = int32(0);
            end
        end
    end
end
C = [num2cell([1:ni]');C];
C = C(1:iter-1+ni);
    
end

function C = subgraphs(A)
n = size(A,1);
iter = 1;
B = cell(nchoosek(n,2),1);

for i = 1:n
    for j = i+1:n
        if any(ismember(A{i,:},A{j,:}))
            B{iter,1} = union(A{i,:},A{j,:});
            iter = iter+1;
        end
    end
end
B = B(1:iter-1);

C = cell(iter-1,1);
n = size(B,1);
iter = 1;
for i = 1:n
    if B{i,:}==0
        continue
    else
        C{iter,1} = B{i,:};
        iter = iter+1;
        for j = i+1:n
            if all(ismember(B{i,:},B{j,:})) && size(B{i,:},2)==size(B{j,:},2)
                B{j,1} = 0;
            end
        end
    end
end
C = C(1:iter-1);

end