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
Carlos A. Iglesias 2c53b81299
Uploaded SNA files
2024-04-17 17:23:28 +02:00

1014 lines
45 KiB
Plaintext

{
"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+PiwwMBAB6VCCCE1Q1JS0l3GWMPStlktuoGBgTh16pR9siKEkBqK47gbZW2j7gVCCHEgKrqEEOJAVrsXHEkQDNDrs8GYHhynhFLpB55XSJ0WIYTYlGRFlzEGjSYRWVlrodEkQKe7Do5TgON4MCaAMQNUqiCo1RHw958ItTocHMdJlS4hhNiEw4suYwz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V+8PbuapvGiMNQny4hNsRxHAIDF4LnPex6HJ73QGDgQlrsxgVR0SXExvz8xsDLqxM4zj5fJDlODi+vzvDzG22X9ol9UdElxMY4jkNIyEbwvJtd2ud5N4SGbqSrXBdFRZcQO1CpAtC+/V7wvLtN2+V5d7Rvvxd16jS2abvEcajoEmInanU4wsL2QybzqnZXA8fJIZN5ISxsPw0Tc3FUdAmxI7U6HF26XIC3d9cq31zjeQ94e5vboYLr+qjoEmJnKlUAOnQ4hODgmIczyDwAlNcfy4HnPaBSNUdwcAw6dDgIlSrAEekSO6NxuoQ4AMdx8PcfAz+/0cjLO4rMzDXQaBKh0117uBwkD0AAYwaoVM2hVoejUaNJ8PbuSjfMahgquoQ4EMeZp+4+6iYQBAP0+uyHy0IqoVT60VoKNRwVXUIkZF4kh7oNahPq0yWEEAeioksIIQ7EsbKe7wyA47g7AG44Lh1CCKkRmjHGGpa2wWrRJYQQYlvUvUAIIQ5ERZcQQhyIii4hhDgQFV1CCHEgKrqEEOJA/w/dAkV3YSANyQAAAABJRU5ErkJggg==\n",
"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": "iVBORw0KGgoAAAANSUhEUgAAAV0AAADnCAYAAAC9roUQAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8qNh9F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65Ik1Eiom3odvaakt7k1eapXBJd2P2YLNOm9L9jEps4dkUsH6y295V1X1pvYhIznkbupGI1VJIwwpsTDcKPIDtzdCM3fN3FaiCyG3Apd1na1xVRPovbwcpQyErXtMHQQ5WQ3g2nTfEB0BoMlbRSoErIv2btz3d2to+L4iIAw9iw7RprSHr7LTPEREpAN6Gbk2NPbZt6/XUi7HebQC7pbWENDfHGTIkR3V3RUS85/0cqKlTIeheOnETVrlgN1Yr7H+wxWa9Cgbt+iIiBcL70J01y8Z2vRAKwcyZ3lxbRMQD3odufb0VIM/1ViplZXbdPu8oISKSP/4ssVq6NPe93VAIli3L7TVFRDzmT+jW1dmOD1VVubleVZVdT7MWRKTA+FdMoLERpk/PPnirqmDGDIcte0RE+jd/K7jMnw9z59rS4L6O8ZaV2fvmzoVbb/WmfSIiHvO/bFZjo229PmmS9Vp7mU5GMGjnTZoE69aphysiBS0/tQrr6mDlSnjuOduKfeRIKC+HwYOhutq+lpfb8YYGO2/lSo3hikjBy+8eMfX1sGSJ/bmjw6qSRSI2M6G2VivNRKTo9J+NuWpqYMqUfLdCRMRT2gpBRMRHCl0RER8pdEVEfKTQFRHxUSDusrNDIBB4F3jDv+aIiBSFsfF4fHiqF1xDV0REckvDCyIiPlLoioj4SKErIuIjha6IiI8UuiIiPvr/EmLLRe/Q7Q4AAAAASUVORK5CYII=\n",
"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
}