Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / examples / drawing / plot_giant_component.py @ 5cef0f13

History | View | Annotate | Download (2.21 KB)

1
#!/usr/bin/env python
2
"""
3
===============
4
Giant Component
5
===============
6

7
This example illustrates the sudden appearance of a
8
giant connected component in a binomial random graph.
9
"""
10
#    Copyright (C) 2006-2019
11
#    Aric Hagberg <hagberg@lanl.gov>
12
#    Dan Schult <dschult@colgate.edu>
13
#    Pieter Swart <swart@lanl.gov>
14
#    All rights reserved.
15
#    BSD license.
16

    
17
import math
18

    
19
import matplotlib.pyplot as plt
20
import networkx as nx
21

    
22
try:
23
    import pygraphviz
24
    from networkx.drawing.nx_agraph import graphviz_layout
25
    layout = graphviz_layout
26
except ImportError:
27
    try:
28
        import pydot
29
        from networkx.drawing.nx_pydot import graphviz_layout
30
        layout = graphviz_layout
31
    except ImportError:
32
        print("PyGraphviz and pydot not found;\n"
33
              "drawing with spring layout;\n"
34
              "will be slow.")
35
        layout = nx.spring_layout
36

    
37

    
38
n = 150  # 150 nodes
39
# p value at which giant component (of size log(n) nodes) is expected
40
p_giant = 1.0 / (n - 1)
41
# p value at which graph is expected to become completely connected
42
p_conn = math.log(n) / float(n)
43

    
44
# the following range of p values should be close to the threshold
45
pvals = [0.003, 0.006, 0.008, 0.015]
46

    
47
region = 220  # for pylab 2x2 subplot layout
48
plt.subplots_adjust(left=0, right=1, bottom=0, top=0.95, wspace=0.01, hspace=0.01)
49
for p in pvals:
50
    G = nx.binomial_graph(n, p)
51
    pos = layout(G)
52
    region += 1
53
    plt.subplot(region)
54
    plt.title("p = %6.3f" % (p))
55
    nx.draw(G, pos,
56
            with_labels=False,
57
            node_size=10
58
           )
59
    # identify largest connected component
60
    Gcc = sorted(nx.connected_component_subgraphs(G), key=len, reverse=True)
61
    G0 = Gcc[0]
62
    nx.draw_networkx_edges(G0, pos,
63
                           with_labels=False,
64
                           edge_color='r',
65
                           width=6.0
66
                          )
67
    # show other connected components
68
    for Gi in Gcc[1:]:
69
        if len(Gi) > 1:
70
            nx.draw_networkx_edges(Gi, pos,
71
                                   with_labels=False,
72
                                   edge_color='r',
73
                                   alpha=0.3,
74
                                   width=5.0
75
                                  )
76
plt.show()