Ejemplo n.º 1
0
    def setup_class(cls):

        G = nx.Graph()
        G.add_edge(0, 1, weight=3)
        G.add_edge(0, 2, weight=2)
        G.add_edge(0, 3, weight=6)
        G.add_edge(0, 4, weight=4)
        G.add_edge(1, 3, weight=5)
        G.add_edge(1, 5, weight=5)
        G.add_edge(2, 4, weight=1)
        G.add_edge(3, 4, weight=2)
        G.add_edge(3, 5, weight=1)
        G.add_edge(4, 5, weight=4)
        cls.G = G
        cls.exact_weighted = {0: 4.0, 1: 0.0, 2: 8.0, 3: 6.0, 4: 8.0, 5: 0.0}
        cls.K = nx.krackhardt_kite_graph()
        cls.P3 = nx.path_graph(3)
        cls.P4 = nx.path_graph(4)
        cls.K5 = nx.complete_graph(5)

        cls.C4 = nx.cycle_graph(4)
        cls.T = nx.balanced_tree(r=2, h=2)
        cls.Gb = nx.Graph()
        cls.Gb.add_edges_from([(0, 1), (0, 2), (1, 3), (2, 3), (2, 4), (4, 5),
                               (3, 5)])
        cls.F = nx.florentine_families_graph()
        cls.LM = nx.les_miserables_graph()
        cls.D = nx.cycle_graph(3, create_using=nx.DiGraph())
        cls.D.add_edges_from([(3, 0), (4, 3)])
Ejemplo n.º 2
0
    def setUp(self):

        G = nx.Graph()
        G.add_edge(0, 1, weight=3)
        G.add_edge(0, 2, weight=2)
        G.add_edge(0, 3, weight=6)
        G.add_edge(0, 4, weight=4)
        G.add_edge(1, 3, weight=5)
        G.add_edge(1, 5, weight=5)
        G.add_edge(2, 4, weight=1)
        G.add_edge(3, 4, weight=2)
        G.add_edge(3, 5, weight=1)
        G.add_edge(4, 5, weight=4)
        self.G = G
        self.exact_weighted = {0: 4.0, 1: 0.0, 2: 8.0, 3: 6.0, 4: 8.0, 5: 0.0}
        self.K = nx.krackhardt_kite_graph()
        self.P3 = nx.path_graph(3)
        self.P4 = nx.path_graph(4)
        self.K5 = nx.complete_graph(5)

        self.C4 = nx.cycle_graph(4)
        self.T = nx.balanced_tree(r=2, h=2)
        self.Gb = nx.Graph()
        self.Gb.add_edges_from([(0, 1), (0, 2), (1, 3), (2, 3), (2, 4), (4, 5),
                                (3, 5)])
        self.F = nx.florentine_families_graph()
        self.D = nx.cycle_graph(3, create_using=nx.DiGraph())
        self.D.add_edges_from([(3, 0), (4, 3)])
Ejemplo n.º 3
0
    def setUp(self):

        G=nx.Graph();
        G.add_edge(0,1,weight=3)
        G.add_edge(0,2,weight=2)
        G.add_edge(0,3,weight=6)
        G.add_edge(0,4,weight=4)
        G.add_edge(1,3,weight=5)
        G.add_edge(1,5,weight=5)
        G.add_edge(2,4,weight=1)
        G.add_edge(3,4,weight=2)
        G.add_edge(3,5,weight=1)
        G.add_edge(4,5,weight=4)
        self.G=G
        self.exact_weighted={0: 4.0, 1: 0.0, 2: 8.0, 3: 6.0, 4: 8.0, 5: 0.0}

        self.K = nx.krackhardt_kite_graph()
        self.P3 = nx.path_graph(3)
        self.P4 = nx.path_graph(4)
        self.K5 = nx.complete_graph(5)

        self.C4=nx.cycle_graph(4)
        self.T=nx.balanced_tree(r=2, h=2)
        self.Gb = nx.Graph()
        self.Gb.add_edges_from([(0,1), (0,2), (1,3), (2,3), 
                                (2,4), (4,5), (3,5)])


        F = nx.florentine_families_graph()
        self.F = F
Ejemplo n.º 4
0
def generate_krackhardt(params={}):

    G = nx.Graph(nx.krackhardt_kite_graph())
    for i, j in G.edges:
        G[i][j]['weight'] = 1.

    return G, None
Ejemplo n.º 5
0
    def test_krackhardt_kite_graph_normalized(self):
        """Betweenness centrality: Krackhardt kite graph normalized"""
        G = nx.krackhardt_kite_graph()
        b_answer = {0: 0.023, 1: 0.023, 2: 0.000, 3: 0.102, 4: 0.000, 5: 0.231, 6: 0.231, 7: 0.389, 8: 0.222, 9: 0.000}
        b = nx.betweenness_centrality(G, weight=None, normalized=True)

        for n in sorted(G):
            assert_almost_equal(b[n], b_answer[n], places=3)
Ejemplo n.º 6
0
def modified_kite():
    G1 = nx.krackhardt_kite_graph()
    G1.remove_edge(1, 4)
    G1.remove_edge(0, 2)
    G1.remove_edge(1, 6)
    G1.remove_edge(4, 6)
    G1.remove_edge(1, 0)
    G1.remove_edge(2, 5)
    return G1
