Example #1
0
def test_shortest_simple_paths():
    G = cnlti(nx.grid_2d_graph(4, 4), first_label=1, ordering="sorted")
    paths = nx.shortest_simple_paths(G, 1, 12)
    assert_equal(next(paths), [1, 2, 3, 4, 8, 12])
    assert_equal(next(paths), [1, 5, 6, 7, 8, 12])
    assert_equal([len(path) for path in nx.shortest_simple_paths(G, 1, 12)],
                 sorted([len(path) for path in nx.all_simple_paths(G, 1, 12)]))
Example #2
0
def test_directed_weighted_shortest_simple_path_issue2427():
    G = nx.DiGraph()
    G.add_edge('IN', 'OUT', weight=2)
    G.add_edge('IN', 'A', weight=1)
    G.add_edge('IN', 'B', weight=2)
    G.add_edge('B', 'OUT', weight=2)
    assert_equal(list(nx.shortest_simple_paths(G, 'IN', 'OUT', weight="weight")),
                 [['IN', 'OUT'], ['IN', 'B', 'OUT']])
    G = nx.DiGraph()
    G.add_edge('IN', 'OUT', weight=10)
    G.add_edge('IN', 'A', weight=1)
    G.add_edge('IN', 'B', weight=1)
    G.add_edge('B', 'OUT', weight=1)
    assert_equal(list(nx.shortest_simple_paths(G, 'IN', 'OUT', weight="weight")),
                 [['IN', 'B', 'OUT'], ['IN', 'OUT']])
Example #3
0
def number_of_hidden_nodes(G):
    path_list = []
    
    for j in range(6):
        if(G.has_node(j)):
            if (nx.has_path(G,j,6)):
                for path in nx.shortest_simple_paths(G, j, 6):
                    path_list = np.append(path_list, (len(path)-2))
            
    for j in range(6):
        if(G.has_node(j)):
            if (nx.has_path(G,j,7)):
                for path in nx.shortest_simple_paths(G, j, 7):
                    path_list = np.append(path_list, (len(path)-2))
    
    return np.max(int(np.max(path_list)),0)
Example #4
0
def test_weight_name():
    G = nx.cycle_graph(7)
    nx.set_edge_attributes(G, 1, 'weight')
    nx.set_edge_attributes(G, 1, 'foo')
    G.adj[1][2]['foo'] = 7
    paths = list(nx.shortest_simple_paths(G, 0, 3, weight='foo'))
    solution = [[0, 6, 5, 4, 3], [0, 1, 2, 3]]
    assert_equal(paths, solution)
Example #5
0
def test_weight_name():
    G = nx.cycle_graph(7)
    nx.set_edge_attributes(G, "weight", 1)
    nx.set_edge_attributes(G, "foo", 1)
    G.edge[1][2]["foo"] = 7
    paths = list(nx.shortest_simple_paths(G, 0, 3, weight="foo"))
    solution = [[0, 6, 5, 4, 3], [0, 1, 2, 3]]
    assert_equal(paths, solution)
Example #6
0
 def get_shortest_path(self, target):
     shortest_paths = list( networkx.shortest_simple_paths(self._graph, self._initial_state, target) )
     shortest_path = shortest_paths[0]
     edges = []
     for i in range(len(shortest_path)-1):
         edges.append( self.get_edge_by_from_to( shortest_path[i].get_id(),
                                                 shortest_path[i+1].get_id() ) )
     return edges
Example #7
0
def simple(request, graph, startNode, endNode):
    start = url_unquote(startNode)
    end = url_unquote(endNode)
    if not (graph.has_node(start) and graph.has_node(end)):
        return request.respondJson({'message': 'node not in graph'},
                                   NOT_FOUND)

    ipaths = nx.shortest_simple_paths(graph, start, end)

    data = {'paths': tuple(ipaths)}
    request.respondJson(data)
Example #8
0
def test_weighted_shortest_simple_path():
    def cost_func(path):
        return sum(G.adj[u][v]['weight'] for (u, v) in zip(path, path[1:]))
    G = nx.complete_graph(5)
    weight = {(u, v): random.randint(1, 100) for (u, v) in G.edges()}
    nx.set_edge_attributes(G, weight, 'weight')
    cost = 0
    for path in nx.shortest_simple_paths(G, 0, 3, weight='weight'):
        this_cost = cost_func(path)
        assert_true(cost <= this_cost)
        cost = this_cost
Example #9
0
def test_weighted_shortest_simple_path():
    def cost_func(path):
        return sum(G.adj[u][v]['weight'] for (u, v) in zip(path, path[1:]))

    G = nx.complete_graph(5)
    weight = {(u, v): random.randint(1, 100) for (u, v) in G.edges()}
    nx.set_edge_attributes(G, weight, 'weight')
    cost = 0
    for path in nx.shortest_simple_paths(G, 0, 3, weight='weight'):
        this_cost = cost_func(path)
        assert cost <= this_cost
        cost = this_cost
