当前位置: 首页>>代码示例>>Python>>正文


Python networkx.max_weight_matching方法代码示例

本文整理汇总了Python中networkx.max_weight_matching方法的典型用法代码示例。如果您正苦于以下问题:Python networkx.max_weight_matching方法的具体用法?Python networkx.max_weight_matching怎么用?Python networkx.max_weight_matching使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在networkx的用法示例。


在下文中一共展示了networkx.max_weight_matching方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: mwgm_networkx

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import max_weight_matching [as 别名]
def mwgm_networkx(pairs, sim_mat):
    def str_splice(prefix, index):
        return prefix + "_" + str(index)

    def remove_prefix(string):
        params = string.split('_')
        assert len(params) == 2
        return int(params[-1])

    prefix1 = 's'
    prefix2 = 't'
    graph = nx.Graph()
    for pair in pairs:
        graph.add_edge(str_splice(prefix1, pair[0]), str_splice(prefix2, pair[1]), weight=sim_mat[pair[0], pair[1]])
    edges = nx.max_weight_matching(graph, maxcardinality=False)
    matching_pairs = set()
    for v1, v2 in edges:
        if v1.startswith(prefix1):
            s = remove_prefix(v1)
            t = remove_prefix(v2)
        else:
            t = remove_prefix(v1)
            s = remove_prefix(v2)
        matching_pairs.add((s, t))
    return matching_pairs 
开发者ID:nju-websoft,项目名称:BootEA,代码行数:27,代码来源:train_bp.py

示例2: match_objects_uniquely

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import max_weight_matching [as 别名]
def match_objects_uniquely(candidates, targets, threshold=0.5):
    g = nx.Graph()
    for t in targets:
        g.add_node(t)
    for c in candidates:
        g.add_node(c)

    for t in targets:
        tbb = BBox(*t.bbox)
        for c in candidates:
            cbb = BBox(*c.bbox)
            intersection = tbb.intersection(cbb).area
            union = tbb.area + cbb.area - intersection

            try:
                current_iou = intersection / float(union)
            except ZeroDivisionError:
                current_iou = float('inf')
                pass

            if current_iou >= threshold:
                g.add_edge(t, c, weight=current_iou)

    target_set = set(targets)
    matching = nx.max_weight_matching(g, maxcardinality=True)  # <- a dict with  v->c and c->v both
    hits = [(t, c) for (t, c) in matching.items() if t in target_set]
    return hits 
开发者ID:jfemiani,项目名称:facade-segmentation,代码行数:29,代码来源:metrics.py

示例3: DSP_Matching

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import max_weight_matching [as 别名]
def DSP_Matching(Syndrome, External, dim, alt_ext):

    # Fully connect check operators
    for check1 in Syndrome.nodes():
        for check2 in Syndrome.nodes():
            if check1 != check2:
                weight = - common.euclidean_dist(check1, check2) +.1
                Syndrome.add_edge(*(check1, check2), weight=weight)

    # Generate Boundary Graph
    External_Graph = nx.Graph()

    for m in Syndrome.nodes():
        ext = DSP_AssociatedExternal(m, External)
        External_Graph.add_node(ext)
        weight = - common.euclidean_dist(m, ext)
        Syndrome.add_edge(*(m, ext), weight=weight)

    # Ensure even number of elements in Syndrome
    # so min weight matching can proceed successfully
    if len(Syndrome.nodes()) % 2 != 0:
        Syndrome.add_node(alt_ext)
        External_Graph.add_node(alt_ext)

    # Connect External Nodes
    edges = itertools.combinations(External_Graph,2)
    Syndrome.add_edges_from(edges, weight = 0)

    TempMatching = nx.max_weight_matching(Syndrome, maxcardinality=True)
    Matching = {}
    # each edge appears twice in TempMatching
    # Should only appear once in Matching
    for node, neighbor in TempMatching.items():
        if neighbor not in Matching and node not in Matching:
            if node not in External or neighbor not in External:
                if node != alt_ext and neighbor != alt_ext:
                    Matching[node] = neighbor

    return Matching 
开发者ID:jacobmarks,项目名称:QTop,代码行数:41,代码来源:dsp.py