Ejemplo n.º 7
0
 def test_krackhardt_kite_graph_normalized(self):
     """Betweenness centrality: Krackhardt kite graph normalized"""
     G = nx.krackhardt_kite_graph()
     b_answer = {0: 0.023, 1: 0.023, 2: 0.000, 3: 0.102, 4: 0.000,
                 5: 0.231, 6: 0.231, 7: 0.389, 8: 0.222, 9: 0.000}
     b = nx.betweenness_centrality(G,
                                   weight=None,
                                   normalized=True)
     for n in sorted(G):
         assert_almost_equal(b[n], b_answer[n], places=3)
    def test_krackhardt_kite_graph(self):
        """Betweenness centrality: Krackhardt kite graph"""
        G=networkx.krackhardt_kite_graph()
        b_answer={0: 1.667,1: 1.667,2: 0.000,3: 7.333,4: 0.000,
                  5: 16.667,6: 16.667,7: 28.000,8: 16.000,9: 0.000}
        for b in b_answer:
            b_answer[b]/=2.0
        b=networkx.betweenness_centrality(G, weight=False, normalized=False)

        for n in sorted(G):
            assert_almost_equal(b[n],b_answer[n],places=3)
Ejemplo n.º 9
0
 def test_krackhardt_kite_graph(self):
     """Betweenness centrality: Krackhardt kite graph"""
     G = nx.krackhardt_kite_graph()
     b_answer = {0: 1.667, 1: 1.667, 2: 0.000, 3: 7.333, 4: 0.000,
                 5: 16.667, 6: 16.667, 7: 28.000, 8: 16.000, 9: 0.000}
     for b in b_answer:
         b_answer[b] /= 2
     b = nx.betweenness_centrality(G,
                                   weight=None,
                                   normalized=False)
     for n in sorted(G):
         assert_almost_equal(b[n], b_answer[n], places=3)
Ejemplo n.º 10
0
    def setUp(self):

        G=nx.Graph();
        G.add_edge(0,1,weight=3)
        G.add_edge(0,2,weight=2)
        G.add_edge(0,3,weight=6)
        G.add_edge(0,4,weight=4)
        G.add_edge(1,3,weight=5)
        G.add_edge(1,5,weight=5)
        G.add_edge(2,4,weight=1)
        G.add_edge(3,4,weight=2)
        G.add_edge(3,5,weight=1)
        G.add_edge(4,5,weight=4)
        self.G=G
        self.exact_weighted={0: 4.0, 1: 0.0, 2: 8.0, 3: 6.0, 4: 8.0, 5: 0.0}

        self.K = nx.krackhardt_kite_graph()
        self.P3 = nx.path_graph(3)
        self.P4 = nx.path_graph(4)
        self.K5 = nx.complete_graph(5)

        self.C4=nx.cycle_graph(4)
        self.T=nx.balanced_tree(r=2, h=2)
        self.Gb = nx.Graph()
        self.Gb.add_edges_from([(0,1), (0,2), (1,3), (2,3), 
                                (2,4), (4,5), (3,5)])


        F = nx.Graph() # Florentine families
        F.add_edge('Acciaiuoli','Medici')
        F.add_edge('Castellani','Peruzzi')
        F.add_edge('Castellani','Strozzi')
        F.add_edge('Castellani','Barbadori')
        F.add_edge('Medici','Barbadori')
        F.add_edge('Medici','Ridolfi')
        F.add_edge('Medici','Tornabuoni')
        F.add_edge('Medici','Albizzi')
        F.add_edge('Medici','Salviati')
        F.add_edge('Salviati','Pazzi')
        F.add_edge('Peruzzi','Strozzi')
        F.add_edge('Peruzzi','Bischeri')
        F.add_edge('Strozzi','Ridolfi')
        F.add_edge('Strozzi','Bischeri')
        F.add_edge('Ridolfi','Tornabuoni')
        F.add_edge('Tornabuoni','Guadagni')
        F.add_edge('Albizzi','Ginori')
        F.add_edge('Albizzi','Guadagni')
        F.add_edge('Bischeri','Guadagni')
        F.add_edge('Guadagni','Lamberteschi')    
        self.F = F
    def setUp(self):

        self.K = nx.krackhardt_kite_graph()
        self.P3 = nx.path_graph(3)
        self.P4 = nx.path_graph(4)
        self.K5 = nx.complete_graph(5)

        self.C4 = nx.cycle_graph(4)
        self.T = nx.balanced_tree(r=2, h=2)
        self.Gb = nx.Graph()
        self.Gb.add_edges_from([(0, 1), (0, 2), (1, 3), (2, 3), (2, 4), (4, 5), (3, 5)])

        F = nx.florentine_families_graph()
        self.F = F
Ejemplo n.º 12
0
    def setUp(self):
        self.K = nx.krackhardt_kite_graph()
        self.P3 = nx.path_graph(3)
        self.P4 = nx.path_graph(4)
        self.K5 = nx.complete_graph(5)

        self.C4 = nx.cycle_graph(4)
        self.T = nx.balanced_tree(r=2, h=2)
        self.Gb = nx.Graph()
        self.Gb.add_edges_from([(0, 1), (0, 2), (1, 3), (2, 3), (2, 4), (4, 5),
                                (3, 5)])

        F = nx.florentine_families_graph()
        self.F = F
