{
  "nbformat_minor": 0, 
  "nbformat": 4, 
  "cells": [
    {
      "execution_count": null, 
      "cell_type": "code", 
      "source": [
        "%matplotlib inline"
      ], 
      "outputs": [], 
      "metadata": {
        "collapsed": false
      }
    }, 
    {
      "source": [
        "\n# Atlas\n\n\nAtlas of all graphs of 6 nodes or less.\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 by\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 random\n\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\nimport matplotlib.pyplot as plt\n\nimport networkx as nx\nfrom networkx.algorithms.isomorphism.isomorph import graph_could_be_isomorphic as isomorphic\nfrom networkx.generators.atlas import graph_atlas_g\n\n\ndef atlas6():\n    \"\"\" Return the atlas of all connected graphs of 6 nodes or less.\n        Attempt to check for isomorphisms and remove.\n    \"\"\"\n\n    Atlas = graph_atlas_g()[0:208]  # 208\n    # remove isolated nodes, only connected graphs are left\n    U = nx.Graph()  # graph for union of all graphs in atlas\n    for G in Atlas:\n        zerodegree = [n for n in G if G.degree(n) == 0]\n        for n in zerodegree:\n            G.remove_node(n)\n        U = nx.disjoint_union(U, G)\n\n    # list of graphs of all connected components\n    C = nx.connected_component_subgraphs(U)\n\n    UU = nx.Graph()\n    # do quick isomorphic-like check, not a true isomorphism checker\n    nlist = []  # list of nonisomorphic graphs\n    for G in C:\n        # check against all nonisomorphic graphs so far\n        if not iso(G, nlist):\n            nlist.append(G)\n            UU = nx.disjoint_union(UU, G)  # union the nonisomorphic graphs\n    return UU\n\n\ndef iso(G1, glist):\n    \"\"\"Quick and dirty nonisomorphism checker used to check isomorphisms.\"\"\"\n    for G2 in glist:\n        if isomorphic(G1, G2):\n            return True\n    return False\n\n\nif __name__ == '__main__':\n    G = atlas6()\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(1, figsize=(8, 8))\n    # layout graphs with positions using graphviz neato\n    pos = graphviz_layout(G, prog=\"neato\")\n    # color nodes the same in each connected subgraph\n    C = nx.connected_component_subgraphs(G)\n    for g in C:\n        c = [random.random()] * nx.number_of_nodes(g)  # random color...\n        nx.draw(g,\n                pos,\n                node_size=40,\n                node_color=c,\n                vmin=0.0,\n                vmax=1.0,\n                with_labels=False\n               )\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"
      }
    }
  }
}