示例4: construct_graph

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import max_weight_matching [as 别名]
def construct_graph(user_ids, disallowed_meetings):
    """
    We can use a maximum matching algorithm for this:
    https://en.wikipedia.org/wiki/Blossom_algorithm
    Yay graphs! Networkx will do all the work for us.
    """

    # special weights that be put on the matching potential of each meeting,
    # depending on heuristics for what makes a good/bad potential meeting.
    meeting_to_weight = {}

    # This creates the graph and the maximal matching set is returned.
    # It does not return anyone who didn't get matched.
    meetings = []
    possible_meetings = {
        meeting for meeting in itertools.combinations(user_ids, 2)
    }
    allowed_meetings = possible_meetings - disallowed_meetings

    for meeting in allowed_meetings:
        weight = meeting_to_weight.get(meeting, 1.0)
        meetings.append(meeting + ({'weight': weight},))

    graph = nx.Graph()
    graph.add_nodes_from(user_ids)
    graph.add_edges_from(meetings)

    return nx.max_weight_matching(graph) 
开发者ID:Yelp,项目名称:beans,代码行数:30,代码来源:pair_match.py

示例5: get_UA_pairs

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import max_weight_matching [as 别名]
def get_UA_pairs(UA, AC, use_graph=True):
    """

    """

    bonds = get_bonds(UA, AC)

    if len(bonds) == 0:
        return [()]

    if use_graph:
        G = nx.Graph()
        G.add_edges_from(bonds)
        UA_pairs = [list(nx.max_weight_matching(G))]
        return UA_pairs

    max_atoms_in_combo = 0
    UA_pairs = [()]
    for combo in list(itertools.combinations(bonds, int(len(UA) / 2))):
        flat_list = [item for sublist in combo for item in sublist]
        atoms_in_combo = len(set(flat_list))
        if atoms_in_combo > max_atoms_in_combo:
            max_atoms_in_combo = atoms_in_combo
            UA_pairs = [combo]

        elif atoms_in_combo == max_atoms_in_combo:
            UA_pairs.append(combo)

    return UA_pairs 
开发者ID:jensengroup,项目名称:xyz2mol,代码行数:31,代码来源:xyz2mol.py

示例6: optimal_permutation

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import max_weight_matching [as 别名]
def optimal_permutation(frompts, topts, allow_reflection=False, 
                        distance_metric='euclidean'):
    # compute distances of points
    D = cdist(frompts, topts, distance_metric)
    if allow_reflection:
        Dreflection = cdist(frompts, -1 * topts)
        reflection_sign = np.where(Dreflection < D, -1, 1)
        D = np.minimum(Dreflection, D)
    else:
        reflection_sign = np.ones(frompts.shape[0])
    Dmax = D.max()

    # construct bipartite graph
    import networkx as nx
    graph = nx.Graph()
    for i in xrange(frompts.shape[0]):
        graph.add_node("A%d" % i)
        graph.add_node("B%d" % i)
    for i in xrange(frompts.shape[0]):
        for j in xrange(frompts.shape[0]):
            w = Dmax - D[i, j]
            graph.add_edge("A%d" % i, "B%d" % j, {'weight': w})
    # compute max matching of graph
    mates = nx.max_weight_matching(graph, True)
    # fill permutation matrix according to matching result
    R = np.zeros((frompts.shape[0], topts.shape[0]))
    for k, v in mates.iteritems():
        if k.startswith('A'):
            i, j = int(k[1:]), int(v[1:])
            R[i, j] = reflection_sign[i, j] if allow_reflection else 1

    R = R.T  # mapping frompts-topts
    return R 
开发者ID:tneumann,项目名称:cmm,代码行数:35,代码来源:align.py

示例7: test_trivial1

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import max_weight_matching [as 别名]
def test_trivial1(self):
        """Empty graph"""
        G = nx.Graph()
        assert_equal(nx.max_weight_matching(G),{}) 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:6,代码来源:test_matching.py

示例8: test_trivial2

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import max_weight_matching [as 别名]
def test_trivial2(self):
        """Self loop"""
        G = nx.Graph()
        G.add_edge(0, 0, weight=100)
        assert_equal(nx.max_weight_matching(G),{}) 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:7,代码来源:test_matching.py

示例9: test_trivial3

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import max_weight_matching [as 别名]
def test_trivial3(self):
        """Single edge"""
        G = nx.Graph()
        G.add_edge(0, 1)
        assert_equal(nx.max_weight_matching(G),
                     {0: 1, 1: 0}) 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:8,代码来源:test_matching.py

