Exemple #1
0
def binary_search_using_charikar(g, root, B, level,
                                 cost_key='c'):
    """
    works for the problem, budgeted k-minimum spanning tree,
    thus, node prize are uniform
    """
    paths = transitive_closure(g)[1][root]
    depth = max(len(p) for p in paths.values())
    print('depth:', depth)
    print('root:', root)
    g_cost = lambda t: sum(t[u][v][cost_key]
                           for u, v in t.edges_iter())
    Q_l = 1.  # feasible for sure
    print('B:', B)
    print('quota_ub:', quota_upperbound(g, root, B))
    Q_u = quota_upperbound(g, root, B)  # might be feasible

    lastest_feasible_t = None
    terminals = g.nodes()
    
    while Q_l < Q_u - 1:
        Q = int(math.floor((Q_l + Q_u) / 2.))
        print('Q_l, Q_u, Q:', Q_l, Q_u, Q)
        # print('g, root, Q, level:', g, root, Q, level)
        t = charikar_algo(g, root, terminals, Q, level)

        assert(len(terminals) == g.number_of_nodes())

        cost = g_cost(t)
        # print('cost, B:', cost, B)
        if cost > B:
            Q_u = Q - 1
        elif cost < B:
            if set(t.nodes()) == set(g.nodes()):
                # all nodes are included
                return t
            lastest_feasible_t = t
            Q_l = Q
        else:
            return t
    
    # print('terminals:', terminals)
    # print('g, root, Q_u, level:', g, root, Q_u, level)
    t_p = charikar_algo(g, root, terminals, Q_u, level)
    print('Q_u, cost(t_p):', Q_u, g_cost(t_p))
    if g_cost(t_p) < B:
        return t_p
    else:
        if lastest_feasible_t is None:
            return charikar_algo(g, root, terminals, Q_l, level)
        else:
            return lastest_feasible_t
Exemple #2
0
 def test_quota_upperbound_2(self):
     assert_equal(
         4,
         quota_upperbound(self.g, 0, B=100)
     )
Exemple #3
0
 def test_quota_upperbound_3(self):
     assert_equal(
         1,
         quota_upperbound(self.g, 0, B=0)
     )
Exemple #4
0
 def test_quota_upperbound_1(self):
     assert_equal(
         3,
         quota_upperbound(self.g, 0, B=2)
     )