1
0
mirror of https://github.com/gsi-upm/sitc synced 2024-11-17 20:12:28 +00:00
sitc/sna/2_Working_with_Graphs.ipynb

1014 lines
45 KiB
Plaintext
Raw Permalink Normal View History

2024-04-17 15:23:28 +00:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"![](images/EscUpmPolit_p.gif \"UPM\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"# Course Notes for Learning Intelligent Systems"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"Department of Telematic Engineering Systems, Universidad Politécnica de Madrid, © Carlos A. Iglesias"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"## [Introduction to Network Analysis](0_Intro_Network_Analysis.ipynb)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"# Table of Contents\n",
"\n",
"* [Working with Graphs](#Working-with-Graphs)\n",
"* [First steps](#First-steps)\n",
"* [Reading Data from a File](#Reading-Data-from-a-File)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Working with Graphs\n",
"\n",
"\n",
"## Import networkx\n",
"The first thing is importing the package.\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"import networkx as nx"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Graph"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"G = nx.Graph()"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Nodes\n",
"NetworkX is very flexible. Nodes can be any hashable object, such as strings, numbers, files, functions, etc."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"G.add_node(1) # integer"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"G.add_node('A') #string"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"import math\n",
"G.add_node(math.cos) #cosine function"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 'A', <built-in function cos>]\n"
]
}
],
"source": [
"print(G.nodes)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Edges\n",
"Edges (links) are represented as tuples of nodes."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"G.add_edge(1, 'A')"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"G.add_edge('B', math.cos)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[(1, 'A'), (<built-in function cos>, 'B')]\n"
]
}
],
"source": [
"print(G.edges)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"## Edge data\n",
"\n",
"Edge data is assigned using a tuple (node1, node2, data). \n",
"\n",
"Default data is {} (empty dictionary), but any Python object is allowed."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"W = nx.Graph()"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"W.add_edge('A', 'B', weight = 1)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"W.add_weighted_edges_from([('A', 'C', 3), ('A', 'D', 4), ('B', 'D', 2)])"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
"text/plain": [
"{('A', 'B'): Text(0.34360816042257025, 0.03772295893763749, '1'),\n",
" ('A', 'C'): Text(-0.6125262066395303, 0.16919086402801908, '3'),\n",
" ('A', 'D'): Text(0.04386563293789962, -0.17119492412470283, '4'),\n",
" ('B', 'D'): Text(0.6125262066395304, -0.16919086402801906, '2')}"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAV0AAADnCAYAAAC9roUQAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8qNh9FAAAACXBIWXMAAAsTAAALEwEAmpwYAAAr80lEQVR4nO3deXxM5/4H8M85s5isY4kkKkiUkFiili4StVRRSxFbGzv5WVtub12t25bS6163V+u2imhvEYJKlaqtVItKLCUVscVSgkQWSibbjFnO8/tjSByTTLaZOTPJ931ffU36PWee853ck2/PPOd5nsMxxkAIIcQxeKkTIISQ2oSKLiGEOBAVXUIIcSAquoQQ4kBUdAkhxIHk1jb6+PiwwMBAB6VCCCE1Q1JS0l3GWMPStlktuoGBgTh16pR9siKEkBqK47gbZW2j7gVCCHEgKrqEEOJAVrsXHEkQDNDrs8GYHhynhFLpB55XSJ0WIYTYlGRFlzEGjSYRWVlrodEkQKe7Do5TgON4MCaAMQNUqiCo1RHw958ItTocHMdJlS4hhNiEw4suYwzZ2XFIS1sAvT4HglA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BMC8vGTMgBj0DLJc15cQ4rw4jktijHUudRsVXedRoC9Ay+UtkVUg7ued0GEClr68FA3cG5TxTkKIM7FWdOlGmpNgjOHS3Uulzkhbl7wOrVe0xoYzG2jGGiEujoquk+A4Dp2e6oTUN1IxtdNUi+13i+5i3Pfj8PKGl3H13lUJMiSE2AIVXSdTV1UXMQNjkDAxAaENQy22/3z9Z7Rb1Q7/PPJP6E16CTIkhFQHFV0nFd40HKennsY/ev6j1CcTv/f
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"pos = nx.spring_layout(W) # positions for all nodes\n",
"labels = nx.get_edge_attributes(W,'weight')\n",
"nx.draw_networkx(W, pos=pos, node_size=800, node_color='y', font_size=20)\n",
"nx.draw_networkx_edges(W,pos,width=4, edge_color='g', arrows=False)\n",
"nx.draw_networkx_edge_labels(W,pos,edge_labels=labels, font_color='brown', font_size=15)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"For more info about draw_networkx see the [NetworkX manual](https://networkx.github.io/documentation/networkx-1.10/reference/generated/networkx.drawing.nx_pylab.draw_networkx.html#networkx.drawing.nx_pylab.draw_networkx)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"Let's access edges"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[('A', 'B'), ('A', 'C'), ('A', 'D'), ('B', 'D')]\n"
]
}
],
"source": [
"print(W.edges) # List of all edges; same than W.edges() with default parameters"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[('A', 'B', {'weight': 1}), ('A', 'C', {'weight': 3}), ('A', 'D', {'weight': 4}), ('B', 'D', {'weight': 2})]\n"
]
}
],
"source": [
"print(W.edges(data=True)) # List of all edges with data"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[('A', 'B', 1), ('A', 'C', 3), ('A', 'D', 4), ('B', 'D', 2)]\n"
]
}
],
"source": [
"print(W.edges(data='weight')) # List of all edges with attribute 'weight'"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"Let's calculate Dijsktra shortest weighted path:"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"data": {
"text/plain": [
"{('A', 'B'): Text(-0.17186208433735098, -0.2876883893655454, '1'),\n",
" ('A', 'C'): Text(0.10060091197009038, 0.6027184602573036, '3'),\n",
" ('A', 'D'): Text(0.1315138115456491, -0.10959315037715107, '4'),\n",
" ('B', 'D'): Text(-0.10060091197009048, -0.6027184602573037, '2')}"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAV0AAADnCAYAAAC9roUQAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8qNh9FAAAACXBIWXMAAAsTAAALEwEAmpwYAAAhvElEQVR4nO3de3xU1b338c9kSIYEJYa7F25q4rFgTQ+UqtVqjxe0aqutbU8btBet9hGMgpUjz9PnvHrzgFih0uLT1mqLBU5ba4t6bI23Wi9HS0HAHqMmIIgKESERgQkTmJnnj18Cwzh7ZzIze09m5vt+veYVZs+ePUtJvqysvdZvBeLxOCIi4o+yfDdARKSUKHRFRHyk0BUR8ZFCV0TERwpdEREfDXB7cdiwYfFx48b51BQRkeKwevXq7fF4fHiq11xDd9y4caxatcqbVomIFKlAIPCG02saXhAR8ZFC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1ak5aLM40vCDSb20BFgI/BXbboVlYPVyHsguPOFzpC90PV6EQzJzZ51ZK36inK9LvtGKbRY7H9jTbffCleqwAea5/csvKYMIEq80rnlLoivQbLwJfBP4JuAvoSnHO4bD0GxAamNuPDlXAsmW5vaakpNAVyas48BS2edkkbO5BqmI2I4D/ADZD3c9h3q1QVZWbJlQB84JQuzc31xNXCl2RvIhhcw1OBT4JPOpw3njgTqyawhzgCDvc2AjTp2cfvFXYXbbGPd3tWJvd9aRXCl0RX+3D5hhMBC4F/uZw3knYfIMW4H8BKZb/zp8Pc+fa0t2yPv4ol5VBZRDmArf2HNwB/Au2gZp4RaEr4os9wCJsUe5XsdmzqZwO/Be2lOHL9DrBqLHRauBOmmS
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"pos = nx.spring_layout(W) # positions for all nodes\n",
"labels = nx.get_edge_attributes(W,'weight')\n",
"nx.draw_networkx(W, pos=pos, node_color='red', node_size=600)\n",
"nx.draw_networkx_edges(W,pos,width=4, edge_color='yellow', arrows=False)\n",
"nx.draw_networkx_edge_labels(W,pos,edge_labels=labels, font_color='brown', font_size=15)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"['C', 'A', 'B', 'D']"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nx.dijkstra_path(W, 'C', 'D')"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## NetworkX design principles\n",
"* \"Node centric\" view of networks\n",
"* Nodes: any hashable object\n",
"* Edges: three tuples (n1, n2, d) with optional edge data\n",
"* Edge data can be defined by users"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"## Network data structure\n",
"* Uses a \"dictionary of dictionaries\""
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"* **G.adj** is the Graph adjacency object: \n",
" * **key**: node; \n",
" * **value**: neighbor-dicts"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'A': {'B': {'weight': 1}, 'C': {'weight': 3}, 'D': {'weight': 4}}, 'B': {'A': {'weight': 1}, 'D': {'weight': 2}}, 'C': {'A': {'weight': 3}}, 'D': {'A': {'weight': 4}, 'B': {'weight': 2}}}\n"
]
}
],
"source": [
"print(W.adj)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Simple operations\n",
"In this way, it is easy..."
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Test if a node is in the graph\n",
"'A' in W"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"A\n",
"B\n",
"C\n",
"D\n"
]
}
],
"source": [
"# loop over all the nodes\n",
"for n in W:\n",
" print(n)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Edge attributes"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"{'weight': 1}"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Access an edge\n",
"\n",
"W['A']['B']"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"{'weight': 1}"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"W.adj['A']['B']"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Access an attribute\n",
"W['A']['B']['weight']"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"## Multigraphs"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"MG = nx.MultiGraph()\n",
"MG.add_edge('A', 'B', relation = 'friend', color = 'b', weight = 3)\n",
"MG.add_edge('A', 'B', relation = 'neighbour', color = 'r', weight = 8)\n",
"MG.add_edge('A', 'C', relation = 'father', color = 'c', weight = 1)\n",
"MG.add_edge('A', 'D', relation = 'friend', color = 'b', weight = 2)\n"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{0: {'relation': 'friend', 'color': 'b', 'weight': 3}, 1: {'relation': 'neighbour', 'color': 'r', 'weight': 8}}\n"
]
}
],
"source": [
"#Dictionary per every edge\n",
"print(MG['A']['B'])"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Node attributes"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"G = nx.Graph()"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"G.add_edge('Predeal', 'Bucharest', distance=158)"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"G.add_edge('Predeal', 'Brasov', distance=26.7)"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"G.add_node('Predeal', population=4755)"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"G.add_node('Bucharest', population=1812290)"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"G.add_node('Brasov', population=550547)"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['Predeal', 'Bucharest', 'Brasov']\n"
]
}
],
"source": [
"# List of nodes \n",
"print(G.nodes())"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[('Predeal', {'population': 4755}), ('Bucharest', {'population': 1812290}), ('Brasov', {'population': 550547})]\n"
]
}
],
"source": [
"#list of nodes attributes\n",
"print(G.nodes(data=True))"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4755\n"
]
}
],
"source": [
"#Access one attribute\n",
"print(G.nodes['Predeal']['population'])"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"# References\n",
"* Networkx tutorial https://networkx.github.io/documentation/stable/tutorial.html"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"## Licence"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"The notebook is freely licensed under under the [Creative Commons Attribution Share-Alike license](https://creativecommons.org/licenses/by/2.0/). \n",
"\n",
"© Carlos A. Iglesias, Universidad Politécnica de Madrid."
]
}
],
"metadata": {
"celltoolbar": "Slideshow",
"datacleaner": {
"position": {
"top": "50px"
},
"python": {
"varRefreshCmd": "try:\n print(_datacleaner.dataframe_metadata())\nexcept:\n print([])"
},
"window_display": false
},
"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.11.7"
},
"latex_envs": {
"LaTeX_envs_menu_present": true,
"autocomplete": true,
"bibliofile": "biblio.bib",
"cite_by": "apalike",
"current_citInitial": 1,
"eqLabelWithNumbers": true,
"eqNumInitial": 1,
"hotkeys": {
"equation": "Ctrl-E",
"itemize": "Ctrl-I"
},
"labels_anchors": false,
"latex_user_defs": false,
"report_style_numbering": false,
"user_envs_cfg": false
}
},
"nbformat": 4,
"nbformat_minor": 4
}