Esempio n. 1
0
def tspRepeats(G, start):
    #make a graph out of the matrix
    all_distances = dict(nx.floyd_warshall(G))

    #initialize graph of distances (complete graph with shortest paths as edges)
    B = G.copy()
    edges = {""}
    edges.pop()
    for _ in G.nodes:
        for __ in G.nodes:
            if not B.has_edge(_, __) and not __ == _:
                edges.add((_, __, all_distances[_][__]))
    B.add_weighted_edges_from(edges)
    #predecessors = nx.floyd_warshall_predecessor_and_distance(B)

    all_distances = dict(nx.floyd_warshall(B))

    #creates returner, a two-opt algorithm version of TSP on the shortest paths graph
    returner = two_opt(B)

    #shifts the array so the starting node is at the beginning of the array
    def shift(seq, n):
        n = n % len(seq)
        return seq[n:] + seq[:n]
    for _ in range(len(returner)):
        if returner[_] == int(start):
            returner = shift(returner, _)
    finalList = []
    if len(returner) > 0:
        finalList = [returner[0]]
        for i in range(len(returner)-1):
            finalList += nx.shortest_path(G, returner[i], returner[i+1], weight='weight')[1:]
            finalList += nx.shortest_path(G, returner[-1], returner[0], weight='weight')[1:]

    return finalList
Esempio n. 2
0
def lab_5():
    def graph(n, k, p):
        G = nx.watts_strogatz_graph(n, k, p)
        if not nx.is_connected(G):
            return graph(size, k, p)
        return G

    iterations = 100
    size = np.arange(1, 6)
    data = np.zeros((iterations, len(size)))
    for i in trange(iterations):
        for j in size:
            G = graph(j * 100, 10, 0.5)
            t = time()
            nx.floyd_warshall(G)
            data[i][j - 1] = time() - t

    np.save('out/lab5.npy', data)

    x = np.arange(100, 600, 100)
    data = np.load('out/lab5.npy')
    y = np.zeros(5)
    for i in range(5):
        y[i] = np.average(data[:, i])

    matplotlib.rc('font', **{'size': 24})

    plt.figure(figsize=(12, 9))
    plt.title('Average Time')
    plt.plot(x, y, color='#FA816D', linewidth=3)

    plt.xlabel('Network Size')
    plt.xticks(x)
    plt.ylabel('Seconds')
    plt.show()
Esempio n. 3
0
    def test_zero_weight(self):
        G = nx.DiGraph()
        edges = [(1,2,-2), (2,3,-4), (1,5,1), (5,4,0), (4,3,-5), (2,5,-7)]
        G.add_weighted_edges_from(edges)
        dist = nx.floyd_warshall(G)
        assert_equal(dist[1][3], -14)

        G = nx.MultiDiGraph()
        edges.append( (2,5,-7) )
        G.add_weighted_edges_from(edges)
        dist = nx.floyd_warshall(G)
        assert_equal(dist[1][3], -14)
Esempio n. 4
0
    def test_zero_weight(self):
        G = nx.DiGraph()
        edges = [(1,2,-2), (2,3,-4), (1,5,1), (5,4,0), (4,3,-5), (2,5,-7)]
        G.add_weighted_edges_from(edges)
        dist = nx.floyd_warshall(G)
        assert_equal(dist[1][3], -14)

        G = nx.MultiDiGraph()
        edges.append( (2,5,-7) )
        G.add_weighted_edges_from(edges)
        dist = nx.floyd_warshall(G)
        assert_equal(dist[1][3], -14)
Esempio n. 5
0
def condense_graph(subG,clusterlabelfile, num_levels, level,nodeL):
    with open(clusterlabelfile,"r+") as f:
        lines = f.readlines()
        line = lines[level]
        num_communs = int(re.findall(r'(\d+) nodes',line)[0])

    if level < 0:
        hier = num_levels + level
    else:
        hier = level
    with open("clusters.csv","w+") as f:
        subprocess.check_call([r"./DirectedLouvain/bin/hierarchy","subG.tree","-l",str(hier)], stdout=f)


    cluster_df = pd.read_csv('clusters.csv',index_col = False, names = ['node','community'],sep=' ')

    def convert_inds(ind):
        return nodeL[ind]

    cluster_df['node'] = cluster_df['node'].map(convert_inds)

    for i, tup in cluster_df.iterrows():
        node, comm = tup
        subG.nodes[node]['community'] = comm
        

    print("Condensing graph...")
    tic = timemod.clock()
    condensed_G = nx.DiGraph()
    for i in range(0,num_communs):
        condensed_G.add_node(i)
        nodes = list(cluster_df[cluster_df['community'] == i]['node'])
        condensed_G.nodes[i]['squished_nodes'] = nodes
        temp = subG.subgraph(nodes)
        distances = nx.floyd_warshall(temp,weight='pace')
        avg_pace = np.nanmean([t for src,tardict in distances.items() for dest,t in tardict.items() if src in nodes and dest in nodes and np.isfinite(t)])
        if np.isfinite(avg_pace):
            condensed_G.add_edge(i,i,pace=avg_pace)


    for start,end in itertools.permutations(condensed_G.nodes,2):
        clus1 = cluster_df[cluster_df['community']==start]
        clus2 = cluster_df[cluster_df['community']==end]
        temp = subG.subgraph(list(clus1['node'])+list(clus2['node']))
        distances = nx.floyd_warshall(temp,weight='pace')
        edges_12_pace = np.nanmean([t for src,tardict in distances.items() for dest,t in tardict.items() 
                                if src in set(clus1['node']) and dest in set(clus2['node']) and np.isfinite(t)])
        if np.isfinite(edges_12_pace):
            condensed_G.add_edge(start,end,pace=edges_12_pace) 
    toc = timemod.clock()
    print("Graph condensed in " + str(toc-tic) + " seconds.")
    return cluster_df, condensed_G
Esempio n. 6
0
def test(adj_mat, elapsed_time, i):
    G = ntx.Graph(adj_mat)
    ntx.draw_networkx(G, pos=ntx.kamada_kawai_layout(G), node_size=50, with_labels=True, font_size=4, width=0.5)
    plt.savefig("graph"+i+".png", dpi=1000, quality=95)
    plt.show()
    t0 = time.perf_counter()
    ntx.floyd_warshall(G)
    elapsed_time[0].append(time.perf_counter() - t0)
    t0 = time.perf_counter()
    ntx.johnson(G)
    elapsed_time[1].append(time.perf_counter() - t0)
    print("finished_test")
    return elapsed_time
