{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "Pxkh0PMuE5i2"
      },
      "outputs": [],
      "source": [
        "import sys\n",
        "import networkx as nx\n",
        "import warnings\n",
        "warnings.filterwarnings('ignore')\n",
        "\n",
        "print(\"Python version:\", sys.version)\n",
        "print(\"NetworkX version:\", nx.__version__)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "mM9k_ZXnE5i4"
      },
      "outputs": [],
      "source": [
        "a = 2\n",
        "b = 3"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "vB19bk63E5i5"
      },
      "outputs": [],
      "source": [
        "print(a)\n",
        "print(b)"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "b = 7"
      ],
      "metadata": {
        "id": "kU1ncctR2b9q"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "Jh2emYcPE5i5"
      },
      "outputs": [],
      "source": []
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "F-JrvYNnE5i5"
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\n",
        "import networkx as nx\n",
        "\n",
        "plt.rcParams['figure.figsize'] = (16,10)\n",
        "\n",
        "G = nx.karate_club_graph()\n",
        "\n",
        "for n in G.nodes():\n",
        "    print(n, G.degree(n))\n",
        "\n",
        "nx.draw(G, with_labels = True)\n",
        "plt.show()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "I9wciHD8E5i5"
      },
      "outputs": [],
      "source": [
        "import networkx as nx\n",
        "import matplotlib.pyplot as plt\n",
        "import random\n",
        "\n",
        "G=nx.Graph()\n",
        "G.add_node(\"Student3\")\n",
        "G.add_edge(\"Student1\", \"Student2\")\n",
        "G.add_edge(\"Student3\", \"Student4\")\n",
        "G.add_edge(\"Student2\", \"Student4\")\n",
        "\n",
        "\n",
        "print(\"Edges list:\")\n",
        "for edge in G.edges():\n",
        "    print(edge)\n",
        "print()\n",
        "\n",
        "print(\"Adjacency list\")\n",
        "for node in G.nodes():\n",
        "    print(node, G[node])\n",
        "print()\n",
        "\n",
        "print(\"Adjacency matrix\")\n",
        "A = nx.adjacency_matrix(G)\n",
        "print(A.todense())"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "rfptIAM7E5i6"
      },
      "outputs": [],
      "source": [
        "import networkx as nx\n",
        "import matplotlib.pyplot as plt\n",
        "import random\n",
        "\n",
        "G=nx.Graph()\n",
        "p = 0.6\n",
        "people = {\"Alice\", \"Bob\", \"Chuck\", \"Dima\", \"Eve\"}\n",
        "\n",
        "color_map = []\n",
        "\n",
        "for p1 in people:\n",
        "    G.add_node(p1)\n",
        "    for p2 in people:\n",
        "        if p1 > p2:\n",
        "            coin = random.uniform(0, 1)\n",
        "            if coin <= p:\n",
        "                G.add_edge(p1, p2)\n",
        "\n",
        "for node in G.nodes():\n",
        "    if G.degree(node) >= 2:\n",
        "        color_map.append(\"red\")\n",
        "    elif G.degree(node) >= 1:\n",
        "        color_map.append(\"orange\")\n",
        "    else:\n",
        "        color_map.append(\"yellow\")\n",
        "\n",
        "\n",
        "nx.draw(G, node_color = color_map, with_labels = True, node_size=1200)\n",
        "plt.show()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "JB7Z4C91E5i7"
      },
      "outputs": [],
      "source": [
        "import networkx as nx\n",
        "\n",
        "G=nx.Graph()\n",
        "G.add_nodes_from([2,3,4,5])\n",
        "\n",
        "nx.draw(G, with_labels = True, node_size=1000)\n",
        "plt.show()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "b2wvKRmIE5i7"
      },
      "outputs": [],
      "source": [
        "import networkx as nx\n",
        "\n",
        "G = nx.Graph()\n",
        "G.add_edges_from([(1,2),\n",
        "                  (1,3),\n",
        "                  (3,4),\n",
        "                  (1,5),\n",
        "                  (3,5),\n",
        "                  (4,2),\n",
        "                  (2,3),\n",
        "                  (1,7),\n",
        "                  (7,8),\n",
        "                  (3,6)])\n",
        "\n",
        "\n",
        "nx.draw(G, with_labels=True, font_weight='bold')\n",
        "plt.show()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "GVjFkBWpE5i7"
      },
      "outputs": [],
      "source": [
        "import collections\n",
        "import matplotlib.pyplot as plt\n",
        "import networkx as nx\n",
        "\n",
        "degree_sequence = sorted([d for n, d in G.degree()], reverse=True)\n",
        "degreeCount = collections.Counter(degree_sequence)\n",
        "deg, cnt = zip(*degreeCount.items())\n",
        "\n",
        "fig, ax = plt.subplots()\n",
        "plt.bar(deg, cnt, width=0.60, color='g')\n",
        "plt.title(\"Degree Histogram\")\n",
        "plt.ylabel(\"Count\")\n",
        "plt.xlabel(\"Degree\")\n",
        "\n",
        "ax.set_xticks(deg)\n",
        "ax.set_xticklabels(deg)\n",
        "\n",
        "# draw graph in inset\n",
        "plt.axes([0.4, 0.4, 0.5, 0.5])\n",
        "pos = nx.spring_layout(G)\n",
        "plt.axis('off')\n",
        "\n",
        "\n",
        "plt.show()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "2Il8YCNkE5i8"
      },
      "outputs": [],
      "source": [
        "CC = nx.clustering(G)\n",
        "for node in CC:\n",
        "    print(node, \"\\t\", CC[node])\n",
        "\n",
        "nx.draw(G, with_labels=True, font_weight='bold')\n",
        "plt.show()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "d_76yq6KE5i8"
      },
      "outputs": [],
      "source": [
        "import networkx as nx\n",
        "\n",
        "G = nx.Graph()\n",
        "G.add_edge(1,2,color='r',weight=5)\n",
        "G.add_edge(2,3,color='b',weight=10)\n",
        "G.add_edge(3,4,color='g',weight=15)\n",
        "\n",
        "pos = nx.circular_layout(G)\n",
        "\n",
        "edges = G.edges()\n",
        "colors = [G[u][v]['color'] for u,v in edges]\n",
        "weights = [G[u][v]['weight'] for u,v in edges]\n",
        "\n",
        "nx.draw(G, pos, with_labels=True, edge_color=colors, width=weights)\n",
        "\n",
        "plt.show()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "BGEzmW4jE5i8"
      },
      "outputs": [],
      "source": [
        "import networkx as nx\n",
        "import numpy as np\n",
        "import matplotlib.pyplot as plt\n",
        "import pylab\n",
        "\n",
        "G = nx.DiGraph()\n",
        "\n",
        "G.add_edges_from([('A','B'),('C','D'),('G','D')], weight=1)\n",
        "G.add_edges_from([('D','A'),('D','E'),('B','D'),('D','E')], weight=2)\n",
        "G.add_edges_from([('B','C'),('E','F')], weight=3)\n",
        "G.add_edges_from([('C','F')], weight=4)\n",
        "\n",
        "\n",
        "\n",
        "edge_labels = dict([((u,v,),d['weight']) for u,v,d in G.edges(data=True)])\n",
        "special_edges = [('C','D'),('D','A')]\n",
        "edge_colors = ['black' if not edge in special_edges else 'red' for edge in G.edges()]\n",
        "\n",
        "pos=nx.kamada_kawai_layout(G)\n",
        "nx.draw_networkx_edge_labels(G,pos,edge_labels=edge_labels)\n",
        "nx.draw(G, pos, with_labels=True, font_weight='bold', node_size=450, edge_color=edge_colors)\n",
        "pylab.show()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "fmcD6eVNE5i9"
      },
      "outputs": [],
      "source": [
        "# Author: Aric Hagberg (hagberg@lanl.gov)\n",
        "\n",
        "import matplotlib.pyplot as plt\n",
        "import networkx as nx\n",
        "\n",
        "G = nx.star_graph(20)\n",
        "pos = nx.spring_layout(G)\n",
        "colors = range(20)\n",
        "nx.draw(G, pos, node_color='#A0CBE2', edge_color=colors,\n",
        "        width=4, edge_cmap=plt.cm.Blues, with_labels=False)\n",
        "plt.show()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": true,
        "id": "LKlOwfRLE5i9"
      },
      "outputs": [],
      "source": [
        "\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.8.8"
    },
    "colab": {
      "provenance": []
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}