コード例 #1
0
def calculate_end_to_end_delay_for_arc(G, edges):
    adjusted_edges = [__get_edge_id_from_label(e) for e in edges]
    dG = nx.DiGraph()
    added_nodes = []
    for u, v in G.edges():
        if G[u][v]['id'] in adjusted_edges:
            if u not in added_nodes:
                dG.add_node(u, **G.nodes[u])
                added_nodes.append(u)
            if v not in added_nodes:
                dG.add_node(v, **G.nodes[v])
                added_nodes.append(v)
            dG.add_edge(u, v, **G[u][v])

    longest_path = dag_longest_path(dG, weight="delay")

    if len(longest_path) == 0:
        return 0
    path_iter = iter(longest_path)
    u = next(path_iter)
    delay = 0
    for v in path_iter:
        delay += dG[u][v]['delay']
        u = v
    return delay
コード例 #2
0
ファイル: finite_poset.py プロジェクト: rusi/mcdp
    def get_test_chain(self, n):  # @UnusedVariable
        if not self.elements:
            raise Uninhabited()
        G = self._get_graph_closure_no_cycles()
        # this returns the paths at least of length 2
        from networkx.algorithms.dag import dag_longest_path

        path = list(dag_longest_path(G))
        if not path:
            one = list(self.elements)[0]
            return [one]
        assert path, (self.elements, self.relations)
        return path
コード例 #3
0
ファイル: finite_poset.py プロジェクト: AndreaCensi/mcdp
    def get_test_chain(self, n):  # @UnusedVariable
        if not self.elements:
            raise Uninhabited()
        G = self._get_graph_closure_no_cycles()
        # this returns the paths at least of length 2
        from networkx.algorithms.dag import dag_longest_path

        path = list(dag_longest_path(G))
        if not path:
            one = list(self.elements)[0]
            return [one]
        assert path, (self.elements, self.relations)
        return path
コード例 #4
0
ファイル: rosalind_hdag.py プロジェクト: sunhuaiyu/rosalind
import networkx as nx
from networkx.algorithms.dag import dag_longest_path

f = open('rosalind_hdag.txt')
k = int(f.readline().rstrip())

result = []
for i in range(k):

    n, m = [int(l) for l in f.readline().rstrip().split()]
    nodes = [i + 1 for i in range(n)]
    G = nx.DiGraph()
    G.add_nodes_from(nodes)

    for j in range(m):
        u, v = [int(l) for l in f.readline().rstrip().split()]
        G.add_edge(u, v)

    output = '-1'
    try:
        path = dag_longest_path(G)
        if len(path) == n:
            output = '1 ' + ' '.join([str(i) for i in path])
    except:
        pass
    result.append(output)

result = '\n'.join(result)
print(result)
open('rosalind_hdag_sub.txt', 'wt').write(result)
コード例 #5
0
def frame_alignment(query_features,
                    refer_features,
                    top_K=5,
                    min_sim=0.80,
                    max_step=10):
    """
      用于计算两组特征(已经做过l2-norm)之间的帧匹配结果
      Args:
        query_features: shape: [N, D]
        refer_features: shape: [M, D]
        top_K: 取前K个refer_frame
        min_sim: 要求query_frame与refer_frame的最小相似度
        max_step: 有边相连的结点间的最大步长
      Returns:
        path_query: shape: [1, L]
        path_refer: shape: [1, L]
    """
    node_pair2id = {}
    node_id2pair = {}
    node_id2pair[0] = (-1, -1)  # source
    node_pair2id[(-1, -1)] = 0
    node_num = 1

    DG = nx.DiGraph()
    DG.add_node(0)

    idxs, unsorted_dists, sorted_dists = dists(query_features, refer_features)

    # add nodes
    for qf_idx in range(query_features.shape[0]):
        for k in range(top_K):
            rf_idx = idxs[qf_idx][k]
            sim = 1 - sorted_dists[qf_idx][k]
            if sim < min_sim:
                break
            node_id2pair[node_num] = (qf_idx, rf_idx)
            node_pair2id[(qf_idx, rf_idx)] = node_num
            DG.add_node(node_num)
            node_num += 1

    node_id2pair[node_num] = (query_features.shape[0], refer_features.shape[0]
                              )  # sink
    node_pair2id[(query_features.shape[0], refer_features.shape[0])] = node_num
    DG.add_node(node_num)
    node_num += 1

    # link nodes

    for i in range(0, node_num - 1):
        for j in range(i + 1, node_num - 1):

            pair_i = node_id2pair[i]
            pair_j = node_id2pair[j]

            if (pair_j[0] > pair_i[0] and pair_j[1] > pair_i[1] and
                    #pair_j[0] - pair_i[0] <= max_step and pair_j[1] - pair_i[1] <= max_step and
                    abs(pair_j[1] - pair_i[1] -
                        (pair_j[0] + pair_i[0])) <= max_step):
                qf_idx = pair_j[0]
                rf_idx = pair_j[1]
                DG.add_edge(i, j, weight=1 - unsorted_dists[qf_idx][rf_idx])

    for i in range(0, node_num - 1):
        j = node_num - 1

        pair_i = node_id2pair[i]
        pair_j = node_id2pair[j]

        if (pair_j[0] > pair_i[0] and pair_j[1] > pair_i[1] and
                #pair_j[0] - pair_i[0] <= max_step and pair_j[1] - pair_i[1] <= max_step and
                abs(pair_j[1] - pair_i[1] -
                    (pair_j[0] - pair_i[0])) <= max_step):
            qf_idx = pair_j[0]
            rf_idx = pair_j[1]
            DG.add_edge(i, j, weight=0)

    longest_path = dag_longest_path(DG)
    if 0 in longest_path:
        longest_path.remove(0)  # remove source node
    if node_num - 1 in longest_path:
        longest_path.remove(node_num - 1)  # remove sink node
    path_query = [node_id2pair[node_id][0] for node_id in longest_path]
    path_refer = [node_id2pair[node_id][1] for node_id in longest_path]

    score = 0.0
    for (qf_idx, rf_idx) in zip(path_query, path_refer):
        score += 1 - unsorted_dists[qf_idx][rf_idx]

    return path_query, path_refer, score
コード例 #6
0
ファイル: rosalind_hdag.py プロジェクト: sunhuaiyu/rosalind
f = open('rosalind_hdag.txt')
k = int(f.readline().rstrip())

result = []
for i in range(k):
    
    n, m = [int(l) for l in f.readline().rstrip().split()]
    nodes = [i+1 for i in range(n)]
    G = nx.DiGraph()
    G.add_nodes_from(nodes)
    
    for j in range(m):
        u, v = [int(l) for l in f.readline().rstrip().split()]
        G.add_edge(u, v)
    
    output = '-1'
    try:
        path = dag_longest_path(G)
        if len(path) == n:
            output = '1 ' + ' '.join([str(i) for i in path])
    except:
        pass
    result.append(output)

    
result = '\n'.join(result)
print(result)
open('rosalind_hdag_sub.txt', 'wt').write(result)