擅长:python、mysql、java
<p>您可以创建一个类<code>Network</code>,用顶点表示每条边,并为自定义可视化创建<code>__repr__</code>方法:</p>
<pre><code>tree = {'X': ['Y'], 'C': ['M'], 'M': ['C', 'F', 'Y'], 'Q': ['F'], 'Y': ['X', 'M'], 'F': ['M', 'Q']}
weights = {('M', 'C'): 44, ('Q', 'F'): 27, ('Y', 'X'): 42, ('X', 'Y'): 42, ('Y', 'M'): 6, ('M', 'F'): 9, ('M', 'Y'): 6, ('F', 'Q'): 27, ('F', 'M'): 9, ('C', 'M'): 44}
class Vertex:
def __init__(self, vertex, children):
self.vertex = vertex
self.children = children
def __repr__(self):
return ' '.join('{} -> {}:{}'.format(self.vertex, i, weights.get((self.vertex, i), weights.get((i, self.vertex), None))) for i in self.children)
class Network:
def __init__(self, tree):
self.__full_tree = [Vertex(*i) for i in tree.items()]
def __repr__(self):
return '\n'.join(repr(i) for i in self.__full_tree)
full_tree = Network(tree)
print(full_tree)
</code></pre>
<p>输出:</p>
^{pr2}$
<p>白色它远远不是教科书上的表现,它确实给出了基本思想。如果您正在寻找更专业的图表,请参阅下面@mattmilten的答案提供的链接。在</p>