def test_circulant_graph(self): # Ci_n(1) is the cycle graph for all n Ci6_1 = nx.circulant_graph(6, [1]) C6 = nx.cycle_graph(6) assert_edges_equal(Ci6_1.edges(), C6.edges()) # Ci_n(1, 2, ..., n div 2) is the complete graph for all n Ci7 = nx.circulant_graph(7, [1, 2, 3]) K7 = nx.complete_graph(7) assert_edges_equal(Ci7.edges(), K7.edges()) # Ci_6(1, 3) is K_3,3 i.e. the utility graph Ci6_1_3 = nx.circulant_graph(6, [1, 3]) K3_3 = nx.complete_bipartite_graph(3, 3) assert is_isomorphic(Ci6_1_3, K3_3)
def test_hkn_harary_graph(self): # When k == 1, the hkn_harary_graph(k,n) is # the path_graph(n) for (k, n) in [(1, 6), (1, 7)]: G1 = hkn_harary_graph(k, n) G2 = nx.path_graph(n) assert is_isomorphic(G1, G2) # When k is even, the hkn_harary_graph(k,n) is # the circulant_graph(n, list(range(1,k/2+1))) for (k, n) in [(2, 6), (2, 7), (4, 6), (4, 7)]: G1 = hkn_harary_graph(k, n) G2 = nx.circulant_graph(n, list(range(1, k // 2 + 1))) assert is_isomorphic(G1, G2) # When k is odd and n is even, the hkn_harary_graph(k,n) is # the circulant_graph(n, list(range(1,(k+1)/2)) plus [n/2]) for (k, n) in [(3, 6), (5, 8), (7, 10)]: G1 = hkn_harary_graph(k, n) L = list(range(1, (k + 1) // 2)) L.append(n // 2) G2 = nx.circulant_graph(n, L) assert is_isomorphic(G1, G2) # When k is odd and n is odd, the hkn_harary_graph(k,n) is # the circulant_graph(n, list(range(1,(k+1)/2))) with # n//2+1 edges added between node i and node i+n//2+1 for (k, n) in [(3, 5), (5, 9), (7, 11)]: G1 = hkn_harary_graph(k, n) G2 = nx.circulant_graph(n, list(range(1, (k + 1) // 2))) eSet1 = set(G1.edges) eSet2 = set(G2.edges) eSet3 = set() half = n // 2 for i in range(0, half + 1): # add half+1 edges between i and i+half eSet3.add((i, (i + half) % n)) assert eSet1 == eSet2 | eSet3 # Raise NetworkXError if k<1 k = 0 n = 0 pytest.raises(nx.NetworkXError, hkn_harary_graph, k, n) # Raise NetworkXError if n<k+1 k = 6 n = 6 pytest.raises(nx.NetworkXError, hkn_harary_graph, k, n)
def watts_strogatz(N, z, p): G = nx.circulant_graph(N, range(1, z + 1)) # will be useful to have some stuff node_labs = np.arange(N) edge_set = set(G.edges()) # ok let's check each edge for rewiring edges = G.edges() for e in edges: if rng.uniform() < p: # no self edges others = node_labs[node_labs != e[0]] # no duplicate edges for _ in others: target = rng.choice(others) new_edge = (e[0], target) if new_edge not in edge_set: G.remove_edge(e[0], e[1]) G.add_edge(e[0], target) break # remake the edge set (maybe not efficient) edge_set = set(G.edges()) return G
def load_graph(graph_config): """Loads graph from provided configuration dictionary.""" print("Graph:") if graph_config["type"] == "regular": num_nodes = graph_config["num nodes"] degree = graph_config["degree"] graph = nx.circulant_graph(num_nodes, range(degree // 2 + 1)) print("\tType: regular") print("\tNumber of Nodes: " + str(num_nodes)) print("\tDegree: " + str(degree)) elif graph_config["type"] == "full": graph = nx.complete_graph(graph_config["num nodes"]) print("\tType: fully connected") print("\tNumber of Nodes: " + str(graph_config["num nodes"])) else: raise ValueError("Graph type '" + graph_config["type"] + "' is not supported") return graph
def __generate_agents(self, population, average_degree): if self.network_type == "lattice": self.network = self.__generate_lattice(population) elif self.network_type == "ring": self.network = nx.circulant_graph(population, [1]) elif self.network_type == "ER": self.network = nx.random_regular_graph(average_degree, population) elif self.network_type == "Complete": self.network = nx.complete_graph(population) elif self.network_type == "WS": self.network = nx.watts_strogatz_graph(population, average_degree, 0.5) elif self.network_type == "BA-SF": rearange_edges = int(average_degree * 0.5) self.network = nx.barabasi_albert_graph(population, rearange_edges) agents = [Agent() for id in range(population)] if self.network_type == "lattice": n = int(np.sqrt(population)) for index, focal in enumerate(agents): neighbors_id = list(self.network[int(index // n), int(index % n)]) for (x, y) in neighbors_id: nb_id = int(x * n + y) focal.neighbors_id.append(nb_id) # When using another topology else: for index, focal in enumerate(agents): neighbors_id = list(self.network[index]) for nb_id in neighbors_id: focal.neighbors_id.append(nb_id) return agents
def test_result_circulant_graph_100(self): assert (calc_and_compare(NX.circulant_graph(100, [1, 2, 3])))
def test_hnm_harary_graph(self): # When d is even and r = 0, the hnm_harary_graph(n,m) is # the circulant_graph(n, list(range(1,d/2+1))) for (n, m) in [(5, 5), (6, 12), (7, 14)]: G1 = hnm_harary_graph(n, m) d = 2 * m // n G2 = nx.circulant_graph(n, list(range(1, d // 2 + 1))) assert is_isomorphic(G1, G2) # When d is even and r > 0, the hnm_harary_graph(n,m) is # the circulant_graph(n, list(range(1,d/2+1))) # with r edges added arbitrarily for (n, m) in [(5, 7), (6, 13), (7, 16)]: G1 = hnm_harary_graph(n, m) d = 2 * m // n G2 = nx.circulant_graph(n, list(range(1, d // 2 + 1))) assert set(G2.edges) < set(G1.edges) assert G1.number_of_edges() == m # When d is odd and n is even and r = 0, the hnm_harary_graph(n,m) # is the circulant_graph(n, list(range(1,(d+1)/2) plus [n//2]) for (n, m) in [(6, 9), (8, 12), (10, 15)]: G1 = hnm_harary_graph(n, m) d = 2 * m // n L = list(range(1, (d + 1) // 2)) L.append(n // 2) G2 = nx.circulant_graph(n, L) assert is_isomorphic(G1, G2) # When d is odd and n is even and r > 0, the hnm_harary_graph(n,m) # is the circulant_graph(n, list(range(1,(d+1)/2) plus [n//2]) # with r edges added arbitrarily for (n, m) in [(6, 10), (8, 13), (10, 17)]: G1 = hnm_harary_graph(n, m) d = 2 * m // n L = list(range(1, (d + 1) // 2)) L.append(n // 2) G2 = nx.circulant_graph(n, L) assert set(G2.edges) < set(G1.edges) assert G1.number_of_edges() == m # When d is odd and n is odd, the hnm_harary_graph(n,m) is # the circulant_graph(n, list(range(1,(d+1)/2)) # with m - n*(d-1)/2 edges added arbitrarily for (n, m) in [(5, 4), (7, 12), (9, 14)]: G1 = hnm_harary_graph(n, m) d = 2 * m // n L = list(range(1, (d + 1) // 2)) G2 = nx.circulant_graph(n, L) assert set(G2.edges) < set(G1.edges) assert G1.number_of_edges() == m # Raise NetworkXError if n<1 n = 0 m = 0 pytest.raises(nx.NetworkXError, hnm_harary_graph, n, m) # Raise NetworkXError if m < n-1 n = 6 m = 4 pytest.raises(nx.NetworkXError, hnm_harary_graph, n, m) # Raise NetworkXError if m > n(n-1)/2 n = 6 m = 16 pytest.raises(nx.NetworkXError, hnm_harary_graph, n, m)
if __name__ == '__main__': config_location, save_location = get_args() config_dir, _ = path.split(config_location) with open(config_location, 'rb') as file_handle: config = json.load(file_handle) random.seed(config["seed"]) np.random.seed(random.getrandbits(32)) # generate graph graph_type = config["graph"]["type"] if graph_type == "regular": num_nodes = config["graph"]["num nodes"] degree = config["graph"]["degree"] graph = nx.circulant_graph(num_nodes, range(degree//2 +1)) elif graph_type == "full": graph = nx.complete_graph(config["graph"]["num nodes"]) else: raise ValueError(f"Graph type '{graph_type}' is not supported") # load strategies strategies = {} for strategy_cfg in config["strategies"]: strategy_type = strategy_cfg["type"] if strategy_type == "constant": # add strategy to strategies dictionary strategies[strategy_cfg["name"]] = { "action num" : get_action_num(strategy_cfg["action"]),
# print('Compute Time for graphs of size',len(p1),'and',len(p2),':',end-start,'seconds') # plt.figure() fig = draw_geodesic_with_node_weights_fixed_coupling_v2( nx.to_numpy_array(G1), nx.to_numpy_array(G2), p1, p2, nodePos_matrix1, nodePos_matrix2, opt_coups[np.argmin(losses)]) fig.suptitle('Binary tree-HK20', fontsize=12) fig.savefig('res_binary_tree-HK20.pdf', bbox_inches='tight') ## Cycle to circulant num_nodes_1 = 20 num_nodes_2 = 20 distribution_exponent = 0 G1 = nx.cycle_graph(num_nodes_1) G2 = nx.circulant_graph(num_nodes_2, [1, 2, 3, 5]) nodePos1 = nx.kamada_kawai_layout(G1) nodePos2 = nx.kamada_kawai_layout(G2) nodePos_matrix1 = np.array(list(nodePos1.values())) nodePos_matrix2 = np.array(list(nodePos2.values())) p1 = node_distribution(G1, 0, distribution_exponent) p2 = node_distribution(G2, 0, distribution_exponent) num_steps = 100 num_skips = 1000 # Sample probability polytope A, b = gw_equality_constraints(p1, p2)
import networkx as nx from src import sparseToBND as sp from src import draw as dr from src import utils as ut n = 10 m = 2 G = nx.circulant_graph(n, [i for i in range(1, m + 1)], create_using=nx.DiGraph) ut.normalizeGraph(G) dr.printInput(G) res = sp.sparseToBND(G) N = res[0] dr.printRes(G, N, None)
fig = plt.figure() nx.draw(G, with_labels=True) fig.text(0.02, 0.1, "complete multipartite graph", fontweight='bold') fig.text(0.02, 0.05, "three subsets whose node counts are 1, 2, and 3") plt.show() #circular ladder graph G = nx.circular_ladder_graph(n) fig = plt.figure() nx.draw(G, with_labels=True) fig.text(0.02, 0.95, "circular ladder graph", fontweight='bold') fig.text(0.02, 0.90, "n(node count) = " + str(n)) plt.show() #circulant graph G = nx.circulant_graph(10, [1, 2]) fig = plt.figure() nx.draw(G, with_labels=True) fig.text(0.02, 0.95, "circulant graph", fontweight='bold') fig.text(0.02, 0.90, "n(node count):10 with offset:1, 2") plt.show() #cycle graph G = nx.cycle_graph(n) fig = plt.figure() nx.draw(G, with_labels=True) fig.text(0.02, 0.95, "cycle graph", fontweight='bold') fig.text(0.02, 0.90, "n(node count) = " + str(n)) plt.show() #dorogovtsev_goltsev_mendes_graph