|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "## Undirected Graph" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "code", |
| 12 | + "execution_count": null, |
| 13 | + "metadata": {}, |
| 14 | + "outputs": [], |
| 15 | + "source": [ |
| 16 | + "import networkx as nx\n", |
| 17 | + "import matplotlib.pyplot as plt\n", |
| 18 | + "\n", |
| 19 | + "G = nx.Graph()\n", |
| 20 | + "V = {'Dublin', 'Paris', 'Milan', 'Rome'}\n", |
| 21 | + "E = [('Milan','Dublin'), ('Milan','Paris'), ('Paris','Dublin'), ('Milan','Rome')]\n", |
| 22 | + "G.add_nodes_from(V)\n", |
| 23 | + "G.add_edges_from(E)\n", |
| 24 | + "draw_graph(G, pos_nodes=nx.shell_layout(G), node_size=500)" |
| 25 | + ] |
| 26 | + }, |
| 27 | + { |
| 28 | + "cell_type": "code", |
| 29 | + "execution_count": null, |
| 30 | + "metadata": {}, |
| 31 | + "outputs": [], |
| 32 | + "source": [ |
| 33 | + "print(f\"V = {G.nodes}\")\n", |
| 34 | + "print(f\"E = {G.edges}\")" |
| 35 | + ] |
| 36 | + }, |
| 37 | + { |
| 38 | + "cell_type": "code", |
| 39 | + "execution_count": null, |
| 40 | + "metadata": {}, |
| 41 | + "outputs": [], |
| 42 | + "source": [ |
| 43 | + "{G.degree(v): v for v in G.nodes}" |
| 44 | + ] |
| 45 | + }, |
| 46 | + { |
| 47 | + "cell_type": "code", |
| 48 | + "execution_count": null, |
| 49 | + "metadata": { |
| 50 | + "scrolled": true |
| 51 | + }, |
| 52 | + "outputs": [], |
| 53 | + "source": [ |
| 54 | + "print(f\"Graph Order: {G.number_of_nodes()}\")\n", |
| 55 | + "print(f\"Graph Size: {G.number_of_edges()}\")\n", |
| 56 | + "print(f\"Degree for nodes: { {v: G.degree(v) for v in G.nodes} }\")\n", |
| 57 | + "print(f\"Neighbors for nodes: { {v: list(G.neighbors(v)) for v in G.nodes} }\")" |
| 58 | + ] |
| 59 | + }, |
| 60 | + { |
| 61 | + "cell_type": "code", |
| 62 | + "execution_count": null, |
| 63 | + "metadata": {}, |
| 64 | + "outputs": [], |
| 65 | + "source": [ |
| 66 | + "ego_graph_milan = nx.ego_graph(G, \"Milan\")\n", |
| 67 | + "print(f\"Nodes: {ego_graph_milan.nodes}\")\n", |
| 68 | + "print(f\"Edges: {ego_graph_milan.edges}\")" |
| 69 | + ] |
| 70 | + }, |
| 71 | + { |
| 72 | + "cell_type": "code", |
| 73 | + "execution_count": null, |
| 74 | + "metadata": {}, |
| 75 | + "outputs": [], |
| 76 | + "source": [ |
| 77 | + "new_nodes = {'London', 'Madrid'}\n", |
| 78 | + "new_edges = [('London','Rome'), ('Madrid','Paris')]\n", |
| 79 | + "G.add_nodes_from(new_nodes)\n", |
| 80 | + "G.add_edges_from(new_edges)\n", |
| 81 | + "print(f\"V = {G.nodes}\")\n", |
| 82 | + "print(f\"E = {G.edges}\")" |
| 83 | + ] |
| 84 | + }, |
| 85 | + { |
| 86 | + "cell_type": "code", |
| 87 | + "execution_count": null, |
| 88 | + "metadata": {}, |
| 89 | + "outputs": [], |
| 90 | + "source": [ |
| 91 | + "node_remove = {'London', 'Madrid'}\n", |
| 92 | + "G.remove_nodes_from(node_remove)\n", |
| 93 | + "print(f\"V = {G.nodes}\")\n", |
| 94 | + "print(f\"E = {G.edges}\")" |
| 95 | + ] |
| 96 | + }, |
| 97 | + { |
| 98 | + "cell_type": "code", |
| 99 | + "execution_count": null, |
| 100 | + "metadata": {}, |
| 101 | + "outputs": [], |
| 102 | + "source": [ |
| 103 | + "node_edges = [('Milan','Dublin'), ('Milan','Paris')]\n", |
| 104 | + "G.remove_edges_from(node_edges)\n", |
| 105 | + "print(f\"V = {G.nodes}\")\n", |
| 106 | + "print(f\"E = {G.edges}\")" |
| 107 | + ] |
| 108 | + }, |
| 109 | + { |
| 110 | + "cell_type": "code", |
| 111 | + "execution_count": null, |
| 112 | + "metadata": {}, |
| 113 | + "outputs": [], |
| 114 | + "source": [ |
| 115 | + "print(nx.to_edgelist(G))" |
| 116 | + ] |
| 117 | + }, |
| 118 | + { |
| 119 | + "cell_type": "code", |
| 120 | + "execution_count": null, |
| 121 | + "metadata": {}, |
| 122 | + "outputs": [], |
| 123 | + "source": [ |
| 124 | + "print(nx.to_pandas_adjacency(G))" |
| 125 | + ] |
| 126 | + }, |
| 127 | + { |
| 128 | + "cell_type": "markdown", |
| 129 | + "metadata": {}, |
| 130 | + "source": [ |
| 131 | + "## Directed Graph" |
| 132 | + ] |
| 133 | + }, |
| 134 | + { |
| 135 | + "cell_type": "code", |
| 136 | + "execution_count": null, |
| 137 | + "metadata": {}, |
| 138 | + "outputs": [], |
| 139 | + "source": [ |
| 140 | + "import networkx as nx\n", |
| 141 | + "G = nx.DiGraph()\n", |
| 142 | + "V = {'Dublin', 'Paris', 'Milan', 'Rome'}\n", |
| 143 | + "E = [('Milan','Dublin'), ('Paris','Milan'), ('Paris','Dublin'), ('Milan','Rome')]\n", |
| 144 | + "G.add_nodes_from(V)\n", |
| 145 | + "G.add_edges_from(E)\n", |
| 146 | + "print(nx.to_pandas_edgelist(G))\n", |
| 147 | + "print(nx.to_pandas_adjacency(G))" |
| 148 | + ] |
| 149 | + }, |
| 150 | + { |
| 151 | + "cell_type": "code", |
| 152 | + "execution_count": null, |
| 153 | + "metadata": {}, |
| 154 | + "outputs": [], |
| 155 | + "source": [ |
| 156 | + "print(f\"Indegree for nodes: { {v: G.in_degree(v) for v in G.nodes} }\")\n", |
| 157 | + "print(f\"Outegree for nodes: { {v: G.out_degree(v) for v in G.nodes} }\")" |
| 158 | + ] |
| 159 | + }, |
| 160 | + { |
| 161 | + "cell_type": "code", |
| 162 | + "execution_count": null, |
| 163 | + "metadata": {}, |
| 164 | + "outputs": [], |
| 165 | + "source": [ |
| 166 | + "draw_graph(G, pos_nodes=nx.shell_layout(G), node_size=500)" |
| 167 | + ] |
| 168 | + }, |
| 169 | + { |
| 170 | + "cell_type": "markdown", |
| 171 | + "metadata": {}, |
| 172 | + "source": [ |
| 173 | + "## Weighted Directed Graph" |
| 174 | + ] |
| 175 | + }, |
| 176 | + { |
| 177 | + "cell_type": "code", |
| 178 | + "execution_count": null, |
| 179 | + "metadata": {}, |
| 180 | + "outputs": [], |
| 181 | + "source": [ |
| 182 | + "import networkx as nx\n", |
| 183 | + "G = nx.MultiDiGraph()\n", |
| 184 | + "V = {'Paris', 'Dublin','Milan', 'Rome'}\n", |
| 185 | + "E = [ ('Paris','Dublin', 11), ('Paris','Milan', 8),\n", |
| 186 | + " ('Milan','Rome', 5),('Milan','Dublin', 19)]\n", |
| 187 | + "G.add_nodes_from(V)\n", |
| 188 | + "G.add_weighted_edges_from(E)\n", |
| 189 | + "draw_graph(G, pos_nodes=nx.shell_layout(G), node_size=500, plot_weight=True)\n", |
| 190 | + "print(nx.to_pandas_edgelist(G))\n", |
| 191 | + "print(nx.to_pandas_adjacency(G))" |
| 192 | + ] |
| 193 | + }, |
| 194 | + { |
| 195 | + "cell_type": "markdown", |
| 196 | + "metadata": {}, |
| 197 | + "source": [ |
| 198 | + "## Multi Graph" |
| 199 | + ] |
| 200 | + }, |
| 201 | + { |
| 202 | + "cell_type": "code", |
| 203 | + "execution_count": null, |
| 204 | + "metadata": {}, |
| 205 | + "outputs": [], |
| 206 | + "source": [ |
| 207 | + "import networkx as nx\n", |
| 208 | + "directed_multi_graph = nx.MultiDiGraph()\n", |
| 209 | + "V = {'Dublin', 'Paris', 'Milan', 'Rome'}\n", |
| 210 | + "E = [('Milan','Dublin'), ('Milan','Dublin'), ('Paris','Milan'), ('Paris','Dublin'), ('Milan','Rome'), ('Milan','Rome')]\n", |
| 211 | + "directed_multi_graph.add_nodes_from(V)\n", |
| 212 | + "directed_multi_graph.add_edges_from(E)\n", |
| 213 | + "\n", |
| 214 | + "draw_graph(G, pos_nodes=nx.shell_layout(G), node_size=500)" |
| 215 | + ] |
| 216 | + }, |
| 217 | + { |
| 218 | + "cell_type": "markdown", |
| 219 | + "metadata": {}, |
| 220 | + "source": [ |
| 221 | + "## Plot Graphs" |
| 222 | + ] |
| 223 | + }, |
| 224 | + { |
| 225 | + "cell_type": "code", |
| 226 | + "execution_count": null, |
| 227 | + "metadata": {}, |
| 228 | + "outputs": [], |
| 229 | + "source": [ |
| 230 | + "def draw_graph(G, pos_nodes, node_names={}, node_size=50, plot_weight=False):\n", |
| 231 | + " nx.draw(G, pos_nodes, with_labels=False, node_size=node_size, edge_color='gray', arrowsize=30)\n", |
| 232 | + " \n", |
| 233 | + " pos_attrs = {}\n", |
| 234 | + " for node, coords in pos_nodes.items():\n", |
| 235 | + " pos_attrs[node] = (coords[0], coords[1] + 0.08)\n", |
| 236 | + " \n", |
| 237 | + " nx.draw_networkx_labels(G, pos_attrs, font_family='serif', font_size=20)\n", |
| 238 | + " \n", |
| 239 | + " \n", |
| 240 | + " if plot_weight:\n", |
| 241 | + " pos_attrs = {}\n", |
| 242 | + " for node, coords in pos_nodes.items():\n", |
| 243 | + " pos_attrs[node] = (coords[0], coords[1] + 0.08)\n", |
| 244 | + " \n", |
| 245 | + " nx.draw_networkx_labels(G, pos_attrs, font_family='serif', font_size=20)\n", |
| 246 | + " edge_labels=dict([((a,b,),d[\"weight\"]) for a,b,d in G.edges(data=True)])\n", |
| 247 | + " nx.draw_networkx_edge_labels(G, pos_nodes, edge_labels=edge_labels)\n", |
| 248 | + " \n", |
| 249 | + " plt.axis('off')\n", |
| 250 | + " axis = plt.gca()\n", |
| 251 | + " axis.set_xlim([1.2*x for x in axis.get_xlim()])\n", |
| 252 | + " axis.set_ylim([1.2*y for y in axis.get_ylim()])" |
| 253 | + ] |
| 254 | + } |
| 255 | + ], |
| 256 | + "metadata": { |
| 257 | + "kernelspec": { |
| 258 | + "display_name": "Python 3", |
| 259 | + "language": "python", |
| 260 | + "name": "python3" |
| 261 | + }, |
| 262 | + "language_info": { |
| 263 | + "codemirror_mode": { |
| 264 | + "name": "ipython", |
| 265 | + "version": 3 |
| 266 | + }, |
| 267 | + "file_extension": ".py", |
| 268 | + "mimetype": "text/x-python", |
| 269 | + "name": "python", |
| 270 | + "nbconvert_exporter": "python", |
| 271 | + "pygments_lexer": "ipython3", |
| 272 | + "version": "3.6.8" |
| 273 | + } |
| 274 | + }, |
| 275 | + "nbformat": 4, |
| 276 | + "nbformat_minor": 4 |
| 277 | +} |
0 commit comments