Exemplo n.º 1
0
def simpleStar(subset, center, direction=ut.Direction.OUTGOING):
    """ create a star graph on the input nodes subset
    with node i as the center
    """

    n = len(subset)

    if n == 0:
        raise NameError('There needs to be at least one node')

    res = np.where(subset == center)
    if len(res[0]) == 0:
        raise NameError('node ' + str(center) + ' is not in the subset')
    centerIndex = res[0][0]


    D = dict(zip(np.array([i for i in range(n)]), subset))
    D[0], D[centerIndex] = center, subset[0]

    G = nx.star_graph(n-1)
    G = nx.to_directed(G)
    G = nx.DiGraph(G)

    if direction == ut.Direction.OUTGOING:
        G.remove_edges_from([(i, 0) for i in range(n)])
    elif direction == ut.Direction.INCOMING:
        G.remove_edges_from([(0, i) for i in range(n)])
    else:
        raise NameError(str(direction) + ' is not a valid value for direction')

    G = nx.relabel_nodes(G, D)
    ut.normalizeGraph(G)
    return G
Exemplo n.º 2
0
def randomGraph2(inDegrees, outDegrees):
    G = nx.directed_configuration_model(inDegrees, outDegrees)
    total = 0
    for (u, v, w) in G.edges(data=True):
        w['weight'] = rd.random()
    ut.normalizeGraph(G)

    return G
Exemplo n.º 3
0
def randomGraph(n, m):
    G = nx.gnm_random_graph(n, m, directed=True)
    total = 0
    for (u, v, w) in G.edges(data=True):
        w['weight'] = rd.random()
    ut.normalizeGraph(G)

    return G
Exemplo n.º 4
0
def simpleCycleGraph(subset):
    n = len(subset)

    D = dict(zip(np.array([i for i in range(n)]), subset))

    G = nx.cycle_graph(n)
    G = nx.to_directed(G)

    G = nx.relabel_nodes(G, D)
    ut.normalizeGraph(G)
    return G
Exemplo n.º 5
0
def triangular(n):
    """ graph construction : node i is connected to (0, ..., n-1) """

    # add edges with arbitratry weight
    M = np.zeros((n, n))
    for i in range(n):
        for j in range(i):
            M[i, j] = 1

    G = nx.from_numpy_matrix(M, create_using=nx.DiGraph)
    ut.normalizeGraph(G)
    return G
Exemplo n.º 6
0
def superStar(k, m, highSubsets, direction=ut.Direction.OUTGOING):

    graphs = []
    graphs.append(severalStarGraphs(m, k, highSubsets))

    last = k*(m+1)
    nodes = np.array([j*(m+1) for j in range(k+1)])
    graphs.append(simpleStar(nodes, last, direction=direction))

    G = nx.compose_all(graphs)
    ut.normalizeGraph(G)

    return G
Exemplo n.º 7
0
def randomGraph(n):
    # p the probability of an edge between two nodes
    # is chosen in order to make the number of edges
    # proportionnal to n
    p = 2. / (n - 1)

    G = nx.fast_gnp_random_graph(n, p, directed=True)
    G.remove_nodes_from(list(nx.isolates(G)))

    _n = nx.number_of_nodes(G)
    nodes = np.array(G.nodes)
    D = dict(zip(nodes, np.array([i for i in range(_n)])))
    G = nx.relabel_nodes(G, D)
    ut.normalizeGraph(G)

    return G
Exemplo n.º 8
0
def highlands(n, m):
    """ graph construction : node is connected to (i+1 mod n, ..., i+h mod n) if i mod m = 0"""

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

    # add edges with arbitratry weight
    for i in range(n):
        if i % m == 0:
            for k in range(1, m+1):
                j = (i + k) % n
                M[i, j] = 1

    G = nx.from_numpy_matrix(M, create_using=nx.DiGraph)
    ut.normalizeGraph(G)

    return G
Exemplo n.º 9
0
def severalWheels(subset, highOutSubsets, highInSubsets):
    """ combination of simples wheels """

    graphs = []

    for highOutNode in highOutSubsets:
        graphs.append(
            simpleWheel(subset, highOutNode, direction=ut.Direction.OUTGOING))

    for highInNode in highInSubsets:
        graphs.append(
            simpleWheel(subset, highInNode, direction=ut.Direction.INCOMING))

    G = nx.compose_all(graphs)
    ut.normalizeGraph(G)
    return G