示例10: test_trivial4

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import max_weight_matching [as 别名]
def test_trivial4(self):
        """Small graph"""
        G = nx.Graph()
        G.add_edge('one', 'two', weight=10)
        G.add_edge('two', 'three', weight=11)
        assert_equal(nx.max_weight_matching(G),
                     {'three': 'two', 'two': 'three'}) 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:9,代码来源:test_matching.py

示例11: test_trivial5

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import max_weight_matching [as 别名]
def test_trivial5(self):
        """Path"""
        G = nx.Graph()
        G.add_edge(1, 2, weight=5)
        G.add_edge(2, 3, weight=11)
        G.add_edge(3, 4, weight=5)
        assert_equal(nx.max_weight_matching(G),
                     {2: 3, 3: 2})
        assert_equal(nx.max_weight_matching(G, 1),
                     {1: 2, 2: 1, 3: 4, 4: 3}) 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:12,代码来源:test_matching.py

示例12: test_negative_weights

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import max_weight_matching [as 别名]
def test_negative_weights(self):
        """Negative weights"""
        G = nx.Graph()
        G.add_edge(1, 2, weight=2)
        G.add_edge(1, 3, weight=-2)
        G.add_edge(2, 3, weight=1)
        G.add_edge(2, 4, weight=-1)
        G.add_edge(3, 4, weight=-6)
        assert_equal(nx.max_weight_matching(G),
                     {1: 2, 2: 1})
        assert_equal(nx.max_weight_matching(G, 1),
                     {1: 3, 2: 4, 3: 1, 4: 2}) 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:14,代码来源:test_matching.py

示例13: test_s_blossom

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import max_weight_matching [as 别名]
def test_s_blossom(self):
        """Create S-blossom and use it for augmentation:"""
        G = nx.Graph()
        G.add_weighted_edges_from([ (1, 2, 8), (1, 3, 9), 
                                    (2, 3, 10), (3, 4, 7) ])
        assert_equal(nx.max_weight_matching(G),
                     {1: 2, 2: 1, 3: 4, 4: 3})

        G.add_weighted_edges_from([ (1, 6, 5), (4, 5, 6) ])
        assert_equal(nx.max_weight_matching(G),
                     {1: 6, 2: 3, 3: 2, 4: 5, 5: 4, 6: 1}) 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:13,代码来源:test_matching.py

示例14: test_s_t_blossom

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import max_weight_matching [as 别名]
def test_s_t_blossom(self):
        """Create S-blossom, relabel as T-blossom, use for augmentation:"""
        G = nx.Graph()
        G.add_weighted_edges_from([ (1, 2, 9), (1, 3, 8), (2, 3, 10), 
                                    (1, 4, 5), (4, 5, 4), (1, 6, 3) ])
        assert_equal(nx.max_weight_matching(G),
                     {1: 6, 2: 3, 3: 2, 4: 5, 5: 4, 6: 1})
        G.add_edge(4, 5, weight=3)
        G.add_edge(1, 6, weight=4)
        assert_equal(nx.max_weight_matching(G),
                     {1: 6, 2: 3, 3: 2, 4: 5, 5: 4, 6: 1})
        G.remove_edge(1, 6)
        G.add_edge(3, 6, weight=4)
        assert_equal(nx.max_weight_matching(G),
                     {1: 2, 2: 1, 3: 6, 4: 5, 5: 4, 6: 3}) 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:17,代码来源:test_matching.py

示例15: test_nested_s_blossom

# 需要导入模块: import networkx [as 别名]
# 或者: from networkx import max_weight_matching [as 别名]
def test_nested_s_blossom(self):
        """Create nested S-blossom, use for augmentation:"""

        G = nx.Graph()
        G.add_weighted_edges_from([ (1, 2, 9), (1, 3, 9), (2, 3, 10), 
                                    (2, 4, 8), (3, 5, 8), (4, 5, 10), 
                                    (5, 6, 6) ])
        assert_equal(nx.max_weight_matching(G),
                     {1: 3, 2: 4, 3: 1, 4: 2, 5: 6, 6: 5}) 
开发者ID:SpaceGroupUCL,项目名称:qgisSpaceSyntaxToolkit,代码行数:11,代码来源:test_matching.py


注:本文中的networkx.max_weight_matching方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。