Ejemplo n.º 13
0
    def setUp(self):

        G = nx.Graph()
        G.add_edge(0, 1, weight=3)
        G.add_edge(0, 2, weight=2)
        G.add_edge(0, 3, weight=6)
        G.add_edge(0, 4, weight=4)
        G.add_edge(1, 3, weight=5)
        G.add_edge(1, 5, weight=5)
        G.add_edge(2, 4, weight=1)
        G.add_edge(3, 4, weight=2)
        G.add_edge(3, 5, weight=1)
        G.add_edge(4, 5, weight=4)
        self.G = G
        self.exact_weighted = {0: 4.0, 1: 0.0, 2: 8.0, 3: 6.0, 4: 8.0, 5: 0.0}

        self.K = nx.krackhardt_kite_graph()
        self.P3 = nx.path_graph(3)
        self.P4 = nx.path_graph(4)
        self.K5 = nx.complete_graph(5)

        self.C4 = nx.cycle_graph(4)
        self.T = nx.balanced_tree(r=2, h=2)
        self.Gb = nx.Graph()
        self.Gb.add_edges_from([(0, 1), (0, 2), (1, 3), (2, 3), (2, 4), (4, 5),
                                (3, 5)])

        F = nx.Graph()  # Florentine families
        F.add_edge('Acciaiuoli', 'Medici')
        F.add_edge('Castellani', 'Peruzzi')
        F.add_edge('Castellani', 'Strozzi')
        F.add_edge('Castellani', 'Barbadori')
        F.add_edge('Medici', 'Barbadori')
        F.add_edge('Medici', 'Ridolfi')
        F.add_edge('Medici', 'Tornabuoni')
        F.add_edge('Medici', 'Albizzi')
        F.add_edge('Medici', 'Salviati')
        F.add_edge('Salviati', 'Pazzi')
        F.add_edge('Peruzzi', 'Strozzi')
        F.add_edge('Peruzzi', 'Bischeri')
        F.add_edge('Strozzi', 'Ridolfi')
        F.add_edge('Strozzi', 'Bischeri')
        F.add_edge('Ridolfi', 'Tornabuoni')
        F.add_edge('Tornabuoni', 'Guadagni')
        F.add_edge('Albizzi', 'Ginori')
        F.add_edge('Albizzi', 'Guadagni')
        F.add_edge('Bischeri', 'Guadagni')
        F.add_edge('Guadagni', 'Lamberteschi')
        self.F = F
Ejemplo n.º 14
0
def solve_2_1():
    # Importing the graph (connected, unweighted, undirected social network)
    krackhardt_kite = nx.krackhardt_kite_graph()

    # TODO: Calculate and print the Kite's degree centrality
    kk_degree_centrality = [
        node_degree_tup[1] for node_degree_tup in krackhardt_kite.degree
    ]
    print('Degree centrality:')
    print('\t\t\t'.join(
        [f'node{i}: {dc}' for i, dc in enumerate(kk_degree_centrality)]))

    kk_closeness_centrality = nx.closeness_centrality(krackhardt_kite,
                                                      normalized=False)
    print('Closeness centrality:')
    print('\t\t\t'.join(
        [f'node{i}: {cc}' for i, cc in enumerate(kk_closeness_centrality)]))
Ejemplo n.º 15
0
 def test_krackhardt_kite_graph_normalized(self):
     """Betweenness centrality: Krackhardt kite graph normalized"""
     G = nx.krackhardt_kite_graph()
     b_answer = {
         0: 0.023,
         1: 0.023,
         2: 0.000,
         3: 0.102,
         4: 0.000,
         5: 0.231,
         6: 0.231,
         7: 0.389,
         8: 0.222,
         9: 0.000,
     }
     b = nx.betweenness_centrality(G, weight=None, normalized=True)
     for n in sorted(G):
         assert b[n] == pytest.approx(b_answer[n], abs=1e-3)
    def setup_class(cls):
        cls.K = nx.krackhardt_kite_graph()
        cls.P3 = nx.path_graph(3)
        cls.P4 = nx.path_graph(4)
        cls.K5 = nx.complete_graph(5)

        cls.C4 = nx.cycle_graph(4)
        cls.T = nx.balanced_tree(r=2, h=2)
        cls.Gb = nx.Graph()
        cls.Gb.add_edges_from([(0, 1), (0, 2), (1, 3), (2, 3), (2, 4), (4, 5), (3, 5)])

        F = nx.florentine_families_graph()
        cls.F = F

        cls.LM = nx.les_miserables_graph()

        # Create random undirected, unweighted graph for testing incremental version
        cls.undirected_G = nx.fast_gnp_random_graph(n=100, p=0.6, seed=123)
        cls.undirected_G_cc = nx.closeness_centrality(cls.undirected_G)
Ejemplo n.º 17
0
 def test_krackhardt_kite_graph(self):
     """Betweenness centrality: Krackhardt kite graph"""
     G = nx.krackhardt_kite_graph()
     b_answer = {
         0: 1.667,
         1: 1.667,
         2: 0.000,
         3: 7.333,
         4: 0.000,
         5: 16.667,
         6: 16.667,
         7: 28.000,
         8: 16.000,
         9: 0.000,
     }
     for b in b_answer:
         b_answer[b] /= 2
     b = nx.betweenness_centrality(G, weight=None, normalized=False)
     for n in sorted(G):
         assert b[n] == pytest.approx(b_answer[n], abs=1e-3)
