{ "cells": [ { "cell_type": "markdown", "id": "f2d7b3c9", "metadata": {}, "source": [ "# NetworkX: Network Analysis in Python\n", "\n", "\n", "
\n", "\"networkx\n", "
\n", "\n", "\n", "\n", "[**NetworkX**](https://networkx.org/) is an open-source Python library designed for creating, manipulating, and analyzing networks made up of nodes and edges. It provides an extensive array of tools and algorithms for general graph operations, catering to both directed and undirected graphs. Beyond basic graph creation and visualization, NetworkX supports a multitude of graph algorithms, ranging from shortest path computations to more complex structural evaluations, making it a versatile toolkit for researchers, developers, and data scientists working in the domain of network analysis.\n", "\n", "On the other hand, CORNETO's `graph` class has a specialized focus. While NetworkX is geared towards using standard graph methods, CORNETO is all about building optimization problems using graphs. The `Graph` class implemented in CORNETO is more flexible, and supports directed, undirected edges, parallel edges, hyperedges, and all mixed in the same graph.\n", "\n", "Despite the differences in their primary uses, CORNETO and NetworkX can work together. Here, we will see an example of this interoperability between CORNETO and NetworkX." ] }, { "cell_type": "code", "execution_count": 1, "id": "6339b26f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Shortest paths from node 1:\n", "Node 1 to node 1: Path: [1] with total weight: 0\n", "Node 1 to node 2: Path: [1, 2] with total weight: 1\n", "Node 1 to node 3: Path: [1, 2, 3] with total weight: 3\n", "Node 1 to node 5: Path: [1, 2, 3, 4, 5] with total weight: 7\n", "Node 1 to node 4: Path: [1, 2, 3, 4] with total weight: 4\n" ] } ], "source": [ "# Create a networkx graph\n", "import networkx as nx\n", "\n", "# Create a graph object\n", "G = nx.Graph()\n", "\n", "# Add nodes\n", "G.add_nodes_from([1, 2, 3, 4, 5])\n", "\n", "# Add edges with weights\n", "G.add_edge(1, 2, weight=1)\n", "G.add_edge(1, 3, weight=4)\n", "G.add_edge(2, 3, weight=2)\n", "G.add_edge(3, 4, weight=1)\n", "G.add_edge(4, 5, weight=3)\n", "G.add_edge(1, 5, weight=8)\n", "G.add_edge(2, 4, weight=5)\n", "\n", "# Compute shortest paths from node 1 to all other nodes\n", "shortest_paths = nx.single_source_dijkstra_path(G, source=1)\n", "shortest_path_lengths = nx.single_source_dijkstra_path_length(G, source=1)\n", "\n", "# Print the paths and their lengths\n", "print(\"Shortest paths from node 1:\")\n", "for target, path in shortest_paths.items():\n", " print(\n", " f\"Node 1 to node {target}: Path: {path} with total weight: {shortest_path_lengths[target]}\"\n", " )" ] }, { "cell_type": "code", "execution_count": 2, "id": "0587de4a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(5, 7)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Create a corneto graph\n", "import corneto as cn\n", "\n", "Gc = cn.Graph()\n", "Gc.add_edge(1, 2, weight=1)\n", "Gc.add_edge(1, 3, weight=4)\n", "Gc.add_edge(2, 3, weight=2)\n", "Gc.add_edge(3, 4, weight=1)\n", "Gc.add_edge(4, 5, weight=3)\n", "Gc.add_edge(1, 5, weight=8)\n", "Gc.add_edge(2, 4, weight=5)\n", "Gc.shape" ] }, { "cell_type": "code", "execution_count": 3, "id": "97fe4b48", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "NodeView((1, 2, 3, 4, 5))" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from corneto.contrib.networkx import corneto_graph_to_networkx\n", "\n", "Gcn = corneto_graph_to_networkx(Gc)\n", "Gcn.nodes()" ] }, { "cell_type": "markdown", "id": "1c1f0f81", "metadata": {}, "source": [ "Corneto automatically transforms the graph to a networkx graph when using the networkx API" ] }, { "cell_type": "code", "execution_count": 4, "id": "89bbe585", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Shortest paths from node 1:\n", "Node 1 to node 1: Path: [1] with total weight: 0\n", "Node 1 to node 2: Path: [1, 2] with total weight: 1\n", "Node 1 to node 3: Path: [1, 2, 3] with total weight: 3\n", "Node 1 to node 5: Path: [1, 2, 3, 4, 5] with total weight: 7\n", "Node 1 to node 4: Path: [1, 2, 3, 4] with total weight: 4\n" ] } ], "source": [ "from corneto.contrib.networkx import networkx as nxc\n", "\n", "# NOTE: everytime a corneto graph is passed to a networkx function, it is converted to a networkx graph\n", "# Think about converting first the graph to a networkx graph and then using networkx functions for better performance\n", "shortest_paths = nxc.single_source_dijkstra_path(Gc, source=1)\n", "shortest_path_lengths = nxc.single_source_dijkstra_path_length(Gc, source=1)\n", "\n", "# Print the paths and their lengths\n", "print(\"Shortest paths from node 1:\")\n", "for target, path in shortest_paths.items():\n", " print(\n", " f\"Node 1 to node {target}: Path: {path} with total weight: {shortest_path_lengths[target]}\"\n", " )" ] }, { "cell_type": "code", "execution_count": 6, "id": "2c273941", "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "e_0_source\n", "\n", "\n", "\n", "\n", "A\n", "\n", "A\n", "\n", "\n", "\n", "e_0_source->A\n", "\n", "\n", "\n", "\n", "\n", "B\n", "\n", "B\n", "\n", "\n", "\n", "A->B\n", "\n", "\n", "\n", "\n", "\n", "e_2_target\n", "\n", "\n", "\n", "\n", "B->e_2_target\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "G = cn.Graph()\n", "G.add_edge((), \"A\")\n", "G.add_edge(\"A\", \"B\")\n", "G.add_edge(\"B\", ())\n", "G.plot()" ] }, { "cell_type": "code", "execution_count": 13, "id": "8f25f204", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "NodeView(('A', 'B'))" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "G_nx = corneto_graph_to_networkx(G, skip_unsupported_edges=True)\n", "G_nx.nodes()" ] } ], "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" }, "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": 5 }