def as_graph(self) -> Graph: # pragma: no cover
"""Returns a :class:`graphviz.Graph` representation of this bipartite graph."""
if Graph is None:
raise ImportError('The graphviz package is required to draw the graph.')
graph = Graph()
nodes_left = {} # type: Dict[TLeft, str]
nodes_right = {} # type: Dict[TRight, str]
node_id = 0
for (left, right), value in self._edges.items():
if left not in nodes_left:
name = 'node{:d}'.format(node_id)
nodes_left[left] = name
graph.node(name, label=str(left))
node_id += 1
if right not in nodes_right:
name = 'node{:d}'.format(node_id)
nodes_right[right] = name
graph.node(name, label=str(right))
node_id += 1
edge_label = value is not True and str(value) or ''
graph.edge(nodes_left[left], nodes_right[right], edge_label)
return graph
评论列表
文章目录