Ejemplo n.º 18
0
    def __init__(self):

        self.K = nx.krackhardt_kite_graph()
        self.P3 = nx.path_graph(3)
        self.K5 = nx.complete_graph(5)

        F = nx.Graph()  # Florentine families
        F.add_edge('Acciaiuoli', 'Medici')
        F.add_edge('Castellani', 'Peruzzi')
        F.add_edge('Castellani', 'Strozzi')
        F.add_edge('Castellani', 'Barbadori')
        F.add_edge('Medici', 'Barbadori')
        F.add_edge('Medici', 'Ridolfi')
        F.add_edge('Medici', 'Tornabuoni')
        F.add_edge('Medici', 'Albizzi')
        F.add_edge('Medici', 'Salviati')
        F.add_edge('Salviati', 'Pazzi')
        F.add_edge('Peruzzi', 'Strozzi')
        F.add_edge('Peruzzi', 'Bischeri')
        F.add_edge('Strozzi', 'Ridolfi')
        F.add_edge('Strozzi', 'Bischeri')
        F.add_edge('Ridolfi', 'Tornabuoni')
        F.add_edge('Tornabuoni', 'Guadagni')
        F.add_edge('Albizzi', 'Ginori')
        F.add_edge('Albizzi', 'Guadagni')
        F.add_edge('Bischeri', 'Guadagni')
        F.add_edge('Guadagni', 'Lamberteschi')
        self.F = F

        G = nx.DiGraph()
        G.add_edge(0, 5)
        G.add_edge(1, 5)
        G.add_edge(2, 5)
        G.add_edge(3, 5)
        G.add_edge(4, 5)
        G.add_edge(5, 6)
        G.add_edge(5, 7)
        G.add_edge(5, 8)
        self.G = G
    def __init__(self):

        self.K = nx.krackhardt_kite_graph()
        self.P3 = nx.path_graph(3)
        self.K5 = nx.complete_graph(5)

        F = nx.Graph() # Florentine families
        F.add_edge('Acciaiuoli','Medici')
        F.add_edge('Castellani','Peruzzi')
        F.add_edge('Castellani','Strozzi')
        F.add_edge('Castellani','Barbadori')
        F.add_edge('Medici','Barbadori')
        F.add_edge('Medici','Ridolfi')
        F.add_edge('Medici','Tornabuoni')
        F.add_edge('Medici','Albizzi')
        F.add_edge('Medici','Salviati')
        F.add_edge('Salviati','Pazzi')
        F.add_edge('Peruzzi','Strozzi')
        F.add_edge('Peruzzi','Bischeri')
        F.add_edge('Strozzi','Ridolfi')
        F.add_edge('Strozzi','Bischeri')
        F.add_edge('Ridolfi','Tornabuoni')
        F.add_edge('Tornabuoni','Guadagni')
        F.add_edge('Albizzi','Ginori')
        F.add_edge('Albizzi','Guadagni')
        F.add_edge('Bischeri','Guadagni')
        F.add_edge('Guadagni','Lamberteschi')    
        self.F = F

        G = nx.DiGraph()
        G.add_edge(0,5)
        G.add_edge(1,5)
        G.add_edge(2,5)
        G.add_edge(3,5)
        G.add_edge(4,5)
        G.add_edge(5,6)
        G.add_edge(5,7)
        G.add_edge(5,8)
        self.G = G
Ejemplo n.º 20
0
    def setup_method(self):

        self.K = nx.krackhardt_kite_graph()
        self.P3 = nx.path_graph(3)
        self.K5 = nx.complete_graph(5)

        F = nx.Graph()  # Florentine families
        F.add_edge("Acciaiuoli", "Medici")
        F.add_edge("Castellani", "Peruzzi")
        F.add_edge("Castellani", "Strozzi")
        F.add_edge("Castellani", "Barbadori")
        F.add_edge("Medici", "Barbadori")
        F.add_edge("Medici", "Ridolfi")
        F.add_edge("Medici", "Tornabuoni")
        F.add_edge("Medici", "Albizzi")
        F.add_edge("Medici", "Salviati")
        F.add_edge("Salviati", "Pazzi")
        F.add_edge("Peruzzi", "Strozzi")
        F.add_edge("Peruzzi", "Bischeri")
        F.add_edge("Strozzi", "Ridolfi")
        F.add_edge("Strozzi", "Bischeri")
        F.add_edge("Ridolfi", "Tornabuoni")
        F.add_edge("Tornabuoni", "Guadagni")
        F.add_edge("Albizzi", "Ginori")
        F.add_edge("Albizzi", "Guadagni")
        F.add_edge("Bischeri", "Guadagni")
        F.add_edge("Guadagni", "Lamberteschi")
        self.F = F

        G = nx.DiGraph()
        G.add_edge(0, 5)
        G.add_edge(1, 5)
        G.add_edge(2, 5)
        G.add_edge(3, 5)
        G.add_edge(4, 5)
        G.add_edge(5, 6)
        G.add_edge(5, 7)
        G.add_edge(5, 8)
        self.G = G
Ejemplo n.º 21
0
    def setUp(self):

        self.K = nx.krackhardt_kite_graph()
        self.P3 = nx.path_graph(3)
        self.P4 = nx.path_graph(4)
        self.K5 = nx.complete_graph(5)

        self.C4=nx.cycle_graph(4)
        self.T=nx.balanced_tree(r=2, h=2)
        self.Gb = nx.Graph()
        self.Gb.add_edges_from([(0,1), (0,2), (1,3), (2,3), 
                                (2,4), (4,5), (3,5)])


        F = nx.Graph() # Florentine families
        F.add_edge('Acciaiuoli','Medici')
        F.add_edge('Castellani','Peruzzi')
        F.add_edge('Castellani','Strozzi')
        F.add_edge('Castellani','Barbadori')
        F.add_edge('Medici','Barbadori')
        F.add_edge('Medici','Ridolfi')
        F.add_edge('Medici','Tornabuoni')
        F.add_edge('Medici','Albizzi')
        F.add_edge('Medici','Salviati')
        F.add_edge('Salviati','Pazzi')
        F.add_edge('Peruzzi','Strozzi')
        F.add_edge('Peruzzi','Bischeri')
        F.add_edge('Strozzi','Ridolfi')
        F.add_edge('Strozzi','Bischeri')
        F.add_edge('Ridolfi','Tornabuoni')
        F.add_edge('Tornabuoni','Guadagni')
        F.add_edge('Albizzi','Ginori')
        F.add_edge('Albizzi','Guadagni')
        F.add_edge('Bischeri','Guadagni')
        F.add_edge('Guadagni','Lamberteschi')    
        self.F = F
Ejemplo n.º 22
0
            color=colors[i % len(colors)],
            linestyle=linestyles[i / len(colors) % len(linestyles)],
            linewidth=linewidth,
            shifts=shifts,
        )
        for e in path:
            edge2shift[e] += single_shift

    return


