它是Petersen提出的一种简单的连通图,它一般画作五边形中包含有五角星的造型。Petersen图的同构多种多样,形态各异,共120多种.
性质:
特点:
import dgl
import warnings
import networkx as nx
import matplotlib.pyplot as plt
%matplotlib inline
warnings.filterwarnings('ignore')
# create a petersen grpah
g_nx = nx.petersen_graph()
# input networkx grpah ,return DGL graph
g_dgl = dgl.DGLGraph(g_nx) # add direction,bidirectional
plt.figure(figsize=(20, 6))
plt.subplot(121)
plt.title('Undirected graph ,networkx',fontsize=20)
nx.draw(g_nx, with_labels=True)
plt.subplot(122)
plt.title('Directed graph ,DGL', fontsize=20)
nx.draw(g_dgl.to_networkx(), with_labels=True)
import dgl
import torch as th
import networkx as nx
import matplotlib.pyplot as plt
g = dgl.DGLGraph()
g.add_nodes(10)
# 逐条往图中添加边
for i in range(1, 4):
g.add_edge(i, 0)
# 批添加
src = list(range(5, 8)); dst = [0]*3
g.add_edges(src, dst)
src = th.tensor([8, 9]); dst = th.tensor([0, 0])
g.add_edges(src, dst)
plt.figure(figsize=(14, 6))
nx.draw(g.to_networkx(), with_labels=True)
g.clear(); g.add_nodes(10)
src = th.tensor(list(range(1, 10)));
g.add_edges(src, 0)
plt.figure(figsize=(14, 6))
nx.draw(g.to_networkx(), with_labels=True)
plt.show()
import dgl
import torch as th
x = th.randn(10, 3)
g.ndata['x'] = x
print(g.nodes[:].data['x'])
tensor([[ 0.0874, 0.0827, 1.0128],
[-1.6501, -0.0043, 1.9106],
[ 1.9255, 1.1269, -0.3605],
[ 0.3809, 0.9866, -1.2076],
[ 1.5794, -0.7897, 0.6195],
[ 0.4960, 0.0229, 1.6241],
[-0.0205, 0.1173, -0.6610],
[-0.9029, 0.3396, 0.6821],
[-1.6409, 0.6686, -1.0845],
[ 0.1309, -0.9126, 0.1297]])
# 修改特征
g.nodes[0].data['x'] = th.zeros(1, 3)
g.nodes[[0, 1, 2]].data['x'] = th.zeros(3, 3)
g.nodes[th.tensor([0, 1, 2])].data['x'] = th.zeros(3, 3)
print(g.nodes[:].data['x']) # = print(g.ndata['x'])
tensor([[ 0.0000, 0.0000, 0.0000],
[ 0.0000, 0.0000, 0.0000],
[ 0.0000, 0.0000, 0.0000],
[ 0.3809, 0.9866, -1.2076],
[ 1.5794, -0.7897, 0.6195],
[ 0.4960, 0.0229, 1.6241],
[-0.0205, 0.1173, -0.6610],
[-0.9029, 0.3396, 0.6821],
[-1.6409, 0.6686, -1.0845],
[ 0.1309, -0.9126, 0.1297]])
g.edata['w'] = th.randn(9, 2)
# 通过边的索引访问,修改feature tensor
g.edges[1].data['w'] = th.randn(1, 2)
g.edges[[0, 1, 2]].data['w'] = th.zeros(3, 2)
g.edges[th.tensor([0, 1, 2])].data['w'] = th.zeros(3, 2)
# 通过边两端连接的节点访问
g.edges[1, 0].data['w'] = th.ones(1, 2) # edge 1 -> 0
g.edges[[1, 2, 3], [0, 0, 0]].data['w'] = th.ones(3, 2) # edges [1, 2, 3] -> 0
# shape,type
g.ndata['x'] = th.zeros((10, 4))
print(g.node_attr_schemes())
{'x': Scheme(shape=(4,), dtype=torch.float32)}
# ndata node_data, edata edge_data
g.ndata.pop('x')
g.edata.pop('w')
tensor([[ 1.0000, 1.0000],
[ 1.0000, 1.0000],
[ 1.0000, 1.0000],
[-0.2754, 0.6408],
[ 1.1895, 1.3651],
[ 0.7568, -1.0425],
[ 0.0378, -0.5885],
[ 1.2551, -1.4866],
[-0.4607, -1.8974]])
g_multi = dgl.DGLGraph(multigraph=True)
g_multi.add_nodes(10)
g_multi.ndata['x'] = th.randn(10, 2)
g_multi.add_edges(list(range(1, 10)), 0)
g_multi.add_edge(1, 0) # two edges on 1->0
g_multi.edata['w'] = th.randn(10, 2)
g_multi.edges[1].data['w'] = th.zeros(1, 2)
print(g_multi.edges())
(tensor([1, 2, 3, 4, 5, 6, 7, 8, 9, 1]), tensor([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]))
多图的情况下(u,v)不能唯一的标识一条边,可以通过dege_id(u, v)
来获得edge的唯一id,通过id来访问边特征.
eid_10 = g_multi.edge_id(1,0) # eid_10 = tensor([0, 9])
g_multi.edges[eid_10].data['w'] = th.ones(len(eid_10), 2)
print(g_multi.edges[eid_10].data['w'])
tensor([[1., 1.],
[1., 1.]])
print(g_multi.edata['w'])
tensor([[ 1.0000e+00, 1.0000e+00],
[ 0.0000e+00, 0.0000e+00],
[ 9.7529e-01, -4.8987e-01],
[-9.8543e-01, -7.8525e-01],
[-1.3410e-01, -2.5699e-01],
[-7.5977e-01, 2.0429e-01],
[ 1.4889e-01, -1.0292e-03],
[ 1.2907e+00, -7.6749e-01],
[-6.9293e-01, -1.0950e+00],
[ 1.0000e+00, 1.0000e+00]])