def test_graph_from_pr_2053(): G = nx.Graph() G.add_edges_from([ ("A", "B"), ("A", "D"), ("A", "F"), ("A", "G"), ("B", "C"), ("B", "D"), ("B", "G"), ("C", "D"), ("C", "E"), ("C", "Z"), ("D", "E"), ("D", "F"), ("E", "F"), ("E", "Z"), ("F", "Z"), ("G", "Z"), ]) for flow_func in flow_funcs: kwargs = dict(flow_func=flow_func) errmsg = f"Assertion failed in function: {flow_func.__name__}" # edge disjoint paths edge_paths = list(nx.edge_disjoint_paths(G, "A", "Z", **kwargs)) assert are_edge_disjoint_paths(G, edge_paths), errmsg assert nx.edge_connectivity(G, "A", "Z") == len(edge_paths), errmsg # node disjoint paths node_paths = list(nx.node_disjoint_paths(G, "A", "Z", **kwargs)) assert are_node_disjoint_paths(G, node_paths), errmsg assert nx.node_connectivity(G, "A", "Z") == len(node_paths), errmsg
def test_graph_from_pr_2053(): G = nx.Graph() G.add_edges_from([ ('A', 'B'), ('A', 'D'), ('A', 'F'), ('A', 'G'), ('B', 'C'), ('B', 'D'), ('B', 'G'), ('C', 'D'), ('C', 'E'), ('C', 'Z'), ('D', 'E'), ('D', 'F'), ('E', 'F'), ('E', 'Z'), ('F', 'Z'), ('G', 'Z') ]) for flow_func in flow_funcs: kwargs = dict(flow_func=flow_func) # edge disjoint paths edge_paths = list(nx.edge_disjoint_paths(G, 'A', 'Z', **kwargs)) assert_true(are_edge_disjoint_paths(G, edge_paths), msg=msg.format(flow_func.__name__)) assert_equal( nx.edge_connectivity(G, 'A', 'Z'), len(edge_paths), msg=msg.format(flow_func.__name__), ) # node disjoint paths node_paths = list(nx.node_disjoint_paths(G, 'A', 'Z', **kwargs)) assert_true(are_node_disjoint_paths(G, node_paths), msg=msg.format(flow_func.__name__)) assert_equal( nx.node_connectivity(G, 'A', 'Z'), len(node_paths), msg=msg.format(flow_func.__name__), )
def test_graph_from_pr_2053(): G = nx.Graph() G.add_edges_from([ ('A', 'B'), ('A', 'D'), ('A', 'F'), ('A', 'G'), ('B', 'C'), ('B', 'D'), ('B', 'G'), ('C', 'D'), ('C', 'E'), ('C', 'Z'), ('D', 'E'), ('D', 'F'), ('E', 'F'), ('E', 'Z'), ('F', 'Z'), ('G', 'Z')]) for flow_func in flow_funcs: kwargs = dict(flow_func=flow_func) # edge disjoint paths edge_paths = list(nx.edge_disjoint_paths(G, 'A', 'Z', **kwargs)) assert_true(are_edge_disjoint_paths(G, edge_paths), msg=msg.format(flow_func.__name__)) assert_equal( nx.edge_connectivity(G, 'A', 'Z'), len(edge_paths), msg=msg.format(flow_func.__name__), ) # node disjoint paths node_paths = list(nx.node_disjoint_paths(G, 'A', 'Z', **kwargs)) assert_true(are_node_disjoint_paths(G, node_paths), msg=msg.format(flow_func.__name__)) assert_equal( nx.node_connectivity(G, 'A', 'Z'), len(node_paths), msg=msg.format(flow_func.__name__), )
def k_shortest_edge_disjoint_paths(G, source, target, k, weight='weight'): def compute_distance(path): return sum(G[u][v][weight] for u, v in path_to_edge_list(path)) return [ remove_cycles(path) for path in sorted(nx.edge_disjoint_paths(G, s_k, t_k), key=lambda path: compute_distance(path))[:k] ]
def routing(G, cur_payments): k = 4 throughput = 0 num_delivered = 0 index = 0 total_probing_messages = 0 total_max_path_length = 0 transaction_fees = 0 for payment in cur_payments: fee = 0 src = payment[0] dst = payment[1] payment_size = payment[2] path_set = list(nx.edge_disjoint_paths(G, src, dst)) index += 1 if (len(path_set) > k): path_set = path_set[0:k] max_path_length = 0 path_cap = [sys.maxsize] * len(path_set) index_p = 0 for path in path_set: total_probing_messages += len(path) - 1 if len(path) - 1 > max_path_length: max_path_length = len(path) - 1 for i in range(len(path) - 1): path_cap[index_p] = np.minimum( path_cap[index_p], G[path[i]][path[i + 1]]["capacity"]) index_p += 1 res = set_credits(payment_size, path_cap) index_p = 0 for path in path_set: for i in range(len(path) - 1): G[path[i]][path[i + 1]]["capacity"] -= res[index_p] G[path[i + 1]][path[i]]["capacity"] += res[index_p] fee += G[path[i]][path[i + 1]]["cost"] * res[index_p] index_p += 1 if sum(res) < payment[2] - 1e-6: for i in range(len(path_set)): p = path_set[i] for j in range(len(p) - 1): G[p[j]][p[j + 1]]["capacity"] += res[i] G[p[j + 1]][p[j]]["capacity"] -= res[i] # remove atomicity # throughput += sum(res) else: num_delivered += 1 throughput += payment[2] total_max_path_length += max_path_length transaction_fees += fee return throughput, transaction_fees / throughput, num_delivered, total_probing_messages, total_max_path_length
def test_icosahedral_disjoint_paths(): G = nx.icosahedral_graph() for flow_func in flow_funcs: kwargs = dict(flow_func=flow_func) # edge disjoint paths edge_dpaths = list(nx.edge_disjoint_paths(G, 0, 6, **kwargs)) assert_true(are_edge_disjoint_paths(G, edge_dpaths), msg=msg.format(flow_func.__name__)) assert_equal(5, len(edge_dpaths), msg=msg.format(flow_func.__name__)) # node disjoint paths node_dpaths = list(nx.node_disjoint_paths(G, 0, 6, **kwargs)) assert_true(are_node_disjoint_paths(G, node_dpaths), msg=msg.format(flow_func.__name__)) assert_equal(5, len(node_dpaths), msg=msg.format(flow_func.__name__))
def test_karate(): G = nx.karate_club_graph() for flow_func in flow_funcs: kwargs = dict(flow_func=flow_func) errmsg = f"Assertion failed in function: {flow_func.__name__}" # edge disjoint paths edge_dpaths = list(nx.edge_disjoint_paths(G, 0, 33, **kwargs)) assert are_edge_disjoint_paths(G, edge_dpaths), errmsg assert nx.edge_connectivity(G, 0, 33) == len(edge_dpaths), errmsg # node disjoint paths node_dpaths = list(nx.node_disjoint_paths(G, 0, 33, **kwargs)) assert are_node_disjoint_paths(G, node_dpaths), errmsg assert nx.node_connectivity(G, 0, 33) == len(node_dpaths), errmsg
def test_icosahedral_disjoint_paths(): G = nx.icosahedral_graph() for flow_func in flow_funcs: kwargs = dict(flow_func=flow_func) errmsg = f"Assertion failed in function: {flow_func.__name__}" # edge disjoint paths edge_dpaths = list(nx.edge_disjoint_paths(G, 0, 6, **kwargs)) assert are_edge_disjoint_paths(G, edge_dpaths), errmsg assert 5 == len(edge_dpaths), errmsg # node disjoint paths node_dpaths = list(nx.node_disjoint_paths(G, 0, 6, **kwargs)) assert are_node_disjoint_paths(G, node_dpaths), errmsg assert 5 == len(node_dpaths), errmsg
def test_octahedral_disjoint_paths(): G = nx.octahedral_graph() for flow_func in flow_funcs: kwargs = dict(flow_func=flow_func) # edge disjoint paths edge_dpaths = list(nx.edge_disjoint_paths(G, 0, 5, **kwargs)) assert are_edge_disjoint_paths(G, edge_dpaths), msg.format( flow_func.__name__) assert 4 == len(edge_dpaths), msg.format(flow_func.__name__) # node disjoint paths node_dpaths = list(nx.node_disjoint_paths(G, 0, 5, **kwargs)) assert are_node_disjoint_paths(G, node_dpaths), msg.format( flow_func.__name__) assert 4 == len(node_dpaths), msg.format(flow_func.__name__)
def test_petersen_disjoint_paths(): G = nx.petersen_graph() for flow_func in flow_funcs: kwargs = dict(flow_func=flow_func) # edge disjoint paths edge_dpaths = list(nx.edge_disjoint_paths(G, 0, 6, **kwargs)) assert_true(are_edge_disjoint_paths(G, edge_dpaths), msg=msg % (flow_func.__name__, )) assert_equal(3, len(edge_dpaths), msg=msg % (flow_func.__name__, )) # node disjoint paths node_dpaths = list(nx.node_disjoint_paths(G, 0, 6, **kwargs)) assert_true(are_node_disjoint_paths(G, node_dpaths), msg=msg % (flow_func.__name__, )) assert_equal(3, len(node_dpaths), msg=msg % (flow_func.__name__, ))
def generate_scenario(tx_amounts, num_nodes, num_txs_per_node, num_paths, cap_log_min, cap_log_max, random_seed): random.seed(random_seed) # RANDOM NETWORK CONSTRUCTION GG = nx.watts_strogatz_graph( num_nodes, 8, 0.8, random_seed ) # num_nodes nodes, connected to nearest 8 neighbors in ring topology, then 0.8 probability of rewiring, random seed assert nx.is_connected(GG) G = nx.DiGraph() for e in GG.edges(): cap_01_log = cap_log_min + random.random() * (cap_log_max - cap_log_min) cap_10_log = cap_log_min + random.random() * (cap_log_max - cap_log_min) cap_01 = math.exp(cap_01_log) cap_10 = math.exp(cap_10_log) G.add_edge(e[0], e[1], capacity=cap_01) G.add_edge(e[1], e[0], capacity=cap_10) # GENERATE PATHS paths = {} for src in G.nodes(): for dst in G.nodes(): if src == dst: continue path_set = list( nx.edge_disjoint_paths(G, src, dst, cutoff=num_paths)) paths[(src, dst)] = path_set # GENERATE PAYMENTS payments = [] for k in range(num_nodes * num_txs_per_node): amt = random.choice(tx_amounts) src = random.randrange(num_nodes) while True: dst = random.randrange(num_nodes) if dst != src: break payments.append((src, dst, amt)) return (G, paths, payments, GG)
def test_cutoff_disjoint_paths(): G = nx.icosahedral_graph() for flow_func in flow_funcs: kwargs = dict(flow_func=flow_func) for cutoff in [2, 4]: kwargs['cutoff'] = cutoff # edge disjoint paths edge_dpaths = list(nx.edge_disjoint_paths(G, 0, 6, **kwargs)) assert are_edge_disjoint_paths(G, edge_dpaths), msg.format( flow_func.__name__) assert cutoff == len(edge_dpaths), msg.format(flow_func.__name__) # node disjoint paths node_dpaths = list(nx.node_disjoint_paths(G, 0, 6, **kwargs)) assert are_node_disjoint_paths(G, node_dpaths), msg.format( flow_func.__name__) assert cutoff == len(node_dpaths), msg.format(flow_func.__name__)
def test_karate(): G = nx.karate_club_graph() for flow_func in flow_funcs: kwargs = dict(flow_func=flow_func) # edge disjoint paths edge_dpaths = list(nx.edge_disjoint_paths(G, 0, 33, **kwargs)) assert are_edge_disjoint_paths(G, edge_dpaths), msg.format( flow_func.__name__) assert nx.edge_connectivity(G, 0, 33) == len(edge_dpaths), msg.format( flow_func.__name__) # node disjoint paths node_dpaths = list(nx.node_disjoint_paths(G, 0, 33, **kwargs)) assert are_node_disjoint_paths(G, node_dpaths), msg.format( flow_func.__name__) assert nx.node_connectivity(G, 0, 33) == len(node_dpaths), msg.format( flow_func.__name__)
def PrepareSQ1(G, d, use_cached=False): global SQ1 if use_cached and SQ1: return # Do not compute SQ1 Data if already computed previously H = build_auxiliary_edge_connectivity(G) R = build_residual_network(H, 'capacity') SQ1 = {n: {} for n in G} for u in G.nodes(): if u.startswith('acc') and (u != d): paths = list( nx.edge_disjoint_paths(G, u, d, auxiliary=H, residual=R)) k = sorted(paths, key=lambda x: len(x)) SQ1[u][d] = k
def test_florentine_families(): G = nx.florentine_families_graph() for flow_func in flow_funcs: kwargs = dict(flow_func=flow_func) errmsg = f"Assertion failed in function: {flow_func.__name__}" # edge disjoint paths edge_dpaths = list( nx.edge_disjoint_paths(G, 'Medici', 'Strozzi', **kwargs)) assert are_edge_disjoint_paths(G, edge_dpaths), errmsg assert nx.edge_connectivity(G, 'Medici', 'Strozzi') == len(edge_dpaths), errmsg # node disjoint paths node_dpaths = list( nx.node_disjoint_paths(G, 'Medici', 'Strozzi', **kwargs)) assert are_node_disjoint_paths(G, node_dpaths), errmsg assert nx.node_connectivity(G, 'Medici', 'Strozzi') == len(node_dpaths), errmsg
def test_graph_from_pr_2053(): G = nx.Graph() G.add_edges_from([ ('A', 'B'), ('A', 'D'), ('A', 'F'), ('A', 'G'), ('B', 'C'), ('B', 'D'), ('B', 'G'), ('C', 'D'), ('C', 'E'), ('C', 'Z'), ('D', 'E'), ('D', 'F'), ('E', 'F'), ('E', 'Z'), ('F', 'Z'), ('G', 'Z') ]) for flow_func in flow_funcs: kwargs = dict(flow_func=flow_func) errmsg = f"Assertion failed in function: {flow_func.__name__}" # edge disjoint paths edge_paths = list(nx.edge_disjoint_paths(G, 'A', 'Z', **kwargs)) assert are_edge_disjoint_paths(G, edge_paths), errmsg assert nx.edge_connectivity(G, 'A', 'Z') == len(edge_paths), errmsg # node disjoint paths node_paths = list(nx.node_disjoint_paths(G, 'A', 'Z', **kwargs)) assert are_node_disjoint_paths(G, node_paths), errmsg assert nx.node_connectivity(G, 'A', 'Z') == len(node_paths), errmsg
def test_karate(): G = nx.karate_club_graph() for flow_func in flow_funcs: kwargs = dict(flow_func=flow_func) # edge disjoint paths edge_dpaths = list(nx.edge_disjoint_paths(G, 0, 33, **kwargs)) assert_true(are_edge_disjoint_paths(G, edge_dpaths), msg=msg.format(flow_func.__name__)) assert_equal( nx.edge_connectivity(G, 0, 33), len(edge_dpaths), msg=msg.format(flow_func.__name__), ) # node disjoint paths node_dpaths = list(nx.node_disjoint_paths(G, 0, 33, **kwargs)) assert_true(are_node_disjoint_paths(G, node_dpaths), msg=msg.format(flow_func.__name__)) assert_equal( nx.node_connectivity(G, 0, 33), len(node_dpaths), msg=msg.format(flow_func.__name__), )
def test_florentine_families(): G = nx.florentine_families_graph() for flow_func in flow_funcs: kwargs = dict(flow_func=flow_func) # edge disjoint paths edge_dpaths = list( nx.edge_disjoint_paths(G, 'Medici', 'Strozzi', **kwargs)) assert are_edge_disjoint_paths(G, edge_dpaths), msg.format( flow_func.__name__) assert nx.edge_connectivity(G, 'Medici', 'Strozzi') == len(edge_dpaths), msg.format( flow_func.__name__) # node disjoint paths node_dpaths = list( nx.node_disjoint_paths(G, 'Medici', 'Strozzi', **kwargs)) assert are_node_disjoint_paths(G, node_dpaths), msg.format( flow_func.__name__) assert nx.node_connectivity(G, 'Medici', 'Strozzi') == len(node_dpaths), msg.format( flow_func.__name__)
def test_florentine_families(): G = nx.florentine_families_graph() for flow_func in flow_funcs: kwargs = dict(flow_func=flow_func) # edge disjoint paths edge_dpaths = list(nx.edge_disjoint_paths(G, 'Medici', 'Strozzi', **kwargs)) assert_true(are_edge_disjoint_paths(G, edge_dpaths), msg=msg.format(flow_func.__name__)) assert_equal( nx.edge_connectivity(G, 'Medici', 'Strozzi'), len(edge_dpaths), msg=msg.format(flow_func.__name__), ) # node disjoint paths node_dpaths = list(nx.node_disjoint_paths(G, 'Medici', 'Strozzi', **kwargs)) assert_true(are_node_disjoint_paths(G, node_dpaths), msg=msg.format(flow_func.__name__)) assert_equal( nx.node_connectivity(G, 'Medici', 'Strozzi'), len(node_dpaths), msg=msg.format(flow_func.__name__), )
def main(): conn = manager.connect(host='mx3.pod1.nanog74.cloud.tesuto.com', username='******', password='******', device_params={'name': 'junos'}, hostkey_verify=False) mx3_ip = socket.gethostbyname('mx3.pod1.nanog74.cloud.tesuto.com') result = conn.command('show ted database extensive', format='json') isis_db = conn.command('show isis database extensive', format='json') json_ted_db = json.loads(result.xpath('.')[0].text) json_isis_db = json.loads(isis_db.xpath('.')[0].text) flow_constraint_mx3 = 200 flow_constraint_mx5 = 60 dest_prefix = "10.1.1.4" graph_nodes = parse_db(json_ted_db) extract_prefix = parse_isis_db(json_isis_db) #mx3_mx4_spf = nx.shortest_path(graph_nodes, source='mx3.00(10.0.0.3)', target='mx1.00(10.0.0.1)', ) #Problem 1 shortest_path = nx.dijkstra_path(graph_nodes, 'mx3.00(10.0.0.3)', 'mx4.00(10.0.0.4)') # Problem 4 edge_disjoint_paths = list( nx.edge_disjoint_paths(graph_nodes, 'mx3.00(10.0.0.3)', 'mx4.00(10.0.0.4)')) #Problem 2 residual_graph = check_capacity_graph(shortest_path, graph_nodes, constraint=flow_constraint_mx3) final_path2 = nx.dijkstra_path(residual_graph, 'mx3.00(10.0.0.3)', 'mx4.00(10.0.0.4)') get_params = create_path(final_path2, residual_graph) msg_body = prep_routes(mx3_ip, get_params) with open(ROUTE_CONFIG_FILE, 'a') as fp: fp.write(msg_body + "\n")
def main(): g = nx.Graph() g.add_edge(0, 1) g.add_edge(0, 2) g.add_edge(0, 3) g.add_edge(1, 4) g.add_edge(2, 5) g.add_edge(3, 6) g.add_edge(4, 7) g.add_edge(5, 7) g.add_edge(6, 7) print(list(nx.node_disjoint_paths(g, 0, 7))) print(list(nx.edge_disjoint_paths(g, 0, 7))) g2 = g.to_directed() print(list(nx.node_disjoint_paths(g2, 0, 7))) vertex_disjoint_paths = list(nx.node_disjoint_paths(g2, 0, 7)) for path in vertex_disjoint_paths: i = 0 while i < len(path) - 1: g2.remove_edge(path[i], path[i + 1]) i = i + 1 # print(list(nx.node_disjoint_paths(g2, 0, 7))) nx.draw_networkx(g2, edge_color='black') plt.draw() plt.show() l1 = list(g2.nodes) print(l1) l1[3] = 5 # l1.remove(3) print(l1) llist = [0] reassign(llist) print(llist) append(llist) print(llist)
def test_karate(): G = nx.karate_club_graph() for flow_func in flow_funcs: kwargs = dict(flow_func=flow_func) # edge disjoint paths edge_dpaths = list(nx.edge_disjoint_paths(G, 0, 33, **kwargs)) assert_true(are_edge_disjoint_paths(G, edge_dpaths), msg=msg % (flow_func.__name__, )) assert_equal( nx.edge_connectivity(G, 0, 33), len(edge_dpaths), msg=msg % (flow_func.__name__, ), ) # node disjoint paths node_dpaths = list(nx.node_disjoint_paths(G, 0, 33, **kwargs)) assert_true(are_node_disjoint_paths(G, node_dpaths), msg=msg % (flow_func.__name__, )) assert_equal( nx.node_connectivity(G, 0, 33), len(node_dpaths), msg=msg % (flow_func.__name__, ), )
spectrum_dict = { get_link_index(u, v): [0] * len(nodes) for u, v in links } shortest_paths = {} before = datetime.now() H = build_auxiliary_edge_connectivity(graph) R = build_residual_network(H, "capacity") for u in nodes: shortest_paths[u] = { v: sorted(nx.edge_disjoint_paths(graph, u, v, auxiliary=H, residual=R), key=len)[:2] for v in nodes if u != v } after = datetime.now() print('Shortest paths:', after - before) def get_number_of_blocks(): return sum([ 1 if not (rsa(*demand, 0) and rsa(*demand, 1)) else 0 for demand in demands ])
def test_isolated_edges(): G = nx.Graph() G.add_node(1) nx.add_path(G, [4, 5]) list(nx.edge_disjoint_paths(G, 1, 5))
def test_not_connected_edges(): G = nx.Graph() nx.add_path(G, [1, 2, 3]) nx.add_path(G, [4, 5]) list(nx.edge_disjoint_paths(G, 1, 5))
def test_missing_target_edge_paths(): G = nx.path_graph(4) list(nx.edge_disjoint_paths(G, 1, 10))
def test_missing_source_edge_paths(): G = nx.path_graph(4) list(nx.edge_disjoint_paths(G, 10, 1))
def test_isolated_edges(): with pytest.raises(nx.NetworkXNoPath): G = nx.Graph() G.add_node(1) nx.add_path(G, [4, 5]) list(nx.edge_disjoint_paths(G, 1, 5))
def simple_paths(self, source, target, cutoff=10): return list( nx.edge_disjoint_paths(self.graph_topology, source, target, cutoff=cutoff))
def main(): # parameters num_node = 10 nflows = 10000 percentage = 90 random.seed(2) f1 = open("graph.txt", 'w+') # graph GG = nx.watts_strogatz_graph(num_node, 8, 0.8, 1) G = nx.DiGraph() for e in GG.edges(): G.add_edge(e[0], e[1], capacity=random.randint(700, 1000)) G.add_edge(e[1], e[0], capacity=random.randint(700, 1000)) f1.write("%d,%d,%d\n" % (e[0], e[1], random.randint(700, 1000))) f1.write("%d,%d,%d\n" % (e[1], e[0], random.randint(700, 1000))) f1.close() # pos = nx.spring_layout(G, iterations=200) # nx.draw(G, pos, node_color=range(10), node_size=800, cmap=plt.cm.Blues) # plt.show() node_list = list(G.nodes()) # trans trans = [] with open('data/ripple_val.csv', 'r') as f: csv_reader = csv.reader(f, delimiter=',') for row in csv_reader: if float(row[2]) > 0: src = int(row[0]) % len(node_list) dst = int(row[1]) % len(node_list) if src == dst: continue trans.append((int(src), int(dst), float(row[2]))) sorted_trans = sorted(trans, key=lambda x: x[2]) threshold = sorted_trans[int(1.0 * percentage / 100 * (len(sorted_trans) - 1))] print 'threshold for ripple trace', threshold[2] f2 = open("payments.txt", 'w+') # payments to send payments = [] for k in range(nflows): index = random.randint(0, len(trans) - 1) while not nx.has_path(G, trans[index][0], trans[index][1]): index = random.randint(0, len(trans) - 1) payments.append( (trans[index][0], trans[index][1], trans[index][2], 1, 0)) f2.write("%d,%d,%f\n" % (trans[index][0], trans[index][1], trans[index][2])) f2.close() f3 = open("path.txt", 'w+') need_num_path = 10 path_visited = [[0 for x in range(num_node)] for y in range(num_node)] for s_payment in payments: src = s_payment[0] dst = s_payment[1] if path_visited[src][dst] == 1: continue path_visited[src][dst] = 1 path_set = list(nx.edge_disjoint_paths(G, src, dst)) if (len(path_set) > need_num_path): path_set = path_set[0:need_num_path] for path in path_set: f3.write("%d,%d," % (src, dst)) for i in range(len(path) - 1): f3.write("%d," % (path[i])) f3.write("%d\n" % (path[len(path) - 1])) f3.close()
def main(): # PARAMETERS # number of nodes in the network num_node = 10 # number of payments to run through the network nflows = 10000 # fraction of payments that should be "mice" payments percentage = 90 # RANDOM NETWORK CONSTRUCTION random.seed(2) f1 = open("graph.txt", 'w+') GG = nx.watts_strogatz_graph( num_node, 8, 0.8, 1 ) # num_node nodes, connected to nearest 8 neighbors in ring topology, 0.8 probability of rewiring, random seed 1 G = nx.DiGraph() for e in GG.edges(): cap_01 = random.randint(700, 1000) cap_10 = random.randint(700, 1000) G.add_edge(e[0], e[1], capacity=cap_01) G.add_edge(e[1], e[0], capacity=cap_10) f1.write("%d,%d,%d\n" % (e[0], e[1], cap_01)) f1.write("%d,%d,%d\n" % (e[1], e[0], cap_10)) f1.close() # PLOT GRAPH # pos = nx.spring_layout(G, iterations=200) # nx.draw(G, pos, node_color=range(10), node_size=800, cmap=plt.cm.Blues) # plt.show() node_list = list(G.nodes()) # LOAD TRANSACTIONS FROM RIPPLE DATASET trans = [] with open('data/ripple_val.csv', 'r') as f: csv_reader = csv.reader(f, delimiter=',') for row in csv_reader: if float(row[2]) > 0: src = int(row[0]) % len(node_list) dst = int(row[1]) % len(node_list) if src == dst: continue trans.append((int(src), int(dst), float(row[2]))) # COMPUTE THRESHOLD FOR MICE/ELEPHANT PAYMENTS sorted_trans = sorted(trans, key=lambda x: x[2]) threshold = sorted_trans[int(1.0 * percentage / 100 * (len(sorted_trans) - 1))][2] print('threshold for ripple trace', threshold) # GENERATE PAYMENTS f2 = open("payments.txt", 'w+') payments = [] for k in range(nflows): while True: index = random.randint(0, len(trans) - 1) tx = trans[index] if nx.has_path(G, tx[0], tx[1]): break payments.append((tx[0], tx[1], tx[2], 1, 0)) f2.write("%d,%d,%f\n" % (tx[0], tx[1], tx[2])) f2.close() # GENERATE PATHS FOR PAYMENTS f3 = open("path.txt", 'w+') # how many paths to compute max. need_num_path = 10 path_visited = [[0 for x in range(num_node)] for y in range(num_node)] for s_payment in payments: src = s_payment[0] dst = s_payment[1] if path_visited[src][dst] == 1: continue path_visited[src][dst] = 1 path_set = list(nx.edge_disjoint_paths(G, src, dst)) if (len(path_set) > need_num_path): path_set = path_set[0:need_num_path] for path in path_set: f3.write("%d,%d," % (src, dst)) f3.write(",".join([str(i) for i in path]) + "\n") f3.close()
capacity = 3600 / ((0.003 / speed) + 2) * 4 capacity_dict[(edge[0], edge[1])] = {'capacity': capacity} # print(capacity) # print(capacity_dict) nx.set_edge_attributes(sub_G, capacity_dict) capa = nx.get_edge_attributes(sub_G, 'capacity') # print(capa) # Q13 flow_value, flow_dict = nx.maximum_flow(sub_G, 2607, 1968) # Stanford and UCSC print(flow_value) print( len( list( nx.edge_disjoint_paths(sub_G, 2607, 1968, flow_func=shortest_augmenting_path)))) labels = {2607: 'Stanford', 1968: 'UCSC'} if True: nx.draw(sub_G, pos=sub_pos, nodecolor='r', node_size=5, edge_color='b', edge_tickness=1) nx.draw_networkx_labels(sub_G, pos=sub_pos, labels=labels, font_size=30) nx.draw_networkx_nodes(sub_G, pos=sub_pos, nodelist=[2607, 1989], node_color='k',
# %% # %% n_neighbor(G, '1', 2) # %% n_neighbor(G, '1', 1) # %% n_neighbor(G, '2', 1) # get node and neighbors # node_n_nbr = [(i, n_neighbor(G, i, 2)) for (i, j) in node] # %% # shows the nodes that connecst 2 parts so far a path exists between them. # [1:-1] removes the start and end node e.g. removes "1" and "3" # Useful for connecting nodes selcted by hubs. [x[1:-1] for x in nx.edge_disjoint_paths(G, "1", "3")] # displays the edge data in as (node, destination, {weight}) G.edges(data=True) # initiate weights by setting all to 1 nx.set_edge_attributes(G, values=1, name='weight') # assign weights dynamically # for u, v, d in G.edges(data=True): # d['weight'] = int(v) # %% # https: // stackoverflow.com/questions/39084661/using-a-list-of-lists-as-a-lookup-table-and-updating-a-value-in-new-list-of-list # https: // stackoverflow.com/questions/32750257/merge-two-dictionaries-by-key edge_range = list(range(1, nx.number_of_edges(G)+1))