{
  "nbformat_minor": 0, 
  "nbformat": 4, 
  "cells": [
    {
      "execution_count": null, 
      "cell_type": "code", 
      "source": [
        "%matplotlib inline"
      ], 
      "outputs": [], 
      "metadata": {
        "collapsed": false
      }
    }, 
    {
      "source": [
        "\n# Giant Component\n\n\nThis example illustrates the sudden appearance of a\ngiant connected component in a binomial random graph.\n\n"
      ], 
      "cell_type": "markdown", 
      "metadata": {}
    }, 
    {
      "execution_count": null, 
      "cell_type": "code", 
      "source": [
        "#    Copyright (C) 2006-2017\n#    Aric Hagberg <hagberg@lanl.gov>\n#    Dan Schult <dschult@colgate.edu>\n#    Pieter Swart <swart@lanl.gov>\n#    All rights reserved.\n#    BSD license.\n\nimport math\n\nimport matplotlib.pyplot as plt\nimport networkx as nx\n\ntry:\n    import pygraphviz\n    from networkx.drawing.nx_agraph import graphviz_layout\n    layout = graphviz_layout\nexcept ImportError:\n    try:\n        import pydot\n        from networkx.drawing.nx_pydot import graphviz_layout\n        layout = graphviz_layout\n    except ImportError:\n        print(\"PyGraphviz and pydot not found;\\n\"\n              \"drawing with spring layout;\\n\"\n              \"will be slow.\")\n        layout = nx.spring_layout\n\n\nn = 150  # 150 nodes\n# p value at which giant component (of size log(n) nodes) is expected\np_giant = 1.0 / (n - 1)\n# p value at which graph is expected to become completely connected\np_conn = math.log(n) / float(n)\n\n# the following range of p values should be close to the threshold\npvals = [0.003, 0.006, 0.008, 0.015]\n\nregion = 220  # for pylab 2x2 subplot layout\nplt.subplots_adjust(left=0, right=1, bottom=0, top=0.95, wspace=0.01, hspace=0.01)\nfor p in pvals:\n    G = nx.binomial_graph(n, p)\n    pos = layout(G)\n    region += 1\n    plt.subplot(region)\n    plt.title(\"p = %6.3f\" % (p))\n    nx.draw(G, pos,\n            with_labels=False,\n            node_size=10\n           )\n    # identify largest connected component\n    Gcc = sorted(nx.connected_component_subgraphs(G), key=len, reverse=True)\n    G0 = Gcc[0]\n    nx.draw_networkx_edges(G0, pos,\n                           with_labels=False,\n                           edge_color='r',\n                           width=6.0\n                          )\n    # show other connected components\n    for Gi in Gcc[1:]:\n        if len(Gi) > 1:\n            nx.draw_networkx_edges(Gi, pos,\n                                   with_labels=False,\n                                   edge_color='r',\n                                   alpha=0.3,\n                                   width=5.0\n                                  )\nplt.show()"
      ], 
      "outputs": [], 
      "metadata": {
        "collapsed": false
      }
    }
  ], 
  "metadata": {
    "kernelspec": {
      "display_name": "Python 2", 
      "name": "python2", 
      "language": "python"
    }, 
    "language_info": {
      "mimetype": "text/x-python", 
      "nbconvert_exporter": "python", 
      "name": "python", 
      "file_extension": ".py", 
      "version": "2.7.12", 
      "pygments_lexer": "ipython2", 
      "codemirror_mode": {
        "version": 2, 
        "name": "ipython"
      }
    }
  }
}