Random and deliberate attacks on complex networks realized by Matlab and Python

Random and deliberate attacks on complex networks realized by Matlab and Python

1. Use Network Toolbox to simulate:

In MATLAB, Network Toolbox can be used to simulate and analyze random and deliberate attacks on complex networks.

Below is a simple demonstration to illustrate how to use MATLAB to perform these tasks.

  1. First, we need to generate a random network. You can use the networkx package to generate a random graph and save it as a GML file format. Here we use a model called ER random graphs:
n = 100; % 节点数
p = 0.05; % 边的概率
A = rand(n) < p; % 生成邻接矩阵
A = A | A'; % 生成对称邻接矩阵
% 保存为GML文件
G = graph(A);
write(G,'er_random_graph.gml','FileType','GML');

insert image description here

  1. Next, we can load this network using the functions in Network Toolbox:
% 加载GML文件
G = read(gml,'er_random_graph.gml');
% 可视化网络
plot(G)
  1. We have now generated and loaded a random network. Next, we can use attack algorithms to disrupt this network. Here we use a function called "removeRandom" to randomly remove some nodes:
% 随机删除一些节点
num_nodes_to_remove = 10; % 删除节点的数量
G_random_attack = G;
for i=1:num_nodes_to_remove
    % 随机选择一个节点进行删除
    node_to_remove = randi(numnodes(G_random_attack));
    G_random_attack = rmnode(G_random_attack,node_to_remove);
end

% 可视化攻击后的网络
plot(G_random_attack)

insert image description here

  1. We can also carry out a purposeful attack using another function called "attack" which will remove the nodes that have the most impact on the entire network:
% 执行有目的的攻击
num_nodes_to_remove = 10; % 删除节点的数量
G_targeted_attack = G;
for i=1:num_nodes_to_remove
    % 找到对网络影响最大的节点
    [~, node_to_remove] = max(sum(adjacency(G_targeted_attack), 2));
    G_targeted_attack = rmnode(G_targeted_attack,node_to_remove);
end

% 可视化攻击后的网络
plot(G_targeted_attack)

In this way, we can use Network Toolbox in MATLAB to simulate and analyze random and deliberate attacks on complex networks. You can modify these codes as needed to adapt to different network models and attack algorithms.
insert image description here


2. Use Python to simulate and analyze:

Random and deliberate attacks on complex networks can be easily simulated and analyzed using Python. Below is a simple demonstration to illustrate how to use Python to perform these tasks.

  1. First, we need to generate a random network. You can use the networkx package to generate a random graph and save it as a GML file format. Here we use a model called ER random graphs:
import networkx as nx

n = 100 # 节点数
p = 0.05 # 边的概率
G = nx.erdos_renyi_graph(n, p)

# 保存为GML文件
nx.write_gml(G, 'er_random_graph.gml')
  1. Next, we can load this network using functions from the networkx package:
# 加载GML文件
G = nx.read_gml('er_random_graph.gml')

# 可视化网络
nx.draw(G, with_labels=True)
  1. We have now generated and loaded a random network. Next, we can use attack algorithms to disrupt this network. Here we use a function called "removeRandom" to randomly remove some nodes:
import random

# 随机删除一些节点
num_nodes_to_remove = 10 # 删除节点的数量
G_random_attack = G.copy()
for i in range(num_nodes_to_remove):
    # 随机选择一个节点进行删除
    node_to_remove = random.choice(list(G_random_attack.nodes))
    G_random_attack.remove_node(node_to_remove)

# 可视化攻击后的网络
nx.draw(G_random_attack, with_labels=True)
  1. We can also carry out a purposeful attack using another function called "attack" which will remove the nodes that have the most impact on the entire network:
import operator

# 执行有目的的攻击
num_nodes_to_remove = 10 # 删除节点的数量
G_targeted_attack = G.copy()
for i in range(num_nodes_to_remove):
    # 找到对网络影响最大的节点
    node_to_remove = max(G_targeted_attack.nodes(), key=lambda n: nx.algorithms.centrality.betweenness_centrality(G_targeted_attack)[n])
    G_targeted_attack.remove_node(node_to_remove)

# 可视化攻击后的网络
nx.draw(G_targeted_attack, with_labels=True)

In this way, we can use Python to simulate and analyze random and deliberate attacks on complex networks. These codes can be modified as needed to adapt to different network models and attack algorithms.


3. Assess the performance and stability of the network:

In network analysis, network efficiency and the largest connected subgraph are two important indicators that can be used to evaluate the performance and stability of the network. You can use the networkx package in Python to compute these metrics and the matplotlib package to visualize the results.

Here is a simple example to illustrate how to calculate network efficiency and maximally connected subgraphs and visualize the results:

import networkx as nx
import matplotlib.pyplot as plt

# 加载GML文件
G = nx.read_gml('er_random_graph.gml')

# 计算网络效率
efficiency = nx.global_efficiency(G)
print('网络效率:', efficiency)

# 计算最大联通子图
max_subgraph = max(nx.connected_component_subgraphs(G), key=len)
print('最大联通子图大小:', len(max_subgraph))

# 可视化网络和最大联通子图
pos = nx.spring_layout(G)
nx.draw_networkx_nodes(G, pos, node_size=50, node_color='r')
nx.draw_networkx_edges(G, pos, width=0.5, alpha=0.5)
nx.draw_networkx_nodes(max_subgraph, pos, node_size=50, node_color='b')
nx.draw_networkx_labels(max_subgraph, pos, font_size=10, font_color='w')
plt.axis('off')
plt.show()

Next, I will explain how to read data from excel and build a network. Then calculate different indicators of the network, and use these indicators to carry out network attacks.

If you need data and code, please follow my official account JdayStudy

This article is published by mdnice multi-platform

Guess you like

Origin blog.csdn.net/weixin_43886163/article/details/129034874
Recommended