{
  "nbformat_minor": 0, 
  "nbformat": 4, 
  "cells": [
    {
      "execution_count": null, 
      "cell_type": "code", 
      "source": [
        "%matplotlib inline"
      ], 
      "outputs": [], 
      "metadata": {
        "collapsed": false
      }
    }, 
    {
      "source": [
        "\n# Print Graph\n\n\nExample subclass of the Graph class.\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#\n__docformat__ = \"restructuredtext en\"\n\nfrom copy import deepcopy\n\nimport matplotlib.pyplot as plt\nimport networkx as nx\nfrom networkx import Graph\n\nclass PrintGraph(Graph):\n    \"\"\"\n    Example subclass of the Graph class.\n\n    Prints activity log to file or standard output.\n    \"\"\"\n\n    def __init__(self, data=None, name='', file=None, **attr):\n        Graph.__init__(self, data=data, name=name, **attr)\n        if file is None:\n            import sys\n            self.fh = sys.stdout\n        else:\n            self.fh = open(file, 'w')\n\n    def add_node(self, n, attr_dict=None, **attr):\n        Graph.add_node(self, n, attr_dict=attr_dict, **attr)\n        self.fh.write(\"Add node: %s\\n\" % n)\n\n    def add_nodes_from(self, nodes, **attr):\n        for n in nodes:\n            self.add_node(n, **attr)\n\n    def remove_node(self, n):\n        Graph.remove_node(self, n)\n        self.fh.write(\"Remove node: %s\\n\" % n)\n\n    def remove_nodes_from(self, nodes):\n        for n in nodes:\n            self.remove_node(n)\n\n    def add_edge(self, u, v, attr_dict=None, **attr):\n        Graph.add_edge(self, u, v, attr_dict=attr_dict, **attr)\n        self.fh.write(\"Add edge: %s-%s\\n\" % (u, v))\n\n    def add_edges_from(self, ebunch, attr_dict=None, **attr):\n        for e in ebunch:\n            u, v = e[0:2]\n            self.add_edge(u, v, attr_dict=attr_dict, **attr)\n\n    def remove_edge(self, u, v):\n        Graph.remove_edge(self, u, v)\n        self.fh.write(\"Remove edge: %s-%s\\n\" % (u, v))\n\n    def remove_edges_from(self, ebunch):\n        for e in ebunch:\n            u, v = e[0:2]\n            self.remove_edge(u, v)\n\n    def clear(self):\n        Graph.clear(self)\n        self.fh.write(\"Clear graph\\n\")\n\n    def subgraph(self, nbunch, copy=True):\n        # subgraph is needed here since it can destroy edges in the\n        # graph (copy=False) and we want to keep track of all changes.\n        #\n        # Also for copy=True Graph() uses dictionary assignment for speed\n        # Here we use H.add_edge()\n        bunch = set(self.nbunch_iter(nbunch))\n\n        if not copy:\n            # remove all nodes (and attached edges) not in nbunch\n            self.remove_nodes_from([n for n in self if n not in bunch])\n            self.name = \"Subgraph of (%s)\" % (self.name)\n            return self\n        else:\n            # create new graph and copy subgraph into it\n            H = self.__class__()\n            H.name = \"Subgraph of (%s)\" % (self.name)\n            # add nodes\n            H.add_nodes_from(bunch)\n            # add edges\n            seen = set()\n            for u, nbrs in self.adjacency_iter():\n                if u in bunch:\n                    for v, datadict in nbrs.items():\n                        if v in bunch and v not in seen:\n                            dd = deepcopy(datadict)\n                            H.add_edge(u, v, dd)\n                    seen.add(u)\n            # copy node and graph attr dicts\n            H.node = dict((n, deepcopy(d))\n                          for (n, d) in self.node.items() if n in H)\n            H.graph = deepcopy(self.graph)\n            return H\n\n\nif __name__ == '__main__':\n    G = PrintGraph()\n    G.add_node('foo')\n    G.add_nodes_from('bar', weight=8)\n    G.remove_node('b')\n    G.remove_nodes_from('ar')\n    print(G.nodes(data=True))\n    G.add_edge(0, 1, weight=10)\n    print(list(G.edges(data=True)))\n    G.remove_edge(0, 1)\n    G.add_edges_from(list(zip(list(range(0o3)), list(range(1, 4)))), weight=10)\n    print(list(G.edges(data=True)))\n    G.remove_edges_from(list(zip(list(range(0o3)), list(range(1, 4)))))\n    print(list(G.edges(data=True)))\n\n    G = PrintGraph()\n    nx.add_path(G, range(10))\n    print(\"subgraph\")\n    H1 = G.subgraph(range(4), copy=False)\n    H2 = G.subgraph(range(4), copy=False)\n    print(list(H1.edges()))\n    print(list(H2.edges()))\n\n    nx.draw(G)\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"
      }
    }
  }
}