Exemplo n.º 10
0
def severalStarGraphs(m, k, highSubsets):
    graphs = []
    centers = []

    for i in range(k):
        center = i*(m+1)
        centers.append(center)
        nodes = np.array([j+center for j in range(m+1)])

        if (highSubsets[i] == 1):
            graphs.append(simpleStar(nodes, center, direction=ut.Direction.OUTGOING))
        else:
            graphs.append(simpleStar(nodes, center, direction=ut.Direction.INCOMING))
    
    graphs.append(simpleCycleGraph(centers))

    G = nx.compose_all(graphs)
    ut.normalizeGraph(G)

    return G
Exemplo n.º 11
0
def superStars(k, m, highSubsets, kernelsSubsets):
    graphs = []
    graphs.append(severalStarGraphs(m, k, highSubsets))

    s = len(kernelsSubsets)
    kernels = []
    for i in range(s):
        kernel = (k+i)*(m+1)
        kernels.append(kernel)
        nodes = np.array([j*(m+1) for j in range(k)]+[kernel])

        if (kernelsSubsets[i] == 1):
             graphs.append(simpleStar(nodes, kernel, direction=ut.Direction.OUTGOING))
        else:
            graphs.append(simpleStar(nodes, kernel, direction=ut.Direction.INCOMING))

    if s > 1:
        graphs.append(simpleCycleGraph(kernels))

    G = nx.compose_all(graphs)
    ut.normalizeGraph(G)

    return G
Exemplo n.º 12
0
    def test_simple(self):
        M = np.array([
            [0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0.25, 0, 0, 0, 0, 0, 0, 0, 0],
            [0.1, 0, 0, 0, 0, 0, 0, 0, 0],
            [0.05, 0, 0, 0, 0, 0, 0, 0, 0],
            [0.1, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0.2, 0, 0.05, 0.1, 0.15],
            [0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0]
        ])

        expected = np.array([
            [0, 0, 0, 0, 1, 0, 0, 0, 0],
            [0, 0, 0, 0, 1, 0, 0, 0, 0],
            [0, 0, 0, 1, 1, 0, 0, 0, 0],
            [0, 0, 1, 0, 0, 0, 0, 0, 0],
            [1, 1, 1, 0, 0, 1, 0, 0, 0],
            [0, 0, 0, 0, 1, 0, 0, 1, 0],
            [0, 0, 0, 0, 0, 0, 0, 1, 0],
            [0, 0, 0, 0, 0, 1, 1, 0, 1],
            [0, 0, 0, 0, 0, 0, 0, 1, 0],
        ])
        
        G = nx.from_numpy_matrix(M, create_using=nx.DiGraph)
        ut.normalizeGraph(G)

        expectedG = nx.from_numpy_matrix(expected)
        ut.normalizeGraph(G)

        resG = tr.treeToBND(M)
        N = nx.to_numpy_matrix(resG)
        np.testing.assert_array_equal(expected, N)

        dr.printInput(G)
        dr.printRes(G, resG, expectedG)
Exemplo n.º 13
0
    def test_simple(self):
        M = np.array([
            [0, 0, 0, 0, 0, 0, 0, 0],
            [0.1, 0, 0, 0, 0, 0, 0, 0],
            [0.15, 0, 0, 0, 0, 0, 0, 0],
            [0.05, 0, 0, 0, 0, 0, 0, 0],
            [0.1, 0, 0, 0, 0, 0.05, 0.35, 0.2],
            [0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0]
        ])

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

        G = nx.from_numpy_matrix(M, create_using=nx.DiGraph)
        ut.normalizeGraph(G)

        expectedG = nx.from_numpy_matrix(expected)
        ut.normalizeGraph(G)

        res = sp.sparseToBND(G)
        resG, layers = res[0], res[1]
        N = nx.to_numpy_matrix(resG)

        dr.printInput(G)
        dr.printRes(G, resG, expectedG, layers)
        np.testing.assert_array_equal(expected, N)
Exemplo n.º 14
0
def randomGraph(n, m):
  G = nx.gnm_random_graph(n, m, directed=True)
  print(G)
  ut.normalizeGraph(G)
  return G
Exemplo n.º 15
0
import networkx as nx

from src import sparseToBND as sp
from src import draw as dr
from src import utils as ut

n = 10
m = 2

G = nx.circulant_graph(n, [i for i in range(1, m + 1)],
                       create_using=nx.DiGraph)
ut.normalizeGraph(G)

dr.printInput(G)
res = sp.sparseToBND(G)
N = res[0]

dr.printRes(G, N, None)