##########################################
if __name__ == "__main__":

    # Example

    import networkx

    g = networkx.krackhardt_kite_graph()
    g.remove_node(9)
    path1 = networkx.shortest_path(g, 2, 8)
    path2 = networkx.shortest_path(g, 0, 8)
    path3 = [(1, 0), (0, 5), (5, 7)]
    path4 = [3, 5, 7, 6]
    pos = networkx.drawing.spring_layout(g)
    normalize_layout(pos)
    networkx.draw(g, pos, node_size=140)
    draw_many_paths(g, pos, [path1, path2, path3, path4], max_shift=0.03)
    matplotlib.pyplot.savefig("PathDrawer.png")
    matplotlib.pyplot.show()
Ejemplo n.º 23
0
import networkx as nx
import matplotlib.pyplot as plt

G = nx.krackhardt_kite_graph()  # 社会网络图Krackhardt Kite。是一种含有10个节点的虚拟图,常用于图的算法证明
nx.draw_networkx(G)
plt.show()

# 检查图的连通性
print(nx.has_path(G, source=1, target=9))               # 两个点是否连通
print(nx.shortest_path(G, source=1, target=9))          # 最短路径
print(nx.shortest_path_length(G, source=1, target=9))   # 最短路径长度
# ------------------------------------------------------------------------------------------------------
# 中介中心性
"""
这种类型的中心性以节点的最短路径数目表示节点的重要性。中介中心性高的节点是网络的核心组成部分,有很多最短路径用过中介中心性高的节点,
下列中,NetWorkX提供了一种直接的方法来计算所有节点的中介中心性
"""
print('中介中心性:\n', nx.betweenness_centrality(G))
# ------------------------------------------------------------------------------------------------------
# 度中心性
"""
一个节点的度中心性可以简单地理解为与本节点有直接联系的节点占其余节点总数的百分比。
需要注意的是,在有向图中每个节点有两个度中心性:入度中心性和出度中心性
"""
print('度中心性:\n', nx.degree_centrality(G))
# ------------------------------------------------------------------------------------------------------
# 接近中心性
"""
反映节点与其他节点之间的接近程度,可以按照如下步骤计算每个节点的接近中心性:
1.计算节点到其他节点最短路径的距离
2.取其平均值,将平均距离除以最长路径
Ejemplo n.º 24
0
 def test_krackhardt(self):
     expected = True
     actual = is_planar(nx.krackhardt_kite_graph())
     self.assertEqual(expected, actual)
Ejemplo n.º 25
0
 def setUpClass(cls):
     cls.g = qt.QueueNetworkDiGraph(nx.krackhardt_kite_graph())
     np.random.seed(10)
Ejemplo n.º 26
0
import matplotlib.pyplot as plt

fb = nx.read_edgelist("facebook/facebook_combined_minimized.txt",
                      create_using=nx.Graph(),
                      nodetype=int)
print(nx.info(fb))  # the information about this graph

cliques = [x for x in nx.find_cliques(fb)]

# draw the network
#pos = nx.spring_layout(fb)
#plt.figure(figsize = (12,12))
#plt.axis('off')
#nx.draw_networkx(fb, pos=pos, with_labels=False, node_size=80)

g = nx.krackhardt_kite_graph()
nx.clustering(g)

g = nx.davis_southern_women_graph()
g.nodes()

#import warnings
#import networkx.drawing.nx_pylab
#warnings.filterwarnings("ignore",category=networkx.drawing.nx_pylab.draw)

#import warnings
#
#def fxn():
#    warnings.warn("deprecated", DeprecationWarning)
#
#with warnings.catch_warnings():
Ejemplo n.º 27
0
 def setUpClass(cls):
     cls.g = qt.QueueNetworkDiGraph(nx.krackhardt_kite_graph())
     np.random.seed(10)
Ejemplo n.º 28
0
    def __init__(self):
        self.concept_network = networkx.krackhardt_kite_graph()
        self.vocabulary = networkx.Graph()
        self.vocabulary.add_nodes_from(self.concept_network)

        self.p_invent = 0
    def test_properties_named_small_graphs(self):
        G = nx.bull_graph()
        assert G.number_of_nodes() == 5
        assert G.number_of_edges() == 5
        assert sorted(d for n, d in G.degree()) == [1, 1, 2, 3, 3]
        assert nx.diameter(G) == 3
        assert nx.radius(G) == 2

        G = nx.chvatal_graph()
        assert G.number_of_nodes() == 12
        assert G.number_of_edges() == 24
        assert list(d for n, d in G.degree()) == 12 * [4]
        assert nx.diameter(G) == 2
        assert nx.radius(G) == 2

        G = nx.cubical_graph()
        assert G.number_of_nodes() == 8
        assert G.number_of_edges() == 12
        assert list(d for n, d in G.degree()) == 8 * [3]
        assert nx.diameter(G) == 3
        assert nx.radius(G) == 3

        G = nx.desargues_graph()
        assert G.number_of_nodes() == 20
        assert G.number_of_edges() == 30
        assert list(d for n, d in G.degree()) == 20 * [3]

        G = nx.diamond_graph()
        assert G.number_of_nodes() == 4
        assert sorted(d for n, d in G.degree()) == [2, 2, 3, 3]
        assert nx.diameter(G) == 2
        assert nx.radius(G) == 1

        G = nx.dodecahedral_graph()
        assert G.number_of_nodes() == 20
        assert G.number_of_edges() == 30
        assert list(d for n, d in G.degree()) == 20 * [3]
        assert nx.diameter(G) == 5
        assert nx.radius(G) == 5

        G = nx.frucht_graph()
        assert G.number_of_nodes() == 12
        assert G.number_of_edges() == 18
        assert list(d for n, d in G.degree()) == 12 * [3]
        assert nx.diameter(G) == 4
        assert nx.radius(G) == 3

        G = nx.heawood_graph()
        assert G.number_of_nodes() == 14
        assert G.number_of_edges() == 21
        assert list(d for n, d in G.degree()) == 14 * [3]
        assert nx.diameter(G) == 3
        assert nx.radius(G) == 3

        G = nx.hoffman_singleton_graph()
        assert G.number_of_nodes() == 50
        assert G.number_of_edges() == 175
        assert list(d for n, d in G.degree()) == 50 * [7]
        assert nx.diameter(G) == 2
        assert nx.radius(G) == 2

        G = nx.house_graph()
        assert G.number_of_nodes() == 5
        assert G.number_of_edges() == 6
        assert sorted(d for n, d in G.degree()) == [2, 2, 2, 3, 3]
        assert nx.diameter(G) == 2
        assert nx.radius(G) == 2

        G = nx.house_x_graph()
        assert G.number_of_nodes() == 5
        assert G.number_of_edges() == 8
        assert sorted(d for n, d in G.degree()) == [2, 3, 3, 4, 4]
        assert nx.diameter(G) == 2
        assert nx.radius(G) == 1

        G = nx.icosahedral_graph()
        assert G.number_of_nodes() == 12
        assert G.number_of_edges() == 30
        assert (list(
            d for n, d in G.degree()) == [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5])
        assert nx.diameter(G) == 3
        assert nx.radius(G) == 3

        G = nx.krackhardt_kite_graph()
        assert G.number_of_nodes() == 10
        assert G.number_of_edges() == 18
        assert (sorted(
            d for n, d in G.degree()) == [1, 2, 3, 3, 3, 4, 4, 5, 5, 6])

        G = nx.moebius_kantor_graph()
        assert G.number_of_nodes() == 16
        assert G.number_of_edges() == 24
        assert list(d for n, d in G.degree()) == 16 * [3]
        assert nx.diameter(G) == 4

        G = nx.octahedral_graph()
        assert G.number_of_nodes() == 6
        assert G.number_of_edges() == 12
        assert list(d for n, d in G.degree()) == 6 * [4]
        assert nx.diameter(G) == 2
        assert nx.radius(G) == 2

        G = nx.pappus_graph()
        assert G.number_of_nodes() == 18
        assert G.number_of_edges() == 27
        assert list(d for n, d in G.degree()) == 18 * [3]
        assert nx.diameter(G) == 4

        G = nx.petersen_graph()
        assert G.number_of_nodes() == 10
        assert G.number_of_edges() == 15
        assert list(d for n, d in G.degree()) == 10 * [3]
        assert nx.diameter(G) == 2
        assert nx.radius(G) == 2

        G = nx.sedgewick_maze_graph()
        assert G.number_of_nodes() == 8
        assert G.number_of_edges() == 10
        assert sorted(d for n, d in G.degree()) == [1, 2, 2, 2, 3, 3, 3, 4]

        G = nx.tetrahedral_graph()
        assert G.number_of_nodes() == 4
        assert G.number_of_edges() == 6
        assert list(d for n, d in G.degree()) == [3, 3, 3, 3]
        assert nx.diameter(G) == 1
        assert nx.radius(G) == 1

        G = nx.truncated_cube_graph()
        assert G.number_of_nodes() == 24
        assert G.number_of_edges() == 36
        assert list(d for n, d in G.degree()) == 24 * [3]

        G = nx.truncated_tetrahedron_graph()
        assert G.number_of_nodes() == 12
        assert G.number_of_edges() == 18
        assert list(d for n, d in G.degree()) == 12 * [3]

        G = nx.tutte_graph()
        assert G.number_of_nodes() == 46
        assert G.number_of_edges() == 69
        assert list(d for n, d in G.degree()) == 46 * [3]

        # Test create_using with directed or multigraphs on small graphs
        pytest.raises(nx.NetworkXError,
                      nx.tutte_graph,
                      create_using=nx.DiGraph)
        MG = nx.tutte_graph(create_using=nx.MultiGraph)
        assert sorted(MG.edges()) == sorted(G.edges())
Ejemplo n.º 30
0
def small_graphs():
    print("Make small graph")
    G = nx.make_small_graph(
        ["adjacencylist", "C_4", 4, [[2, 4], [1, 3], [2, 4], [1, 3]]])
    draw_graph(G)
    G = nx.make_small_graph(
        ["adjacencylist", "C_4", 4, [[2, 4], [3], [4], []]])
    draw_graph(G)
    G = nx.make_small_graph(
        ["edgelist", "C_4", 4, [[1, 2], [3, 4], [2, 3], [4, 1]]])
    draw_graph(G)
    print("LCF graph")
    G = nx.LCF_graph(6, [3, -3], 3)
    draw_graph(G)
    G = nx.LCF_graph(14, [5, -5], 7)
    draw_graph(G)
    print("Bull graph")
    G = nx.bull_graph()
    draw_graph(G)
    print("Chvátal graph")
    G = nx.chvatal_graph()
    draw_graph(G)
    print("Cubical graph")
    G = nx.cubical_graph()
    draw_graph(G)
    print("Desargues graph")
    G = nx.desargues_graph()
    draw_graph(G)
    print("Diamond graph")
    G = nx.diamond_graph()
    draw_graph(G)
    print("Dodechaedral graph")
    G = nx.dodecahedral_graph()
    draw_graph(G)
    print("Frucht graph")
    G = nx.frucht_graph()
    draw_graph(G)
    print("Heawood graph")
    G = nx.heawood_graph()
    draw_graph(G)
    print("House graph")
    G = nx.house_graph()
    draw_graph(G)
    print("House X graph")
    G = nx.house_x_graph()
    draw_graph(G)
    print("Icosahedral graph")
    G = nx.icosahedral_graph()
    draw_graph(G)
    print("Krackhardt kite graph")
    G = nx.krackhardt_kite_graph()
    draw_graph(G)
    print("Moebius kantor graph")
    G = nx.moebius_kantor_graph()
    draw_graph(G)
    print("Octahedral graph")
    G = nx.octahedral_graph()
    draw_graph(G)
    print("Pappus graph")
    G = nx.pappus_graph()
    draw_graph(G)
    print("Petersen graph")
    G = nx.petersen_graph()
    draw_graph(G)
    print("Sedgewick maze graph")
    G = nx.sedgewick_maze_graph()
    draw_graph(G)
    print("Tetrahedral graph")
    G = nx.tetrahedral_graph()
    draw_graph(G)
    print("Truncated cube graph")
    G = nx.truncated_cube_graph()
    draw_graph(G)
    print("Truncated tetrahedron graph")
    G = nx.truncated_tetrahedron_graph()
    draw_graph(G)
    print("Tutte graph")
    G = nx.tutte_graph()
    draw_graph(G)
Ejemplo n.º 31
0
from simuling.simulation import *
import pytest

import networkx


@pytest.fixture(params=[
    networkx.krackhardt_kite_graph(),
    networkx.binomial_graph(8, 0.5, seed=0)
])
def lfs(request):
    return LanguageForwardSimulation(request.param, 0)


@pytest.fixture
def speaker():
    return Speaker()


def test_speaker_is_set(lfs):
    assert lfs.population_graph.node[0]['speaker'], (
        "Node 0 should have a speaker attached")


def test_run_lfs(lfs):
    lfs.run(100)


def test_time_step(lfs):
    before = networkx.Graph(lfs.population_graph)
    lfs.run(1)
Ejemplo n.º 32
0
# coding: utf-8

# In[1]:


import networkx as nx
import matplotlib.pyplot as plt
import pandas as pd
from numpy import linalg as LA
from collections import defaultdict


# In[2]:


Knetwork = nx.krackhardt_kite_graph()


# In[3]:


nx.draw(Knetwork, with_labels = True)


# In[4]:


norm_degree_centrality = []              #stores the normalized degree centrality vector
number_of_nodes = Knetwork.number_of_nodes()
for i,j in list(Knetwork.degree()):
    norm_degree_centrality.append((j / (number_of_nodes-1)))
Ejemplo n.º 33
0
import sys
import json
import networkx

from .simulation import LanguageForwardSimulation

options = json.load(open(sys.argv[1]))

seed = options.get("seed", 0)
""" randomizer seed used for the simulation """

population_graph = options["population_graph"]
if population_graph == 'krackhardt':
    population = networkx.krackhardt_kite_graph()
else:
    raise ValueError("Graph model {:s} not recognized.".format(
        population_graph))

t = options.get("critical_period", 1)

simulation = LanguageForwardSimulation(
    population, t,
    seed=seed, logfile=open("Data/log", 'w'))
simulation.run(options['time_steps'])
Ejemplo n.º 34
0
import networkx as nx
import matplotlib.pylab as plt
from plot_multigraph import plot_multigraph

graphs = [
  ("bull", nx.bull_graph()),
  ("chvatal", nx.chvatal_graph()),
  ("cubical", nx.cubical_graph()),
  ("desargues", nx.desargues_graph()),
  ("diamond", nx.diamond_graph()),
  ("dodecahedral", nx.dodecahedral_graph()),
  ("frucht", nx.frucht_graph()),
  ("heawood", nx.heawood_graph()),
  ("house", nx.house_graph()),
  ("house_x", nx.house_x_graph()),
  ("icosahedral", nx.icosahedral_graph()),
  ("krackhardt_kite", nx.krackhardt_kite_graph()),
  ("moebius_kantor", nx.moebius_kantor_graph()),
  ("octahedral", nx.octahedral_graph()),
  ("pappus", nx.pappus_graph()),
  ("petersen", nx.petersen_graph()),
  ("sedgewick_maze", nx.sedgewick_maze_graph()),
  ("tetrahedral", nx.tetrahedral_graph()),
  ("truncated_cube", nx.truncated_cube_graph()),
  ("truncated_tetrahedron", nx.truncated_tetrahedron_graph()),
]

plot_multigraph(graphs, 4, 5, node_size=50)
plt.savefig('graphs/small.png')

Ejemplo n.º 35
0

if __name__ == '__main__':
    targets = {
        'bull': nx.bull_graph(),  # 1-connected planar
        'chvatal': nx.chvatal_graph(),  # 4-connected non-planar
        'cubical': nx.cubical_graph(),  # 3-connected planar
        'desargues': nx.desargues_graph(),  # 3-connected non-planar
        'diamond': nx.diamond_graph(),  # 2-connected planar
        'dodecahedral': nx.dodecahedral_graph(),  # 3-connected planar
        'frucht': nx.frucht_graph(),  # 3-connected planar
        'heawood': nx.heawood_graph(),  # 3-connected non-planar
        'house': nx.house_graph(),  # 2-connected planar
        'house_x': nx.house_x_graph(),  # 2-connected planar
        'icosahedral': nx.icosahedral_graph(),  # 5-connected planar
        'krackhardt': nx.krackhardt_kite_graph(),  # 1-connected planar
        'moebius': nx.moebius_kantor_graph(),  # non-planar
        'octahedral': nx.octahedral_graph(),  # 4-connected planar
        'pappus': nx.pappus_graph(),  # 3-connected non-planar
        'petersen': nx.petersen_graph(),  # 3-connected non-planar
        'sedgewick': nx.sedgewick_maze_graph(),  # 1-connected planar
        'tetrahedral': nx.tetrahedral_graph(),  # 3-connected planar
        'truncated_cube': nx.truncated_cube_graph(),  # 3-conn. planar
        'truncated_tetrahedron': nx.truncated_tetrahedron_graph(),
        # 3-connected planar
        'tutte': nx.tutte_graph()
    }  # 3-connected planar
    for g_name, g in targets.items():
        print g_name, is_planar(g)

#    g = nx.petersen_graph()
Ejemplo n.º 36
0
def setup(config: dict):
    # define type of model + settings
    model_t = models.Potts
    model_settings = dict(agentStates=np.arange(2))

    # output directory
    output_directory = f"{__file__}:{datetime.now().isoformat()}"
    # magnetization params
    n_magnetize = int(1e4)
    temps = np.linspace(0, 5, 50)

    # fitting params
    maxfev = 100_000

    thetas = [.85]  # match_temperatures

    # TODO : implement
    # graphs = [gen.generate(5)]
    graphs = [nx.krackhardt_kite_graph()]
    #graphs = [nx.florentine_families_graph()]
    #graphs = [recursive_tree(6, 0)]
    # graphs = [nx.star_graph(5)]
    # graphs = [nx.path_graph(5)]
    #

    # memoize phase transition results
    phase_transitions = {}

    # hold tasks
    experiments = []

    impacts = np.array([])
    interventions = []
    for g in graphs:
        # unperturbed
        interventions.append({})
        for node in g.nodes():
            for impact in impacts:
                tmp = {node: impact}
                # nodal intervention
                interventions.append(tmp)

    print(f"int: {len(interventions)},g: {len(graphs)},theta: {len(thetas)}")
    combinations = itertools.product(thetas, graphs, interventions)

    snaps = {}
    for comb in combinations:
        # unpack

        instance_settings = model_settings.copy()
        theta, graph, intervention = comb
        instance_settings['sampleSize'] = 1  # len(graph)
        instance_settings['graph'] = graph
        # instance_settings['sampleSize'] = graph.number_of_nodes()

        # get params fit
        if phase_transition := phase_transitions.get(graph):
            opts, cov = phase_transition.get('fit')
        else:
            # create model
            model = model_t(**instance_settings)
            magnetization, susceptibility = model.magnetize(temps,
                                                            n=n_magnetize)

            # fit phase transition
            opts, cov = optimize.curve_fit(sig,
                                           xdata=temps,
                                           ydata=magnetization,
                                           maxfev=maxfev)

            # store function fit and output
            phase_transitions[graph] = dict(fit=(opts, cov),
                                            magnetization=magnetization,
                                            susceptibility=susceptibility)



        res = optimize.minimize(lambda x: abs(sig(x, *opts) - theta), \
                                 x0 = 0,\
                                 method = 'COBYLA',\
                                 )

        # setup model
        t = res.x
        print(f"setting temperature to {t}")
        instance_settings['t'] = t

        # reinit model
        model = model_t(**instance_settings)

        # s = snaps.get(graph, {})
        # if len(s) == 0:
        #      sim = infcy.Simulator(model)
        #      ss = config['snapshots']
        #      s = sim.snapshots(**ss)

        #      print(s)
        #      snaps[graph] = s
        #      print(f"snaps:{len(s)}")
        config['intervention'] = intervention
        import copy
        settings = dict(
            model=copy.deepcopy(model),
            instance_settings=instance_settings,
            t=t,
            config=config.copy(),
            magnetisation=theta,
            # snapshots         = s
        )

        task = Experiment(settings, output_directory=output_directory)
        experiments.append(task)
import networkx as nx
import matplotlib.pylab as plt
from plot_multigraph import plot_multigraph

graphs = [
    ("bull", nx.bull_graph()),
    ("chvatal", nx.chvatal_graph()),
    ("cubical", nx.cubical_graph()),
    ("desargues", nx.desargues_graph()),
    ("diamond", nx.diamond_graph()),
    ("dodecahedral", nx.dodecahedral_graph()),
    ("frucht", nx.frucht_graph()),
    ("heawood", nx.heawood_graph()),
    ("house", nx.house_graph()),
    ("house_x", nx.house_x_graph()),
    ("icosahedral", nx.icosahedral_graph()),
    ("krackhardt_kite", nx.krackhardt_kite_graph()),
    ("moebius_kantor", nx.moebius_kantor_graph()),
    ("octahedral", nx.octahedral_graph()),
    ("pappus", nx.pappus_graph()),
    ("petersen", nx.petersen_graph()),
    ("sedgewick_maze", nx.sedgewick_maze_graph()),
    ("tetrahedral", nx.tetrahedral_graph()),
    ("truncated_cube", nx.truncated_cube_graph()),
    ("truncated_tetrahedron", nx.truncated_tetrahedron_graph()),
]

plot_multigraph(graphs, 4, 5, node_size=50)
plt.savefig('graphs/small.png')
Ejemplo n.º 38
0
"""
# Author: Aric Hagberg ([email protected])
# Date: 2005-05-12 14:33:11 -0600 (Thu, 12 May 2005)
# Revision: 998

#    Copyright (C) 2004-2019 by
#    Aric Hagberg <*****@*****.**>
#    Dan Schult <*****@*****.**>
#    Pieter Swart <*****@*****.**>
#    All rights reserved.
#    BSD license.

import matplotlib.pyplot as plt
import networkx as nx

G = nx.krackhardt_kite_graph()

print("Betweenness")
b = nx.betweenness_centrality(G)
for v in G.nodes():
    print("%0.2d %5.3f" % (v, b[v]))

print("Degree centrality")
d = nx.degree_centrality(G)
for v in G.nodes():
    print("%0.2d %5.3f" % (v, d[v]))

print("Closeness centrality")
c = nx.closeness_centrality(G)
for v in G.nodes():
    print("%0.2d %5.3f" % (v, c[v]))