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 get_all_paths(self,src, dst): # [[2,3,4,],[4,5,6]] all_paths = list( nx.node_disjoint_paths(self.net, self.hostmac_to_dpid[src], self.hostmac_to_dpid[dst])) return all_paths
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 get_bset_path(self,src,dpid,dst): if src not in self.net: # Learn it self.net.add_node(src) # Add a node to the graph # Add a link from the node to it's edge switch self.net.add_edge(dpid, src) self.hostmac_to_dpid[src] = dpid # record the link host to dpid all_paths = 0 if dst in self.net: path = nx.shortest_path(self.net, src, dst) next_hop = path[path.index(dpid) + 1] # get next hop # get output port # out_port = self.net[dpid][next]['port'] out_port = self.switch_topo[dpid][next_hop] # disjoint if self.hostmac_to_dpid[src] != self.hostmac_to_dpid[dst]: all_paths = list(nx.node_disjoint_paths (self.net, self.hostmac_to_dpid[src], self.hostmac_to_dpid[dst])) for paths in all_paths: paths.append(dst) paths.insert(0, src) # print "*********{}to{}*******".format(self.mac_ipv4[src], # self.mac_ipv4[dst]) # if all_paths != 0: print all_paths return out_port
def get_path_port(self, src, dpid, dst): out_port = None if self.hostmac_to_dpid[src] != self.hostmac_to_dpid[dst]: all_paths = [] all_paths = list( nx.node_disjoint_paths(self.net, self.hostmac_to_dpid[src], self.hostmac_to_dpid[dst])) if all_paths: for paths in all_paths: paths.append(dst) paths.insert(0, src) best_path = self.get_best_path(all_paths) # get back ''' print"**** Best Path {} ---- {} is *****".format(src,dst) print best_path # print dpid ''' if dpid in best_path: next_hop = best_path[best_path.index(dpid) + 1] out_port = self.switch_topo[dpid][next_hop] return out_port elif dst in self.switch_mac_port[dpid]: out_port = self.switch_mac_port[dpid][dst] return out_port else: return out_port
def graphDisjointPath(NG, group1, group2, operation): nmbOutputs = len(NG) nmbOutputs2 = len(NG[0]) #operation depicts if the minimum disconnected set should be located (operation==0) or the disjoint path (operation==1) #below function will read through the mat file and try to find how many modules their are #using the network functions create a direction graph (nodes with a connection with a direction so connection 1 to 2 has a direction and is not the same as 2 to 1) plot = nx.DiGraph() plot.add_nodes_from(range(nmbOutputs)) for x in range(nmbOutputs): for y in range(nmbOutputs2): if (NG[x][y] == 1): plot.add_edge(y, x) plot.add_node(nmbOutputs) plot.add_node(nmbOutputs + 1) for x in group1: plot.add_edge(nmbOutputs, x[1].nmb - 1) for x in group2: plot.add_edge(x[1].nmb - 1, nmbOutputs + 1) if (operation): result = list(nx.node_disjoint_paths(plot, nmbOutputs, nmbOutputs + 1)) else: result = list(nx.minimum_node_cut(plot, nmbOutputs, nmbOutputs + 1)) return result
def get_paths(self, src, dst): all_paths = [] all_paths = list(nx.node_disjoint_paths(self.net, src, dst)) # print all_paths if len(all_paths) > 1: return all_paths else: all_paths = list(nx.shortest_simple_paths(self.net, src, dst)) return all_paths
def count_bgp_proposal(Internet, asn_src, asn_dst, number_of_prefixes): count = 0 prefix_size = PREFIX_SIZE link_size = LINK_SIZE #asns = Internet.nodes(data=False) paths_reachable_as = list(nx.bfs_edges(Internet, asn_src)) try: if len(list(nx.node_disjoint_paths(Internet, asn_src, asn_dst))) >= 2: print list(nx.node_disjoint_paths(Internet, asn_src, asn_dst)), "<" #print len(list(nx.node_disjoint_paths(Internet, asn_src, asn_dst)))-1,asn_src,asn_dst count += number_of_prefixes * prefix_size * (len( list(nx.node_disjoint_paths(Internet, asn_src, asn_dst))) - 1) except: pass #print count,"<<<",prefix_size,number_of_prefixes count += number_of_prefixes * prefix_size * len(paths_reachable_as) #print count return count
def purify(Q, source, target): """ This function performs a multi-path entanglement purification between a source and target node using all available paths. :param Q: Qnet Graph :param source: Name of source node :param target: Name of target node If none, will purify all possible paths :param string, optional, method: The method used to do the purification. Supported options: "edge_disjoint", "node_disjoint", "total_disjoint", "greedy". edge_disjoint: No intersecting edges node_disjoint: No intersecting nodes total_disjoint: No intersecting edges or nodes Other inputs produce a ValueError :return: float """ # TODO: Implement a threshold attribute so user doesn't have to iterate through all paths def fidTransform(F1, F2): return (F1 * F2) / (F1 * F2 + (1 - F1) * (1 - F2)) # Get paths for Graph u = Q.getNode(source) v = Q.getNode(target) # TODO: Find a better way of producing disjoint paths generator = nx.node_disjoint_paths(Q, u, v) # Get p values for each path f_arr = [] for path in generator: new_path = QNET.Path(Q, path) # check if path is valid if new_path.is_valid() == True: f = new_path.cost_vector['f'] f_arr.append(f) else: pass assert (len(f_arr) != 0), f"No path exists from {source} to {target}" # Initialize purified fidelity as the max fidelity value pure_cost = max(f_arr) f_arr.remove(pure_cost) # Purify fidelities together # TODO: Depreciate this code while len(f_arr) != 0: pmax = max(f_arr) pure_cost = fidTransform(pure_cost, pmax) f_arr.remove(pmax) return pure_cost
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 find_node_disjoint_paths(G, s, t): '''Returns a set of paths that share a source and target node, but have exactly 0 shared edges. ''' ndp = set() if s in G and t in G: paths = list(nx.node_disjoint_paths(G, s, t)) [ndp.add(tuple(path)) for path in paths] else: raise ValueError('Source and/or target not in graph G!') return ndp
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) 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_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_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 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_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 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 get_topology(self, ev): switch_list = get_switch(self.topology_api_app, None) switches = [switch.dp.id for switch in switch_list] self.net.add_nodes_from(switches) links_list = get_link(self.topology_api_app, None) # print links_list links = [(link.src.dpid, link.dst.dpid, { 'port': link.src.port_no }) for link in links_list] # print links self.net.add_edges_from(links) links = [(link.dst.dpid, link.src.dpid, { 'port': link.dst.port_no }) for link in links_list] # print links self.net.add_edges_from(links) # host_list = get_host(self.topology_api_app, None) path = list(nx.node_disjoint_paths(self.net, 1, 4)) print path
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__, ), )
def run3(): gd = generateDisks() vis = visualize() unitDistance = 2 s = 1 t = 12 belts = (s, t) k = 2 # generate d random unit disks d = 20 disks = gd.generateRandomUnitDisks(d, belts, unitDistance) # generate belt rectangles beltRectangles = gd.getBeltRegions(k, belts, unitDistance) # visualize circles vis.visualizeCircles(disks, beltRectangles, unitDistance) # create residual graph r1 = ResidualGraph(disks, s, t, unitDistance * 2) # starting vertex start = r1.getUindex() # ending vertex end = r1.getVindex() # visualize G graph vis.visualizeGraph( r1.G, list(nx.node_disjoint_paths(r1.G, r1.getUindexofG(), r1.getVindexOfG())), r1.getUindexofG(), r1.getVindexOfG()) # visualize G' graph vis.visualizeGraph(r1.Gp, r1.disjoint_paths, start, end) # retrieve residual graph r_Graph = r1.getResidualGraph() # visualize residual graph vis.visualizeGraph(r_Graph, [], start, end)
def test_not_connected_nodes(): G = nx.Graph() nx.add_path(G, [1, 2, 3]) nx.add_path(G, [4, 5]) list(nx.node_disjoint_paths(G, 1, 5))
def get_paths(self, src, dst): all_paths = [] all_paths = list(nx.node_disjoint_paths(self.net, src, dst)) # print all_paths return all_paths
def test_missing_source_node_paths(): G = nx.path_graph(4) list(nx.node_disjoint_paths(G, 10, 1))
def test_missing_target_node_paths(): G = nx.path_graph(4) list(nx.node_disjoint_paths(G, 1, 10))
def test_invalid_auxiliary(): G = nx.complete_graph(5) list(nx.node_disjoint_paths(G, 0, 3, auxiliary=G))
def test_isolated_nodes(): G = nx.Graph() G.add_node(1) nx.add_path(G, [4, 5]) list(nx.node_disjoint_paths(G, 1, 5))
# E_B is composed of edges between sensors for t in range(1, (T + 1) - 1): for sensor in sensors: act_edge = ( sensor + "_B_" + str(t), sensor + "_A_" + str(t + 1) ) # Create v^B_j,t -> v^A_i,t+1 by creating sensor1_t -> node2_t+1 linking_graph.add_edges_from([act_edge]) # E_C # E_C is composed of edges between muscles for muscle in muscles: # Makes it impossible for c_ij = 0 act_edge = (muscle + "_A_" + str(T), muscle + "_C" ) # Create v^A_j,t -> v^C_i by creating linking_graph.add_edges_from([act_edge]) for node in VB: # We're adding a SOURCE node for every sensor, so basically we control every sensor linking_graph.add_edge( "SOURCE", node) # Add an edge between SOURCE and every copy of every sensor for node in VC: # We're adding a TARGET node for every muscle, so we're checking the output at each muscle linking_graph.add_edge( node, "TARGET") # Add an edge between every muscle and TARGET linking_size = nx.node_disjoint_paths( linking_graph, "SOURCE", "TARGET") # Find all disjoint paths between SOURCE and TARGET # Disjoint paths are paths that only share their first and last nodes print("upper bound: " + str(len(list(linking_size))))
def test_isolated_nodes(): G = nx.Graph() G.add_node(1) nx.add_path(G, [4, 5]) list(nx.node_disjoint_paths(G, 1, 5))
def test_invalid_auxiliary(): G = nx.complete_graph(5) list(nx.node_disjoint_paths(G, 0, 3, auxiliary=G))
def stitch(self, add_back_residuals=True): if self.G is None: raise ValueError("Inexistent graph. Call `build_graph` first") try: _, self.flow = nx.capacity_scaling(self.G) self.paths = self.reconstruct_paths() except nx.exception.NetworkXUnfeasible: warnings.warn( "No optimal solution found. Employing black magic...") # Let us prune the graph by removing all source and sink edges # but those connecting the `n_tracks` first and last tracklets. in_to_keep = [ self._mapping[first_tracklet]["in"] for first_tracklet in self._first_tracklets ] out_to_keep = [ self._mapping[last_tracklet]["out"] for last_tracklet in self._last_tracklets ] in_to_remove = set(node for _, node in self.G.out_edges( "source")).difference(in_to_keep) out_to_remove = set( node for node, _ in self.G.in_edges("sink")).difference(out_to_keep) self.G.remove_edges_from( zip(["source"] * len(in_to_remove), in_to_remove)) self.G.remove_edges_from( zip(out_to_remove, ["sink"] * len(out_to_remove))) # Preflow push seems to work slightly better than shortest # augmentation path..., and is more computationally efficient. paths = [] for path in nx.node_disjoint_paths(self.G, "source", "sink", preflow_push, self.n_tracks): temp = set() for node in path[1:-1]: self.G.remove_node(node) temp.add(self._mapping_inv[node]) paths.append(list(temp)) incomplete_tracks = self.n_tracks - len(paths) remaining_nodes = set(self._mapping_inv[node] for node in self.G if node not in ("source", "sink")) if (incomplete_tracks == 1 ): # All remaining nodes must belong to the same track # Verify whether there are overlapping tracklets for t1, t2 in combinations(remaining_nodes, 2): if t1 in t2: # Pick the segment that minimizes "smoothness", computed here # with the coefficient of variation of the differences. if t1 in remaining_nodes: remaining_nodes.remove(t1) if t2 in remaining_nodes: remaining_nodes.remove(t2) track = sum(remaining_nodes) hyp1 = track + t1 hyp2 = track + t2 dx1 = np.diff(hyp1.centroid, axis=0) cv1 = dx1.std() / np.abs(dx1).mean() dx2 = np.diff(hyp2.centroid, axis=0) cv2 = dx2.std() / np.abs(dx2).mean() if cv1 < cv2: remaining_nodes.add(t1) self.residuals.append(t2) else: remaining_nodes.add(t2) self.residuals.append(t1) paths.append(list(remaining_nodes)) elif incomplete_tracks > 1: # Rebuild a full graph from the remaining nodes without # temporal constraint on what tracklets can be stitched together. self.build_graph(list(remaining_nodes), max_gap=np.inf) self.G.nodes["source"]["demand"] = -incomplete_tracks self.G.nodes["sink"]["demand"] = incomplete_tracks _, self.flow = nx.capacity_scaling(self.G) paths += self.reconstruct_paths() self.paths = paths if len(self.paths) != self.n_tracks: warnings.warn( f"Only {len(self.paths)} tracks could be reconstructed.") finally: if self.paths is None: raise ValueError( f"Could not reconstruct {self.n_tracks} tracks from the tracklets given." ) self.tracks = np.asarray( [sum(path) for path in self.paths if path]) if add_back_residuals: _ = self._finalize_tracks()
resfilename = dirname + '/' + basename.rstrip('.py') + '.res' file = open(resfilename, "w") #file = open("/home/jeremie/sim/BroadcastSign/simulations/res.txt", "w") ##print(len(list(nx.node_disjoint_paths(G, 0, maxDepth*numNodes+selfId)))) found = False for d in range(1, maxDepth + 1): # no need to search in first layer if G.has_edge((d - 1) * numNodes + broadcasterId, d * numNodes + selfId): found = True file.write('1') break try: #print("Search for disj. paths between 0 and ",d*numNodes+selfId) if len( list( nx.node_disjoint_paths(G, broadcasterId, d * numNodes + selfId))) > f: found = True file.write('1') break except: pass if not found: file.write('0') file.close() #print(found) #nx.draw(G, pos, with_labels=True) #plt.show() ##plt.close()