Example #10
0
def test_Greg_Bernstein():
    g1 = nx.Graph()
    g1.add_nodes_from(["N0", "N1", "N2", "N3", "N4"])
    g1.add_edge("N4", "N1", weight=10.0, capacity=50, name="L5")
    g1.add_edge("N4", "N0", weight=7.0, capacity=40, name="L4")
    g1.add_edge("N0", "N1", weight=10.0, capacity=45, name="L1")
    g1.add_edge("N3", "N0", weight=10.0, capacity=50, name="L0")
    g1.add_edge("N2", "N3", weight=12.0, capacity=30, name="L2")
    g1.add_edge("N1", "N2", weight=15.0, capacity=42, name="L3")
    solution = [['N1', 'N0', 'N3'], ['N1', 'N2', 'N3'], ['N1', 'N4', 'N0', 'N3']]
    result = list(nx.shortest_simple_paths(g1, 'N1', 'N3', weight='weight'))
    assert_equal(result, solution)
Example #11
0
    def get_paths(
        self,
        source: Address,
        target: Address,
        value: TokenAmount,
        max_paths: int,
        diversity_penalty: float = DIVERSITY_PEN_DEFAULT,
        fee_penalty: float = FEE_PEN_DEFAULT,
        **kwargs: Any,
    ) -> List[dict]:
        """ Find best routes according to given preferences

        value: Amount of transferred tokens. Used for capacity checks
        diversity_penalty: One previously used channel is as bad as X more hops
        fee_penalty: One RDN in fees is as bad as X more hops
        """
        visited: Dict[ChannelID, float] = {}
        paths: List[List[Address]] = []

        for _ in range(max_paths):
            # update edge weights
            for node1, node2 in self.G.edges():
                edge = self.G[node1][node2]
                edge["weight"] = self.edge_weight(visited, edge, value, fee_penalty)

            # find next path
            all_paths = nx.shortest_simple_paths(self.G, source, target, weight="weight")
            try:
                # skip duplicates and invalid paths
                path = next(
                    path
                    for path in all_paths
                    if self.check_path_constraints(value, path) and path not in paths
                )
            except StopIteration:
                break
            # update visited penalty dict
            for node1, node2 in zip(path[:-1], path[1:]):
                channel_id = self.G[node1][node2]["view"].channel_id
                visited[channel_id] = visited.get(channel_id, 0) + diversity_penalty

            paths.append(path)
            if len(paths) >= max_paths:
                break
        result = []

        for path in paths:
            fee = 0
            for node1, node2 in zip(path[:-1], path[1:]):
                fee += self.G[node1][node2]["view"].fee(value)

            result.append(dict(path=path, estimated_fee=fee))
        return result
Example #12
0
def test_Greg_Bernstein():
    g1 = nx.Graph()
    g1.add_nodes_from(["N0", "N1", "N2", "N3", "N4"])
    g1.add_edge("N4", "N1", weight=10.0, capacity=50, name="L5")
    g1.add_edge("N4", "N0", weight=7.0, capacity=40, name="L4")
    g1.add_edge("N0", "N1", weight=10.0, capacity=45, name="L1")
    g1.add_edge("N3", "N0", weight=10.0, capacity=50, name="L0")
    g1.add_edge("N2", "N3", weight=12.0, capacity=30, name="L2")
    g1.add_edge("N1", "N2", weight=15.0, capacity=42, name="L3")
    solution = [['N1', 'N0', 'N3'], ['N1', 'N2', 'N3'], ['N1', 'N4', 'N0', 'N3']]
    result = list(nx.shortest_simple_paths(g1, 'N1', 'N3', weight='weight'))
    assert_equal(result, solution)
    def find_all_paths(self, max_paths=10, sort=True, score=True):
        """ Find all paths through DAG to End

            Params:
               max_paths (int :default:=10): Number of paths to find
                          If this is > 1000, all paths will be found
               sort (bool)                 : If True (default), sort paths
                                             in ascending order of length
        """
        if self.roots:
            self.lock_start()
        if score:
            self.score_graph()
        # shortest_simple_paths is slow for >1000 paths
        if max_paths <= 1000:
            if score:
                paths = list(six.moves.map(lambda x: x[1:-1],
                                           islice(nx.shortest_simple_paths(
                                                        self.G, self.start, self.end, weight='weight'),
                                                  max_paths)))
                scores = self.scorer.score_splits(paths)
                path_scores = zip(paths, scores)
                sorted_path_scores = sorted(path_scores, key=operator.itemgetter(1), reverse=True)
                logger.debug("Sorted paths with scores:\n %s", sorted_path_scores)
                # Strip the scores from the returned result, to be consistent with no-scoring option
                sorted_paths, _ = zip(*sorted_path_scores)
                return list(sorted_paths)
            else:
                paths = list(six.moves.map(lambda x: x[1:-1],
                                           islice(nx.shortest_simple_paths(
                                                        self.G, self.start, self.end),
                                                  max_paths)))
                return paths
        else:  # Fall back to all_simple_paths
            ps = list(six.moves.map(lambda x: x[1:-1],
                                    nx.all_simple_paths(self.G, self.start, self.end)))
            # If we do not intend to display paths, no need to sort them
            if sort:
                ps.sort(key=lambda x: len(x))
            return ps
