def _build_path_dict(self): """ Construct dictionaries of dictionaries containing shortest paths from one format to another. """ from networkx import DiGraph, all_pairs_shortest_path # all_pairs_shortest_path yields pairs (n, d) where # * n is a node # * d is a dict where # * keys are other nodes # * values are shortest paths (i.e. lists of nodes) # from n to these nodes # Converting this to dict results in a dict of dicts where # * the first key is the source node # * the second key is the destination node # * the value is a shortest path between these nodes. # self._path_dict[in_fmt][out_fmt] = [in_fmt, ..., out_fmt] self._path_dict = dict( all_pairs_shortest_path( # Directed graph of available in_fmt -> out_fmt conversions DiGraph(self.get_conversions()))) # self._path_dict_ext[in_ext][out_ext] = [in_ext, ..., out_ext] self._path_dict_ext = dict( all_pairs_shortest_path( # Directed graph of available in_ext -> out_ext conversions DiGraph(self.get_conversions_from_ext())))
def next_move(self, who): all_paths = nx.all_pairs_shortest_path(who.graph.clear_networkx_graph(self.cannottouch)) all_paths2 = nx.all_pairs_shortest_path(who.graph.networkx_graph()) temp = [] for i in self.graph.graph[who.position]: if i in all_paths: temp.append(i) if not temp == []: return random.choice(temp)
def _build_path_dict(self): """ Constructs a dictionary containing shortest paths from one format to another. """ from networkx import DiGraph, all_pairs_shortest_path self._path_dict = dict( all_pairs_shortest_path(DiGraph(self.get_conversions()))) self._path_dict_ext = dict( all_pairs_shortest_path(DiGraph(self.get_conversions_from_ext())))
def shortest_path_policy(graph1, graph2): # Take two graphs as arguments, one with all the switches and one # with some removed. For each host, flip a coin to decide if it # will be routed using the full or partial graph. # Because hosts are single-homed, we can save a lot of computation # by computing shortest paths on the subgraph of switches instead # the full graph of switches and hosts raw = defaultdict(lambda: []) sub_graph1 = graph1.copy() sub_graph1.remove_nodes_from(sub_graph1.hosts()) sub_graph2 = graph2.copy() sub_graph2.remove_nodes_from(sub_graph2.hosts()) paths1 = nx.all_pairs_shortest_path(sub_graph1) paths2 = nx.all_pairs_shortest_path(sub_graph2) hosts = graph1.hosts() for src in hosts: for dst in hosts: if src != dst: if hash(src + dst) % 5: paths = paths1 graph = graph1 else: paths = paths2 graph = graph2 # Grab hosts' switches try: if len(graph1.neighbors(src)) > 1: log.debug( str(src) + " nbrs: " + str(graph1.neighbors(src))) src_sw = graph1.neighbors(src)[0] except IndexError: # log.debug("Index error! src host " + str(src)) break try: dst_sw = graph1.neighbors(dst)[0] except IndexError: # log.debug("Index error! dst host " + str(dst)) continue try: path = [src] + paths[src_sw][dst_sw] + [dst] except KeyError: # log.debug("Key error! switches " + str(src_sw) + " and " + str(dst_sw)) continue last = path.pop(0) curr = path.pop(0) for next in path: inport = graph1.node[curr]['ports'][last] outport = graph1.node[curr]['ports'][next] pat = { IN_PORT:inport, DL_TYPE:0x800, \ NW_SRC:ip(src), NW_DST:ip(dst) } acts = [forward(outport)] raw[curr].append((pat, acts)) last = curr curr = next return policy_of_dict(raw)
def shortest_path_policy(graph1, graph2): # Take two graphs as arguments, one with all the switches and one # with some removed. For each host, flip a coin to decide if it # will be routed using the full or partial graph. # Because hosts are single-homed, we can save a lot of computation # by computing shortest paths on the subgraph of switches instead # the full graph of switches and hosts raw = defaultdict(lambda:[]) sub_graph1 = graph1.copy() sub_graph1.remove_nodes_from(sub_graph1.hosts()) sub_graph2 = graph2.copy() sub_graph2.remove_nodes_from(sub_graph2.hosts()) paths1 = nx.all_pairs_shortest_path(sub_graph1) paths2 = nx.all_pairs_shortest_path(sub_graph2) hosts = graph1.hosts() for src in hosts: for dst in hosts: if src != dst: if hash(src + dst) % 5: paths = paths1 graph = graph1 else: paths = paths2 graph = graph2 # Grab hosts' switches try: if len(graph1.neighbors(src)) > 1: log.debug(str(src) + " nbrs: " + str(graph1.neighbors(src))) src_sw = graph1.neighbors(src)[0] except IndexError: # log.debug("Index error! src host " + str(src)) break try: dst_sw = graph1.neighbors(dst)[0] except IndexError: # log.debug("Index error! dst host " + str(dst)) continue try: path = [src] + paths[src_sw][dst_sw] + [dst] except KeyError: # log.debug("Key error! switches " + str(src_sw) + " and " + str(dst_sw)) continue last = path.pop(0) curr = path.pop(0) for next in path: inport = graph1.node[curr]['ports'][last] outport = graph1.node[curr]['ports'][next] pat = { IN_PORT:inport, DL_TYPE:0x800, \ NW_SRC:ip(src), NW_DST:ip(dst) } acts = [forward(outport)] raw[curr].append((pat,acts)) last = curr curr = next return policy_of_dict(raw)
def add_switch(self, dpid, stats): if not dpid in self.topology.nodes(): self.topology.add_node(dpid) self.topology.node[dpid]['rules'] = {} self.topology.node[dpid]['nActives'] = 0 if not self.__setting_up__: self.paths_dict = nx.all_pairs_shortest_path(self.topology) return
def _Neighbors2(self): path = dict(nx.all_pairs_shortest_path(self.__graph)) rootNode = DEFAULT_ROOT_NODE if rootNode not in path.keys(): self.__logger.error("rootNode is not keys") return sum = 0 for node in self.__graph.nodes: nodeList = path[rootNode].get(node) if nodeList is None: self.__logger.error("there is no path {}---->{}".format(rootNode, node)) sum += 1 nodeList = self.FindLongestPath(node) graph = nx.DiGraph() preNode = None for subNode in nodeList: if preNode is None: preNode = subNode continue else: graph.add_edge(preNode, subNode) preNode = subNode self.__nodeNeighbors[node] = graph self.__logger.error("sum number of no path from root is {}".format(sum))
def solve(list_of_locations, list_of_homes, starting_car_location, adjacency_matrix, params=[]): """ Write your algorithm here. Input: list_of_locations: A list of locations such that node i of the graph corresponds to name at index i of the list list_of_homes: A list of homes starting_car_location: The name of the starting location for the car adjacency_matrix: The adjacency matrix from the input file Output: A list of locations representing the car path A dictionary mapping drop-off location to a list of homes of TAs that got off at that particular location NOTE: both outputs should be in terms of indices not the names of the locations themselves """ homes = list_of_homes start = starting_car_location V = len(list_of_locations) G = nx.Graph() G.add_nodes_from(list_of_locations) for i in range(V): for j in range(i+1, V): if adjacency_matrix[i][j] != "x": G.add_edge(list_of_locations[i], list_of_locations[j], weight=float(adjacency_matrix[i][j])) shortest_paths = dict(nx.all_pairs_shortest_path(G)) for i in range(V): for j in range(i+1, V): if adjacency_matrix[i][j] == "x": G.add_edge(list_of_locations[i], list_of_locations[j], weight=float(nx.shortest_path_length(G, list_of_locations[i], list_of_locations[j], 'weight'))) return clusterSolve(G, homes, start, shortest_paths, adjacency_matrix, list_of_locations)
def ShortestPathSimilarity(self, X): """ Creates shortest path similarity for the Deep Kernel :param X: List of nx graphs """ vocabulary = set() prob_map = {} corpus = [] for gidx, graph in enumerate(X): prob_map[gidx] = {} # label of each node label_map = list(nx.get_node_attributes(graph,'label').values()) # get all pairs shortest paths all_shortest_paths = nx.all_pairs_shortest_path(graph) # nx.floyd_warshall(G) # traverse all paths and subpaths tmp_corpus = [] # source is node we are going from # sink is node that we are walking to for source, sink_map in all_shortest_paths: for sink, path in sink_map.items(): sp_length = len(path)-1 label = "_".join(map(str, sorted([label_map[source],label_map[sink]]))) + "_" + str(sp_length) tmp_corpus.append(label) prob_map[gidx][label] = prob_map[gidx].get(label, 0) + 1 vocabulary.add(label) corpus.append(tmp_corpus) # Normalize frequency prob_map = {gidx: {path: count/float(sum(paths.values())) for path, count in paths.items()} for gidx, paths in prob_map.items()} return prob_map, vocabulary, corpus
def getOwnerCentrality(self, graph): """ compute the network centrality of all the nodes owned by a person with respect to all shortest paths beteween nodes""" personCent = defaultdict(int) ownerNodes, nodeOwner = self.get_owner_distribution(graph) allp = nx.all_pairs_shortest_path(graph) counter = 0 for s in allp: for d in allp[s]: counter += 1 matched_people = [] for node in allp[s][d]: if nodeOwner[node] not in matched_people: personCent[nodeOwner[node]] += 1 matched_people.append(nodeOwner[node]) print "# owner".rjust(long_align_space), ",",\ "owner centrality".rjust(long_align_space) for (p, c) in sorted(personCent.items(), key=lambda x: -x[1]): print p.rjust(long_align_space), ",",\ str(c*1.0/counter).rjust(long_align_space) print "" print "" return personCent, counter
def idea1(G, dom_set): """ Construct new complete graph containing the sp between every node """ all_sp_length = dict(nx.all_pairs_shortest_path_length(G)) all_sp_path = dict(nx.all_pairs_shortest_path(G)) complete = nx.complete_graph(G.number_of_nodes()) for i in all_sp_length: for j in all_sp_length[0]: if i != j: sp_length = all_sp_length[i][j] complete[i][j]['weight'] = sp_length min_tree = mwrc_approx(complete) used = set() for edge in min_tree.edges(): path = all_sp_path[edge[0]][edge[1]] prev = None curr = path[0] for node in path[1:]: prev = curr curr = node used.add((prev, curr)) pruned_tree = G.edge_subgraph(list(used)) if dom_set == 'prune': return _prune_degree_1(pruned_tree)
def __init__(self, dataset): self.graph = nx.Graph() edges = self._read_edges(dataset) self.graph.add_edges_from(edges) self.edges = list(self.graph.edges()) self.nodes = list(self.graph.nodes()) self.paths = dict(nx.all_pairs_shortest_path(self.graph))
def pred_to_mask(pred_regression): BF =(pred_regression >= thred)*1.0 maskBW = BF for ii in range(1): BE = np.reshape(BF[ii,:,:,:], pred_regression.shape[1:3]) skeleton = bwmorph_thin(BE, n_iter = math.inf) index = np.where(skeleton>0.5) if index[0].shape != (0,): sizeA= index[0].shape[0] # print(sizeA) A = np.zeros((sizeA,sizeA)) for i in range(sizeA-1): for j in range(i,sizeA): A[i,j]=np.linalg.norm(np.array([index[0][i],index[1][i]]) - np.array([index[0][j],index[1][j]])) A[j,i]=A[i,j] G=nx.Graph(A) Tcsr = nx.minimum_spanning_tree(G) legth = np.zeros((sizeA,sizeA)) path=dict(nx.all_pairs_shortest_path(Tcsr)) for i in range(sizeA-1): for j in range(i+1, sizeA): legth[i,j] = len(path[i][j]) index2= np.where(legth==np.amax(legth)) pp = path[index2[0][0]][index2[1][0]] maskBW[ii,:,:,0] = poly2mask(index[0][pp[0:len(pp)]+pp[0:1]],index[1][pp[0:len(pp)]+pp[0:1]],[321,321]) return maskBW.astype("float32")
def __init__(self, view, controller, intra_routing, inter_routing='LCE', **kwargs): """Constructor Parameters ---------- view : NetworkView An instance of the network view controller : NetworkController An instance of the network controller intra_routing : str Intra-cluster content routing scheme: SYMM, ASYMM or MULTICAST inter_routing : str Inter-cluster content routing scheme. Only supported LCE """ super(HashroutingClustered, self).__init__(view, controller) if intra_routing not in ('SYMM', 'ASYMM', 'MULTICAST'): raise ValueError('Intra-cluster routing policy %s not supported' % intra_routing) self.intra_routing = intra_routing self.inter_routing = inter_routing self.cluster_topology = extract_cluster_level_topology(view.topology()) self.cluster_sp = nx.all_pairs_shortest_path(self.cluster_topology)
def degree_of_separation(self, graph, start, end): path = nx.all_pairs_shortest_path(graph) try: degree = len(path[start][end]) - 2 except KeyError: degree = 'X' return degree
def _neighbors2(self): path = dict(nx.all_pairs_shortest_path(self.__graph)) root_node = DEFAULT_ROOT_NODE if root_node not in path.keys(): self.__logger.error("rootNode is not keys") return sum_no_path = 0 for node in self.__graph.nodes: node_list = path[root_node].get(node) if node_list is None: self.__logger.error("there is no path %s---->%s", root_node, node) sum_no_path += 1 node_list = self.find_longest_path(node) graph = nx.DiGraph() pre_node = None for subNode in node_list: if pre_node is None: pre_node = subNode continue else: graph.add_edge(pre_node, subNode) pre_node = subNode self.__node_neighbors[node] = graph self.__logger.error("sum number of no path from root is %s", sum_no_path)
def sim_treatment(Y, inputType): G = nx.Graph() G.add_node(np.arange(Y.shape[0]), bipartite=0) G.add_node(np.arange(Y.shape[0], Y.shape[0] + Y.shape[1]), bipartite=1) for i in range(Y.shape[0]): for j in range(Y.shape[1]): if Y[i, j] == 1: G.add_edge(i, Y.shape[0] + j) path = nx.all_pairs_shortest_path(G) if inputType == 'drug': S_ct = np.zeros(Y.shape[0], Y.shape[0]) for i in range(Y.shape[0]): for j in range(Y.shape[0]): D = len(path[i, j]) S_ct[i, j] = 0.3 * np.exp(-0.1 * D) return S_ct if inputType == 'disease': S_dt = np.zeros(Y.shape[1], Y.shape[1]) for i in range(Y.shape[1]): for j in range(Y.shape[1]): D = len(path[i + Y.shape[0], j + Y.shape[0]]) S_dt[i, j] = 0.3 * np.exp(-0.1 * D) return S_dt
def shortest_path(G, source=None, target=None, weight=None): if source is None: if target is None: if weight is None: paths = dict(nx.all_pairs_shortest_path(G)) else: paths = dict(nx.all_pairs_dijkstra_path(G, weight=weight)) else: with nx.utils.reversed(G): if weight is None: paths = single_source_shortest_path(G, target) else: paths = nx.single_source_dijkstra_path(G, target, weight=weight) for target in paths: paths[target] = list(reversed(paths[target])) else: if target is None: if weight is None: paths = single_source_shortest_path(G, source) else: paths = nx.single_source_dijkstra_path(G, source, weight=weight) else: if weight is None: paths = nx.bidirectional_shortest_path(G, source, target) else: paths = nx.dijkstra_path(G, source, target, weight) return paths
def main(): inf = float('inf') G = nx.erdos_renyi_graph(5, 0.55) N = len(G.nodes) ForwardingTable = [[0 for i in range(N)] for j in range(N)] path = dict(nx.all_pairs_shortest_path(G)) print(f"There are {len(G.nodes)} nodes") for i in range(len(G.nodes)): for j in range(len(G.nodes)): try: mypath = path[i][j] # print(f"{i}to{j} path = {path[i][j]} nexthop = {path[i][j][1]}") ForwardingTable[i][j] = path[i][j][1] except KeyError: # print(f"{i}to{j}path = NONE") ForwardingTable[i][j] = inf # No paths except IndexError: # print(f"{i}to{j} nexthop = NONE") ForwardingTable[i][j] = i # There is a path but length = 1 (self) print('\n'.join([''.join(['{:3}'.format(item) for item in row]) for row in ForwardingTable])) nx.draw(G, with_labels=True, font_weight='bold') plt.show()
def str_paths(nx1): """ Function to process the stream network to determine the nodes and edges to downstream gauging sites. The input is from the output of nx_shp. The output is a two dictorionary object list of site nodes and site edges. """ def iter1(g1, d2, site): keys1 = g1.keys() sites2 = [i for i in keys1 if ((i != site) & (i < 10000000))] if not sites2: output = [site] else: len1 = [d2[site][i] for i in sites2] down_site = sites2[np.argmin(len1)] output = g1[down_site] return output ## Determine all paths p1 = nx.all_pairs_shortest_path(nx1) d1 = nx.all_pairs_dijkstra_path_length(nx1, None, 'len') ## Make list of all sites sites = [i for i in nx1.nodes() if (i < 10000000)] ## Extract the paths for all sites (into a dict) p2 = {i: p1[i] for i in sites} d2 = {i: d1[i] for i in sites} site_nodes = {i: iter1(p2[i], d2, i) for i in p2} site_paths = { i: [j[2] for j in nx1.out_edges(site_nodes[i], data='num')][0:-1] for i in site_nodes } return site_nodes, site_paths
def getPathFrequencies(g, label_name='label'): apsp = nx.all_pairs_shortest_path(g) l = [v0 for v in apsp.itervalues() for v0 in v.itervalues()] l_label = [[g.node[i][label_name] for i in item] for item in l] prepared_l_label0 = [zip(path, ['/' for i in range(len(path))]) for path in l_label] prepared_l_label1 = [[el for subsublist in sublist for el in subsublist][:-1] for sublist in prepared_l_label0] all_paths = ['//' + ''.join(str_list) for str_list in prepared_l_label1] all_paths.sort() result = [[all_paths[0], 1]] for i in range(1, len(all_paths)): if all_paths[i] == all_paths[i-1]: result[-1][1] += 1 else: result.append([all_paths[i], 1]) freq = {} for el in result: freq[el[0]] = el[1] return freq
def all_links(self): """ All links in graph :return: """ try: graph = self.graph all_pairs_links = nx.all_pairs_shortest_path(graph) all_links = [ path[1][key] for path in all_pairs_links for key in path[1] if len(path[1][key]) == 2 ] actual_nodes = self.all_nodes self.set_actual_nodes(actual_nodes) cycle_pairs = self.__cycle_pairs links = [ link for link in all_links if (''.join(link)) not in cycle_pairs ] link_list = [ dict({ 'source_id': str(link[0]), 'target_id': str(link[1]) }.items() + graph[link[0]][link[1]].items()) for link in links ] _link_result = { 'paths': link_list, 'type': 'link', 'circle_id': None } return _link_result except (nx.NetworkXException, nx.NetworkXNoPath): return []
def add_shortest_path_edges(graph, cutoff=2): if graph.number_of_edges() == 0 or graph.number_of_nodes() == 0: return shortest_paths = nx.all_pairs_shortest_path(graph, cutoff=cutoff) for source, target_dict in shortest_paths.items(): for target, path in target_dict.items(): graph.add_edge(source, target, attr_dict={'weight': 1 / len(path)})
def make_graph(list_of_person_friends): G = nx.DiGraph() G.add_nodes_from(set(list_of_person_friends)) for i in range(0, len(list_of_person_friends), 4): G.add_edges_from([ (list_of_person_friends[i], list_of_person_friends[i + 1]), (list_of_person_friends[i], list_of_person_friends[i + 2]), (list_of_person_friends[i], list_of_person_friends[i + 3]) ]) x = [] for element in set(list_of_person_friends): x.append(G.degree[element]) #return num of nodes incident to element print("Max node adjacencies: ", max(x)) #print(nx.shortest_path(G, "a", "d")) sp = dict(nx.all_pairs_shortest_path(G)) print(sp[list_of_person_friends[0]]) nx.draw(G, with_labels=True, font_weight='bold') plt.savefig("graph.png") plt.show()
def approx_steiner(graph, terms): res = [] Gt = nx.complete_graph(terms) #calcul de tout les plus court chemins path = dict(nx.all_pairs_shortest_path(graph)) #pour chaque combinaison de 2 noeuds for (i, j) in it.combinations(terms, 2): #on met le poids au plus court chemin dans Gt Gt.add_edge(i, j, weight=nx.path_weight(graph, path[i][j], weight="weight")) #arbre couvrant minimum de Gt A = list(nx.minimum_spanning_tree(Gt).edges(data=True)) for i in range(len(A)): pcc = path[A[i][0]][A[i][1]] for j in range(len(pcc) - 1): pair = (pcc[j], pcc[j + 1]) res.append(pair) print_graph(Gt, terms) return res
def get_paths_net(self): """ Gets the dictionary of shortest paths using invisibles transitions Returns --------------- dictio_spaths Dictionary of shortest paths """ import networkx as nx G = nx.DiGraph() for pl in self.net.places: G.add_node(pl) for tr in self.net.transitions: G.add_node(tr) if tr.label is None: for a in tr.out_arcs: target_place = a.target G.add_edge(tr, target_place) for a in tr.in_arcs: source_place = a.source G.add_edge(source_place, tr) shortest_path = nx.all_pairs_shortest_path(G) dictio_spaths = {} for el in shortest_path: if type(el[0]) is PetriNet.Place: for sel in el[1]: spath = [x for x in el[1][sel][1:-2] if type(x) is PetriNet.Transition] if spath: if not el[0] in dictio_spaths: dictio_spaths[el[0]] = {} dictio_spaths[el[0]][sel] = spath return dictio_spaths
def __init__(self, *args, **kwargs): super(UmbrellaLINX, self).__init__(*args, **kwargs) CONF = cfg.CONF CONF.register_opts([ cfg.StrOpt('topo_file', default='', help=('The specification of the IXP')), ]) self.areas = {} # Dictionary of Graphs self.datapaths = {} self.dpid_name = {} self.mac_to_port = {} self.paths = {} self.ports = [] self.hops_area = {} self.borders = {} self._topo_from_json(CONF.topo_file) for a in self.areas: g = self.areas[a] self.paths[a] = {} paths = nx.all_pairs_shortest_path(g) for p in paths: self.paths[a][p[0]] = p[1]
def next_move(self, who): all_paths = nx.all_pairs_shortest_path(who.graph.networkx_graph()) print who, ":" for i in all_paths: print str(i) + "##################################################3" for j in all_paths[i]: print " " + str(j) + str(all_paths[i][j])
def initialize_graph(size): points_x = [] points_y = [] terminals = [] for i in range(size): points_x.append(random.uniform(-100, 100)) points_y.append(random.uniform(-100, 100)) for i in range(size): if i % 2 == 0: terminals.append(i) h = nx.erdos_renyi_graph(size, 0.3) g = nx.Graph() for i in range(size): g.add_node(i) for i in range(size): for j in range(i, size): if h.has_edge(i, j): g.add_edge(i, j) # weight=manhattan_distance((points_x[i], points_y[i]), (points_x[j], points_y[j]))) length_shortest_paths = nx.all_pairs_dijkstra_path_length(g) shortest_paths = nx.all_pairs_shortest_path(g) metric_closure = nx.Graph() for i in range(size): metric_closure.add_node(i) for i in range(size): for j in range(size): metric_closure.add_edge(i, j, weight=length_shortest_paths[i][j]) print metric_closure return (terminals, shortest_paths, metric_closure, points_x, points_y, g)
def get_links(self, cutoff): graph = self.graph all_links = [] pairs_links = nx.all_pairs_shortest_path(graph, cutoff) for link in pairs_links: all_links.append(link) ret_links = {} # 1. Find the path starting from target which path's length <= cutoff for links in all_links: _id = links[0] _links = links[1] temp = [] for k in _links: if len(_links[k]) > 1: temp.append(_links[k]) ret_links[_id] = temp # Turn to pandas for fast key-value search pd_ret_links = Series(ret_links) # 2. Find the path ending at target which path's length <= cutoff for links in all_links: _links = links[1] for k in _links: if len(_links[k]) > 1: pd_ret_links[k].append(_links[k]) return pd_ret_links
def kNN( pos:torch.Tensor, edges:torch.LongTensor, neighbors_num:int=256, cutoff:int=3): device = pos.device if len(pos.shape)!= 2 or pos.shape[1] != 3: raise ValueError("The vertices matrix must have shape [n,3] and type float!") if len(edges.shape) != 2 or edges.shape[1] != 2 or edges.dtype != torch.long: raise ValueError("The edge index matrix must have shape [m,2] and type long!") n = pos.shape[0] m = edges.shape[0] k = neighbors_num edge_index = edges.cpu().clone().detach().numpy() # they are all necessary unfortunately graph = nx.Graph() graph.add_nodes_from(range(n)) graph.add_edges_from(edge_index) N = np.zeros([n,k], dtype=float) spiral = nx.all_pairs_shortest_path(graph, cutoff=cutoff) for node_idx, neighborhood in spiral: if len(neighborhood) < k: raise RuntimeError("Node {} has only {} neighbours, increase cutoff value!".format(node_idx, len(neighborhood))) for i, neighbour_idx in enumerate(neighborhood.keys()): if i >= k: break else: N[node_idx, i] = neighbour_idx node_neighbours_matrix = torch.tensor(N, device=device, dtype=torch.long) return node_neighbours_matrix
def __init__(self, molecule_name): self.molecule = structures_group.get_group(molecule_name) self.coordinates = self.molecule[['x', 'y', 'z']].values self.atoms = self.molecule['atom'].tolist() self.num = len(self.atoms) self.radii = self.molecule['radii'].values self.electron = self.molecule['electron'].values # atom type self.atoms_dict = defaultdict(list) for i, atom in enumerate(self.atoms): self.atoms_dict[atom].append(i) # calculate distance matrix and bulid graph self.dist_matrix = self.calc_distance() self.bond_distance = self.radii[:, None] + self.radii self.graph_edges = (self.dist_matrix < 1.3 * self.bond_distance) - np.identity(self.num) self.graph = nx.Graph(self.graph_edges) self.graph_shortest_path = dict(nx.all_pairs_shortest_path(self.graph)) self.graph_shortest_path_length = { x: {i: len(path) - 1 for i, path in v.items()} for x, v in self.graph_shortest_path.items() } self.func_map = { '1JHC': self.get_1JHx_features, '1JHN': self.get_1JHx_features, '2JHC': self.get_2JHx_features, '2JHN': self.get_2JHx_features, '2JHH': self.get_2JHH_features, '3JHC': self.get_3JHx_features, '3JHN': self.get_3JHx_features, '3JHH': self.get_3JHH_features, }
def test_symmetric_paths(self): topology = fnss.Topology() topology.add_path([1, 2, 4, 5, 3, 6, 1]) path = nx.all_pairs_shortest_path(topology) self.assertNotEqual(list(path[1][5]), list(reversed(path[5][1]))) network.symmetrify_paths(path) self.assertEqual(list(path[1][5]), list(reversed(path[5][1])))
def _check_rule_typing(hierarchy, rule_id, graph_id, lhs_mapping, rhs_mapping): all_paths = nx.all_pairs_shortest_path(hierarchy) paths_from_target = {} for s in hierarchy.nodes(): if s == graph_id: for key in all_paths[graph_id].keys(): paths_from_target[key] = all_paths[graph_id][key] for t in paths_from_target.keys(): if t != graph_id: new_lhs_h = compose( lhs_mapping, hierarchy.compose_path_typing(paths_from_target[t])) new_rhs_h = compose( rhs_mapping, hierarchy.compose_path_typing(paths_from_target[t])) try: # find homomorphisms from s to t via other paths s_t_paths = nx.all_shortest_paths(hierarchy, rule_id, t) for path in s_t_paths: lhs_h, rhs_h = hierarchy.compose_path_typing(path) if lhs_h != new_lhs_h: raise HierarchyError( "Invalid lhs typing: homomorphism does not " "commute with an existing " "path from '%s' to '%s'!" % (s, t)) if rhs_h != new_rhs_h: raise HierarchyError( "Invalid rhs typing: homomorphism does not " "commute with an existing " + "path from '%s' to '%s'!" % (s, t)) except (nx.NetworkXNoPath): pass return
def Optimize_Add(G, N, B, U, C_Val, Path_E): path = nx.all_pairs_shortest_path(G) path = [path[x].values() for x in path.keys()] path = [j for x in path for j in x if len(j) > 1] for i in N: if i not in range(10, 13): for j in N: r = 0 for p in path: p = ','.join(str(i) for i in p) if str(i) + ',' + str(j) in p: #print i,j,p r = 1 break if r == 0: #print nx.node_connectivity(G,i,j) try: G.remove_edge(i, j) print N_warn N_warn.append([i, j]) cost = C_Val[i][j][0] + C_Val[i][j][2] B[i] = B[i] + cost except: continue LOG.info('Optimized Add') return G, B
def buildSteinerTree(queryVertexes, G): '假设只包含一个节点的话直接返回' if (len(queryVertexes) == 1): return G.subgraph(queryVertexes) '多个查询节点的情况' '1:计算每个查询节点到其他查询节点的最短路径' shortestLength = nx.all_pairs_shortest_path_length(G) ###计算皆有节点对知己恩的最短路径 shortestPath = nx.all_pairs_shortest_path(G) ##计算最短路径 '2:构造只有查询节点,边权重是最短路径长度的完全图G1' weightedG = nx.Graph() for V1 in queryVertexes: for V2 in queryVertexes: weightedG.add_edge(V1, V2, weight=shortestLength[V1][V2]) '3:在上一步的G1中构造最小生成树G2' G2 = nx.minimum_spanning_tree(weightedG) '4:将原图G2的边替换成G2两节点之间的最短路径,构建G的子图G3' G3 = nx.Graph() for edge in G2.edges_iter(): path = shortestPath[edge[0]][edge[1]] for v in path: if not G3.has_node(v): G3.add_node(v) for nei in G.neighbors(v): if G3.has_node(nei): G3.add_edge(v, nei) ##EECS到这里就结束了 # return G3 print 'G3:', G3.nodes() '5:在G3找最小生成树G4' G4 = nx.minimum_spanning_tree(G3) print 'G4:', G4.nodes() '6:Construct a ST tree G5 from G4 by deleting edges in G4,' \ 'if necessary,so that no leaves in G5 are ST vertices' return G4
def rem_switch(self, dpid): if dpid in self.topology.nodes(): self.topology.remove_node(dpid) for mac in self.switch_to_host[dpid]: del host_to_switch[mac] del switch_to_host[dpid] if not self.__setting_up__: self.paths_dict = nx.all_pairs_shortest_path(self.topology) return
def next_move(self, who): all_paths = nx.all_pairs_shortest_path(who.graph.clear_networkx_graph(self.cannottouch)) all_paths2 = nx.all_pairs_shortest_path(who.graph.networkx_graph()) if who.position in all_paths: if self.target in all_paths[who.position]: if len(all_paths[who.position][self.target]) > 1: # print "way to target available" return all_paths[who.position][self.target][1] else: # print "way to target NOT available" for i in all_paths[who.position].values(): if len(i) > 1: return i[1] else: if len(all_paths2[who.position][self.target]) == 2: return self.target temp = [] for i in self.graph.graph[who.position]: if i in all_paths: temp.append(i) if not temp == []: if self.target in all_paths2[who.position]: dicti = {} # print for i in temp: # print str(i) + " " + str(all_paths2[i][self.target]) dicti[i] = len(all_paths2[i][self.target]) if not dicti == {}: return min(dicti, key=dicti.get) else: return [] else: # print "way to target NOT available" for i in all_paths[who.position].values(): if len(i) > 1: return i[1] print "temp: " + str(temp) if self.target in all_paths2[who.position]: if len(all_paths2[who.position][self.target]) > 1: print "random way to target available" nextep = all_paths2[who.position][self.target][1] else: print "random way to target NOT available" for i in all_paths2[who.position].values(): if len(i) > 1: return i[1]
def compareToBinary(complexSentencesById, classifications, exampleBuilder, options): # Load corpus and make sentence graphs print >> sys.stderr, "Calculating performance on binary corpus" classificationsBySentence = {} for classification in classifications: example = classification[0][0] sentenceId = example[0].rsplit(".",1)[0] sentenceOrigId = complexSentencesById[sentenceId].sentence.attrib["origId"] if not classificationsBySentence.has_key(sentenceOrigId): classificationsBySentence[sentenceOrigId] = [] classificationsBySentence[sentenceOrigId].append(classification) print >> sys.stderr, "Loading Binary corpus" binaryCorpusElements = loadCorpus(options.binaryCorpus) binaryClassifications = [] counter = ProgressCounter(len(binaryCorpusElements.sentences), "Build binary classifications") for binarySentence in binaryCorpusElements.sentences: counter.update(1, "Building binary classifications ("+binarySentence.sentence.attrib["id"]+"): ") if(classificationsBySentence.has_key(binarySentence.sentence.attrib["origId"])): complexClassificationGraph = NX.XGraph(multiedges = multiedges) for token in binarySentence.sentenceGraph.tokens: complexClassificationGraph.add_node(token) for classification in classificationsBySentence[binarySentence.sentence.attrib["origId"]]: if classification[1] > 0: example = classification[0][0] t1 = example[3]["t1"] t2 = example[3]["t2"] t1Binary = None for token in binarySentence.sentenceGraph.tokens: if token.attrib["charOffset"] == t1.attrib["charOffset"]: t1Binary = token t2Binary = None for token in binarySentence.sentenceGraph.tokens: if token.attrib["charOffset"] == t2.attrib["charOffset"]: t2Binary = token assert(t1Binary != None and t2Binary != None) complexClassificationGraph.add_edge(t1Binary, t2Binary) paths = NX.all_pairs_shortest_path(complexClassificationGraph, cutoff=999) for pair in binarySentence.pairs: t1 = binarySentence.sentenceGraph.entityHeadTokenByEntity[pair.attrib["e1"]] t2 = binarySentence.sentenceGraph.entityHeadTokenByEntity[pair.attrib["e2"]] assert(pair.attrib["interaction"] == "True" or pair.attrib["interaction"] == "False") if pair.attrib["interaction"] == "True": pairClass = 1 else: pairClass = -1 extra = {"xtype":"edge","type":"i","t1":t1,"t2":t2} if paths.has_key(t1) and paths[t1].has_key(t2): binaryClassifications.append( [[pair.attrib["id"], pairClass, None, extra], 1, "binary"] ) else: binaryClassifications.append( [[pair.attrib["id"], pairClass, None, extra], -1, "binary"] ) print >> sys.stderr, "Evaluating binary classifications" evaluation = Evaluation(predictions, classSet=exampleBuilder.classSet) print >> sys.stderr, evaluation.toStringConcise() if options.output != None: evaluation.saveCSV(options.output + "/binary_comparison_results.csv")
def buildExamples(self, sentenceGraph): self.makeGSEvents(sentenceGraph) eventNodes = [] nameNodes = [] for entity in sentenceGraph.entities: if entity.get("type") == "neg": continue if entity.get("isName") == "True": nameNodes.append(entity) else: eventNodes.append(entity) allNodes = eventNodes + nameNodes examples = [] exampleIndex = 0 undirected = sentenceGraph.dependencyGraph.to_undirected() paths = NX.all_pairs_shortest_path(undirected, cutoff=999) for eventNode in eventNodes: eventType = eventNode.get("type") if eventType in [ "Gene_expression", "Transcription", "Protein_catabolism", "Localization", "Phosphorylation", ]: for nameNode in nameNodes: if self.isPotentialGeniaInteraction(eventNode, nameNode): examples.append(self.buildExample(exampleIndex, sentenceGraph, paths, eventNode, nameNode)) exampleIndex += 1 elif eventType in ["Regulation", "Positive_regulation", "Negative_regulation"]: combinations = combine.combine(allNodes + [None], allNodes + [None]) for combination in combinations: if combination[0] == combination[1]: continue if combination[0] == eventNode or combination[1] == eventNode: continue if combination[0] != None and not self.isPotentialGeniaInteraction(eventNode, combination[0]): continue if combination[1] != None and not self.isPotentialGeniaInteraction(eventNode, combination[1]): continue examples.append( self.buildExample(exampleIndex, sentenceGraph, paths, eventNode, combination[0], combination[1]) ) exampleIndex += 1 elif eventType in ["Binding"]: continue else: assert False, eventType self.gsEvents = None return examples
def test_all_pairs_shortest_path(self): p=nx.shortest_path(self.cycle) assert_equal(p[0][3],[0,1,2,3]) assert_equal(p,nx.all_pairs_shortest_path(self.cycle)) p=nx.shortest_path(self.grid) assert_equal(p[1][12],[1, 2, 3, 4, 8, 12]) # now with weights p=nx.shortest_path(self.cycle,weighted=True) assert_equal(p[0][3],[0,1,2,3]) assert_equal(p,nx.all_pairs_dijkstra_path(self.cycle)) p=nx.shortest_path(self.grid,weighted=True) assert_equal(p[1][12],[1, 2, 3, 4, 8, 12])
def run(self): """here we wait for the topology to be "complete", then calculate what is needed and stop the thread""" self.__cur__ = self.__init_idle__ = time.time() self.__setting_up__ = True while (self.__cur__ < self.__init_idle__ + 5): self.__cur__ = time.time() #print 'Detecting topology, idle for ',self.__cur__ - self.__init_idle__ time.sleep(0.001) print 'Finished detecting topology, creating data...' self.paths_dict = nx.all_pairs_shortest_path(self.topology) self.__setting_up__ = False print 'Finished creating data'
def __init__(self, topology, shortest_path=None): """Constructors Parameters ---------- topology : fnss.Topology The topology object shortest_path : dict of dict, optional The all-pair shortest paths of the network """ # Filter inputs if not isinstance(topology, fnss.Topology): raise ValueError('The topology argument must be an instance of ' 'fnss.Topology or any of its subclasses.') # Shortest paths of the network self.shortest_path = shortest_path if shortest_path is not None \ else nx.all_pairs_shortest_path(topology) # Network topology self.topology = topology # Dictionary mapping each content object to its source # dict of location of contents keyed by content ID self.content_source = {} # Dictionary of cache sizes keyed by node self.cache_size = {} self.colla_table = {} # Dictionary of link types (internal/external) self.link_type = nx.get_edge_attributes(topology.to_directed(), 'type') self.link_delay = fnss.get_delays(topology.to_directed()) policy_name = topology.graph['cache_policy'] # Initialize attributes for node in topology.nodes_iter(): stack_name, stack_props = fnss.get_stack(topology, node) if stack_name == 'cache': self.cache_size[node] = stack_props['size'] self.colla_table[node] = {} elif stack_name == 'source': contents = stack_props['contents'] for content in contents: self.content_source[content] = node cache_size = dict((node, fnss.get_stack(topology, node)[1]['size']) for node in topology.nodes_iter() if fnss.get_stack(topology, node)[0] == 'cache') # The actual cache object storing the content self.caches = dict((node, cache_policy_register[policy_name](cache_size[node])) for node in cache_size)
def test_all_pairs_shortest_path(self): p=nx.shortest_path(self.cycle) assert_equal(p[0][3],[0,1,2,3]) assert_equal(p,nx.all_pairs_shortest_path(self.cycle)) p=nx.shortest_path(self.grid) validate_grid_path(4, 4, 1, 12, p[1][12]) # now with weights p=nx.shortest_path(self.cycle,weight='weight') assert_equal(p[0][3],[0,1,2,3]) assert_equal(p,nx.all_pairs_dijkstra_path(self.cycle)) p=nx.shortest_path(self.grid,weight='weight') validate_grid_path(4, 4, 1, 12, p[1][12])
def transitveReduction(self, digraph): paths = dict(NX.all_pairs_shortest_path(digraph)) def hasPath(node1, node2): if node1 == node2: return False return (node1 in paths and node2 in paths[node1] and len(paths[node1][node2]) > 0) for x in digraph.nodes(): for y in digraph.nodes(): for z in digraph.nodes(): if (x != z and digraph.has_edge(x, z) and hasPath(x, y) and hasPath(y, z)): digraph.remove_edge(x, z)
def all_pairs_shortest_path(cls,topology): location_paths = {} switch_paths = nx.all_pairs_shortest_path(topology) for s1, paths in switch_paths.items(): location_paths[s1] = {} for s2, path in paths.items(): location_paths[s1][s2] = [] cur = s1 for nxt in path + [s2]: if cur != nxt: link = topology[cur][nxt] loc = Location(cur,link[cur]) location_paths[s1][s2].append(loc) cur = nxt return location_paths
def __init__(self, costfun="nocore", tm="2004", tm_factor_name="max-50", EPscalingFactor=1, mappingseed=None): """ costfun is a string in ["10xcore", "nocore"] """ logger.info("Initializing a new EnterpriseTopology") logger.debug("Traffic matrix: %s" % str(tm)) logger.debug("Traffic costfun: %s" % str(costfun)) # Use this to initialize the random seed when a EnterpriseTopology instance # is not created using panoptisim.py if mappingseed is not None: random.seed(mappingseed) self.name = "enterprise" self.tm = tm self.port = OrderedDict() endpoint_scaling_factor = EPscalingFactor assert EPscalingFactor == int(EPscalingFactor) self.trace_duration = trace_durations[tm] lbnl = json.load(open("traces/tm"+tm+".json")) self.graph = self.__enterprise_to_nx() self.leaves = self.__prune_access_switches(endpoint_scaling_factor) self.paths = nx.all_pairs_shortest_path(self.graph) #Ensure symmetry in the original shortest paths for src in self.graph.nodes(): for dst in self.graph.nodes(): self.paths[src][dst] = list(reversed(self.paths[dst][src])) # Map endpoints and project load over the shortest path topology self.__map_end_points(lbnl) self.__project_base_load() # Scale up the traffic according to the tm_factor_name and re-project # new scaled-up traffic load self.tm_scaling_factor = self.__calculate_traffic_scaling_factor(tm_factor_name) self.__scale_traffic(self.tm_scaling_factor) self.__project_base_load() #Ensure every link has at any flow traversing it at most once for src, dst, e in self.graph.edges(data=True): flows_by_srcdst = e['flows'] for key, value in flows_by_srcdst.iteritems(): assert value == 1
def main(list_of_filenames): """ the function that runs all the experiments """ list_of_param_graphs = read_graphs_params(list_of_filenames[0]) list_of_coeffs = read_coeff(list_of_filenames[1]) list_of_setP = [] list_of_path_dict = [] list_of_uncovered_edges = [] list_of_graph_iDs = [] for param_graphs in list_of_param_graphs: # getting each graph parameters list_of_graph_iDs.append(param_graphs[0]) H = nx.fast_gnp_random_graph(param_graphs[1], param_graphs[2], param_graphs[3], False) randSeed = param_graphs[6] for (u,v) in H.edges(): random.seed(randSeed) rInt = random.randint(param_graphs[4], param_graphs[5]) H[u][v]['w'] = rInt randSeed += 1 set_of_paths_P = nx.all_pairs_shortest_path(H) setP = gc.remove_duplicate_paths(set_of_paths_P, nx.number_of_nodes(H)) list_of_setP.append(setP) path_dict = create_path_dict(setP) list_of_path_dict.append(path_dict) uncovered_edges = gc.get_all_edges_from_SOP(setP) list_of_uncovered_edges.append(uncovered_edges) out_file = ''.join(["./data/csv/",list_of_filenames[2],".csv"]) file = open(os.path.expanduser(out_file), 'at') writer = csv.writer(file) for idx in xrange(len(list_of_setP)): #loop through the graphs graph_iD = list_of_graph_iDs[idx] writer.writerow(('Graph Params: ', list_of_param_graphs[idx])) for coeffs in list_of_coeffs: for iteration in xrange(1): # change the input to xrange() depending on the number of iterations on a single experiment t_path_dict = list_of_path_dict[idx] t_uncovered_edges = set(list_of_uncovered_edges[idx]) t_setP = list_of_setP[idx] six_tuple = gf.func_wrapper_greedy_algo(t_path_dict, t_uncovered_edges, coeffs[0]) writer.writerow((graph_iD, coeffs[0], six_tuple[0], len(t_setP), six_tuple[1], six_tuple[2], six_tuple[3], six_tuple[4], six_tuple[5])) file.close()
def approximate(G): D = nx.diameter(G)-3 paths = nx.all_pairs_shortest_path(G,cutoff=D) centralityScore= {} total = 0.0 for path in paths.keys(): for p in paths[path].keys(): for nodes in paths[path][p]: if nodes not in centralityScore: centralityScore[nodes] = 1.0 total += 1.0 else: centralityScore[nodes] += 1.0 total += 1.0 for x in centralityScore.keys(): centralityScore[x] /= total return centralityScore
def buildExamples(self, sentenceGraph): examples = [] exampleIndex = 0 undirected = sentenceGraph.dependencyGraph.to_undirected() #undirected = self.makeUndirected(sentenceGraph.dependencyGraph) paths = NX.all_pairs_shortest_path(undirected, cutoff=4) #key1 = paths.keys()[0] #key2 = paths[key1].keys()[0] #print key1 #print key2 #print paths[key1][key2] #sys.exit(0) for i in range(len(sentenceGraph.tokens)-1): for j in range(i+1,len(sentenceGraph.tokens)): tI = sentenceGraph.tokens[i] tJ = sentenceGraph.tokens[j] # only consider paths between entities (NOTE! entities, not only named entities) if (sentenceGraph.tokenIsEntityHead[tI] == None) or (sentenceGraph.tokenIsEntityHead[tJ] == None): continue # find the path if paths.has_key(tI) and paths[tI].has_key(tJ): path = paths[tI][tJ] elif paths.has_key(tJ) and paths[tJ].has_key(tI): path = paths[tJ][tI] else: continue if len(path) > 2: # define class if sentenceGraph.interactionGraph.has_edge(path[0], path[-1]): self.buildExample(path, sentenceGraph, 1, examples, exampleIndex) exampleIndex += 1 else: self.buildExample(path, sentenceGraph, -1, examples, exampleIndex) exampleIndex += 1 if sentenceGraph.interactionGraph.has_edge(path[-1], path[0]): self.buildExample(path[::-1], sentenceGraph, 1, examples, exampleIndex) exampleIndex += 1 else: self.buildExample(path[::-1], sentenceGraph, -1, examples, exampleIndex) exampleIndex += 1 #print "examples:",len(examples) #sys.exit(0) return examples
def read_test_data(): points_x = [] points_y = [] terminals = [] f = open("input.txt") for i in range(8): f.readline() number_of_nodes = int((f.readline().split())[1]) number_of_edges = int((f.readline().split())[1]) g = nx.Graph() for i in range(number_of_nodes): g.add_node(i) for i in range(number_of_edges): temp = f.readline().split() g.add_edge(int(temp[1]) - 1, int(temp[2]) - 1, weight=int(temp[3])) for i in range(3): f.readline() number_of_terminals = int(f.readline().split()[1]) for i in range(number_of_terminals): terminals.append(int(f.readline().split()[1]) - 1) for i in range(3): f.readline() for i in range(number_of_nodes): temp = f.readline().split() points_x.append(int(temp[2])) points_y.append(int(temp[3])) length_shortest_paths = nx.all_pairs_dijkstra_path_length(g) shortest_paths = nx.all_pairs_shortest_path(g) metric_closure = nx.Graph() for i in range(number_of_nodes): metric_closure.add_node(i) for i in range(number_of_nodes): for j in range(number_of_nodes): metric_closure.add_edge(i, j, weight=length_shortest_paths[i][j]) print metric_closure return (terminals, shortest_paths, metric_closure, points_x, points_y, g)
def get_pairwise_distances(npalign, tree_file=None, seq_file=None): if seq_file is None: fasta_handle = NTF(mode="w") else: fasta_handle = open("/tmp/tmp.fasta", "w") if tree_file is None: tree_handle = NTF() else: tree_handle = open(tree_file, "w") seq_names = fasta_write(fasta_handle, npalign) fasta_handle.flush() os.fsync(fasta_handle.fileno()) cmd = "muscle -in %(ifile)s -tree2 %(treefile)s -gapopen -2.9" cmdlist = shlex.split(cmd % {"ifile": fasta_handle.name, "treefile": tree_handle.name}) try: t = check_output(cmdlist) tree = Phylo.read(open(tree_handle.name), "newick") except CalledProcessError: # print('Could not make tree') return None except ValueError: # print('no tree present') return None except RuntimeError: return None seq_names = sorted(tree.get_terminals(), key=lambda x: x.name) net = Phylo.to_networkx(tree) dmat = networkx.all_pairs_shortest_path(net) terminals = tree.get_terminals() dists = np.zeros((npalign.shape[0], npalign.shape[0])) for t1, t2 in product(terminals, terminals): path = dmat[t1][t2] dist = sum(c.branch_length for c in path) i1 = int(t1.name.split("-")[1]) i2 = int(t2.name.split("-")[1]) dists[i1, i2] = dist return dists
def test_os3e_weighted(self): '''Ensure unit-weighted version of graph yields same availability.''' link_fail_prob = 0.01 g = OS3EGraph() g_unit = set_unit_weights(g.copy()) apsp = nx.all_pairs_shortest_path_length(g) apsp_paths = nx.all_pairs_shortest_path(g) combos = [["Sunnyvale, CA", "Boston"], ["Portland"], ["Sunnyvale, CA", "Salt Lake City"], ["Seattle", "Boston"], ["Seattle", "Portland"]] for combo in combos: for max_failures in range(1, 2): a, c = availability_one_combo(g, combo, apsp, apsp_paths, False, link_fail_prob, max_failures) a_u, c_u = availability_one_combo(g_unit, combo, apsp, apsp_paths, True, link_fail_prob, max_failures) self.assertAlmostEqual(a, a_u) self.assertAlmostEqual(c, c_u)
def add_link(self, link): #switch1(dpsrc) ao swtch2(dpdst) na porta do switch1(sport) ou do switch2(dport) """Adds the link. If the switch does not exist it is created. No rules have to be added, as the new switch will not be active.""" print 'Link between', link.dpsrc, 'and', link.dpdst, 'reported.' print 'source port:', link.sport, 'destination port', link.dport if not link.dpsrc in self.topology.nodes(): self.add_switch(link.dpsrc, None) if not link.dpdst in self.topology.nodes(): self.add_switch(link.dpdst, None) if not link.dpdst in self.topology[link.dpsrc]: self.__cur__ = self.__init_idle__ = time.time() self.topology.add_edge(link.dpsrc, link.dpdst) self.topology.add_edge(link.dpdst, link.dpsrc) self.topology[link.dpsrc][link.dpdst]['port'] = link.sport self.topology[link.dpdst][link.dpsrc]['port'] = link.dport if not self.__setting_up__: self.paths_dict = nx.all_pairs_shortest_path(self.topology) return CONTINUE
def test_all_pairs_shortest_path(self): p = nx.shortest_path(self.cycle) assert_equal(p[0][3], [0, 1, 2, 3]) assert_equal(p, dict(nx.all_pairs_shortest_path(self.cycle))) p = nx.shortest_path(self.grid) validate_grid_path(4, 4, 1, 12, p[1][12]) # now with weights p = nx.shortest_path(self.cycle, weight='weight') assert_equal(p[0][3], [0, 1, 2, 3]) assert_equal(p, dict(nx.all_pairs_dijkstra_path(self.cycle))) p = nx.shortest_path(self.grid, weight='weight') validate_grid_path(4, 4, 1, 12, p[1][12]) # weights and method specified p = nx.shortest_path(self.cycle, weight='weight', method='dijkstra') assert_equal(p[0][3], [0, 1, 2, 3]) assert_equal(p, dict(nx.all_pairs_dijkstra_path(self.cycle))) p = nx.shortest_path(self.cycle, weight='weight', method='bellman-ford') assert_equal(p[0][3], [0, 1, 2, 3]) assert_equal(p, dict(nx.all_pairs_bellman_ford_path(self.cycle)))
def closure(g): ''' Compute the transitive closure of a graph. Undirected: >>> g = Graph({1: [2], 2: [3, 4]}) >>> sorted( g.edges() ) [(1, 2), (2, 3), (2, 4)] >>> sorted( closure(g).edges() ) [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)] Directed: >>> g = DiGraph({1: [2], 2: [3, 4]}) >>> sorted( g.edges() ) [(1, 2), (2, 3), (2, 4)] >>> sorted( closure(g).edges() ) [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4)] ''' return g.__class__(all_pairs_shortest_path(g))
def test_latency_bound(self): '''Ensure fraction of nodes within bound is reasonable.''' g = OS3EGraph() g_unit = set_unit_weights(g.copy()) apsp = nx.all_pairs_shortest_path_length(g) apsp_paths = nx.all_pairs_shortest_path(g) path_lens = [] for a in apsp: for b in apsp: path_lens.append(apsp[a][b]) max_path_len = max(path_lens) combos = [["Sunnyvale, CA", "Boston"], ["Portland"], ["Sunnyvale, CA", "Salt Lake City"], ["Seattle", "Boston"], ["Seattle", "Portland"]] for combo in combos: one = fraction_within_latency(g, combo, apsp, max_path_len + 1.0) self.assertEqual(one, 1.0) two = fraction_within_latency(g, combo, apsp, 0.000001) self.assertEqual(two, len(combo) / float(g.number_of_nodes()))
def TwoStepGreedy(graph,b,debug=True): G = graph.copy() A = set() Gp,_,_ = RemoveCore(G) paths = nx.all_pairs_shortest_path(Gp) cnt = 0 saved_size = OrderedDict() saved_size[0] = FindKCore(G).number_of_nodes() while b > 0: Gp,Gs,kc_nodes = RemoveCore(G,anchor=A) R_cand,S_cand = PartitionGraph(Gs,kc_nodes,anchor=A) if debug: print list(R_cand),list(S_cand), if len(R_cand) == 0 and len(S_cand) ==0: break # Find optimal 1st and 2nd anchors from R i.e. v1 and v2 N1,A1,kc_nodes_R = FindBestAnchor(G,R_cand,paths,kc_nodes) R_cand = R_cand.difference(A1) N2,A2,kc_nodes_R = FindBestAnchor(G,R_cand,paths,kc_nodes_R,anchor=A1) N2 = max(N1,N2) # Find optimal pair from S i.e. (v3,v4) N3,A3,kc_nodes_S = FindBestAnchor(G,S_cand,paths,kc_nodes,n=2) S_cand = S_cand.difference(A3) if b==1 or N2 > N3: A = A.union(A1) b -= 1 cnt += 1 else: saved_size[cnt+1] = FindKCore(graph,anchor=A.union(A1)).number_of_nodes() A = A.union(A3) b -= 2 cnt += 2 saved_size[cnt] = FindKCore(graph,anchor=A).number_of_nodes() if debug: print "Final anchors:",list(A) return saved_size
def buildExamples(self, sentenceGraph): examples = [] exampleIndex = 0 undirected = sentenceGraph.dependencyGraph.to_undirected() #undirected = self.makeUndirected(sentenceGraph.dependencyGraph) paths = NX.all_pairs_shortest_path(undirected, cutoff=4) for i in range(len(sentenceGraph.tokens)-1): for j in range(i+1,len(sentenceGraph.tokens)): tI = sentenceGraph.tokens[i] tJ = sentenceGraph.tokens[j] # only consider paths between entities (NOTE! entities, not only named entities) if (sentenceGraph.tokenIsEntityHead[tI] == None) or (sentenceGraph.tokenIsEntityHead[tJ] == None): continue # find the path if paths.has_key(tI) and paths[tI].has_key(tJ): path = paths[tI][tJ] elif paths.has_key(tJ) and paths[tJ].has_key(tI): path = paths[tJ][tI] else: continue if len(path) > 1:#> 2: # define class if sentenceGraph.interactionGraph.has_edge(path[0], path[-1]): categoryName = sentenceGraph.interactionGraph.get_edge(path[0], path[-1]).attrib["type"] self.buildExample(path, sentenceGraph, categoryName, examples, exampleIndex) exampleIndex += 1 else: self.buildExample(path, sentenceGraph, "neg", examples, exampleIndex) exampleIndex += 1 if sentenceGraph.interactionGraph.has_edge(path[-1], path[0]): categoryName = sentenceGraph.interactionGraph.get_edge(path[-1], path[0]).attrib["type"] #categoryName += "_rev" self.buildExample(path[::-1], sentenceGraph, categoryName, examples, exampleIndex) exampleIndex += 1 else: self.buildExample(path[::-1], sentenceGraph, "neg", examples, exampleIndex) exampleIndex += 1 return examples
def makeHierarchy(V,E): """Convert hierarchy into a network graph and creates a dictionary of ordered list to describe the hiearchy""" G = nx.DiGraph() for v in V: G.add_node(v) for e in E: if is_string_like(e[0]): s = e[0] else: s = e[0][0] if is_string_like(e[1]): t = e[1] else: t = e[1][0] G.add_edge(s,t) H = dict([(o,v.keys()) for (o,v) in nx.all_pairs_shortest_path(G).items()]) T = nx.topological_sort(G) for k in H.keys(): H[k] = [j for j in T if j in H[k]] return [G,H]