Пример #1
0
def test_one_exchange_basic():
    G = nx.complete_graph(5)
    random.seed(5)
    for (u, v, w) in G.edges(data=True):
        w["weight"] = random.randrange(-100, 100, 1) / 10

    initial_cut = set(random.sample(G.nodes(), k=5))
    cut_size, (set1, set2) = maxcut.one_exchange(G,
                                                 initial_cut,
                                                 weight="weight",
                                                 seed=5)

    _is_valid_cut(G, set1, set2)
    _cut_is_locally_optimal(G, cut_size, set1)
Пример #2
0
def test_one_exchange_optimal():
    # Greedy one exchange should find the optimal solution for this graph (14)
    G = nx.Graph()
    G.add_edge(1, 2, weight=3)
    G.add_edge(1, 3, weight=3)
    G.add_edge(1, 4, weight=3)
    G.add_edge(1, 5, weight=3)
    G.add_edge(2, 3, weight=5)

    cut_size, (set1, set2) = maxcut.one_exchange(G, weight="weight", seed=5)

    _is_valid_cut(G, set1, set2)
    _cut_is_locally_optimal(G, cut_size, set1)
    # check global optimality
    assert cut_size == 14
Пример #3
0
def test_negative_weights():
    G = nx.complete_graph(5)
    random.seed(5)
    for (u, v, w) in G.edges(data=True):
        w["weight"] = -1 * random.random()

    initial_cut = set(random.sample(G.nodes(), k=5))
    cut_size, (set1, set2) = maxcut.one_exchange(G,
                                                 initial_cut,
                                                 weight="weight")

    # make sure it is a valid cut
    _is_valid_cut(G, set1, set2)
    # check local optimality
    _cut_is_locally_optimal(G, cut_size, set1)
    # test that all nodes are in the same partition
    assert len(set1) == len(G.nodes) or len(set2) == len(G.nodes)