这是一个务实的答案,而不是纯粹的数学答案。
我认为您有两个问题 - 一是布局问题,二是网络问题。
1. 网络
网络中的边太多,无法代表单位超立方体。Caveat我不是这里的数学专家 - 只是从绘图角度(matplotlib 标签)得出这个结论。如果我错了,请解释一下。
您想要的投影,例如沃尔夫拉姆数学世界 http://mathworld.wolfram.com/HasseDiagram.htmln=4 的哈斯图的页面只有 4 条边连接所有节点,而 2 位节点有 6 条边,3 位节点有 7 条边。你的图完全连接了每个“级别”,即 4-D 向量与 01
值连接到所有向量 11
value,然后用 2 连接到所有向量1
价值观等。这在基于维基百科答案的投影中最为明显(下面的第二张图)
2. 投影
我找不到预先编写的算法或库来自动将 4D 超立方体投影到 2D 平面上,但我确实找到了几个例子,。由此,您可以计算出适合您的坐标集并将其传递到nx.draw()
call.
这是一个例子 - 我包含了两个坐标集,一个看起来像上面显示的投影,一个匹配这个来自维基百科 https://en.wikipedia.org/wiki/File:Hypercubeorder_binary.svg.
import networkx as nx
import matplotlib.pyplot as plt
import itertools
H = nx.DiGraph()
axis_labels = ['p','q','r','s']
D_len_node = {}
#Iterate through axis labels
for i in xrange(0,len(axis_labels)+1):
#Create edge from empty set
if i == 0:
for ax in axis_labels:
H.add_edge('O',ax)
else:
#Create all non-overlapping combinations
combinations = [c for c in itertools.combinations(axis_labels,i)]
D_len_node[i] = combinations
#Create edge from len(i-1) to len(i) #eg. pq >>> pqr, pq >>> pqs
if i > 1:
for node in D_len_node[i]:
for p_node in D_len_node[i-1]:
if set.intersection(set(p_node),set(node)):
H.add_edge(''.join(p_node),''.join(node))
#This is manual two options to project tesseract onto 2D plane
# - many projections are available!!
wikipedia_projection_coords = [(0.5,0),(0.85,0.25),(0.625,0.25),(0.375,0.25),
(0.15,0.25),(1,0.5),(0.8,0.5),(0.6,0.5),
(0.4,0.5),(0.2,0.5),(0,0.5),(0.85,0.75),
(0.625,0.75),(0.375,0.75),(0.15,0.75),(0.5,1)]
#Build the "two cubes" type example projection co-ordinates
half_coords = [(0,0.15),(0,0.6),(0.3,0.15),(0.15,0),
(0.55,0.6),(0.3,0.6),(0.15,0.4),(0.55,1)]
#make the coords symmetric
example_projection_coords = half_coords + [(1-x,1-y) for (x,y) in half_coords][::-1]
print example_projection_coords
def powerset(s):
ch = itertools.chain.from_iterable(itertools.combinations(s, r) for r in range(len(s)+1))
return [''.join(t) for t in ch]
pos={}
for i,label in enumerate(powerset(axis_labels)):
if label == '':
label = 'O'
pos[label]= example_projection_coords[i]
#Show Plot
nx.draw(H,pos,with_labels = True,node_shape = 'o')
plt.show()
注意 - 除非您更改我在上面 1 中提到的内容,否则它们仍然具有您的边缘结构,因此看起来与网络上的示例不完全相同。这是您现有的网络生成代码的样子 - 如果将其与您的示例进行比较,您可以看到额外的边缘(例如,我不这样做pr
应该连接到pqs
:
“两个立方体”投影
维基媒体示例投影
Note
如果您想进行自己的预测(并建立pos
数学上),你可能会看看这篇研究论文 http://www.researchgate.net/profile/Daniela_Velichova/publication/265003103_MODELLING_OF_ORTHOGONAL_PROJECTIONS/links/54328df00cf225bddcc7b9c4.pdf.
EDIT:
好奇心战胜了我,我必须寻找一种数学方法来做到这一点。我发现这个博客 https://andrewharvey4.wordpress.com/2008/10/21/an-introduction-to-hypercubes/- 其主要结果是投影矩阵:
这促使我开发了这个函数来投影每个标签,将包含“p”的标签表示该点在“p”轴上的值为 1,即我们正在处理单位超正方体。因此:
def construct_projection(label):
r1 = r2 = 0.5
theta = math.pi / 6
phi = math.pi / 3
x = int( 'p' in label) + r1 * math.cos(theta) * int('r' in label) - r2 * math.cos(phi) * int('s' in label)
y = int( 'q' in label) + r1 * math.sin(theta) * int('r' in label) + r2 * math.sin(phi) * int('s' in label)
return (x,y)
为规则的 2D 八边形提供良好的投影,所有点都不同。
这将在上面的程序中运行,只需替换
pos[label] = example_projection_coords[i]
with
pos[label] = construct_projection(label)
这给出了结果:
玩r1
,r2
,theta
and phi
随心所欲:)