def ich(M, colNames=[], dictDefault=-1, useFullDistance=True, name='', stateFile='', randOrder=True, procs=1):
  progressFile = name+'_ich_progress'
  if name: saveState({}, [], progressFile, overwrite=True)
  if isinstance(M, nx.classes.graph.Graph) and useFullDistance:
    nodes = sorted(M.nodes())
    M = nx.floyd_warshall(M)
    M = [[int(M[u][v]) if (v in M and M[u][v]!=np.inf) else -1 for v in nodes] for u in nodes]
  distr = {}
  tags = {}
  chosen = []
  if stateFile:
    (tags, chosen) = readState(stateFile)[-1]
    for t in tags:
      if tags[t] not in distr: distr[tags[t]] = []
      distr[tags[t]].append(t)
  elif isinstance(M, list): tags = {i:'' for i in range(len(M))}
  elif isinstance(M, dict): tags = {i:'' for i in M}
  elif isinstance(M, nx.classes.graph.Graph): tags = {i:'' for i in M.nodes()}
  n = len(tags)
  check = colNames if (isinstance(M, dict) and colNames) else (sorted(M[M.keys()[0]].keys()) if isinstance(M, dict) else sorted(tags.keys()))
  for col in chosen: check.remove(col)
  check = list(np.random.permutation(check)) if randOrder else sorted(check)
  while len(distr) < n and len(chosen) < n:
    (distr, tags, _, chosen) = pickColumn(M, tags, check, chosen=chosen, dictDefault=dictDefault, procs=procs)
    check.remove(chosen[-1])
    if name: saveState(tags, chosen, progressFile, overwrite=True)
  if len(distr) < n and len(chosen) == n: return 'NO SOLUTION EXISTS'
  return chosen
Esempio n. 8
0
    def __init__(self, file_name):
        """Sets up basic Information about the graph

        graph.labels = [all original labels with index = network x node number] (i.e graph.labels[0] == 'soda')

        graph.homes = [node numbers of homes] with len() = k

        graph.nxgraph = network x graph with number [1, 2, ..., n]

        graph.num_houses = number of TAs (k)
        
        graph.num_locs = number of locations (n)

        graph.dist = shorest all paths computed dynamically by floyd_warshall
        (i.e graph.dist[0][0] == 0)

        """
        data = read_file(file_name)
        parsed = data_parser(data)

        self.parsed = parsed

        self.num_locs = parsed[0]
        self.num_houses = parsed[1]
        self.labels = parsed[2]

        homes = parsed[3]
        start = parsed[4]
        matrix = parsed[5]

        self.homes = [self.labels.index(node) for node in homes]
        self.start = self.labels.index(start)

        self.nxgraph = adjacency_matrix_to_graph(matrix)[0]
        self.dist = nx.floyd_warshall(self.nxgraph)
Esempio n. 9
0
def moving(G, F):
    d = nx.floyd_warshall(G, weight='weight')
    # res[i - 1][0]: the result to unload ith item w/o carrying the (i + 1)th item
    # res[i - 1][1]: the result to unload ith item w carrying the (i + 1)th item
    res = [[]]
    try:
        res[0].append(d['1'][F[0][0]] + d[F[0][0]][F[0][1]])
        if len(F) == 1:
            return res[0][0]
        res[0].append(d['1'][F[0][0]] + d[F[0][0]][F[1][0]] +
                      d[F[1][0]][F[0][1]])

        for i in range(1, len(F)):
            res.append([])
            res[i].append(
                min(
                    res[i - 1][0] + d[F[i - 1][1]][F[i][0]] +
                    d[F[i][0]][F[i][1]],
                    res[i - 1][1] + d[F[i - 1][1]][F[i][1]]))
            if i == len(F) - 1:
                return res[i][0]
            res[i].append(
                min(
                    res[i - 1][0] + d[F[i - 1][1]][F[i][0]] +
                    d[F[i][0]][F[i + 1][0]] + d[F[i + 1][0]][F[i][1]],
                    res[i - 1][1] + d[F[i - 1][1]][F[i + 1][0]] +
                    d[F[i + 1][0]][F[i][1]]))
    except:
        return -1
Esempio n. 10
0
def expected_time(nodes, edges, p, g):
    G = nx.Graph()
    G.add_weighted_edges_from(g)

    lengths = nx.floyd_warshall(G)
    node_sum = {
            node: sum(d.values())
            for node, d in lengths.items()
    }

    cache = {}
    def recurse(last, depth):

        if (last, depth) not in cache:
            if depth == 1:
                cache[(last, depth)] = node_sum[last]
            else:
                cache[(last, depth)] = sum(
                    recurse(n, depth - 1)
                    for n in range(1, nodes + 1)
                    if n != last
                ) + node_sum[last]
            cache[(last, depth)] /= (nodes - 1)
        return cache[(last, depth)]

    #pprint(cache)
    return recurse(1, p)
Esempio n. 11
0
    def construct_diff(self):
        # real knowledge structure obtained by using IITA, without transitive closure
        real_knowledge_structure = nx.DiGraph(self.load_ks(self.e_ks.get()))

        # print(real_knowledge_structure.nodes())
        # transitive closure of the real knowledge structure
        tc_knowledge_structure = nx.DiGraph([(u, v, {
            'd': l
        }) for u, adj in nx.floyd_warshall(real_knowledge_structure).items()
                                             for v, l in adj.items()
                                             if l > 0 and l < float('inf')])

        # if there is any predecessor to the given node, than it is not in the fringe
        def in_fringe(node, graph):
            for edge in graph.edges():
                if edge[1] == node:
                    return False
            return True

        fringe = []
        complexity = []
        level = 1
        while tc_knowledge_structure.nodes():
            for node in tc_knowledge_structure.nodes():
                if in_fringe(node, tc_knowledge_structure):
                    fringe.append(node)
                    complexity.append((node, level))
            tc_knowledge_structure.remove_nodes_from(fringe)
            fringe = []
            level += 1

        self.txt_diff.delete(1.0, END)
        self.txt_diff.insert(END, "" + str(complexity))
 def __init__(self, train, test, path_feature=False):
     self.train = train
     self.test = test
     self.info = json.load(open("attributes.json"))
     if path_feature:
         self.shortest_path = dict(nx.floyd_warshall(train,
                                                     weight='weight'))
