注意
跳转到末尾以下载完整的示例代码。
同构
本示例展示了如何使用 igraph.GraphBase.isomorphic()
检查小型图之间的 同构。
import igraph as ig
import matplotlib.pyplot as plt
首先,我们生成三个不同的图
为了检查它们是否同构,我们可以使用一个简单的方法
print("Are the graphs g1 and g2 isomorphic?")
print(g1.isomorphic(g2))
print("Are the graphs g1 and g3 isomorphic?")
print(g1.isomorphic(g3))
print("Are the graphs g2 and g3 isomorphic?")
print(g2.isomorphic(g3))
# Output:
# Are the graphs g1 and g2 isomorphic?
# True
# Are the graphs g1 and g3 isomorphic?
# False
# Are the graphs g2 and g3 isomorphic?
# False
Are the graphs g1 and g2 isomorphic?
True
Are the graphs g1 and g3 isomorphic?
False
Are the graphs g2 and g3 isomorphic?
False
注意
图同构 是一种等价关系,即如果 g1 ~ g2 且 g2 ~ g3,那么自动地 g1 ~ g3。因此,我们可以跳过最后的检查。
我们可以绘制这些图,以了解问题
visual_style = {
"vertex_color": "lightblue",
"vertex_label": [0, 1, 2, 3, 4],
"vertex_size": 25,
}
fig, axs = plt.subplots(1, 3)
ig.plot(
g1,
layout=g1.layout("circle"),
target=axs[0],
**visual_style,
)
ig.plot(
g2,
layout=g1.layout("circle"),
target=axs[1],
**visual_style,
)
ig.plot(
g3,
layout=g1.layout("circle"),
target=axs[2],
**visual_style,
)
fig.text(0.38, 0.5, '$\\simeq$' if g1.isomorphic(g2) else '$\\neq$', fontsize=15, ha='center', va='center')
fig.text(0.65, 0.5, '$\\simeq$' if g2.isomorphic(g3) else '$\\neq$', fontsize=15, ha='center', va='center')
plt.show()

脚本总运行时间:(0 分 0.275 秒)