Exemplo n.º 1
0
def test_caveman_graph():
    G = nx.caveman_graph(4,3)
    assert_equal(len(G),12)

    G = nx.caveman_graph(1,5)
    K5 = nx.complete_graph(5)
    assert_true(nx.is_isomorphic(G,K5))
Exemplo n.º 2
0
  def test_limited_infection_caveman_graph(self):
    # A caveman graph has a bunch of cliques of the same size.
    # We can only approximate the limit by the size of the cliques.
    graph = nx.caveman_graph(10, 3)  # 10 cliques of size 3

    # 9 is multiple of 3, we can do match this limit precisely.
    infected = infection.limited_infection(graph, 9)
    self.assertEqual(9, len(infected))

    # 10 is not a multiple of 3. We'll approximate with the next multiple of 3.
    infected = infection.limited_infection(graph, 10)
    self.assertEqual(12, len(infected))
Exemplo n.º 3
0
def relaxed_caveman_graph(l, k, p, seed=None, directed=False):
    """Return a relaxed caveman graph.

    A relaxed caveman graph starts with l cliques of size k.  Edges
    are then randomly rewired with probability p to link different
    cliques.

    Parameters
    ----------
    l : int
      Number of groups
    k : int
      Size of cliques
    p : float
      Probabilty of rewiring each edge.
    seed : int,optional
      Seed for random number generator(default=None)
    directed : bool,optional (default=False)
      If True return a directed graph

    Returns
    -------
    G : NetworkX Graph
      Relaxed Caveman Graph

    Raises
    ------
    NetworkXError:
     If p is not in [0,1]

    Examples
    --------
    >>> G = nx.relaxed_caveman_graph(2, 3, 0.1, seed=42)

     References
    ----------
    .. [1] Santo Fortunato, Community Detection in Graphs,
       Physics Reports Volume 486, Issues 3-5, February 2010, Pages 75-174.
       http://arxiv.org/abs/0906.0612
    """
    if not seed is None:
        random.seed(seed)
    G = nx.caveman_graph(l, k)
    nodes = G.nodes()
    G.name = "relaxed_caveman_graph (%s,%s,%s)" % (l, k, p)
    for (u, v) in G.edges():
        if random.random() < p:  # rewire the edge
            x = random.choice(nodes)
            if G.has_edge(u, x):
                continue
            G.remove_edge(u, v)
            G.add_edge(u, x)
    return G
Exemplo n.º 4
0
def relaxed_caveman_graph(l, k, p, seed=None):
    """Returns a relaxed caveman graph.

    A relaxed caveman graph starts with `l` cliques of size `k`.  Edges are
    then randomly rewired with probability `p` to link different cliques.

    Parameters
    ----------
    l : int
      Number of groups
    k : int
      Size of cliques
    p : float
      Probabilty of rewiring each edge.
    seed : integer, random_state, or None (default)
        Indicator of random number generation state.
        See :ref:`Randomness<randomness>`.

    Returns
    -------
    G : NetworkX Graph
      Relaxed Caveman Graph

    Raises
    ------
    NetworkXError:
     If p is not in [0,1]

    Examples
    --------
    >>> G = nx.relaxed_caveman_graph(2, 3, 0.1, seed=42)

    References
    ----------
    .. [1] Santo Fortunato, Community Detection in Graphs,
       Physics Reports Volume 486, Issues 3-5, February 2010, Pages 75-174.
       https://arxiv.org/abs/0906.0612
    """
    G = nx.caveman_graph(l, k)
    nodes = list(G)
    for (u, v) in G.edges():
        if seed.random() < p:  # rewire the edge
            x = seed.choice(nodes)
            if G.has_edge(u, x):
                continue
            G.remove_edge(u, v)
            G.add_edge(u, x)
    return G
Exemplo n.º 5
0
def caveman_special(c=2,k=20,p_path=0.1,p_edge=0.3):
    p = p_path
    path_count = max(int(np.ceil(p * k)),1)
    G = nx.caveman_graph(c, k)
    # remove 50% edges
    p = 1-p_edge
    for (u, v) in list(G.edges()):
        if np.random.rand() < p and ((u < k and v < k) or (u >= k and v >= k)):
            G.remove_edge(u, v)
    # add path_count links
    for i in range(path_count):
        u = np.random.randint(0, k)
        v = np.random.randint(k, k * 2)
        G.add_edge(u, v)
    G = max(nx.connected_component_subgraphs(G), key=len)
    return G
Exemplo n.º 6
0
def connected_caveman_graph(l, k):
    """Returns a connected caveman graph of n cliques of size k.

    The connected caveman graph is formed by creating n cliques of size k. Then
    a single node in each clique is rewired to a node in the adjacent clique.

    Parameters
    ----------
    n : int
      number of cliques
    k : int
      size of cliques
    directed : boolean optional (default=True)
      if true return directed caveman graph

    Returns
    -------
    G : NetworkX Graph
      caveman graph

    Notes
    -----
    This returns an undirected graph, it can be converted to a directed
    graph using nx.to_directed(), or a multigraph using
    nx.MultiGraph(nx.caveman_graph). Only the undirected version is
    described in [1]_ and it is unclear which of the directed
    generalizations is most useful.

    Examples
    --------
    >>> G = nx.caveman_graph(3, 3)

    Reference
    ---------
    .. [1] Watts, D. J. 'Networks, Dynamics, and the Small-World Phenomenon.'
       Amer. J. Soc. 105, 493-527, 1999.
    """
    G = nx.caveman_graph(l, k)
    G.name = "connected_caveman_graph(%s,%s)" % (l, k)
    for start in range(0, l*k, k):
        G.remove_edge(start, start+1)
        G.add_edge(start, (start-1) % (l*k))
    return G