Esempio n. 13
0
def cost_of_solution_no_print(G, car_cycle, dropoff_mapping):
    cost = 0
    dropoffs = dropoff_mapping.keys()
    if not is_valid_walk(G, car_cycle):
        print('This is not a valid walk for the given graph.\n')
        sys.exit()

    if not car_cycle[0] == car_cycle[-1]:
        print('The start and end vertices are not the same.\n')
        sys.exit()
    if cost != 'infinite':
        if len(car_cycle) == 1:
            car_cycle = []
        else:
            car_cycle = get_edges_from_path(
                car_cycle[:-1]) + [(car_cycle[-2], car_cycle[-1])]
        if len(car_cycle) != 1:
            driving_cost = sum([G.edges[e]['weight']
                                for e in car_cycle]) * 2 / 3
        else:
            driving_cost = 0
        walking_cost = 0
        shortest = dict(nx.floyd_warshall(G))

        for drop_location in dropoffs:
            for house in dropoff_mapping[drop_location]:
                walking_cost += shortest[drop_location][house]
        cost = driving_cost + walking_cost
    return cost
Esempio n. 14
0
def plot_path_length(g, directory):

    fig1, ax1 = plt.subplots()
    fig1.subplots_adjust(left=0.15)
    hist = []
    am = {}

    table = nx.floyd_warshall(g)

    for n in list(table.values()):
        #print(n.values())
        for k in list(n.values()):
            #hist[int(k)] = hist.get(int(k), 0) + 1
            if k < 10000:
                if int(k) != 0:
                    hist.append(k)
                    am[int(k)] = am.get(int(k), 0) + 1

    plt.hist(hist,
             len(am.values()) - 1,
             facecolor='blue',
             edgecolor='darkblue',
             alpha=0.75,
             align='left')
    ax1.set_xlabel("Path length")
    ax1.set_ylabel("Paths")
    plt.savefig("plots/{}/histogram_shortest_path".format(directory))
    print("AVERAGE: ", sum(hist) / len(hist))
	def compute_greedy_heuristic(self):
		"""
		Computes WMSCP Greedy Heuristic. self.graph is the NetworkX graph. self.clusters is a list of lists of node incides 
		where each list corresponds to the list of nodes in each cluster (so [[0,1,2,3],[4,5,6,7]] would be two clusters with 0-3 
		in one cluster and 4-7 in other).
		"""
		controller_set = []  # Stores controller indices
		distances = nx.floyd_warshall(self.graph, 'weight')  # Stores distances between every node
		weights_index = 0  # Converts index of minimum weight to actual node index (only works because clusters are in-order and sorted
		for i in range(len(self.clusters)):
			# Go through each node in cluster and compute weights
			weights = []
			for node in self.clusters[i]:
				# Go through every cluster and find shortest distance, shortest distance to controller
				weight = 0  # Stores weight of this node
				for j in range(len(self.clusters)):
					if i == j:  # Matching cluster, ignore
						continue
					cluster_distance = 1e6
					controller_distance = 0
					for cluster_node in self.clusters[j]:
						# Go through every node in other clusters and compute distance to find shortest node per cluster
						cluster_distance = min(cluster_distance, distances[node][cluster_node])
						if cluster_node in controller_set:
							# If node is a controller, add its distance a "second" time to correspond 
							# to second sigma-sum of weight calculation
							assert controller_distance == 0, print(cluster_node, controller_set)
							controller_distance = distances[node][cluster_node]
					assert cluster_distance != 1e6, "Cluster distance was too low, it was not modified"
					weight += cluster_distance + controller_distance  # Add to node weight
					weight /= len(self.clusters[i]) + 1  # Divide by nodes covered (the length of cluster upcoming controller is in)
				weights.append(weight)
			controller_set.append(weights.index(min(weights)) + weights_index)  # Add node index to controller list
			weights_index += len(self.clusters[i])
		return controller_set, super().step(controller_set)  # Return controller list and corresponding cost (minimum distance between all controllers)
Esempio n. 16
0
def cost_of_solution(G, car_cycle, dropoff_mapping):
    cost = 0
    message = ''
    dropoffs = dropoff_mapping.keys()
    if not is_valid_walk(G, car_cycle):
        message += 'This is not a valid walk for the given graph.\n'
        cost = 'infinite'

    if not car_cycle[0] == car_cycle[-1]:
        message += 'The start and end vertices are not the same.\n'
        cost = 'infinite'
    if cost != 'infinite':
        if len(car_cycle) == 1:
            car_cycle = []
        else:
            car_cycle = get_edges_from_path(car_cycle[:-1]) + [(car_cycle[-2], car_cycle[-1])]
        driving_cost = sum([G.edges[e]['weight'] for e in car_cycle]) * 2 / 3
        walking_cost = 0
        shortest = dict(nx.floyd_warshall(G))

        for drop_location in dropoffs:
            for house in dropoff_mapping[drop_location]:
                walking_cost += shortest[drop_location][house]

        message += f'The driving cost of your solution is {driving_cost}.\n'
        message += f'The walking cost of your solution is {walking_cost}.\n'
        cost = driving_cost + walking_cost

    message += f'The total cost of your solution is {cost}.\n'
    return cost, message
Esempio n. 17
0
def eloc_node(G, xNode):
    '''
    A function to calculate the nodal local efficiency
    from a node xNode.
    
    input parameters:
          G:      A graph in networkX format.
          xNode:  The node where the nodal global efficiency is calculated.

    returns:
          Eloc:   The nodal local efficiency at node xNode.
    '''

    subG = subgraph(G, xNode)
    #Eloc, tmpEloci, tmpNodes = eglob_net(subG)
    NNodes = len(subG.nodes())
    if NNodes>1:
        #Dij = nx.all_pairs_shortest_path_length(subG)
        Dij = nx.floyd_warshall(subG)
        D = [Dij[i].values() for i in subG.nodes()]
        cD = []
        for irow in D:
            cD += irow            
        NZD = np.array(cD)[np.nonzero(cD)]
        if len(NZD)>0:
            Eloc = (1.0/(NNodes*(NNodes-1.0))) * np.sum(1.0/NZD)
        else:
            Eloc = 0
    else:
        Eloc = 0
    return Eloc
