{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# NetworkGraph Examples\n", "\n", "The `NetworkGraph` class in the `power_grid_model_ds` library provides a comprehensive framework for managing and analyzing network graphs. This documentation covers the usage of the `NetworkGraph` class, including graph initialization, node and branch operations, and various graph analysis functions.\n", "\n", "## Graph Initialization\n", "\n", "To initialize a `NetworkGraph`, simply create an instance of the class. This will create an empty graph structure ready to be populated with nodes and branches.\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from power_grid_model_ds.graph_models import RustworkxGraphModel\n", "\n", "graph = RustworkxGraphModel()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Node and Branch Operations\n", "\n", "### Adding Nodes and Branches\n", "\n", "You can add nodes and branches to the graph using the add_node and add_branch methods.\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# Adding nodes\n", "graph.add_node(1)\n", "graph.add_node(2)\n", "\n", "# Adding a branch between the nodes\n", "graph.add_branch(1, 2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Deleting Nodes and Branches\n", "\n", "Nodes and branches can be deleted from the graph using the delete_node and delete_branch methods.\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# Deleting a branch\n", "graph.delete_branch(1, 2)\n", "\n", "# Deleting a node\n", "graph.delete_node(1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Graph Analysis Functions\n", "\n", "### Shortest Path\n", "\n", "The get_shortest_path method calculates the shortest path between two nodes in the graph.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "graph = RustworkxGraphModel()\n", "\n", "for node in range(1, 6):\n", " graph.add_node(node)\n", "\n", "# Adding branches to form a circular network\n", "graph.add_branch(1, 2)\n", "graph.add_branch(2, 3)\n", "graph.add_branch(3, 4)\n", "graph.add_branch(4, 5)\n", "graph.add_branch(5, 1)\n", "\n", "# Calculating the shortest path from node 1 to node 3\n", "path, length = graph.get_shortest_path(1, 3)\n", "print(f\"Shortest path: {path}, Length: {length}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### All Paths\n", "\n", "The get_all_paths method retrieves all possible paths between two nodes in the graph.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Retrieving all paths from node 1 to node 3\n", "paths = graph.get_all_paths(1, 3)\n", "print(f\"All paths from 1 to 3: {paths}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Connected Components\n", "\n", "The get_components method identifies all connected components in the graph, starting from a set of nodes.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "\n", "# Adding branches to form two separate routes\n", "graph.add_branch(1, 2)\n", "graph.add_branch(2, 3)\n", "graph.add_branch(1, 5)\n", "graph.add_branch(5, 4)\n", "\n", "substation_nodes = np.array([1])\n", "components = graph.get_components(substation_nodes=substation_nodes)\n", "print(f\"Connected components: {components}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Connected Nodes\n", "\n", "The get_connected method retrieves all nodes connected to a given node. You can include the node itself in the results by setting inclusive=True.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Getting nodes connected to node 1, exclusive of node 1\n", "connected_nodes = graph.get_connected(node_id=1)\n", "print(f\"Nodes connected to 1: {connected_nodes}\")\n", "\n", "# Getting nodes connected to node 1, inclusive of node 1\n", "connected_nodes = graph.get_connected(node_id=1, inclusive=True)\n", "print(f\"Nodes connected to 1 (inclusive): {connected_nodes}\")" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "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.12.6" } }, "nbformat": 4, "nbformat_minor": 2 }