Example #14
0
def testshortestpath():
    G = nx.Graph()
    G.add_edges_from([('a','e'),('a','d'),('d','e'),('b','d'),('b','c'),('c','e'),('c','d'),('b','e')])
    gen = nx.shortest_simple_paths(G,'a','b')
    for n in gen:
        print n
    print G.nodes()
    print '-----'
    G1 = nx.Graph()
    G1.add_edges_from([('a', 'e'), ('a', 'd'), ('d', 'e'), ('b', 'd'), ('b', 'c'), ('c', 'e'), ('c', 'd'), ('b', 'e'),('d', 'f'),('d', 'g'),('g','f')])
    gen1 = nx.shortest_simple_paths(G1, 'a', 'b')
    for n in gen1:
        print n
    print G1.nodes()


    print '-------'
    G3=nx.read_gpickle('try.gpickle')
    for (a,b) in G3.edges():
        G3[a][b]['Fw'] = int(G3[a][b]['Fw']*10000)

    gen3=nx.shortest_simple_paths(G3,230349,542752,weight='Fw')
    for path in gen3:
        l=0
        for n1,n2 in zip(path,path[1:]):
            l += G3[n1][n2]['Fw']
        print l,path

    print '-------'

    gen3 = nx.shortest_simple_paths(G3, 230349, 542752)
    allp=[]
    for path in gen3:
        l = 0
        for n1, n2 in zip(path, path[1:]):
            l += G3[n1][n2]['Fw']
        allp.append((l,path))
    allp=sorted(allp, key=lambda x:x[0])
    for p in allp:
        print p
Example #15
0
def k_shortest_path_loop_free_version(self,
                                      k,
                                      src_datapath_id,
                                      src_port,
                                      dst_datapath_id,
                                      dst_port,
                                      check_G=None,
                                      weight="weight"):
    """
    這個利用先深搜尋確保路徑沒有loop
    """
    loop_free_path = []
    path_length = []
    #初始化拓樸當check_G==None就新增一個空的有向拓樸
    loop_check = Loop_Free_Check(check_G)

    shortest_simple_paths = nx.shortest_simple_paths(
        GLOBAL_VALUE.G, (src_datapath_id, src_port),
        (dst_datapath_id, dst_port),
        weight=weight)

    #從最好的路線開始挑選

    for path in shortest_simple_paths:
        #當挑出來的路到達k條就可以離開
        if len(loop_free_path) == k:
            break
        #依序藉由節點塞入拓樸
        prev_node = None
        for node in path:
            if prev_node != None:
                print(prev_node, node, GLOBAL_VALUE.G[prev_node][node].keys())
                if weight in GLOBAL_VALUE.G[prev_node][node]:
                    loop_check.add_edge(
                        prev_node,
                        node,
                        weight=GLOBAL_VALUE.G[prev_node][node][weight])
                else:
                    loop_check.add_edge(prev_node, node, weight=0)
            prev_node = node

        #確認是否沒有發生loop
        _check_free = loop_check.check_free_loop()
        if _check_free:
            #沒有發生loop所以我們可以蒐集起來
            loop_free_path.append(path)
            print(path)
            path_length.append(path_weight(GLOBAL_VALUE.G, path,
                                           weight=weight))
            #所有k條loop free路線,這些路線的權重

    return loop_free_path, path_length
Example #16
0
def PST(G, k, source=None, target=None, weight=None):
    """Paths shorther than K"""
    if source != None or target != None:
        paths = nx.shortest_simple_paths(G, source, target, weight=weight)
        selected_paths = []
        for p in paths:
            if len(p) - 1 <= k:
                selected_paths.append(p)
            else:
                break
        return selected_paths

        return list(
            islice(nx.shortest_simple_paths(G, source, target, weight=weight),
                   k))
    else:
        paths = [
            PST(G, k, source=i, target=j, weight=weight) for i in G.nodes
            for j in G.nodes if i != j
        ]
        return [[(p[i], p[i + 1]) for i in range(len(p) - 1)] for ps in paths
                for p in ps]
Example #17
0
def KSP(G, k, source=None, target=None, weight=None):
    """K-shortest paths"""
    if source != None or target != None:
        return list(
            islice(nx.shortest_simple_paths(G, source, target, weight=weight),
                   k))
    else:
        paths = [
            KSP(G, k, source=i, target=j, weight=weight) for i in G.nodes
            for j in G.nodes if i != j
        ]
        return [[(p[i], p[i + 1]) for i in range(len(p) - 1)] for ps in paths
                for p in ps]
Example #18
0
def test_directed_weighted_shortest_simple_path():
    def cost_func(path):
        return sum(G.edge[u][v]["weight"] for (u, v) in zip(path, path[1:]))

    G = nx.complete_graph(5)
    G = G.to_directed()
    weight = {(u, v): random.randint(1, 100) for (u, v) in G.edges()}
    nx.set_edge_attributes(G, "weight", weight)
    cost = 0
    for path in nx.shortest_simple_paths(G, 0, 3, weight="weight"):
        this_cost = cost_func(path)
        assert_true(cost <= this_cost)
        cost = this_cost