Esempio n. 18
0
    def WD(self) -> (np.array, np.array):
        """
        Creates the matrices W and D.

        :return: W, D matrices as 2D np.array
        """

        # makes a copy of the original graph
        # could it be expensive?
        new_g = nx.DiGraph()
        n_vertices = self.g.number_of_nodes()
        W = np.empty((n_vertices, n_vertices), dtype=np.int)
        D = np.empty((n_vertices, n_vertices))

        # 1. Weight each edge u-e->_ in E with the ordered
        # pair (w(e), -d(u))
        new_g.add_weighted_edges_from([
            (u, v, CustomWeight(self.g.edges[u, v]["weight"], -self.delay[u]))
            for (u, v) in self.g.edges
        ])

        # 2. compute the all-pairs shortest paths
        # add of the weights: componentwise addition
        # comparison of the weights: lexicographic order
        path_len = dict(nx.floyd_warshall(new_g))

        # 3. For each u,v vertices, their shortest path weight is (x,y)
        # set W(u,v) = x, D(u,v) = d(v) - y
        for u in new_g.nodes:
            for v in new_g.nodes:
                cw = path_len[u][v]
                W[int(u), int(v)] = cw.x
                D[int(u), int(v)] = self.delay[v] - cw.y

        return W, D
Esempio n. 19
0
def eloc_node(G, xNode):
    '''
    A function to calculate the nodal local efficiency
    from a node xNode.
    
    input parameters:
          G:      A graph in networkX format.
          xNode:  The node where the nodal global efficiency is calculated.
    returns:
          Eloc:   The nodal local efficiency at node xNode.
    '''

    subG = subgraph(G, xNode)
    #Eloc, tmpEloci, tmpNodes = eglob_net(subG)
    NNodes = len(subG.nodes())
    if NNodes > 1:
        #Dij = nx.all_pairs_shortest_path_length(subG)
        Dij = nx.floyd_warshall(subG)
        D = [Dij[i].values() for i in subG.nodes()]
        cD = []
        for irow in D:
            cD += irow
        NZD = np.array(cD)[np.nonzero(cD)]
        if len(NZD) > 0:
            Eloc = (1.0 / (NNodes * (NNodes - 1.0))) * np.sum(1.0 / NZD)
        else:
            Eloc = 0
    else:
        Eloc = 0
    return Eloc
Esempio n. 20
0
def is_metricFlexible(G):
    shortest = dict(nx.floyd_warshall(G))
    for u, v, datadict in G.edges(data=True):
        if abs(shortest[u][v] - datadict["weight"]) >= 0.00001:
            print(u, v, shortest[u][v])
            return False
    return True
Esempio n. 21
0
def is_metric(G):
    shortest = dict(nx.floyd_warshall(G))
    for u, v, datadict in G.edges(data=True):
        assert shortest[u][v] == datadict[
            'weight'], 'Direct path from {} to {} (weight {}) is not shortest path (weight {})'.format(
                u, v, datadict['weight'], shortest[u][v])
    return True
Esempio n. 22
0
def chinese_postman(g, weight="weight"):
    """
    中国人郵便配達問題
    入力
        g: 無向グラフ
        weight: 重みの属性文字
    出力
        距離と頂点リスト
    """
    import networkx as nx
    from itertools import combinations

    assert not g.is_directed()
    g = nx.MultiGraph(g)
    subnd = [nd for nd, dg in g.degree() if dg % 2 == 1]  # 奇数次数ノード群
    dd = nx.floyd_warshall(g, weight=weight)  # 完全距離表
    mx = max(d for dc in dd.values() for d in dc.values())  # 最大距離
    h = nx.Graph()
    for i, j in combinations(subnd, 2):
        h.add_edge(i, j, weight=mx - dd[i][j])
    for i, j in nx.max_weight_matching(h, True):  # 最大重み最大マッチング問題
        g.add_edge(i, j, weight=dd[i][j])
    return (
        sum(d[weight] for (i, j, _), d in g.edges.items()),
        list(nx.eulerian_circuit(g)),
    )
def hierarchy_clustering(G):
    N = len(G)
    S = np.zeros((N, N))

    floyd = nx.floyd_warshall(G)
    for i in range(N):
        for j in range(N):
            S[i, j] = floyd[i][j]
    # print(S)

    Y = squareform(S)
    Z = hierarchy.linkage(Y, method='average')
    hierarchy.dendrogram(Z)
    plt.show()
    clusters = hierarchy.fcluster(Z, 3.25, criterion='distance')
    n_clusters = max(clusters)
    print(n_clusters)

    nodes = np.arange(N)
    for i in range(1, n_clusters + 1):
        GG = G.subgraph(nodes[clusters == i])
        # print(len(GG), GG.edges())
        nx.draw(GG)
        plt.show()

    return clusters
Esempio n. 24
0
def MakeCostMatrix(d):
    """ Make a ransom metrix matrix as described in Cuturi 2013 (Figure 4) """
    G = nx.erdos_renyi_graph(d, 0.5)
    for u, v, w in G.edges(data=True):
        w['weight'] = np.random.uniform(0, 1)
    # All pair shortest path
    M = nx.floyd_warshall(G)
    return M
Esempio n. 25
0
    def run(self):
        start = time.time()

        answer = nx.floyd_warshall(self.data['g'])

        if self.verbose:
            print('FloydWarshall took %.2f s.' % (time.time() - start))
        return answer
Esempio n. 26
0
def computeGraphDistances(G):
    '''
        Computes all pairs shortest paths on the given graph.
    '''

    G_undirected = nx.Graph(G)
    distances = nx.floyd_warshall(G_undirected)
    return distances
Esempio n. 27
0
    def test_floyd_warshall_predecessor_and_distance(self):
        XG = nx.DiGraph()
        XG.add_weighted_edges_from([('s', 'u', 10), ('s', 'x', 5),
                                    ('u', 'v', 1), ('u', 'x', 2),
                                    ('v', 'y', 1), ('x', 'u', 3),
                                    ('x', 'v', 5), ('x', 'y', 2),
                                    ('y', 's', 7), ('y', 'v', 6)])
        path, dist = nx.floyd_warshall_predecessor_and_distance(XG)
        assert dist['s']['v'] == 9
        assert path['s']['v'] == 'u'
        assert (dist ==
                {'y': {'y': 0, 'x': 12, 's': 7, 'u': 15, 'v': 6},
                 'x': {'y': 2, 'x': 0, 's': 9, 'u': 3, 'v': 4},
                 's': {'y': 7, 'x': 5, 's': 0, 'u': 8, 'v': 9},
                 'u': {'y': 2, 'x': 2, 's': 9, 'u': 0, 'v': 1},
                 'v': {'y': 1, 'x': 13, 's': 8, 'u': 16, 'v': 0}})

        GG = XG.to_undirected()
        # make sure we get lower weight
        # to_undirected might choose either edge with weight 2 or weight 3
        GG['u']['x']['weight'] = 2
        path, dist = nx.floyd_warshall_predecessor_and_distance(GG)
        assert dist['s']['v'] == 8
        # skip this test, could be alternate path s-u-v
