def apply_to_graph(fun, G = None):
"""
Applies given algorithm to random geometric graph and displays the results side by side
:param fun: A function which has the signature f(G) and returns iterator of edges of graph G
:param G: a networkx Graph. If None, random geometric graph is created and applied
:return: Plot showing G and fun(G)
"""
if G is None:
G = nx.random_geometric_graph(100, .125)
# position is stored as node attribute data for random_geometric_graph
pos = nx.get_node_attributes(G, 'pos')
nodesize = 80
for u, v in G.edges():
G.edge[u][v]['weight'] = ((G.node[v]['pos'][0] - G.node[u]['pos'][0]) ** 2 +
(G.node[v]['pos'][1] - G.node[u]['pos'][1]) ** 2) ** .5
else:
pos = graphviz_layout(G)
nodesize = 200
# find node near center (0.5,0.5)
color = {}
dmin = 1
ncenter = 0
for n in pos:
x, y = pos[n]
d = (x - 0.5) ** 2 + (y - 0.5) ** 2
color[n] = d
if d < dmin:
ncenter = n
dmin = d
res = nx.Graph(list(fun(G)))
plt.figure(figsize=(10, 8))
plt.suptitle(fun.__name__ + " algorithm application")
plt.subplot(1, 2, 1)
plt.title("Original Graph G")
nx.draw_networkx_edges(G, pos, nodelist=[ncenter], alpha=0.4)
nx.draw_networkx_nodes(G, pos,
nodelist=color.keys(),
node_size=nodesize,
node_color=list(color.values()),
cmap=plt.get_cmap("Reds_r")
).set_edgecolor('k')
if G is not None:
nx.draw_networkx_labels(G,pos)
nx.draw_networkx_edge_labels(G,pos,
edge_labels=nx.get_edge_attributes(G,'weight'))
plt.axis('off')
plt.subplot(1, 2, 2)
plt.title("Resultant Graph, R = {0}(G)".format(fun.__name__))
nx.draw_networkx_edges(res, pos, nodelist=[ncenter], alpha=0.4)
nx.draw_networkx_nodes(res, pos,
node_color=list(color[n] for n in res.nodes()),
node_size=nodesize,
cmap=plt.get_cmap("Greens_r")).set_edgecolor('k')
if G is not None:
nx.draw_networkx_labels(res,pos)
nx.draw_networkx_edge_labels(res, pos,
edge_labels=nx.get_edge_attributes(res, 'weight'))
plt.axis('off')
plt.show()
评论列表
文章目录