def connect_subgraphs_by_spanning_trees(self): subgraphs = {} labels = graph_tool.label_components(self.g)[0].a for i in range(len(labels)): label = labels[i] if label in subgraphs: subgraphs[label].append(self.g.vertex(i)) else: subgraphs[label] = [self.g.vertex(i)] vertices_idx_to_connect = [] print(len(subgraphs.keys())) for l, s in tqdm(subgraphs.items()): # print(l) subgraph = graph_tool.GraphView(self.g, vfilt=labels == l, directed=True) tree_edges = graph_tool.min_spanning_tree(subgraph) tree = graph_tool.GraphView(subgraph, efilt=tree_edges, directed=True) sort = graph_tool.topological_sort(tree) vertices_idx_to_connect.append(sort[0]) self.v_root = self.g.add_vertex() for i in vertices_idx_to_connect: self.g.add_edge(self.v_root, i)
def compute(self): """ Get the Browsing Depth Variables Required: * Max Span of Graph Calculation: Browsing Depth (BD) = Edges on Minimum Span Tree (from initial vertex) Get the minimum spanning tree using Prim's algorithm Analysis: More browsing depth means that the requester isn't targeting on a particular resource """ mst = gt.min_spanning_tree( self._session_graph.graph, root=self._session_graph.graph.vertex(0) ) all_edges = mst.get_array() # edge in mst will have a value of 1, # and other edges will have a value of 0 # therefore, sum of edges will give number of edges in mst. # this way parallel edges will also be ignored. edges_in_path = sum(all_edges) bd = edges_in_path self.append_graph_factor('int', bd) print "Browsing Depth : ", bd pass
def greedy(graph, delay, cost, budget): # set number of vertices n = len(graph.get_vertices()) # set upgrades vector upgrade = [0] * n while (True): vu = graph.vertex(0) mincost = cost[vu] for v in graph.vertices(): if (cost[v] <= budget): if (cost[v] < mincost and upgrade[int(v)] == 0): mincost = cost[v] vu = graph.vertex(v) if (mincost > budget): break else: upgrade[int(vu)] = 1 budget -= cost[vu] d = get_delay(graph, delay, upgrade) tree = gt.min_spanning_tree(graph, weights=d) tdelay = sum([a * b for a, b in zip(list(tree), list(d))]) best[0] = tdelay best[1] = upgrade return 0
def random_greedy(graph, delay, cost, budget): # set number of vertices n = len(graph.get_vertices()) # set upgrades vector upgrade = [0] * n while (True): vu = [] for v in graph.vertices(): if (cost[v] <= budget and upgrade[int(v)] == 0): vu += [(v, cost[v])] vu.sort(key=takeSecond) if (len(list(graph.vertices())) >= 3): vu = vu[:3] if (vu): sample = random.sample(vu, 1)[0] else: break vu = sample[0] mincost = sample[1] if (mincost > budget): break else: upgrade[int(vu)] = 1 budget -= cost[vu] d = get_delay(graph, delay, upgrade) tree = gt.min_spanning_tree(graph, weights=d) tdelay = sum([a * b for a, b in zip(list(tree), list(d))]) best[0] = tdelay best[1] = upgrade return 0
def initialize_step(self): tree_prop = gt.min_spanning_tree(self.graph, weights = self.e_weights) tree = gt.GraphView(self.graph, efilt=tree_prop) gt.graph_draw(tree, self.graph_class.position) self.best_tree = tree tree_cost = 0 for e in tree.edges(): tree_cost += self.e_weights[e] t = (1/(2.0*self.graph_class.size)) * tree_cost return t
def comparison(n, pp, pt): g = gt.complete_graph(n) weight = g.new_edge_property("double", random(g.num_edges())) t = gt.min_spanning_tree(g, weights=weight) p = g.new_edge_property('bool') q = permutation(n) for i in range(n-1): p[g.edge(q[i], q[i+1])] = True hp = g.new_edge_property("bool", weight.a < pp) ht = g.new_edge_property("bool", weight.a < pt) draw2(g, hp, p, 'gnp+path-{}.png'.format(pp)) draw2(g, ht, p, 'gnp+path-{}.png'.format(pt)) draw2(g, hp, t, 'gnp+mst-{}.png'.format(pp)) draw2(g, ht, t, 'gnp+mst-{}.png'.format(pt))
def update_edge_weights(self): for v in self.graph.vertices(): v_ind = self.graph.vertex_index[v] subgraph = gt.GraphView(self.graph, efilt=self.neighborhoods[v_ind]) for e in subgraph.edges(): v2 = e.target() self.costs[e] = self.e_weights[e] + self.v_weights[v] + self.v_weights[v2] tree_prop = gt.min_spanning_tree(self.not_zero_graph, weights=self.costs) (s1, t1), (s2, t2) = self.least_edges() e1 = self.graph.edge(s1,t1) e2 = self.graph.edge(s2,t2) tree_prop[e1] = True tree_prop[e2] = True return tree_prop
def brute_force(graph, delay, cost, budget, upgrade, i, pcost): global best if (i == len(list(graph.vertices()))): d = get_delay(graph, delay, upgrade) tree = gt.min_spanning_tree(graph, weights=d) pdelay = sum([a * b for a, b in zip(list(tree), list(d))]) if (pdelay < best[0] and pcost <= budget): best[0] = pdelay best[1] = upgrade else: upgrade2 = upgrade.copy() upgrade[i] = 0 brute_force(graph, delay, cost, budget, upgrade, i + 1, pcost) upgrade2[i] = 1 brute_force(graph, delay, cost, budget, upgrade2, i + 1, pcost + cost[i]) return 0
def create_mst(words, keyword): size = len(words) parses = [parse(w, keyword) for w in words] graph = gt.Graph(directed=False) vertices = list(graph.add_vertex(size)) words_vp = graph.new_vertex_property('string') for v, w in itt.izip(vertices, words): words_vp[v] = w.encode('utf8') graph.vp['words'] = words_vp root = vertices[words.index(keyword)] weights_ep = graph.new_edge_property('double') vp1 = itt.izip(vertices, parses) for i, (u, pu) in enumerate(vp1): vp2 = itt.islice(itt.izip(vertices, parses), i + 1, size) for j, (v, pv) in enumerate(vp2): e = graph.add_edge(u, v) weights_ep[e] = calc_weight(pu, pv, i, i + j + 1, size) graph.ep['weights'] = weights_ep mst = gt.min_spanning_tree(graph, root=root, weights=weights_ep) graph.set_edge_filter(mst) return (graph, root)
def connected_random(n, p): g = gt.complete_graph(n) weight = g.new_edge_property("double", random(g.num_edges())) t = gt.GraphView(g, efilt=gt.min_spanning_tree(g, weights=weight)) h = gt.GraphView(g, efilt=g.new_edge_property("bool", weight.a < p)) return graph_union(h, t)
def min_spanning_tree(g, pos): weight = g.new_edge_property("double") for e in g.edges(): weight[e] = linalg.norm(pos[e.target()].a - pos[e.source()].a) return gt.min_spanning_tree(g, weights=weight)
bran_deg_avg, bran_deg_std = gt.vertex_average(g_bran, deg="total") #Centrality vp_btwn_link, ep_btwn_link = gt.betweenness(g_link) link_btwn = [vp_btwn_link[v] for v in g_link.vertices()] vp_btwn_bran, ep_btwn_bran = gt.betweenness(g_bran) bran_btwn = [vp_btwn_bran[v] for v in g_bran.vertices()] link_btwn_avg = stats.mean(link_btwn) link_btwn_std = stats.stdev(link_btwn) bran_btwn_avg = stats.mean(bran_btwn) bran_btwn_std = stats.stdev(bran_btwn) #Cost and efficiency link_mst = gt.min_spanning_tree(g_link) bran_mst = gt.min_spanning_tree(g_bran) link_shortest = [x for vector in gt.shortest_distance(g_link) for x in vector] bran_shortest = [x for vector in gt.shortest_distance(g_bran) for x in vector] g_link.set_edge_filter(link_mst) g_bran.set_edge_filter(bran_mst) link_mst_shortest = [x for vector in gt.shortest_distance(g_link) for x in vector] bran_mst_shortest = [x for vector in gt.shortest_distance(g_bran) for x in vector] def efficiency(graph): all_shortest_paths = gt.shortest_distance(g_link) N = graph.num_vertices() path_list = [x for vector in all_shortest_paths for x in vector] inverse_sum = sum([1/x if x > 0 else x for x in path_list]) efficiency = 1/(N*(N-1))*inverse_sum
def make_min_span_tree(self): tree = gt.min_spanning_tree(self.g) self.g.set_edge_filter(tree)
def initialize_degree(self): tree_prop = gt.min_spanning_tree(self.graph, self.e_weights) tree = gt.GraphView(self.graph, efilt=tree_prop) degree = tree.degree_property_map("total") return degree
def view(self,filename,degree): u = gt.GraphView(self.graph, vfilt=lambda v: v.out_degree() > degree) tree = gt.min_spanning_tree(u) u = gt.GraphView(u, efilt=tree) gt.graph_draw(u, output=filename)