#        assert_equal(path['s']['v'],'y')

        G = nx.DiGraph()  # no weights
        G.add_edges_from([('s', 'u'), ('s', 'x'),
                          ('u', 'v'), ('u', 'x'),
                          ('v', 'y'), ('x', 'u'),
                          ('x', 'v'), ('x', 'y'),
                          ('y', 's'), ('y', 'v')])
        path, dist = nx.floyd_warshall_predecessor_and_distance(G)
        assert dist['s']['v'] == 2
        # skip this test, could be alternate path s-u-v
        # assert_equal(path['s']['v'],'x')

        # alternate interface
        dist = nx.floyd_warshall(G)
        assert dist['s']['v'] == 2

        # floyd_warshall_predecessor_and_distance returns
        # dicts-of-defautdicts
        # make sure we don't get empty dictionary
        XG = nx.DiGraph()
        XG.add_weighted_edges_from([('v', 'x', 5.0), ('y', 'x', 5.0),
                                    ('v', 'y', 6.0), ('x', 'u', 2.0)])
        path, dist = nx.floyd_warshall_predecessor_and_distance(XG)
        inf = float("inf")
        assert (dist ==
                {'v': {'v': 0, 'x': 5.0, 'y': 6.0, 'u': 7.0},
                 'x': {'x': 0, 'u': 2.0, 'v': inf, 'y': inf},
                 'y': {'y': 0, 'x': 5.0, 'v': inf, 'u': 7.0},
                 'u': {'u': 0, 'v': inf, 'x': inf, 'y': inf}})
        assert (path ==
                {'v': {'x': 'v', 'y': 'v', 'u': 'x'},
                 'x': {'u': 'x'},
                 'y': {'x': 'y', 'u': 'x'}})
Esempio n. 28
0
def is_metric(G):
    shortest = dict(nx.floyd_warshall(G))
    answer = True
    for u, v, datadict in G.edges(data=True):
        if abs(shortest[u][v] - datadict['weight']) >= 0.00001:
            print(shortest[u][v])
            print(datadict['weight'])
            answer = False
    return answer
Esempio n. 29
0
def check_triangle(adj): 
    matrix = np.matrix(adj)
    G = nx.from_numpy_matrix(matrix) 
    shortest = dict(nx.floyd_warshall(G))
    for u, v, datadict in G.edges(data=True):
        val = datadict['weight']
        if not isinstance(val, str) and abs(shortest[u][v] - datadict['weight']) >= 0.00001:
            assert False
    print("Adjacency matrix satisfies triangle inequality")
Esempio n. 30
0
    def __init__(self, G=None):
        self.G = G if G else self.createGraph()
        self.nodes = list(self.G.nodes)
        self.number_of_nodes = self.G.number_of_nodes
        self.edges = sorted(graph, key=lambda x: x[2])

        print('networkx: ', nx.floyd_warshall(self.G))
        self.floyd()
        self.draw()
Esempio n. 31
0
def floyd_warshall(distance):
    '''
    :param distance: distance model with maxsize
    :return: shortest_dis, predecessors
    '''
    G = nx.DiGraph(distance)
    shortest_dis = nx.floyd_warshall(G)
    predecessors, _ = nx.floyd_warshall_predecessor_and_distance(G)
    return shortest_dis, predecessors
Esempio n. 32
0
def get_pred_path(G,v1,v2):
    pred = nx.floyd_warshall(G)[1]
    edges = deque([])
    u = v2
    while u != v1:
        edges.appendleft((pred[v1][u],u,G[pred[v1][u]][u]['e']))
        u = pred[v1][u]

    return edges
Esempio n. 33
0
def makeMetricCompleteFlexible(graph):
    data = graph['data']
    npy = np.array(data)
    shortest = dict(nx.floyd_warshall(nx.from_numpy_matrix(npy)))
    for i in range(len(data)):
        for j in range(i, len(data)):
            if (data[i][j] == graph['header'][3]):
                data[i][j], data[j][
                    i] = shortest[i][j] - .0001, shortest[i][j] - .0001,
Esempio n. 34
0
def get_paths(data, edgeDict=None, graph=None, algorithm='Floyd', weights='aff',
              saveResults=False, filepathDist=None):
    '''
    a function that computes all pairwise distances (shortest paths)
    and puts it into a dictionary.
    Input: graph with edges as distances OR
    (default) dict with pairwise affinities (not distances)
    Output: shortest paths in a dict of dicts.
    '''
    unmatched = []
    distGraph = nx.Graph()
    if weights == 'aff':
        print "computing 1/affinities..."
        for i in data:
            matched = 0
            if i in edgeDict:
                distGraph.add_node(i)
                matched = 1
                for j in edgeDict[i]:
                    if edgeDict[i][j] == 0:
                        distGraph.add_edge(i, j, weight='inf')
                    else:
                        distGraph.add_edge(i, j, weight=1.0 / edgeDict[i][j])
            if matched == 0: unmatched.append(i)
    elif weights == 'dist':
        distGraph = graph
    else:
        print "Error: weight should be either 'aff' or 'dist'"
        return 1

    print "number of unmatched nodes: ", len(unmatched)
    print unmatched

    if algorithm == 'Floyd':
        print "\ncomputing all shortest paths with Floyd-Warshall algorithm..."
        dist = nx.floyd_warshall(distGraph)
        print "pairwise distances obtained"
    elif algorithm == "Dijkstra":
        print "\ncomputing all shortest paths with Dijkstra algorithm..."
        dist = nx.all_pairs_dijkstra_path_length(distGraph)
        print "pairwise distances obtained"
    else:
        print "\nError: cannot recognize what algorithm to use"
        return 1

    print "num of rows in dist matrix: ", len(dist.keys())
    # print dist.keys()

    if saveResults:
        if filepathDist == None:
            #filepathDist = "/home/krybachuk/SHORTESTPATH_" + VersionTime
            filepathDist = "SHORTESTPATH_" + "latest"
        json.dump(dist, open(filepathDist, 'w'))
        print "shortest path dict saved\n\n"

    return dist
