{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Models" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook shows how to load some graphs based on simple models." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "pycharm": { "is_executing": false }, "scrolled": true }, "outputs": [], "source": [ "from IPython.display import SVG" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "pycharm": { "is_executing": false } }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "pycharm": { "is_executing": false } }, "outputs": [], "source": [ "from sknetwork.data import erdos_renyi, block_model, linear_graph, cyclic_graph, linear_digraph, cyclic_digraph, grid, albert_barabasi, watts_strogatz\n", "from sknetwork.visualization import visualize_graph" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Erdos-Renyi model" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "adjacency = erdos_renyi(20, 0.2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image = visualize_graph(adjacency)\n", "SVG(image)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Stochastic block model" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "graph = block_model([20,25,30], p_in=[0.5,0.4,0.3], p_out=0.02, metadata=True)\n", "adjacency = graph.adjacency\n", "labels = graph.labels" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image = visualize_graph(adjacency, labels=labels)\n", "SVG(image)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Linear graph" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "graph = linear_graph(8, metadata=True)\n", "adjacency = graph.adjacency\n", "position = graph.position" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image = visualize_graph(adjacency, position)\n", "SVG(image)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# adjacency matrix only\n", "adjacency = linear_graph(8)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# directed\n", "graph = linear_digraph(8, metadata=True)\n", "adjacency = graph.adjacency\n", "position = graph.position" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image = visualize_graph(adjacency, position)\n", "SVG(image)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Cyclic graph" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "graph = cyclic_graph(8, metadata=True)\n", "adjacency = graph.adjacency\n", "position = graph.position" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image = visualize_graph(adjacency, position, width=200, height=200)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "SVG(image)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Grid" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "graph = grid(6, 4, metadata=True)\n", "adjacency = graph.adjacency\n", "position = graph.position" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image = visualize_graph(adjacency, position)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "SVG(image)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Albert-Barabasi model" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "adjacency = albert_barabasi(n=100, degree=3)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image = visualize_graph(adjacency, labels={i:0 for i in range(3)}, display_node_weight=True, node_order=np.flip(np.arange(100)))\n", "SVG(image)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Watts-Strogatz model" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "adjacency = watts_strogatz(n=100, degree=6, prob=0.2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image = visualize_graph(adjacency, display_node_weight=True, node_size_max=10)\n", "SVG(image)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.13" }, "pycharm": { "stem_cell": { "cell_type": "raw", "metadata": { "collapsed": false }, "source": [] } }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }