{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Explore Random Graphs Using NetworkX" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this example, we build a simple UI for exploring random graphs with [NetworkX](http://networkx.github.io/)." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from IPython.html.widgets import interact" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import networkx as nx" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# wrap a few graph generation functions so they have the same signature\n", "\n", "def random_lobster(n, m, k, p):\n", " return nx.random_lobster(n, p, p / m)\n", "\n", "def powerlaw_cluster(n, m, k, p):\n", " return nx.powerlaw_cluster_graph(n, m, p)\n", "\n", "def erdos_renyi(n, m, k, p):\n", " return nx.erdos_renyi_graph(n, p)\n", "\n", "def newman_watts_strogatz(n, m, k, p):\n", " return nx.newman_watts_strogatz_graph(n, k, p)\n", "\n", "def plot_random_graph(n, m, k, p, generator):\n", " g = generator(n, m, k, p)\n", " nx.draw(g)\n", " plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "interact(plot_random_graph, n=(2,30), m=(1,10), k=(1,10), p=(0.0, 1.0, 0.001),\n", " generator={'lobster': random_lobster,\n", " 'power law': powerlaw_cluster,\n", " 'Newman-Watts-Strogatz': newman_watts_strogatz,\n", " u'Erdős-Rényi': erdos_renyi,\n", " });" ] } ], "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.4.0" } }, "nbformat": 4, "nbformat_minor": 0 }