我正在使用networkx并尝试在图表中查找周期。 我写了以下代码:
import networkx as nx
import matplotlib.pyplot as plt
import pylab
tg = nx.Graph()
h_lat_dir = {1: [("A", "A"), ("B", "B"), ("A", "B")], 2: [("C", "D")],
3: [("C", "F")], 4: [("F", "F")], 5: [("C", "C"), ("C", "E"), ("D", "E"), ("E", "E")],
6: [("D", "D")]}
for wght, edgelist in h_lat_dir.iteritems():
tg.add_edges_from(edgelist, weight=wght)
print nx.cycle_basis(tg)
nx.write_dot(tg, 'multi.dot')
nx.draw_graphviz(tg)
pylab.show()
结果是
[['A'], ['B'], ['C'], ['F'], ['E', 'D', 'C'], ['D'], ['E']]
和这张图
为什么我看不到self_loops? (每个顶点都有一个) 是否有可能以某种方式绘制它们?
答案 0 :(得分:1)
使用Graphviz的NetworkX接口(通过pygraphviz或pydot):
import networkx as nx
import matplotlib.pyplot as plt
G = nx.Graph()
G.add_edges_from([(0,1), (0,2), (1,1), (1,2)])
nx.write_dot(G,'graph.dot')
然后运行
dot -Tpng graph.dot > graph.png