Example #19
0
def test_Greg_Bernstein():
    g1 = nx.Graph()
    g1.add_nodes_from(["N0", "N1", "N2", "N3", "N4"])
    g1.add_edge("N4", "N1", weight=10.0, capacity=50, name="L5")
    g1.add_edge("N4", "N0", weight=7.0, capacity=40, name="L4")
    g1.add_edge("N0", "N1", weight=10.0, capacity=45, name="L1")
    g1.add_edge("N3", "N0", weight=10.0, capacity=50, name="L0")
    g1.add_edge("N2", "N3", weight=12.0, capacity=30, name="L2")
    g1.add_edge("N1", "N2", weight=15.0, capacity=42, name="L3")
    solution = [["N1", "N0", "N3"], ["N1", "N2", "N3"],
                ["N1", "N4", "N0", "N3"]]
    result = list(nx.shortest_simple_paths(g1, "N1", "N3", weight="weight"))
    assert result == solution
Example #20
0
def _sample_path_in_graph(graph,
                          s=None,
                          t=None,
                          k=None,
                          p=0.2,
                          weight='weight'):
    """K-th shortest path sampling, where K ~ Geometric(p)."""
    k = np.random.geometric(p) if k is None else k
    s = np.random.choice(list(graph.nodes.keys())) if s is None else s
    t = np.random.choice(list(graph.nodes.keys())) if t is None else t
    paths = nx.shortest_simple_paths(graph, s, t, weight=weight)

    return list(itertools.islice(paths, k))[-2:]
Example #21
0
	def yen(self, A, o, d, k):
		""" Apply k-shortest path algorithm for routing only """

		if not self.is_od_pair_ok(A, o, d) or k < 0:
			sys.stderr.write('Error: source (%d) or destination (%d) ' % (s,d))
			sys.stderr.write('indexes might be invalid.\n')
			sys.stderr.write('Check the k value (%d) as well.\n' % k)
			sys.stderr.flush()
			return None

		G = nx.from_numpy_matrix(A, create_using=nx.Graph())
		paths = list(nx.shortest_simple_paths(G, s, d, weight=None))
		return paths[:k]
Example #22
0
 def k_shortest_paths(self, graph, src, dst, weight='weight', k=1):
     generator = nx.shortest_simple_paths(graph, source=src,
                                          target=dst, weight=weight)
     shortest_paths = []
     try:
         for path in generator:
             if k <= 0:
                 break
             shortest_paths.append(path)
             k -= 1
         return shortest_paths
     except:
         self.logger.debug("No path between %s and %s" % (src, dst))
Example #23
0
 def get_shortest_path_length(self):
     try:
         #self.simple_paths=list(nx.shortest_simple_paths(self.graph, source=self.node_1, target=self.node_2, weight=None))
         self.simple_paths = list(
             islice(
                 nx.shortest_simple_paths(self.graph,
                                          source=self.node_1,
                                          target=self.node_2,
                                          weight=None), self.k))
         sp = len(self.simple_paths[0]) - 1
     except:
         sp = -1
     return sp
def k_prob_shortest_paths(G, source, target, p=0.95, weight=None):
    paths = []
    cp = 0
    count = 0
    d=[]
    for path in nx.shortest_simple_paths(G, source, target, weight=weight):
        d.append([count, cp])
        paths.append(path)
        cp += ProbOfPath(path, G)
        count += 1
        if cp > p:
            df = pd.DataFrame(d, columns=["count","cdf"])
            return paths, df
    def get_paths(
        self,
        source: Address,
        target: Address,
        value: int,
        max_paths: int,
        diversity_penalty: float = DIVERSITY_PEN_DEFAULT,
        hop_bias: float = 1,
        **kwargs,
    ):
        assert hop_bias == 1, 'Only hop_bias 1 is supported'
        max_paths = min(max_paths, MAX_PATHS_PER_REQUEST)
        visited: Dict[ChannelIdentifier, float] = {}
        paths: List[List[Address]] = []

        for _ in range(max_paths):
            # update edge weights
            for node1, node2 in self.G.edges():
                edge = self.G[node1][node2]
                edge['weight'] = self.edge_weight(visited, edge)

            # find next path
            all_paths = nx.shortest_simple_paths(self.G, source, target, weight='weight')
            try:
                path = next(all_paths)
                while path in paths:  # skip duplicates
                    path = next(all_paths)
            except StopIteration:
                break

            # update visited penalty dict
            for node1, node2 in zip(path[:-1], path[1:]):
                channel_id = self.G[node1][node2]['view'].channel_id
                visited[channel_id] = visited.get(channel_id, 0) + diversity_penalty

            paths.append(path)
            if len(paths) >= max_paths:
                break

        result = []
        for path in paths:
            fee = 0
            for node1, node2 in zip(path[:-1], path[1:]):
                fee += self.G[node1][node2]['view'].relative_fee

            result.append(dict(
                path=path,
                estimated_fee=0,
            ))

        return result