Esempio n. 35
0
def test_shortest_path_all_pairs(iterations=20):
    for i in range(iterations):
        g = SmallWorldGraph(20, 4, 0.5)
        print("Finding my results: ")
        my_results = g.shortest_path_all_pairs()
        print("Finding nx results: ")
        nx_results = nx.floyd_warshall(g)
        for key in my_results:
            my_res = my_results[key]
            nx_res = nx_results[key[0]][key[1]]
            if not my_res == nx_res:
                print("Mismatch!")
Esempio n. 36
0
def prob_err(tree, leaves, budget):
    
    assert budget >= 2
    
    distance_matrix = nx.floyd_warshall(tree)
    opt_err = 2
    opt_sensors = []
    for sensors in itertools.combinations(leaves, budget):
        (classes, cardinalities) = equivalence_classes(distance_matrix, sensors)
        err = prob_err_from_cardinalities(cardinalities)
        if err < opt_err:
            opt_err = err
            opt_sensors = sensors
    return (opt_err, opt_sensors)
Esempio n. 37
0
def exp_dist(tree, leaves, budget):
    
    assert budget >= 2
    
    distance_matrix = nx.floyd_warshall(tree)
    opt_exp_dist = sum([sum(distance_matrix[u].values()) for u in
            distance_matrix])
    opt_sensors = []
    for sensors in itertools.combinations(leaves, budget):
        (classes, cardinalities) = equivalence_classes(distance_matrix, sensors)
        exp_dist = exp_dist_from_classes(distance_matrix, classes)
        if exp_dist < opt_exp_dist:
            opt_exp_dist = exp_dist
            opt_sensors = sensors
    return (opt_exp_dist, opt_sensors)
def floyd_warshall_solution_remove_stations(cityMap):
  nodes = cityMap.g.nodes()
  for v in nodes:
    if v not in cityMap.rv:
      cityMap.g.remove_node(v)

  all_short = networkx.floyd_warshall(cityMap.g)
  route = []
  for v in cityMap.g.nodes():
    route.append(min([node for node in all_short[v] if node not in route], key=lambda x: all_short[v][x]))

  routes = []
  segment = int(len(route) / cityMap.number_of_busses) + 1
  for i in range(segment):
    routes.append(route[i*segment: i*segment + segment])
    cityMap.set_route(i+1, route[i*segment: i*segment + segment + 1])
  return cityMap
Esempio n. 39
0
def inverse_weight_shortest_path(G,weight_name='weight',dir='./',filename=' '):
	if G.is_directed():
		T=nx.DiGraph();
	else: 
		T=nx.Graph();
	T.add_nodes_from(G.nodes(data=True));	
	T.add_edges_from(G.edges());
	for e in G.edges(data=True):
		T[e[0]][e[1]][weight_name]=float(1/float(G[e[0]][e[1]][weight_name]));
	
	distances=nx.floyd_warshall(T, weight=weight_name);
	
	if filename!=' ':
		nx.write_gexf(T,dir+filename.split('.')[-2]+'_inverse_edge_weight.gexf');
		pickle.dump(distances,open(dir+filename+'_inverse_weight_shortest_path.pck','w'));
		print 'Distance file saved to '+dir+filename+'_inverse_weight_shortest_path.pck'
	return distances;
Esempio n. 40
0
    def test_floyd_warshall(self):
        XG=nx.DiGraph()
        XG.add_weighted_edges_from([('s','u',10) ,('s','x',5) ,
                                    ('u','v',1) ,('u','x',2) ,
                                    ('v','y',1) ,('x','u',3) ,
                                    ('x','v',5) ,('x','y',2) ,
                                    ('y','s',7) ,('y','v',6)])
        dist, path =nx.floyd_warshall(XG)
        assert_equal(dist['s']['v'],9)
        assert_equal(path['s']['v'],'u')
        assert_equal(dist,
                     {'y': {'y': 0, 'x': 12, 's': 7, 'u': 15, 'v': 6}, 
                      'x': {'y': 2, 'x': 0, 's': 9, 'u': 3, 'v': 4}, 
                      's': {'y': 7, 'x': 5, 's': 0, 'u': 8, 'v': 9}, 
                      'u': {'y': 2, 'x': 2, 's': 9, 'u': 0, 'v': 1}, 
                      'v': {'y': 1, 'x': 13, 's': 8, 'u': 16, 'v': 0}})


        GG=XG.to_undirected()
        dist, path = nx.floyd_warshall(GG)
        assert_equal(dist['s']['v'],8)
        assert_equal(path['s']['v'],'y')

        G=nx.DiGraph()  # no weights
        G.add_edges_from([('s','u'), ('s','x'), 
                          ('u','v'), ('u','x'), 
                          ('v','y'), ('x','u'), 
                          ('x','v'), ('x','y'), 
                          ('y','s'), ('y','v')])
        dist, path = nx.floyd_warshall(G)
        assert_equal(dist['s']['v'],2)
        assert_equal(path['s']['v'],'x')


        dist, path = nx.floyd_warshall(self.cycle)
        assert_equal(dist[0][3],3)
        assert_equal(path[0][3],2)
        assert_equal(dist[0][4],3)

        XG3=nx.Graph()
        XG3.add_weighted_edges_from([ [0,1,2],[1,2,12],[2,3,1],
                                      [3,4,5],[4,5,1],[5,0,10] ])
        dist, path = nx.floyd_warshall(XG3)
        assert_equal(dist[0][3],15)
        assert_equal(path[0][3],2)

        XG4=nx.Graph()
        XG4.add_weighted_edges_from([ [0,1,2],[1,2,2],[2,3,1],
                                      [3,4,1],[4,5,1],[5,6,1],
                                      [6,7,1],[7,0,1] ])
        dist, path = nx.floyd_warshall(XG4)
        assert_equal(dist[0][2],4)
        assert_equal(path[0][2],1)
