可视化链接攻击的最佳方法是什么 [英] What's the best way to visualize link attacks

查看:114
本文介绍了可视化链接攻击的最佳方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Networkx图形,如下图所示(


我执行边缘攻击,并观察所得子图的节点处值的变化。


例如,
如果我攻击边缘(a,2):边缘(a,2)和(2,1)将被移除。稍微说明一下,当边缘(a,2)受到攻击时,节点2的度数小于。 2.因此,也删除了连接到节点2的边缘。



The上面的攻击会产生一个子图



每次攻击边缘时,该值随时间变化观察到的标记为 e 的终端节点的数量。假设我执行了5次(攻击= 5)攻击,我有一个 time x Attack 矩阵(time = 25,attack = 5),该矩阵存储节点的时间序列数据 e


我想就如何可视化这些攻击对节点<$ c的值的影响提出建议$ c> e 随时间变化。
编辑:


您想从
可视化中看到或识别哪些信息?


我想看看在哪个边缘上进行的攻击对 e 观察到的时间过程值具有最大影响。我们可以想象这是一个运输网络,节点上的值反映了到达位置/节点的产品数量。货物从源节点 b 运送到目标节点 e 。观察到的是攻击边缘后节点值的变化,并且没有观察到边缘值。


请找到用于攻击边缘的代码

 导入networkx为nx 
导入matplotlib.pyplot为plt


def攻击(G):
print(G.edges())

for i,enumerate(G.edges())中的edge:
no_attack = [(6,9),(3,16)]
,如果边缘不在no_attack中:
data = {}
print(f'攻击边缘{edge}')

H = G.copy()

#攻击边缘
H.remove_edges_from(ebunch = [edge])

n = len(G.nodes)
keep_node_ids = [9,3]
H.add_edges_from([(u,v)中的u在(n + 1,n + 2)中的v的保留节点ID中])

#删除度为< 2
H = nx.k_core(H,k = 2)
H.remove_nodes_from([n + 1,n + 2])
#graph_utils_py.draw_graph3d(H,图= 2, show = True)

#H = nx.convert_node_labels_to_integers(H,first_label = 1,ordering ='default',label_attribute = None)

#删除连接的节点和边
diff_nodes = set(G.nodes())。difference(H.nodes())
diff_edges = {如果e中为n,则diff_nodes中n为g.edges()中的e。 b
print(f删除连接的节点{diff_nodes} ...)
print(f删除连接的边缘{diff_edges} ...)

数据['diff_nodes'] =列表(diff_nodes)
数据['diff_edges'] =列表(diff_edges)
数据['edge'] =边缘


如果__name__ =='__main__':
n = 20
G = nx.gnm_random_graph(n = 20,m = 30,seed = 1)
#nx.draw(G,with_labels = True)
#plt.show()

keep_node_ids = [11,4]
G.add_edges_from([[u,v)for u在(n,n + 1)中的v在retain_node_ids中]))

G = nx.k_core (G,k = 2)
G.remove_nodes_from([n,n + 1])$ ​​b $ b#nx.draw(G,with_labels = True)
#plt.show()

G = nx.convert_node_labels_to_integers(G,first_label = 1,ordering ='default',label_attribute = None)
nx.draw(G,with_labels = True)
plt.show( )

攻击(G)

EDIT2:
下面给出的答案表明通过改变不透明度并设置不同的配色方案来进行边缘攻击。不幸的是,这无济于事。必须为每种攻击创建不同的映像。我仍在寻找其他建议。


EDIT3:进一步阐明我想直观展示的内容,以简化操作。


我m寻找类似下面的交互式图形。



EDIT4:下面发布的答案会有所帮助。但是当受影响的边缘重叠时会出现问题。


例如,
攻击(H,(6,4),color ='red')
攻击( H,(5,4),color ='yellow')


给予



颜色重叠并且很难可视化。如果我们可以将受影响的边缘彼此相邻且不重叠地绘制,如上面在edit3中发布的图像所示,那将是很好的。

解决方案

您可以首先删除受攻击的边缘,看看它是否使另一个相邻节点退役(受撞击的边缘),然后在找到右边缘后,用特定于该攻击的颜色绘制它们。在这里,我以实心样式绘制了主要攻击,以虚线形式绘制了受影响的攻击。<​​/ p>

  import matplotlib.pyplot as plt 
导入网络x为nx


H = nx.gnm_random_graph(n = 8,m = 9,seed = 5)#生成随机图
H.add_edges_from ([['In',1),(5,'Out')])#添加输入/输出节点
pos = nx.spring_layout(H,迭代次数= 400)#找到节点$ b $的好位置b
edge = []
impacted_edges = []


def攻击(G,边缘,颜色):
G.remove_edge(* edge) #首先删除边缘

#检查如果G.degree [edge [0]] == 1:
neighbor = [n for n in G.neighbors(edge [0])] [0]
impacted_edge =(edge [0],邻居,颜色)

elif G.degree [edge [1]] == 1 :
邻居= [n等于G.neighbors(edge [1])中的n] [0]
impacted_edge =(edge [1],邻居,颜色)

else :
受影响了_边=无

如果impacted_edge:
impacted_edges.append(impacted_edge)

edge.append((edge [0],edge [1],color))
nx.draw_networkx_edges(
H,
edgelist = [edge],
pos = pos,
edge_color = color,
style ='solid',
label = f'Attack {edge [0]}-{edge [1]}',
width = 4

G.add_edge(* edge)

#攻击某些边缘
Attack(H,(6,4),color ='red')
Attack(H,(3,6),color ='blue')
攻击(H,(1,2),color ='green')
攻击(H,(5,4),color ='purple')

ax = plt .gca()
for impacted_edges中的边:
ax.annotate('',
xy = pos [edge [0]],
xytext = pos [edge [1] ],
zorder = 1,
arrowprops = dict(
color = edge [2],
arrowstyle ='-',
connectionstyle ='arc3,rad = 0.2',
lw = 4 ,
linestyle ='-'



H.remove_edges_from([[e [0],e [1])为e在impacted_edges中] )
H.remove_edges_from([[e [0],e [1])for e in edge]])

nx.draw(H,pos,node_size = 700,with_labels = True ,node_color ='gray',edge_color ='gray')

plt.legend()
plt.show()


我希望您会在此答案中找到想要的东西。


I have a Networkx graph like the following image (image source)

I perform edge attacks and observe the change in values at the node of the resulting subgraph.

Example, If I attack edge (a,2): edge (a, 2) and (2, 1) will be removed. To explain a bit, when edge (a, 2) is attacked the node 2 will have a degree < 2. So the edge that's connected to node 2 is also removed.

The above attack results in a subgraph

Each time an edge is attacked, the value of the terminal node labelled e observed over time changes. Let's say I perform 5 (attack = 5) attacks, I have a time x attack matrix (time=25, attack=5) that stores the time-series data of node e.

I would like to ask for suggestions on how to visualize the effect of these attacks on the value of node e changing over time. EDIT:

What information do you want to be able to see or identify from your visualizations?

I want to see the attack on which edge has the maximum effect on the time course value observed at e. We could imagine this to be a transportation network and the values at node reflect the amount of a product that has reached the location/node. From the source node b, the goods are transported to target node e. The observation made is the change in node values after an edge is attacked and no observation of the edge value is available.

Please find the code that is used to attack edges

import networkx as nx
import matplotlib.pyplot as plt


def attack(G):
    print(G.edges())

    for i, edge in enumerate(G.edges()):
        no_attack = [(6, 9), (3, 16)]
        if edge not in no_attack:
            data = {}
            print(f'attacking edge {edge}')

            H = G.copy()

            # attack an edge
            H.remove_edges_from(ebunch=[edge])

            n = len(G.nodes)
            retain_node_ids = [9, 3]
            H.add_edges_from([(u, v) for u in retain_node_ids for v in (n+1, n+2)])

            # remove nodes with degree < 2
            H = nx.k_core(H, k=2)
            H.remove_nodes_from([n + 1, n + 2])
            # graph_utils_py.draw_graph3d(H, fig=2, show=True)

            # H = nx.convert_node_labels_to_integers(H, first_label=1, ordering='default', label_attribute=None)

            # delete connected nodes and edges
            diff_nodes = set(G.nodes()).difference(H.nodes())
            diff_edges = {e for e in G.edges() for n in diff_nodes if n in e}

            print(f"deleting connected nodes {diff_nodes} ...")
            print(f"deleting connected edges {diff_edges} ...")

            data['diff_nodes'] = list(diff_nodes)
            data['diff_edges'] = list(diff_edges)
            data['edge'] = edge


if __name__ == '__main__':
    n = 20
    G = nx.gnm_random_graph(n=20, m=30, seed=1)
    # nx.draw(G, with_labels=True)
    # plt.show()

    retain_node_ids = [11, 4]
    G.add_edges_from([(u, v) for u in retain_node_ids for v in (n, n + 1)])

    G = nx.k_core(G, k=2)
    G.remove_nodes_from([n, n + 1])
    # nx.draw(G, with_labels=True)
    # plt.show()

    G = nx.convert_node_labels_to_integers(G, first_label=1, ordering='default', label_attribute=None)
    nx.draw(G, with_labels=True)
    plt.show()

    attack(G)

EDIT2: The answer posted below suggests visualizing the edge attacks by varying the opacity and setting different color schemes. Unfortunately, this doesn't help. One has to create a different image for each attack. I am still looking for other suggestions.

EDIT3: Clarifying a bit more on what exactly I want to visualize to keep things simple.

I'm looking for an interactive graph like the following.

One could click the edge that is attacked and the LHS plot will display the observation made at the target node. The dashed lines are the edges that are affected (stored in variable diff_edges in the code) as a result of an attack on a given edge (stored in variable edge).

If there are overlaps in the edges that are affected after attacking a link, we could display it as multiple lines with the corresponding color mappings. An interactive graph will help the user pick and choose the edge attacks to compare the observation at node e. The edges that are attacked can be displayed by varying the opacity/ line style/ color.

EDIT4: The answer posted below helps. But there is a problem when the impacted edges overlap.

Example, attack(H, (6, 4), color='red') attack(H, (5, 4), color='yellow')

gives

The colors overlap and it's hard to visualize. If we can draw the impacted edges next to each other, without overlapping, as shown in the image posted above in edit3 that will be good.

解决方案

You can first remove the attacked edge and see if it makes another neighboring node decommissioned (impacted edge), then after finding the right edges you draw them with a color specific to that attack. Here I drew the main attack in solid style and the impacted one in dashed style.

import matplotlib.pyplot as plt
import networkx as nx


H = nx.gnm_random_graph(n=8, m=9, seed=5)  # generate a random graph
H.add_edges_from([('In', 1), (5, 'Out')])  # adding input/output nodes
pos = nx.spring_layout(H, iterations=400)  # find good positions for nodes

edges = []
impacted_edges = []


def attack(G, edge, color):
    G.remove_edge(*edge)  # first remove the edge

    # check if another could be also impacted
    if G.degree[edge[0]] == 1:
        neighbor = [n for n in G.neighbors(edge[0])][0]
        impacted_edge = (edge[0], neighbor, color)

    elif G.degree[edge[1]] == 1:
        neighbor = [n for n in G.neighbors(edge[1])][0]
        impacted_edge = (edge[1], neighbor, color)

    else:
        impacted_edge = None

    if impacted_edge:
        impacted_edges.append(impacted_edge)

    edges.append((edge[0], edge[1], color))
    nx.draw_networkx_edges(
        H,
        edgelist=[edge],
        pos=pos,
        edge_color=color,
        style='solid',
        label=f'Attack {edge[0]}-{edge[1]}',
        width=4
    )
    G.add_edge(*edge)

# attack some edges
attack(H, (6, 4), color='red')
attack(H, (3, 6), color='blue')
attack(H, (1, 2), color='green')
attack(H, (5, 4), color='purple')

ax = plt.gca()
for edge in impacted_edges:
    ax.annotate('',
                xy=pos[edge[0]],
                xytext=pos[edge[1]],
                zorder=1,
                arrowprops=dict(
                    color=edge[2],
                    arrowstyle='-',
                    connectionstyle='arc3,rad=0.2',
                    lw=4,
                    linestyle='--'
                )
                )

H.remove_edges_from([(e[0], e[1]) for e in impacted_edges])
H.remove_edges_from([(e[0], e[1]) for e in edges])

nx.draw(H, pos, node_size=700, with_labels=True, node_color='gray', edge_color='gray')

plt.legend()
plt.show()

I hope you will find what you want in this answer.

这篇关于可视化链接攻击的最佳方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