Python实现两个图的交运算、并运算、差运算、对称差运算、联运算、积图、合成图

Python实现两个图的交运算、并运算、差运算、对称差运算、联运算、积图、合成图
用python随机生成两张图G1,G2
在这里插入图片描述在这里插入图片描述

import networkx as nx
import matplotlib.pyplot as plt
import numpy as np
from numpy import random


G1 = nx.Graph()
Matrix1 = np.array(random.randint((2),size=(50,50)))
#print(Matrix)
for i in range(len(Matrix1)):
    for j in range(len(Matrix1)):
        G1.add_edge(i, j)
nx.draw_networkx(G1)
plt.title("G1")
plt.show()


G2= nx.Graph()
Matrix2 = np.array(random.randint((2),size=(50,50)))
for i in range(len(Matrix2)):
    for j in range(len(Matrix2)):
        G2.add_edge(i, j)
nx.draw_networkx(G2)
plt.title("G2")
plt.show()



np.union1d(Matrix1,Matrix2)
plt.title("G1与G2的并运算")
plt.show()
np.intersect1d(Matrix1,Matrix2, assume_unique=True)
plt.title("G1与G2的交运算")
plt.show()
np.setdiff1d(Matrix1,Matrix2, assume_unique=True)
plt.title("G1与G2的差运算")
plt.show()
np.setdiff1d(Matrix1,Matrix2, assume_unique=True)
plt.title("G2与G1的差运算")
plt.show()
Matrix1^Matrix2
plt.title("G1与G2的对称差运算")
plt.show()
Matrix1+Matrix2
plt.title("G1与G2的联运算")
plt.show()
Matrix1*Matrix2
plt.title("G1与G2的积运算")
plt.show()
Matrix1[Matrix2]
plt.title("G1与G2的合成运算")
plt.show()

在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_21201679/article/details/88557440