Esempio n. 41
0
def process(filename):
    edges, vertexesNumber, edgesNumber = read(filename)
    G = nx.DiGraph()
    G.add_nodes_from(xrange(1, vertexesNumber + 1))
    G.add_weighted_edges_from(edges)

    print "Graph %s formed" % filename

    if nx.negative_edge_cycle(G):
        print "Graph %s has negative cycle" % filename
        return

    print "Begin FW for %s" % filename
    lengths = nx.floyd_warshall(G)

    shortestPathLengths = map(dict.values, lengths.values())

    shortestPathLength = min(min(raw) for raw in shortestPathLengths)

    print "Shortest path for %s: %d" % (filename, shortestPathLength)
Esempio n. 42
0
    def test_floyd_warshall_predecessor_and_distance(self):
        XG=nx.DiGraph()
        XG.add_weighted_edges_from([('s','u',10) ,('s','x',5) ,
                                    ('u','v',1) ,('u','x',2) ,
                                    ('v','y',1) ,('x','u',3) ,
                                    ('x','v',5) ,('x','y',2) ,
                                    ('y','s',7) ,('y','v',6)])
        path, dist =nx.floyd_warshall_predecessor_and_distance(XG)
        assert_equal(dist['s']['v'],9)
        assert_equal(path['s']['v'],'u')
        assert_equal(dist,
                     {'y': {'y': 0, 'x': 12, 's': 7, 'u': 15, 'v': 6},
                      'x': {'y': 2, 'x': 0, 's': 9, 'u': 3, 'v': 4},
                      's': {'y': 7, 'x': 5, 's': 0, 'u': 8, 'v': 9},
                      'u': {'y': 2, 'x': 2, 's': 9, 'u': 0, 'v': 1},
                      'v': {'y': 1, 'x': 13, 's': 8, 'u': 16, 'v': 0}})


        GG=XG.to_undirected()
        # make sure we get lower weight
        # to_undirected might choose either edge with weight 2 or weight 3
        GG['u']['x']['weight']=2
        path, dist = nx.floyd_warshall_predecessor_and_distance(GG)
        assert_equal(dist['s']['v'],8)
        # skip this test, could be alternate path s-u-v
#        assert_equal(path['s']['v'],'y')

        G=nx.DiGraph()  # no weights
        G.add_edges_from([('s','u'), ('s','x'),
                          ('u','v'), ('u','x'),
                          ('v','y'), ('x','u'),
                          ('x','v'), ('x','y'),
                          ('y','s'), ('y','v')])
        path, dist = nx.floyd_warshall_predecessor_and_distance(G)
        assert_equal(dist['s']['v'],2)
        # skip this test, could be alternate path s-u-v
 #       assert_equal(path['s']['v'],'x')

        # alternate interface
        dist = nx.floyd_warshall(G)
        assert_equal(dist['s']['v'],2)
Esempio n. 43
0
    def test_floyd_warshall_predecessor_and_distance(self):
        XG=nx.DiGraph()
        XG.add_weighted_edges_from([('s','u',10) ,('s','x',5) ,
                                    ('u','v',1) ,('u','x',2) ,
                                    ('v','y',1) ,('x','u',3) ,
                                    ('x','v',5) ,('x','y',2) ,
                                    ('y','s',7) ,('y','v',6)])
        path, dist =nx.floyd_warshall_predecessor_and_distance(XG)
        assert_equal(dist['s']['v'],9)
        assert_equal(path['s']['v'],'u')
        assert_equal(dist,
                     {'y': {'y': 0, 'x': 12, 's': 7, 'u': 15, 'v': 6},
                      'x': {'y': 2, 'x': 0, 's': 9, 'u': 3, 'v': 4},
                      's': {'y': 7, 'x': 5, 's': 0, 'u': 8, 'v': 9},
                      'u': {'y': 2, 'x': 2, 's': 9, 'u': 0, 'v': 1},
                      'v': {'y': 1, 'x': 13, 's': 8, 'u': 16, 'v': 0}})


        GG=XG.to_undirected()
        path, dist = nx.floyd_warshall_predecessor_and_distance(GG)
        assert_equal(dist['s']['v'],8)
        assert_equal(path['s']['v'],'y')

        G=nx.DiGraph()  # no weights
        G.add_edges_from([('s','u'), ('s','x'),
                          ('u','v'), ('u','x'),
                          ('v','y'), ('x','u'),
                          ('x','v'), ('x','y'),
                          ('y','s'), ('y','v')])
        path, dist = nx.floyd_warshall_predecessor_and_distance(G)
        assert_equal(dist['s']['v'],2)
        assert_equal(path['s']['v'],'x')

        # alternate interface
        dist = nx.floyd_warshall(G)
        assert_equal(dist['s']['v'],2)
e = [(0, 1, 4), (2, 5, 4), (0, 7, 8), (3, 5, 14), (1, 2, 8), (3, 4, 9), (1, 7, 11), (4, 5, 10), (2, 8, 2),
     (5, 6, 2), (2, 3, 7), (6, 8, 6), (6, 7, 1), (7, 8, 7), (9, 0, 6), (10, 3, 1), (10, 4, 1), (11, 12, 1),
     (12, 13, 1), (11, 13, 1), (11, 3, 20), (13, 3, 20)]

#e = [(1, 2, 1), (2, 3, 1), (4, 2, 1), (4, 5, 1)]

G.add_weighted_edges_from(e)
pos = nx.spring_layout(G)

#############################################################################
#############################################################################
#############################################################################

density_q = G.degree(weight='weight')  # Density function with respect to the node.
D = nx.floyd_warshall(G, 'weight')  # Dictionary of shortest paths for each node.

n_k = 3  # Number of voroni nodes

#K = random.sample(G.nodes(), n_k)
K = [0, 8, 10, 12]
K = deque(K)

cont = 0
while cont < 10:

    print "K vector: ", K
    vor.parallel_dijkstra(G, K, 1)

    vor_seg = groups(nx.get_node_attributes(G,'V'))
Esempio n. 45
0
	#	if x == s1[y][0]:
	#		temp.append(s1[y][:])
	#		k=k+1
	# print x
	k,faltu = x.split()
	data[k]=sd[k]
	
	#if count == 50:
	#	break

fname = 'newcatalans.json'
f = open(fname, 'w')
data = json.dumps(data, indent=4)
f.write(data)
f.close()

h=open("newcatalans.json", 'r')
jd=json.loads(h.read())
G = nx.Graph(jd)

length = nx.floyd_warshall(G, weight='weight')

# print length

bname = 'fw_on_catalans.json'
b = open(bname, 'w')
data = json.dumps(length, indent=4)
b.write(data)
b.close()

