Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Graph - add subgraphs and attacks #1

Open
3 tasks
giovannivolpe opened this issue Mar 25, 2023 · 0 comments
Open
3 tasks

Graph - add subgraphs and attacks #1

giovannivolpe opened this issue Mar 25, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@giovannivolpe
Copy link
Owner

giovannivolpe commented Mar 25, 2023

Add to Graph:

  • subgraphs
  • node attacks
  • edge attacks

% subgraphs
function sg = subgraph(g, nodes)
%SUBGRAPH extracts subgraph
%
% SG = SUBGRAPH(G, NODES) extracts the graph SG as a subgraph of G
% containing only the nodes specified by NODES.
% If NODES is a vector, the specified nodes are kept from
% all layers. If NODES is a cell array of vectors, the
% specified nodes are kept from each layer.
%
% see also nodeattack, edgeattack.

A = g.get('A');
L = g.layernumber();

if ~iscell(nodes)
nodes = repmat({nodes}, 1, L);
end

switch Graph.getGraphType(g)
case Graph.GRAPH
B = A{1};
B = B(nodes{1}, nodes{1});
sg = eval([g.getClass() '(''B'', B)']);

case Graph.MULTIGRAPH
    temp_B = g.get('B');
    B2 = temp_B(nodes{1}, nodes{1});
    if isa(g, 'MultigraphBUD')
        sg = MultigraphBUD('B', B2, 'Densities', g.get('Densities'));
    else
        sg = MultigraphBUT('B', B2, 'Thresholds', g.get('Thresholds'));
    end
    
otherwise  % multiplex
    if isa(g, 'MultiplexBUD') || isa(g, 'MultiplexBUT')
        temp_B = g.get('B');
        for li = 1:1:length(temp_B)
            Aii = temp_B{li};
            if ~isempty(Aii)
                B(li) = {Aii(nodes{li}, nodes{li})};
            end
        end
        if isa(g, 'MultiplexBUD')
            sg = MultiplexBUD('B', B, 'Densities', g.get('Densities'));
        else
            sg = MultiplexBUT('B', B, 'Thresholds', g.get('Thresholds'));
        end
    else
        for li = 1:1:L
            Aii = A{li, li};
            if ~isempty(Aii)
                B(li) = {Aii(nodes{li}, nodes{li})};
            end
        end
        sg = eval([g.getClass() '(''B'', B)']);
    end

end
end
function ga = nodeattack(g, nodes, layernumbers)
%NODEATTACK removes given nodes from a graph
%
% GA = NODEATTACK(G, NODES) creates the graph GA resulting by removing
% the nodes specified by NODES from G. For non single layer
% graphs, it removes NODES in every layer.
%
% GA = NODEATTACK(G, NODES, LAYERNUMBERS) creates the graph GA
% resulting by removing the nodes specified by NODES from G.
% For non single layer graphs, it removes NODES in the layers
% specified by LAYERNUMBERS.
%
% NODES are removed by setting all the connections from and to
% the nodes in the connection matrix to 0.
%
% See also edgeattack, subgraphs.

if nargin < 3
layernumbers = 1:1:g.layernumber();
end

A = g.get('A');

switch Graph.getGraphType(g)
case Graph.GRAPH
A = A{1};
A(nodes(:), :) = 0;
A(:, nodes(:)) = 0;

otherwise
    for li = layernumbers
        Aii = A{li, li};
        Aii(nodes(:), :) = 0;
        Aii(:, nodes(:)) = 0;
        A(li, li) = {Aii};
        if Graph.getGraphType(g) ~= Graph.MULTIGRAPH
            for lj = 1:1:g.layernumber()
                Aij = A{li, lj};  % li cell row
                Aji = A{lj, li};  % li cell column
                if isempty(Aij) == 0
                    Aij(nodes(:), :) = 0;
                    Aij(:, nodes(:)) = 0;
                end
                if isempty(Aji) == 0
                    Aji(nodes(:), :) = 0;
                    Aji(:, nodes(:)) = 0;
                end
                A(li, lj) = {Aij};
                A(lj, li) = {Aji};
            end
        end
    end

end
ga = eval([g.getClass() '(''B'', A)']);
end
function ga = edgeattack(g, nodes1, nodes2, layernumbers1, layernumbers2)
%EDGEATTACK removes given edges from a graph
%
% GA = EDGEATTACK(G, NODES1, NODES2) creates the graph GA resulting
% by removing the edges going from NODES1 to NODES2 from G. For
% non single layer graphs, it removes the edges from NODES1 to
% NODES2 in every layer.
%
% GA = EDGEATTACK(G, NODES1, NODES2, LAYERNUMBERS_I) creates the graph GA
% resulting by removing the edges going from NODES1 to NODES2 from G.
% For non single layer graphs, it removes the edges from NODES1 to
% NODES2 in the layers specified by LAYERNUMBERS.
%
% GA = EDGEATTACK(G, NODES1, NODES2, LAYERNUMBERS_I, LAYERNUMBERS_J)
% creates the graph GA resulting by removing the edges going
% from NODES1 to NODES2 from G. For non single layer graphs, it
% removes the edges from NODES1 to NODES2 in and between the layers
% specified by LAYERNUMBERS_I and LAYERNUMBERS_J.
%
% EDGES are removed by setting all the connections from NODES1 to
% NODES2 in the connection matrix to 0.
%
% NODES1 and NODES2 must have the same dimensions.
%
% See also nodeattack, subgraphs.

if nargin < 4
layernumbers1 = 1:1:g.layernumber();
end

if nargin < 5
layernumbers2 = layernumbers1;
end

A = g.get('A');

switch Graph.getGraphType(g)
case Graph.GRAPH
if iscell(A)
A = A{1};
end
A(sub2ind(size(A), nodes1, nodes2)) = 0;

    if g.is_undirected(g)
        A(sub2ind(size(A), nodes2, nodes1)) = 0;
    end
    
otherwise
    directionality = g.getDirectionalityType(g.layernumber());
    for n = 1:1:length(layernumbers1)
        li = layernumbers1(n);
        lj = layernumbers2(n);
        
        Aij = A{li, lj};
        Aij(sub2ind(size(Aij), nodes1, nodes2)) = 0;
        A(li, lj) = {Aij};
        
        if directionality(li, lj) == Graph.UNDIRECTED
            Aji = A{lj, li};
            Aji(sub2ind(size(Aji), nodes2, nodes1)) = 0;
            A(lj, li) = {Aji};
        end
    end

end
ga = eval([g.getClass() '(''B'', A)']);
end

@giovannivolpe giovannivolpe added the enhancement New feature or request label Mar 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant