Пример #1
0
def addBinaryTrees(subGraphs, resMarices):

    finalSubGraphs = []

    for i in range(len(subGraphs)):
        subGraph = subGraphs[i]
        M = nx.to_numpy_matrix(subGraph)
        _M = np.copy(M)

        outChildren = ut.arrayToDictArray(_M[i, :])
        rows = np.where(outChildren[:, 1] > 0)
        outChildren = outChildren[rows]

        inChildren = ut.arrayToDictArray(_M[:, i])
        rows = np.where(inChildren[:, 1] > 0)
        inChildren = inChildren[rows]

        ml.melhornTree(outChildren,
                       i,
                       resMarices[i],
                       direction=ut.Direction.OUTGOING)
        ml.melhornTree(inChildren,
                       i,
                       resMarices[i],
                       direction=ut.Direction.INCOMING)

        finalSubGraph = nx.from_numpy_matrix(resMarices[i])
        finalSubGraphs.append(finalSubGraph)

    return finalSubGraphs
Пример #2
0
def buildTrees(M, reverseM, root, parent, visited, N, direction):

    if visited[root] == 1:
        return

    visited[root] = 1
    n = len(M)

    children = np.zeros(n)
    if direction == ut.Direction.OUTGOING:
        for i in range(n):
            if i != parent:
                children[i] = M[root, i]
            else:
                children[i] = 0
    elif direction == ut.Direction.INCOMING:
        for i in range(n):
            if i != parent:
                children[i] = M[i, root]
            else:
                children[i] = 0

    # In order to keep in mind the indexes
    # we change the array into an array of two figures [[index, value], ...]
    children = ut.arrayToDictArray(children)
    rows = children[:, 1]
    children = children[np.where(rows > 0)]
    ml.melhornTree(children, root, N, direction)

    for i in range(n):
        if (i != parent):
            if M[root, i] > 0:
                buildTrees(M, reverseM, i, root, visited, N, direction)
            if M[i, root] > 0:
                buildTrees(M, reverseM, i, root, visited, N, direction)
Пример #3
0
def addBinarytrees(_M, N, highOutDegrees, highInDegrees):
    n, _ = N.shape
    layers = []  # used to save the sub-graphs generated
    # by the melhorn algorithm
    # Binary trees from highOutDegrees and highInDegrees
    trees = []
    for i in highOutDegrees:
        # We remove from N the out-neigbourhood of i in M
        # We do not want to remove edges we have already created
        aux = np.ceil(_M[i[0], :])
        N[i[0], :] -= aux

        layer = np.zeros((n, n))

        # In order to keep in mind the indexes
        # we change the array into an array of two figures [[index, value], ...]
        children = ut.arrayToDictArray(_M[i[0], :])
        rows = np.where(children[:, 1] > 0)
        children = children[rows]
        trees.append([i[0], children])

        ml.melhornTree(children, i[0], N, ut.Direction.OUTGOING, layer)
        layers.append(layer)

    for i in highInDegrees:
        # We remove from N the in-neigbourhood of i in M
        aux = np.ceil(_M[:, i[0]])
        N[:, i[0]] -= aux

        layer = np.zeros((n, n))

        # In order to keep in mind the indexes
        # we change the array into an array of two figures [[index, value], ...]
        children = ut.arrayToDictArray(_M[:, i[0]])
        rows = np.where(children[:, 1] > 0)
        children = children[rows]
        trees.append([i[0], children])

        ml.melhornTree(children, i[0], N, ut.Direction.INCOMING, layer)
        layers.append(layer)

    return [layers, trees]
Пример #4
0
    def test_simple3(self):
        N = np.zeros((4, 4))
        children = np.array([1, 2, 0, 3])

        children = ut.arrayToDictArray(children)

        expected = np.array([
            [0, 0, 0, 0],
            [1, 0, 0, 1],
            [0, 1, 0, 0],
            [0, 0, 0, 0]
        ])

        ml.melhornTree(children, 2, N, direction=ut.Direction.OUTGOING)
        np.testing.assert_array_equal(expected, N)
Пример #5
0
    def test_random(self):
        tries = 10
        minLength = 0
        maxLength = 1000
        maxWeight = 100
        for _ in range(tries):
            n = rd.randint(minLength, maxLength)
            N = np.zeros((n, n))
            children = np.random.random_integers(1, maxWeight, size=n)
            total = np.sum(children)
            children = np.true_divide(children, total)

            root = rd.randint(0, n-1)
            children[root] = 0

            childrenDict = ut.arrayToDictArray(children)

            ml.melhornTree(childrenDict, root, N, direction=ut.Direction.OUTGOING)
            length = ut.getWeigtedPathLength(N, children, root, 0)

            H = ut.getEntropy(children)
            bound = 2 +1.44*H # melhorn bound

            self.assertTrue(length < bound)