{ "metadata": { "name": "", "signature": "sha256:cdd7ce0cdc54e2a5fe548627d10cb732c60e431c7b213aeb8403c3f56872bb66" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Network Models: Small World" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "plt.xkcd()\n", "import networkx as nx\n", "%matplotlib inline" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "During this seminar we will work with **Watts and Strogatz model**.\n", "Again, the idea of the model:\n", "\n", "1. Start with regular lattice with N nodes, K nearest neighbours\n", "2. For each node take every edge and rewire it with probability $p$, assuming that there is no loops and edge duplications" ] }, { "cell_type": "heading", "level": 5, "metadata": {}, "source": [ "Task" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " 1. Implement *rewind(G, p)* function that takes graph G and probability p as input. The function should produce a graph after step 2 of the model above.\n", " 2. Run experiments for various values of p, compute and show \n", " * average path length \n", " * clustering coefficients (transitivity) \n", " * degree distribution" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def gen_regular_graph(n, k):\n", " G = nx.Graph()\n", " nodes = list(range(n))\n", " for j in range(1, k // 2+1):\n", " targets = nodes[j:] + nodes[0:j] # first j nodes are now last in list\n", " G.add_edges_from(zip(nodes,targets))\n", " return G\n", "\n", "def degree_distr(k, bins = 100):\n", " yh, binEdges = np.histogram(k, bins)\n", " bincenters = 0.5*(binEdges[1:]+binEdges[:-1])\n", " return yh, bincenters" ], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }