{
  "nbformat_minor": 0, 
  "nbformat": 4, 
  "cells": [
    {
      "execution_count": null, 
      "cell_type": "code", 
      "source": [
        "%matplotlib inline"
      ], 
      "outputs": [], 
      "metadata": {
        "collapsed": false
      }
    }, 
    {
      "source": [
        "\n# Lanl Routes\n\n\nRoutes to LANL from 186 sites on the Internet.\n\nThis uses Graphviz for layout so you need PyGraphviz or pydot.\n\n\n"
      ], 
      "cell_type": "markdown", 
      "metadata": {}
    }, 
    {
      "execution_count": null, 
      "cell_type": "code", 
      "source": [
        "# Author: Aric Hagberg (hagberg@lanl.gov)\n\n#    Copyright (C) 2004-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\n\nimport matplotlib.pyplot as plt\nimport networkx as nx\ntry:\n    import pygraphviz\n    from networkx.drawing.nx_agraph import graphviz_layout\nexcept ImportError:\n    try:\n        import pydot\n        from networkx.drawing.nx_pydot import graphviz_layout\n    except ImportError:\n        raise ImportError(\"This example needs Graphviz and either \"\n                          \"PyGraphviz or pydot\")\n\ndef lanl_graph():\n    \"\"\" Return the lanl internet view graph from lanl.edges\n    \"\"\"\n    try:\n        fh = open('lanl_routes.edgelist', 'r')\n    except IOError:\n        print(\"lanl.edges not found\")\n        raise\n\n    G = nx.Graph()\n\n    time = {}\n    time[0] = 0  # assign 0 to center node\n    for line in fh.readlines():\n        (head, tail, rtt) = line.split()\n        G.add_edge(int(head), int(tail))\n        time[int(head)] = float(rtt)\n\n    # get largest component and assign ping times to G0time dictionary\n    G0 = sorted(nx.connected_component_subgraphs(G), key=len, reverse=True)[0]\n    G0.rtt = {}\n    for n in G0:\n        G0.rtt[n] = time[n]\n\n    return G0\n\n\nif __name__ == '__main__':\n\n    G = lanl_graph()\n\n    print(\"graph has %d nodes with %d edges\"\n          % (nx.number_of_nodes(G), nx.number_of_edges(G)))\n    print(nx.number_connected_components(G), \"connected components\")\n\n    plt.figure(figsize=(8, 8))\n    # use graphviz to find radial layout\n    pos = graphviz_layout(G, prog=\"twopi\", root=0)\n    # draw nodes, coloring by rtt ping time\n    nx.draw(G, pos,\n            node_color=[G.rtt[v] for v in G],\n            with_labels=False,\n            alpha=0.5,\n            node_size=15)\n    # adjust the plot limits\n    xmax = 1.02 * max(xx for xx, yy in pos.values())\n    ymax = 1.02 * max(yy for xx, yy in pos.values())\n    plt.xlim(0, xmax)\n    plt.ylim(0, ymax)\n    plt.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"
      }
    }
  }
}