Esempio n. 46
0
def shortest_path_weight(G,v1,v2):
    return nx.floyd_warshall(G)[0][v1][v2]
Esempio n. 47
0
NOC.add_edges_from([(1,3),(3,1)],cost_per_bit=0.02,latency=0.01)

w=[[1,10,10,10],[10,10,10,10],[1,10,10,10],[10,10,10,10],[10,10,10,10]]
#w=[[1,10,10,10],[10,1,10,10],[1,10,10,10],[10,10,1,10],[10,10,10,1]]
N=5
M=4

###########################################################################
f=open('results.txt','w')
results={}
cnt=0
threshold=60
better_solns=0
total=24576
best_cost=threshold
E_c=nx.floyd_warshall(NOC,'cost_per_bit')
L_c=nx.floyd_warshall(NOC,'latency')
sorted_nodes=nx.topological_sort(TG)

def modify_time(node):
    global t,Z,L_c,TG,h
    Q= queue.Queue()
    Q.put(node)
    while not Q.empty():
        i=Q.get()
        for j in TG.successors(i):
            m=Z[i].index(1)
            n=Z[j].index(1)
            t[j]=max(t[j],t[i]+h[i]+TG.edge[i][j]['volume']*L_c[m][n])
            Q.put(j)
Esempio n. 48
0
def main(path_nodes, path_edges, gid_nodes, gid_edges, weight_edges):
    # ----------------------------------------------------------
    # NetworkX
    # ----------------------------------------------------------
    # Import des données SHP à grapher:

    G = nx.Graph(name="Floyd_Warshall_script", date=str(datetime.datetime.now()))
    E = nx.read_shp(str(path_edges))
    N = nx.read_shp(str(path_nodes))

    G.add_nodes_from(N.nodes(data=True))
    G.add_edges_from(E.edges(data=True))

    # Changement de leur nom (pour l'instant, chaque valeur a le nom de ses coordonnées géographiques, c'est nul):
    # ----------------------------------------------------------
    # D'abord pour les edges:

    x = 0
    dict_edges = {}
    total_edges = G.number_of_edges()
    print("Processing Edges")
    while x < total_edges:
        try:  # En raison de la structure possible des fichiers, on place un try qui propose [1] ou [2], histoire de parer à toute éventualité.
            dict_edges[G.edges(data=True)[x][0]] = G.edges(data=True)[x][2][str(gid_edges)]
            x = x + 1
        except:
            dict_edges[G.edges(data=True)[x][0]] = G.edges(data=True)[x][1][str(gid_edges)]
            x = x + 1
    # Maintenant pour les nodes:

    x = 0
    dict_nodes = {}
    count_nodes = {}
    total_nodes = G.number_of_nodes()
    print("Processing Nodes")
    while x < total_nodes:
        try:  # En raison de la structure possible des fichiers, on place un try qui propose [1] ou [2], histoire de parer à toute éventualité.
            dict_nodes[G.nodes(data=True)[x][0]] = G.nodes(data=True)[x][1][str(gid_nodes)]
            count_nodes[x] = G.nodes(data=True)[x][1][str(gid_nodes)]
            x = x + 1
        except:
            dict_nodes[G.nodes(data=True)[x][0]] = G.nodes(data=True)[x][2][str(gid_nodes)]
            count_nodes[x] = G.nodes(data=True)[x][1][str(gid_nodes)]
            x = x + 1

    # On renomme les nodes/edges par leur id/gid originels:
    G = nx.relabel_nodes(G, dict_nodes)
    print("Processing Graph")
    # G=nx.relabel_edges(G,dict_edges) # Il semblerait que cette fonction n'existe pas... A rajouter manuellement, cela ne doit pas être bien dur.

    # ----------------------------------------------------------
    # On lance maintenant le calcul d'itinéraire pour l'ensemble des paires de noeuds du réseau:
    starting_point = datetime.datetime.now()
    results = nx.floyd_warshall(G, weight=str(weight_edges))
    print(starting_point)
    print(datetime.datetime.now())
    print("Il aura fallu:  " + str(datetime.datetime.now() - starting_point) + " pour effectuer le calcul")

    text_file = open("Shitty_output.txt", "w")
    text_file.write(str(results))
    text_file.close()

    open(
        "Output.txt", "w"
    ).close()  # On efface Output.txt avant de lancer des opérations "append" dessus, histoire d'eviter d'avoir un mélange de résultats de plusieurs fichiers.
    with open("Output.txt", "a") as text_file:

        gid = 0
        i = 0
        text_file.writelines("gid" + ";" + "Origin" + ";" + "Destination" + ";" + "FloydWarshall_Cost" + "\n")
        while i < total_nodes:

            origins = results[str(count_nodes[i])]
            i1 = 0
            for origin in origins:
                while i1 < total_nodes:
                    gid = gid + 1
                    destinations = origins[str(count_nodes[i1])]
                    text_file.writelines(
                        str(gid)
                        + ";"
                        + str(count_nodes[i] + ";" + str(count_nodes[i1]) + ";" + str(destinations))
                        + "\n"
                    )
                    i1 = i1 + 1
            i = i + 1
    text_file.close()
    print(
        'Un fichier .TXT nommé "Output" a maintenant du apparaitre dans le dossier depuis lequel nous avons chargé le shapefile'
    )
Esempio n. 49
0
#Calculate Betweenness Centrality
bcdict = nx.betweenness_centrality(G, normalized=False, weighted_edges=True)
for el1,el2 in bcdict.iteritems():
	print "NODE: ",el1, "\t" + "BC",el2
print "\n"

#Calculate NC
ncdict = bcdict.copy()
for el1 in ncdict.keys():
	ncdict[el1]=ncdict[el1]+(len(ncdict)-1)
	print "NODE: ",el1, "\t" + "NC",ncdict[el1]
print "\n"

#Calculate F.W.
fwdict = nx.floyd_warshall(G,huge=99)
#print "FW",fwdict	

#create and init a vector with my pl to other nodes	
mypl = bcdict.copy()
for i in mypl.iterkeys():
	mypl[i]=9999		


fcdict = bcdict.copy()
for i in fcdict.iterkeys():
	fcdict[i]=0		

# Average path length that entering node would have by selecting the node #node, even considering the already selected nodes
pldict = bcdict.copy()
for i in pldict.iterkeys():
Esempio n. 50
0
	def bm_networkx(self):
		nx.floyd_warshall(self.nx_ER_G)