Exemplo n.º 7
0
def connected_caveman_graph(l, k):
    """Returns a connected caveman graph of `l` cliques of size `k`.

    The connected caveman graph is formed by creating `n` cliques of size
    `k`, then a single edge in each clique is rewired to a node in an
    adjacent clique.

    Parameters
    ----------
    l : int
      number of cliques
    k : int
      size of cliques

    Returns
    -------
    G : NetworkX Graph
      connected caveman graph

    Notes
    -----
    This returns an undirected graph, it can be converted to a directed
    graph using :func:`nx.to_directed`, or a multigraph using
    ``nx.MultiGraph(nx.caveman_graph(l, k))``. Only the undirected version is
    described in [1]_ and it is unclear which of the directed
    generalizations is most useful.

    Examples
    --------
    >>> G = nx.connected_caveman_graph(3, 3)

    References
    ----------
    .. [1] Watts, D. J. 'Networks, Dynamics, and the Small-World Phenomenon.'
       Amer. J. Soc. 105, 493-527, 1999.
    """
    G = nx.caveman_graph(l, k)
    for start in range(0, l * k, k):
        G.remove_edge(start, start + 1)
        G.add_edge(start, (start - 1) % (l * k))
    return G
Exemplo n.º 8
0
# Social Graph
path_output_data = path_input + 'social/'
makedir(path_output_data)

# Karate club graph
path_output_data_type = path_output_data + 'karate_club/'
makedir(path_output_data_type)
G = nx.karate_club_graph()
makedir(path_output_data_type + 'entity_' + str(0))
save_graph(path_output_data_type + 'entity_' + str(0) + '/' + 'J_ij.csv', G)

# Caveman Graph
path_output_data_type = path_output_data + 'caveman_graph/'
makedir(path_output_data_type)
G = nx.caveman_graph(int(default_size / 10), 10)
makedir(path_output_data_type + 'entity_' + str(0))
save_graph(path_output_data_type + 'entity_' + str(0) + '/' + 'J_ij.csv', G)

path_output_data = path_input + 'biological/'
makedir(path_output_data)

#Enzimes structure
#http://networkrepository.com/ENZYMES-g16.php
path = '/home/brainlab/Downloads/ENZYMES_g16 (1)/ENZYMES_g16.edgelist'
path_output_data_type = path_output_data + 'enzimes_g16/'
makedir(path_output_data_type)
G = nx.read_edgelist(path,
                     nodetype=int,
                     create_using=nx.DiGraph,
                     data=(('weight', float), ))
Exemplo n.º 9
0

if __name__ == "__main__":
    # random graph, relaxed caveman 100 clusters with 50 each
    # Massachusetts nursing homes, 500 nursing homes, nursing home has 100 residents,
    #                               30 employees each, transmission rate within nursing home,
    #                               some rate outside
    #                               500 tests a day, 10 of the nursing homes actually have COVID
    #                               30-40% in the nursing homes that have them
    #                               start with 1, 10, 50 groups
    for simulation_num in range(2, 11):
        print("Creating the nursing_home_graph... ")
        l = 500
        k = 100
        N = l * k + 15000
        G = nx.caveman_graph(l, k)
        nursing_homes = [set(i) for i in (nx.algorithms.find_cliques(G))]
        worker_id = 50000
        # frequencies = {}
        print("Finding workers jobs... ")
        for worker in range(50000, 50000 + 12500):
            home = random.sample(nursing_homes, 1)[0]
            G.add_node(worker)
            # if worker in frequencies:
            #
            #     frequencies[worker] += 1
            # else:
            #     frequencies[worker] = 1
            G.add_edge(worker, next(iter(home)))
            home.add(worker)
Exemplo n.º 10
0
            elif (InitMatrix[i][j] == 1) and (random.random() <= beta):
                break

    return AdjMatrix


a = [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1]
print

m = 30  ## dimensionality
n = 280  ## organization size
## [Code to Individual] socialization coefficient
## [Individual to Code] learning coefficient
## [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9] [0.1, 0.5, 0.9]

G = nx.caveman_graph(n, 7)
InitMatrix = [[0] * n for i in range(n)]
for i in range(0, n):
    Adjs = nx.all_neighbors(G, i)
    for ele in Adjs:
        InitMatrix[i][ele] = 1

f = open('./Fang16.txt', 'a')
"""

for beta in numpy.arange(0,1,0.05):
	for learning in [0.3]:
		Equils = []
		for instance in range(0, 100):
			AdjMatrix = copy.deepcopy(InitMatrix)