def calculateDifferenceInRealGraphWeights(fileName): graph = loadGraphWithWeight(fileName) dualGraph = getGraphDual(graph) writeDualGraph(dualGraph, inputGraphFile) loadGraph() vertexCover = min_weighted_vertex_cover(dualGraph) writeVertexCover(vertexCover, inputGraphFile) edgeIdToWeightMap = map(lambda e: (getEdgeId(e[:2]), e[2]['w']), graph.edges_iter(data=True)) weakEdges = [] strongEdges = [] for e in graph.edges_iter(data=True): edgeId = getEdgeId(e[:2]) weight = e[2]['w'] if edgeId in vertexCover: weakEdges.append(weight) else: strongEdges.append(weight) numElements = -1 if len(weakEdges) > len(strongEdges): numElements = len(strongEdges) else: numElements = len(weakEdges) print np.mean(weakEdges) print np.mean(strongEdges) print pearsonr(random.sample(weakEdges, numElements), random.sample(strongEdges, numElements))
def greedy_cnp(G, k): S0 = vertex_cover.min_weighted_vertex_cover(G) while len(S0) > k: B = objective_function.minimize_pairwise_connectivity(G, S0) i = random.choice(B) S0.discard(i) return S0
def find_mds_two_max_count(adj_matrix, nb_iters, rnds, add_loops_in_middle=False, exact=False): out_degrees = adj_matrix.sum(axis=1).A1 weights = out_degrees + rnds neigh_weights = adj_matrix.multiply(np.transpose([weights])) max_dominate_neighs = neigh_weights.argmax(axis=0).A1 for it in range(nb_iters + 1): max_idxs, counts = np.unique(max_dominate_neighs, return_counts=True) weights = np.zeros_like(weights) for i in range(len(max_idxs)): weights[max_idxs[i]] = counts[i] weights = weights + rnds neigh_weights = adj_matrix.multiply(np.transpose([weights])) max_dominate_neighs = neigh_weights.argmax(axis=0).A1 if it == nb_iters - 1 and add_loops_in_middle: adj_matrix = add_loops(adj_matrix) neigh_weights = neigh_weights.tocsr() neigh_weights[max_dominate_neighs, np.arange(adj_matrix.shape[0])] = -1 second_max_dominate_neighs = neigh_weights.argmax(axis=0).A1 for i in range(adj_matrix.shape[0]): if neigh_weights[second_max_dominate_neighs[i], i] < 1.0: second_max_dominate_neighs[i] = max_dominate_neighs[i] rows = np.append(second_max_dominate_neighs, max_dominate_neighs) cols = np.append(max_dominate_neighs, second_max_dominate_neighs) rows, cols = zip(*set(zip(rows, cols))) new_adj_matrix = csr_matrix((np.ones(len(rows)), (rows, cols)), shape=adj_matrix.get_shape()) vertex_cover = new_adj_matrix.diagonal().nonzero()[0].tolist() nonzero_in_rows = new_adj_matrix[np.array(vertex_cover), :].nonzero() new_adj_matrix[np.array(vertex_cover)[nonzero_in_rows[0]], nonzero_in_rows[1]] = 0 nonzero_in_columns = new_adj_matrix[:, np.array(vertex_cover)].nonzero() new_adj_matrix[nonzero_in_columns[0], np.array(vertex_cover)[nonzero_in_columns[1]]] = 0 new_adj_matrix.eliminate_zeros() if exact: vertex_cover += find_min_vertex_cover(new_adj_matrix.A)[1] else: new_graph = nx.from_scipy_sparse_matrix(new_adj_matrix) vertex_cover += list(min_weighted_vertex_cover(new_graph)) in_degrees = adj_matrix.sum(axis=0).A1 return vertex_cover + np.where(in_degrees == 0)[0].tolist()
def create_graph(reviews_file, graph_file): print('%s: start' % time.strftime("%Y/%d/%m-%H:%M:%S")) with open(reviews_file, 'rb') as read_file: reviews = pickle.load(read_file) reviews = reviews[:13] print('num reviews: %d' % len(reviews)) print('%s: loaded reviews' % time.strftime("%Y/%d/%m-%H:%M:%S")) all_nouns = list(get_all_nouns(reviews)) print('num nouns: %d' % len(all_nouns)) print('%s: obtained nouns' % time.strftime("%Y/%d/%m-%H:%M:%S")) all_senses = generate_all_senses(reviews) total_possible_vertices = scipy.misc.comb(len(all_senses), 2, exact=True) print('num senses: %d' % len(all_senses)) print('total possible senses: %d' % total_possible_vertices) print('%s: obtained senses' % time.strftime("%Y/%d/%m-%H:%M:%S")) graph = networkx.Graph() for sense in all_senses: graph.add_node(sense.name()) print('%s: created graph' % time.strftime("%Y/%d/%m-%H:%M:%S")) print('num nodes: %d' % len(graph.nodes())) cycle = 0 for synset1, synset2 in itertools.combinations(all_senses, 2): cycle += 1 if not cycle % 100000: print('sense cycle: %d/%d\r' % (cycle, total_possible_vertices)), if synset1.wup_similarity(synset2) >= 0.7: graph.add_edge(synset1.name(), synset2.name()) print('%s: added vertices' % time.strftime("%Y/%d/%m-%H:%M:%S")) print('num edges: %d' % len(graph.edges())) with open(graph_file, 'wb') as write_file: pickle.dump(graph, write_file, pickle.HIGHEST_PROTOCOL) with open(graph_file, 'rb') as read_file: graph = pickle.load(read_file) print('num nodes: %d' % len(graph.nodes())) print('num edges: %d' % len(graph.edges())) my_dominating_set = dominating_set.min_weighted_dominating_set(graph) print('%s found dominating set' % time.strftime("%Y/%d/%m-%H:%M:%S")) print('dominating set length: %d' % len(my_dominating_set)) my_vertex_cover = vertex_cover.min_weighted_vertex_cover(graph) print('%s found vertex cover' % time.strftime("%Y/%d/%m-%H:%M:%S")) print('vertex cover length: %d' % len(my_vertex_cover))
def approx_ap(X, y, eps, sep_measure, nn_solver="exact", solver_kwargs=None, kmeans_clusters: int = 1): if isinstance(X, csr_matrix): X = X.toarray() ori_shape = list(X.shape) X = X.reshape((len(X), -1)) if kmeans_clusters > 1: kmeans = KMeans(n_clusters=kmeans_clusters, n_jobs=10).fit(X) kmeans_ind = kmeans.labels_ else: kmeans_ind = np.zeros_like(y) final_idx = [] inv_indexs = [] for i in range(kmeans_clusters): inv_indexs.append(np.where(kmeans_ind == i)[0]) tempX = X[inv_indexs[-1]] print(f"solving {tempX.shape}") if nn_solver == "exact": nn = NearestNeighbors(n_jobs=-1, p=sep_measure).fit(tempX) ind = nn.radius_neighbors(tempX, radius=eps, return_distance=False) #elif nn_solver == "faiss": # index = faiss.IndexLSH(d, solver_kwargs['n_bits']) # index.add(X) # _, ind = self.index.search(X, ) else: raise ValueError(f"Not supported nn_solver, {nn_solver}") G = nx.Graph() G.add_nodes_from(list(range(len(tempX)))) for i in range(len(tempX)): for j in ind[i]: if y[i] != y[j]: G.add_edge(i, j) idx = min_weighted_vertex_cover(G) idx = list(set(range(len(tempX))) - idx) final_idx += list(inv_indexs[-1][idx]) X = X.reshape([len(X)] + ori_shape[1:]) return X[final_idx], y[final_idx]
def approx_ap(X, y, eps, sep_measure): ori_shape = list(X.shape) X = X.reshape((len(X), -1)) nn = NearestNeighbors(n_jobs=-1, p=sep_measure).fit(X) ind = nn.radius_neighbors(X, radius=eps, return_distance=False) G = nx.Graph() G.add_nodes_from(list(range(len(X)))) for i in range(len(X)): for j in ind[i]: if y[i] != y[j]: G.add_edge(i, j) idx = min_weighted_vertex_cover(G) idx = list(set(range(len(X))) - idx) X = X.reshape([len(X)] + ori_shape[1:]) return X[idx], y[idx]
def second_approx_dominating_set(graph, rnds=None): edges, _ = extract_max_new_degree(graph, 2, rnds) new_graph = [] for i in range(len(graph)): new_graph.append([]) for j in range(len(graph)): new_graph[i].append(0) for i, edge in enumerate(edges): new_graph[edge[0]][edge[1]] = 1 new_graph[edge[1]][edge[0]] = 1 new_graph = create_networkx_graph(new_graph) vertext_cover = list(min_weighted_vertex_cover(new_graph)) return vertext_cover
def printLabeledEdges(fileName): graph = loadGraphWithWeight(fileName) dualGraph = loadGraph(fileName+'_dual') # writeDualGraph(dualGraph, inputGraphFile) vertexCover = min_weighted_vertex_cover(dualGraph) writeVertexCover(vertexCover, inputGraphFile) edgeIdToWeightMap = map(lambda e: (getEdgeId(e[:2]), e[2]['w']), graph.edges_iter(data=True)) weakEdges = [] strongEdges = [] for e in graph.edges_iter(data=True): edgeId = getEdgeId(e[:2]) weight = e[2]['w'] if edgeId in vertexCover: print edgeId.replace('_', ' '), '0' else: print edgeId.replace('_', ' '), '1'
def find_mds_max_count_seprate_neigh(adj_matrix, nb_iters, rnds): out_degrees = adj_matrix.sum(axis=1).A1 weights = out_degrees + rnds neigh_weights = adj_matrix.multiply(np.transpose([weights])) max_dominate_neighs = neigh_weights.argmax(axis=0).A1 for iter in range(nb_iters + 1): max_idxs, counts = np.unique(max_dominate_neighs, return_counts=True) weights = np.zeros_like(weights) for i in range(len(max_idxs)): weights[max_idxs[i]] = counts[i] weights = weights + rnds neigh_weights = adj_matrix.multiply(np.transpose([weights])) max_dominate_neighs = neigh_weights.argmax(axis=0).A1 common_neigh_count = adj_matrix.dot(adj_matrix.T) common_neigh_count = common_neigh_count[max_dominate_neighs, :] seprate_neighs = adj_matrix.multiply(np.transpose([out_degrees]) + 1).T - common_neigh_count seprate_neighs = seprate_neighs.argmax(axis=1).A1 rows = np.append(seprate_neighs, max_dominate_neighs) cols = np.append(max_dominate_neighs, seprate_neighs) rows, cols = zip(*set(zip(rows, cols))) new_adj_matrix = csr_matrix((np.ones(len(rows)), (rows, cols)), shape=adj_matrix.get_shape()) vertex_cover = new_adj_matrix.diagonal().nonzero()[0].tolist() nonzero_in_rows = new_adj_matrix[np.array(vertex_cover), :].nonzero() new_adj_matrix[np.array(vertex_cover)[nonzero_in_rows[0]], nonzero_in_rows[1]] = 0 nonzero_in_columns = new_adj_matrix[:, np.array(vertex_cover)].nonzero() new_adj_matrix[nonzero_in_columns[0], np.array(vertex_cover)[nonzero_in_columns[1]]] = 0 new_adj_matrix.eliminate_zeros() new_graph = nx.from_scipy_sparse_matrix(new_adj_matrix) vertex_cover += list(min_weighted_vertex_cover(new_graph)) return vertex_cover
def remove_cryptic_relations(relations_list, relations_dot=None, relations_fig=None, threshold=None, verbose=False): """"Returns minimum set of individuals to remove for set of cryptic relations""" # construct a graph of relations from given iterable of tuples of node ids, # then remove individuals corresponding to exact minimum vertex cover, # which has exponential-time complexity, but most subgraphs are small # unless size of subgraph is greater than the argument 'threshold', # then remove individuals corresponding to approximate minimum vertex cover, # which has linear-time complexity, and guarantees a factor-2 approximation # can handle loop edges, for inbred individuals, where haplotypes are related G = nx.Graph() for i, j in relations_list: G.add_edge(i, j) # iterate over each connected component removed_list = set() if verbose: print 'Removing cryptic relations...' for g in list(connected_component_subgraphs(G)): if verbose: print 'Size of current connected component is ' + str(len(g)) # approximate solution if threshold is not None and g.number_of_nodes() >= threshold: for i in min_weighted_vertex_cover(g): removed_list.add(i) # exact solution to mvc else: for i in exact_min_vertex_cover(g): removed_list.add(i) # color nodes that were removed for i in removed_list: G.node[i]['color'] = 'red' # save dot file and pdf file of graph if relations_dot is not None and relations_fig is not None: write_dot(G, relations_dot) call('dot -Tpdf {0} -o {1}'.format(relations_dot, relations_fig), shell=True) return removed_list
def third_approx_dominating_set(graph, repeat=[1], rnds=None): neigh_graph, probs = extract_max_new_degree(graph, rnds=rnds) vertext_covers = [] new_probs = normalize(probs) for r in repeat: best_vertext_cover_len = np.Inf best_vertext_cover = None for k in range(r): edges1, new_i, new_p = choose_random( np.array(new_probs).T, np.array(neigh_graph).T) new_p = np.array(normalize(new_p)) edges2, _, _ = choose_random(new_p.T, new_i.T) new_graph = [] for i in range(len(graph)): new_graph.append([]) for j in range(len(graph)): new_graph[i].append(0) for i in range(len(edges1)): new_graph[edges1[i]][edges2[i]] = 1 new_graph[edges2[i]][edges1[i]] = 1 new_graph = create_networkx_graph(new_graph) vertext_cover = list(min_weighted_vertex_cover(new_graph)) if len(vertext_cover) < best_vertext_cover_len: best_vertext_cover_len = len(vertext_cover) best_vertext_cover = vertext_cover vertext_covers.append(best_vertext_cover) return vertext_covers
def forth_approx_dominating_set(graph, rnds=None): dom_len, dom_set = find_approx_min_dominating_set(graph, totally=True) dom_sub_graph = [] for i in range(len(graph)): dom_sub_graph.append([]) for j in range(len(graph)): dom_sub_graph[i].append(0) for v in dom_set: for i in range(len(graph)): if graph[i][v]: dom_sub_graph[i][v] = 1 dom_sub_graph[v][i] = 1 dominants, _ = extract_max_new_degree(dom_sub_graph, 1, rnds=rnds) max_neighs, _ = extract_max_new_degree(graph, 2, rnds=rnds) new_graph = [] for i in range(len(graph)): new_graph.append([]) for j in range(len(graph)): new_graph[i].append(0) for i in range(len(graph)): neigh1 = dominants[i][0] neigh2 = max_neighs[i][0] if neigh1 == neigh2: neigh2 = max_neighs[i][1] new_graph[neigh1][neigh2] = 1 new_graph[neigh2][neigh1] = 1 new_graph = create_networkx_graph(new_graph) vertext_cover = list(min_weighted_vertex_cover(new_graph)) return vertext_cover
def prune_graph_min_weight_vc(G): vc = min_weighted_vertex_cover(G) return G.subgraph(min_weighted_vertex_cover(G))
def __call__(self, graph, **kwargs): cover = min_weighted_vertex_cover(graph) return set(graph.nodes) - set(cover)
print() # e) Maximum clique print("e) Q =", clique.max_clique(G)) print() # f) Maximum stable set print("f) S =", independent_set.maximum_independent_set(G)) print() # g) Maximum matching print("g) M =", nx.max_weight_matching(G)) print() # h) Minimum vertex cover print("h) R =", vertex_cover.min_weighted_vertex_cover(G)) print() # i) Minimum edge cover print("i) F =", covering.min_edge_cover(G)) print() # j) Shortest closed path (circuit) that visits every vertex print("j) W =", end=" ") for i in range(len(tournament.hamiltonian_path(D))): print(tournament.hamiltonian_path(D)[i], "-", end=" ") print(tournament.hamiltonian_path(D)[0]) print() # k) Shortest closed path (circuit) that visits every edge H = nx.eulerize(G)
if __name__ == '__main__': G = nx.Graph() for line in sys.stdin: line = line.replace('\n', '') line = line.replace('\r', '') if line.startswith('#'): continue para = line.split('|') if len(para) != 3: continue node1 = int(para[0]) node2 = int(para[1]) if G.has_node(node1) == False: G.add_node(node1) if G.has_node(node2) == False: G.add_node(node2) G.add_edge(node1, node2) nodes = G.nodes() vc = vertex_cover.min_weighted_vertex_cover(G) is_vc = 0 for node in sorted(nodes): if node in vc: is_vc = 1 else: is_vc = 0 print "%d, %d" % (node, is_vc)
if __name__ == '__main__': G = nx.Graph() for line in sys.stdin: line = line.replace('\n', '') line = line.replace('\r', '') if line.startswith('#'): continue para = line.split('|') if len(para) != 3: continue node1 = int(para[0]) node2 = int(para[1]) if G.has_node(node1)==False: G.add_node(node1) if G.has_node(node2)==False: G.add_node(node2) G.add_edge(node1, node2) nodes = G.nodes() vc = vertex_cover.min_weighted_vertex_cover(G) is_vc = 0 for node in sorted(nodes): if node in vc: is_vc = 1 else: is_vc = 0 print "%d, %d" % (node, is_vc)