{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Lanl Routes\n\nRoutes to LANL from 186 sites on the Internet.\n\nThe data file can be found at:\n\n- https://github.com/networkx/networkx/blob/main/examples/graphviz_layout/lanl_routes.edgelist\n\nThis example needs Graphviz and PyGraphviz.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nimport networkx as nx\n\n\ndef lanl_graph():\n    \"\"\"Return the lanl internet view graph from lanl.edges\"\"\"\n    try:\n        fh = open(\"lanl_routes.edgelist\")\n    except OSError:\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    Gcc = sorted(nx.connected_components(G), key=len, reverse=True)[0]\n    G0 = G.subgraph(Gcc)\n    G0.rtt = {}\n    for n in G0:\n        G0.rtt[n] = time[n]\n\n    return G0\n\n\nG = lanl_graph()\n\nprint(G)\nprint(nx.number_connected_components(G), \"connected components\")\n\nplt.figure(figsize=(8, 8))\n# use graphviz to find radial layout\npos = nx.nx_agraph.graphviz_layout(G, prog=\"twopi\", root=0)\n# draw nodes, coloring by rtt ping time\noptions = {\"with_labels\": False, \"alpha\": 0.5, \"node_size\": 15}\nnx.draw(G, pos, node_color=[G.rtt[v] for v in G], **options)\n# adjust the plot limits\nxmax = 1.02 * max(xx for xx, yy in pos.values())\nymax = 1.02 * max(yy for xx, yy in pos.values())\nplt.xlim(0, xmax)\nplt.ylim(0, ymax)\nplt.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.12.7"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}