Example #26
0
def find_paths_from_adj_per_inst(input):
    adj, concepts, qm, am = input
    adj = adj.toarray()
    ij, k = adj.shape

    adj = np.any(adj.reshape(ij // k, k, k), axis=0)
    simple_schema_graph = nx.from_numpy_matrix(adj)
    mapping = {i: int(c) for (i, c) in enumerate(concepts)}
    simple_schema_graph = nx.relabel_nodes(simple_schema_graph, mapping)
    qcs, acs = concepts[qm].tolist(), concepts[am].tolist()
    pfr_qa = []
    lengths = []
    for ac in acs:
        for qc in qcs:
            if qc not in simple_schema_graph.nodes(
            ) or ac not in simple_schema_graph.nodes():
                print('QA pair doesn\'t exist in schema graph.')
                pf_res = None
                lengths.append([0] * 3)
            else:
                all_path = []
                try:
                    for p in nx.shortest_simple_paths(simple_schema_graph,
                                                      source=qc,
                                                      target=ac):
                        if len(p) >= 5:
                            break
                        if len(p) >= 2:  # skip paths of length 1
                            all_path.append(p)
                except nx.exception.NetworkXNoPath:
                    pass

                length = [len(x) for x in all_path]
                lengths.append(
                    [length.count(2),
                     length.count(3),
                     length.count(4)])
                pf_res = []
                for p in all_path:
                    rl = []
                    for src in range(len(p) - 1):
                        src_concept = p[src]
                        tgt_concept = p[src + 1]
                        rel_list = get_edge(src_concept, tgt_concept)
                        rl.append(rel_list)
                    pf_res.append({"path": p, "rel": rl})
            pfr_qa.append({"ac": ac, "qc": qc, "pf_res": pf_res})
    g = nx.convert_node_labels_to_integers(simple_schema_graph,
                                           label_attribute='cid')

    return pfr_qa, nx.node_link_data(g), lengths
Example #27
0
    def k_shortestpaths_from_src_to_dst(self, graph, src, dst, weight='weight', k=1):

        generator = nx.shortest_simple_paths(graph, source=src,
                                             target=dst, weight=weight)
        shortest_paths = []
        try:
            for path in generator:
                if k <= 0:
                    break
                shortest_paths.append(path)
                k -= 1
            return shortest_paths
        except:
            self.logger.debug("No path between %s and %s" % (src, dst))
def shortest_paths(file_path, num_paths):
    #initialize graph
    subway_graph = nx.DiGraph()
    file = open(file_path)

    #read in edges from file
    for line in file:
        line_num, from_id, to_id, w = line.split(",")
        subway_graph.add_edge(from_id[1:-1], to_id[1:-1], weight=float(w[:-1]))

    #compute shortest paths
    paths = nx.shortest_simple_paths(subway_graph, "R19", "104", "weight")
    for i in range(num_paths):
        print(next(paths))
Example #29
0
    def get_optimal_num_paths(self, src, dst):
        """
        Get the n-most optimal paths according to MAX_PATHS
        """
        all_paths = []
        # get all available path between src and dst
        all_paths = list(nx.shortest_simple_paths(self.net, src, dst))

        counter = len(
            all_paths) if len(all_paths) < MAX_NUM_PATHS else MAX_NUM_PATHS
        # return the two least cost paths [[5, 4, 3, 1], [5, 2, 1]]
        paths = sorted(all_paths,
                       key=lambda x: self.get_each_path_cost(x))[0:counter]
        return paths
Example #30
0
 def creat_k_paths(self, src_switch_dp, dst_switch_dp, k, weight='weight'):
     import copy
     allpaths = networkx.shortest_simple_paths(copy.deepcopy(self.graph), src_switch_dp, dst_switch_dp, weight=weight)
     kpaths = []
     try:
         for path in allpaths:
             kpaths.append(path)
             k = k-1
             if k == 0:
                 break
     except Exception as e:
         #print(e)
         pass
     return kpaths
Example #31
0
    def __init__(self, connection):
        # Keep track of the connection to the switch so that we can
        # send it messages!
        self.connection = connection

        # This binds our PacketIn event listener
        connection.addListeners(self)

        # Use this table to keep track of which ethernet address is on
        # which switch port (keys are MACs, values are ports).
        self.mac_to_port = {}

        S = 10
        N = 10
        r = 4
        use_ecmp = True

        # seed = 100 is constant so that the corresponding topo and controller graph are the same
        rrg = nx.random_regular_graph(r, N, 100)
        for i in range(S):
            src_hwaddr = EthAddr('00:00:00:00:00:%02d' % (i + 1))
            for j in range(S):
                if i == j:
                    continue
                dst_hwaddr = EthAddr('00:00:00:00:00:%02d' % (j + 1))
                src_switch = i % N
                dst_switch = j % N

                if src_switch == dst_switch:
                    path = [[src_switch]]
                elif use_ecmp:
                    paths = []
                    for p in nx.all_shortest_paths(rrg, src_switch,
                                                   dst_switch):
                        paths.append(p)
                    path = random.sample(paths, min(len(paths), 8))
                    log.error(path)
                    log.error('PATH LEN: %d' % (len(path)))
                    log.error('\n')
                else:
                    path = list(
                        islice(
                            nx.shortest_simple_paths(rrg, src_switch,
                                                     dst_switch), 8))
                    log.error(path)
                    log.error('PATH LEN: %d' % (len(path)))
                    log.error('\n')
                for path_ in path:
                    self.add_entry(src_hwaddr, dst_hwaddr, [i] + path_ + [j],
                                   S)
Example #32
0
def testksp():
    """
    测试ksp算法,kth shortpath 算法,找出两点之间最短的k条路径
    :return:
    """
    G = nx.DiGraph()
    #给图添加节点
    for i in range(11):
        G.add_node(str(i + 1))
    # link = [(1, 2), (2, 1),
    #         (1, 4), (4, 1),
    #         (2, 3), (3, 2),
    #         (2, 4), (4, 2),
    #         (3, 5), (5, 3),
    #         (4, 6), (6, 4),
    #         (5, 6), (6, 5),
    #         (5, 7), (7, 5),
    #         (6, 8), (8, 6),
    #         (7, 8), (8, 7),
    #         (7, 9), (9, 7),
    #         (8, 10), (10, 8),
    #         (9, 11), (11, 9),
    #         (10, 11), (11, 10)]

    link = [(1, 2), (2, 1), (1, 3), (3, 1), (2, 4), (4, 2), (3, 4), (4, 3)]
    #给图添加边
    for i in range(len(link)):
        G.add_edge(str((link[i][0])), str(link[i][1]), weight=1)

    print(list(nx.shortest_simple_paths(G, '1', '4', weight=None)))
    k = 5
    allpath = list(
        itertools.islice(nx.shortest_simple_paths(G, '1', '4', weight=None),
                         k))  #获取无重复节点的路径
    print("所有的路径")
    for path in allpath:
        print(path)
Example #33
0
 def find_shortest_paths(cls,
                         network,
                         source,
                         target,
                         method='all_shortest',
                         k=1):
     assert method in ['first_shortest', 'k_shortest', 'all_shortest']
     if method == 'first_shortest':
         shortest_path = nx.dijkstra_path(network, source, target)
         return [shortest_path]
     elif method == 'k_shortest':
         return list(
             islice(nx.shortest_simple_paths(network, source, target), k))
     elif method == 'all_shortest':
         return list(nx.all_shortest_paths(network, source, target))
Example #34
0
def k_shortest_paths(G, source, target, k, weight=None):
    """Calculate k shortest paths
    :param G: networkx topology
           source: source node
           target: target node
           k: the number of shortest paths
           weight: weight for calculation of shortest path
    :return: a list of k shortest paths
    """
    try:
        return list(
            islice(nx.shortest_simple_paths(G, source, target, weight=weight),
                   k))
    except:
        return []
 def path_relevance_user(self, path):
     user = path[0]
     index = 1
     rel = []
     while index < len(path) - 1:
         current_node = path[index]
         relevance = 0
         con_paths = nx.shortest_simple_paths(self.graph, user, current_node)
         for con_path in con_paths:
             if len(con_path) >= 4:
                 break
             relevance += 1
         rel.append(relevance)
         index += 1
     return self.aggregate_func(rel)
 def k_shortest_paths(self,graph,src,dst,weight='weight',k=1):
     """
        get the k shortest path between src and dst
     """
     paths = nx.shortest_simple_paths(graph,source=src,target=dst,weight='weight')
     shortest_paths = []
     try:
         for path in paths:
             if k <= 0:
                 break
             shortest_paths.append(path)
             k -= 1
         return shortest_paths
     except:
         self.logger.debug("No path between %s and %s" % (src, dst))
Example #37
0
File: rwanet.py Project: zzp110/ZTE
 def k_shortest_paths(self, source, target):
     if source is None:
         return [None]
     generator = shortest_simple_paths(self,
                                       source,
                                       target,
                                       weight=self.weight)
     rtn = []
     index = 0
     for i in generator:
         index += 1
         if index > self.k:
             break
         rtn.append(i)
     return rtn
Example #38
0
def k_shortest_paths(G, o, d, k):
    r"""返回o到d的前k短路径,不存在的返回空列表"""
    try:
        paths = nx.shortest_simple_paths(G, o, d)
        res = []
        i = 0
        for path in paths:
            res.append(path)
            i += 1
            if i >= k:
                break
        return res
    except nx.exception.NetworkXNoPath:
        print(f'No this path between node {o} and node {d}. skip...')
        return []
Example #39
0
 def all_paths_src_dst(self, graph, src, dst, weight='weight'):
     """
         Great all paths of src to dst.
     """
     generator = nx.shortest_simple_paths(graph, source=src,
                                          target=dst, weight=weight)
     paths = []
     try:
         for path in generator:
             # if k <= 0:
             #     break
             paths.append(path)
             # k -= 1
         return paths
     except:
         self.logger.debug("No path between %s and %s" % (src, dst))
Example #40
0
 def k_shortest_paths(self, graph, src, dst, weight='weight', k=4):
     """
         Creat K shortest paths from src to dst.
         generator produces lists of simple paths, in order from shortest to longest.
     """
     generator = nx.shortest_simple_paths(graph, source=src, target=dst, weight=weight)
     shortest_paths = []
     try:
         for path in generator:
             if k <= 0:
                 break
             shortest_paths.append(path)
             k -= 1
         return shortest_paths
     except:
         self.logger.debug("No path between %s and %s" % (src, dst))
Example #41
0
 def get_mol_path(self):
     """
     get all molecular paths from end to end (end pairs of atom in molecule)
     :return:
     """
     end_pairs = self._get_end_pairs()  # get end point pairs
     paths_with_attr = []  # with attribute
     all_paths = []
     num_node_longest_path = 0
     num_max_path_neighbors = 0
     # num_max_atom_in_path = 0
     if len(self.n2n) >= 2:
         for pairs in end_pairs:
             # print(pairs)
             shortest_path = nx.shortest_simple_paths(
                 self.g, pairs[0], pairs[1])
             shortest_path = list(shortest_path)[0]
             num_neig = self.count_neighbors(shortest_path)
             num_atoms = np.sum([
                 get_num_atom_by_smiles(self.id2smiles[i])
                 for i in shortest_path
             ])
             paths_with_attr.append({
                 'path': shortest_path,
                 'len_path': len(shortest_path),
                 'num_neig': num_neig,
                 'num_atoms': num_atoms
             })
             # print(shortest_path)
             if len(shortest_path) > num_node_longest_path:
                 num_node_longest_path = len(shortest_path)
             if num_neig > num_max_path_neighbors:
                 num_max_path_neighbors = num_neig
             # if num_atoms > num_max_atom_in_path:
             #     num_max_atom_in_path = num_atoms
         paths_with_attr = sorted(paths_with_attr,
                                  key=itemgetter('num_atoms'),
                                  reverse=True)
         paths_with_attr = sorted(paths_with_attr,
                                  key=itemgetter('num_neig'),
                                  reverse=True)  # test data LINE 401
         for path in paths_with_attr:
             all_paths.append(path['path'])
     if len(self.n2n
            ) == 1:  # only one node in all graph, test data LINE 546
         all_paths = list(self.n2n.keys())
     return all_paths
Example #42
0
def inversion(graph, reps):
    reps_so_far = 0
    while reps_so_far < int(reps):
        # we assume that each chromosome has an equal chance of rearrangement
        chromosomes = []
        for chromosome in nx.connected_components(graph):
            chromosomes.append(chromosome)
        # select a chromosome to rearrange
        r_chromosome = random.choice(chromosomes)
        # select two genes to be breakpoints
        r_genes = random.sample(r_chromosome, 2)
        print("Inversion between marker {} and {}".format(
            r_genes[0], r_genes[1]))
        # get nodes captured in the inversion
        inversion_nodes_list = [
            node
            for node in nx.shortest_simple_paths(graph, r_genes[0], r_genes[1])
        ][0]
        inversion_nodes_set = set(inversion_nodes_list)
        # record new graph edges
        new_edges = []
        # loop over all edges in the graph
        for n1, n2 in graph.edges():
            # difference can be both nodes -> therefore not in the inversion
            diff = {n1, n2}.difference(inversion_nodes_set)
            if len(diff) == 2:
                new_edges.append(diff)
            # or one node -> therefore one node is right next to the inversion
            elif len(diff) == 1:
                # n is a node next to the inversion, graph[n] are the nodes that n connects to
                # inversion_nodes_list[0] and inversion_nodes_list[-1] are the ends of the inversion
                n = list(diff)[0]
                if inversion_nodes_list[0] in graph[n] or n in graph[
                        inversion_nodes_list[0]]:
                    new_edges.append((n, inversion_nodes_list[-1]))
                if inversion_nodes_list[-1] in graph[n] or n in graph[
                        inversion_nodes_list[-1]]:
                    new_edges.append((n, inversion_nodes_list[0]))
            # or no nodes -> therefore fully within the inversion
            else:
                new_edges.append((n1, n2))
        # make a graph from new_edges
        reps_so_far += 1
        new_graph = nx.Graph()
        new_graph.add_edges_from(new_edges)
        graph = new_graph
    return (graph)
    def get_shortest_paths(self, device, gateway):
        """Return list of shortest paths from device to gateway.

        Each path in the list is a list containing entity IDs. The first
        element of the list will be device.getPrimaryId(), and the last will
        be gateway.getPrimaryId().
        
        * Node IDs beginning with a / are a Device.getPrimaryId()
        * Node IDs beginning with a !/ are a DeviceComponent.getPrimaryId()
        * Remaining node IDs are MAC addresses.
        
        An empty list is returned if not paths from device to gateway exist.

        """
        device_entity = self.to_entity(device)
        gateway_entity = self.to_entity(gateway)
        cache_key = (device_entity, gateway_entity)

        cached_paths = self.paths_cache.get(cache_key)
        if cached_paths is not None:
            return cached_paths

        g = self.get_graph(gateway_entity)

        # No paths if the device or gateway isn't in the gateway's graph.
        if device_entity not in g or gateway_entity not in g:
            return []

        shortest_path = None
        shortest_paths = []

        for path in shortest_simple_paths(g, device_entity, gateway_entity):
            path_len = len(path)
            if not shortest_path or path_len <= shortest_path:
                # Interested in all paths the same length as the shortest.
                shortest_path = path_len
                shortest_paths.append(path)
            else:
                # Not interested in paths longer than the shortest.
                break

        self.paths_cache.set(cache_key, shortest_paths)

        return shortest_paths
Example #44
0
def test_ssp_source_missing():
    G = nx.Graph()
    nx.add_path(G, [0, 1, 2])
    nx.add_path(G, [3, 4, 5])
    paths = list(nx.shortest_simple_paths(G, 0, 3))
Example #45
0
def test_ssp_multigraph():
    G = nx.MultiGraph()
    nx.add_path(G, [1, 2, 3])
    paths = list(nx.shortest_simple_paths(G, 1, 4))
Example #46
0
def test_ssp_target_missing():
    G = nx.Graph()
    nx.add_path(G, [1, 2, 3])
    paths = list(nx.shortest_simple_paths(G, 1, 4))
Example #47
0
 def find_shortest_path(node1, node2, number):
     p = list(nx.shortest_simple_paths(self.graph, node1, node2, weight = 'distance'))
     return p[number], len(p)
Example #48
0
def test_shortest_simple_paths_directed():
    G = nx.cycle_graph(7, create_using=nx.DiGraph())
    paths = nx.shortest_simple_paths(G, 0, 3)
    assert_equal([path for path in paths], [[0, 1, 2, 3]])
Example #49
0
	def k_shortest_path(self, source, target, k = 6, weight = None):
		"""weight is the key and not the actual weight value"""
		#source = fromNode.get_id()
		#target = toNode.get_id()
		paths = nx.shortest_simple_paths(self.G, source, target, weight = weight)
		return list(islice(paths, k))
Example #50
0
 def k_shortest_paths(G, source, target, k, weight=None):
     return list(islice(nx.shortest_simple_paths(G, source, target, weight=weight), k))
Example #51
0
def longest_path(edge_list, nodes, verbose=False,
                 skeleton_arrays=None, save_png=False, save_name=None):
    '''
    Takes the output of pre_graph and runs the shortest path algorithm.

    Parameters
    ----------

    edge_list : list
        Contains the connectivity information for the graphs.

    nodes : list
        A complete list of all of the nodes. The other nodes lists have
        been separated as they are labeled differently.

    verbose : bool, optional
        If True, enables the plotting of the graph.

    skeleton_arrays : list, optional
        List of the skeleton arrays. Required when verbose=True.

    save_png : bool, optional
        Saves the plot made in verbose mode. Disabled by default.

    save_name : str, optional
        For use when ``save_png`` is enabled.
        **MUST be specified when ``save_png`` is enabled.**

    Returns
    -------

    max_path : list
        Contains the paths corresponding to the longest lengths for
        each skeleton.

    extremum : list
        Contains the starting and ending points of max_path

    '''
    num = len(nodes)

    # Initialize lists
    max_path = []
    extremum = []
    graphs = []

    for n in range(num):
        G = nx.Graph()
        G.add_nodes_from(nodes[n])
        for i in edge_list[n]:
            G.add_edge(i[0], i[1], weight=i[2][1])
        # networkx 2.0 returns a two-element tuple. Convert to a dict first
        paths = dict(nx.shortest_path_length(G, weight='weight'))
        values = []
        node_extrema = []

        for i in paths.keys():
            j = max(paths[i].items(), key=operator.itemgetter(1))
            node_extrema.append((j[0], i))
            values.append(j[1])
        max_path_length = max(values)
        start, finish = node_extrema[values.index(max_path_length)]
        extremum.append([start, finish])

        # def get_weight(pat):
        #     return sum([G.edge[x][y]['weight'] for x, y in
        #                 zip(pat[:-1], pat[1:])])

        # for pat in nx.shortest_simple_paths(G, start, finish):
        #     if np.isclose(get_weight(pat), max_path_length) or get_weight(pat) > max_path_length:
        #         long_path = pat
        #         break

        long_path = \
            list(nx.shortest_simple_paths(G, start, finish, 'weight'))[-1]

        max_path.append(long_path)
        graphs.append(G)

        if verbose or save_png:
            if not skeleton_arrays:
                Warning("Must input skeleton arrays if verbose or save_png is"
                        " enabled. No plots will be created.")
            elif save_png and save_name is None:
                Warning("Must give a save_name when save_png is enabled. No"
                        " plots will be created.")
            else:
                # Check if skeleton_arrays is a list
                assert isinstance(skeleton_arrays, list)
                import matplotlib.pyplot as p
                # if verbose:
                #     print("Filament: %s / %s" % (n + 1, num))
                p.subplot(1, 2, 1)
                p.imshow(skeleton_arrays[n], interpolation="nearest",
                         origin="lower")

                p.subplot(1, 2, 2)
                elist = [(u, v) for (u, v, d) in G.edges(data=True)]
                pos = nx.spring_layout(G)
                nx.draw_networkx_nodes(G, pos, node_size=200)
                nx.draw_networkx_edges(G, pos, edgelist=elist, width=2)
                nx.draw_networkx_labels(
                    G, pos, font_size=10, font_family='sans-serif')
                p.axis('off')

                if save_png:
                    p.savefig(save_name)
                    p.close()
                if verbose:
                    p.show()
                    p.clf()

    return max_path, extremum, graphs
Example #52
0
def test_ssp_source_missing():
    G = nx.Graph()
    G.add_path([1,2,3])
    paths = list(nx.shortest_simple_paths(G, 0, 3))