def iterativeAlgorithmPrima(self): T = nx.Graph() for n in T: T.node[n]=self.node[n].copy() T.graph=self.graph.copy() for u, v, d in nx.prim_mst_edges(self, data=True): T.add_edge(u,v,d) yield T
def map_mst_bipartite(subset, frame_instances): logging.debug('Calculating MST in bipartite graph') graph = nx.Graph() for id_frame1 in subset['left']: for id_frame2 in subset['right']: distance = calculate_distance_measure(frame_instances[id_frame1], frame_instances[id_frame2]) graph.add_edge(id_frame1, id_frame2, weight=distance) return list(nx.prim_mst_edges(graph, data=True))
def map_mst_complete(subset, frame_instances): logging.debug('Calculating MST in completed graph') graph = nx.Graph() size = len(subset) for i in xrange(size): id_frame1 = subset[i] for j in xrange(i + 1, size): id_frame2 = subset[j] distance = calculate_distance_measure(frame_instances[id_frame1], frame_instances[id_frame2]) graph.add_edge(id_frame1, id_frame2, weight=distance) return list(nx.prim_mst_edges(graph, data=True))
def prim_mst(G, weight="weight"): """Return a minimum spanning tree or forest of an undirected weighted graph. A minimum spanning tree is a subgraph of the graph (a tree) with the minimum sum of edge weights. If the graph is not connected a spanning forest is constructed. A spanning forest is a union of the spanning trees for each connected component of the graph. Parameters ---------- G : NetworkX Graph weight : string Edge data key to use for weight (default 'weight'). Returns ------- G : NetworkX Graph A minimum spanning tree or forest. Examples -------- >>> G=nx.cycle_graph(4) >>> G.add_edge(0,3,weight=2) # assign weight 2 to edge 0-3 >>> T=nx.prim_mst(G) >>> print(sorted(T.edges(data=True))) [(0, 1, {}), (1, 2, {}), (2, 3, {})] Notes ----- Uses Prim's algorithm. If the graph edges do not have a weight attribute a default weight of 1 will be used. """ T = nx.Graph(nx.prim_mst_edges(G, weight=weight, data=True)) # Add isolated nodes if len(T) != len(G): T.add_nodes_from([n for n, d in G.degree().items() if d == 0]) # Add node and graph attributes as shallow copy for n in T: T.node[n] = G.node[n].copy() T.graph = G.graph.copy() return T
def prim_mst(G, weight='weight'): """Return a minimum spanning tree or forest of an undirected weighted graph. A minimum spanning tree is a subgraph of the graph (a tree) with the minimum sum of edge weights. If the graph is not connected a spanning forest is constructed. A spanning forest is a union of the spanning trees for each connected component of the graph. Parameters ---------- G : NetworkX Graph weight : string Edge data key to use for weight (default 'weight'). Returns ------- G : NetworkX Graph A minimum spanning tree or forest. Examples -------- >>> G=nx.cycle_graph(4) >>> G.add_edge(0,3,weight=2) # assign weight 2 to edge 0-3 >>> T=nx.prim_mst(G) >>> print(sorted(T.edges(data=True))) [(0, 1, {}), (1, 2, {}), (2, 3, {})] Notes ----- Uses Prim's algorithm. If the graph edges do not have a weight attribute a default weight of 1 will be used. """ T = nx.Graph(nx.prim_mst_edges(G, weight=weight, data=True)) # Add isolated nodes if len(T) != len(G): T.add_nodes_from([n for n, d in G.degree().items() if d == 0]) # Add node and graph attributes as shallow copy for n in T: T.node[n] = G.node[n].copy() T.graph = G.graph.copy() return T
def test_prim_mst_edges(self): edgelist=sorted(nx.prim_mst_edges(self.G)) edgelist=sorted((sorted((u, v))[0], sorted((u, v))[1], d) for u,v,d in edgelist) assert_equal(edgelist,self.tree_edgelist)
def test_prim_mst_edges(self): edgelist = sorted(nx.prim_mst_edges(self.G)) edgelist = sorted( (sorted((u, v))[0], sorted((u, v))[1], d) for u, v, d in edgelist) assert_equal(edgelist, self.tree_edgelist)
for new_dst in g[dst]: # skip edges already queued if not g[dst][new_dst].get('queued'): g[dst][new_dst]['queued'] = True g[new_dst][dst]['queued'] = True heapq.heappush(h, (g[dst][new_dst]['weight'], dst, new_dst)) return mst if __name__ == '__main__': g = utils.read_graph() # edges = run(g) # print '-'*10, 'Your answer', '-'*10 # print 'Edges:', edges # print 'Costs:', sum([e[2]['weight'] for e in edges]) # print # mst = list(nx.prim_mst_edges(g, data=True)) # print '-'*10, 'Model answer', '-'*10 # print 'Edges:', mst # print 'Costs:', sum([e[2]['weight'] for e in mst]) for _ in range(N_TESTS): h = g.copy() edges = run(h) mst = list(nx.prim_mst_edges(h, data=True)) assert(sum([e[2]['weight'] for e in edges]) == sum([e[2]['weight'] for e in mst]))