NetworkGraph Examples

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.

Graph Initialization

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.

from power_grid_model_ds.graph_models import RustworkxGraphModel

graph = RustworkxGraphModel()

Node and Branch Operations

Adding Nodes and Branches

You can add nodes and branches to the graph using the add_node and add_branch methods.

# Adding nodes
graph.add_node(1)
graph.add_node(2)

# Adding a branch between the nodes
graph.add_branch(1, 2)

Deleting Nodes and Branches

Nodes and branches can be deleted from the graph using the delete_node and delete_branch methods.

# Deleting a branch
graph.delete_branch(1, 2)

# Deleting a node
graph.delete_node(1)

Graph Analysis Functions

Shortest Path

The get_shortest_path method calculates the shortest path between two nodes in the graph.

graph = RustworkxGraphModel()

for node in range(1, 6):
    graph.add_node(node)

# Adding branches to form a circular network
graph.add_branch(1, 2)
graph.add_branch(2, 3)
graph.add_branch(3, 4)
graph.add_branch(4, 5)
graph.add_branch(5, 1)

# Calculating the shortest path from node 1 to node 3
path, length = graph.get_shortest_path(1, 3)
print(f"Shortest path: {path}, Length: {length}")
Shortest path: [1, 2, 3], Length: 2

All Paths

The get_all_paths method retrieves all possible paths between two nodes in the graph.

# Retrieving all paths from node 1 to node 3
paths = graph.get_all_paths(1, 3)
print(f"All paths from 1 to 3: {paths}")
All paths from 1 to 3: [[1, 2, 3], [1, 5, 4, 3]]

Connected Components

The get_components method identifies all connected components in the graph, starting from a set of nodes.

import numpy as np

# Adding branches to form two separate routes
graph.add_branch(1, 2)
graph.add_branch(2, 3)
graph.add_branch(1, 5)
graph.add_branch(5, 4)

substation_nodes = np.array([1])
components = graph.get_components(substation_nodes=substation_nodes)
print(f"Connected components: {components}")
Connected components: [[2, 3, 4, 5]]
/tmp/ipykernel_698/2936963407.py:10: DeprecationWarning: 
get_components: substation_nodes argument is deprecated and will be removed in a future release.
The functionality is still available with the use of the `tmp_remove_nodes` context manager.

Example:
>>> with graph.tmp_remove_nodes(substation_nodes):
>>>    components = graph.get_components()

  components = graph.get_components(substation_nodes=substation_nodes)

Connected Nodes

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.

# Getting nodes connected to node 1, exclusive of node 1
connected_nodes = graph.get_connected(node_id=1)
print(f"Nodes connected to 1: {connected_nodes}")

# Getting nodes connected to node 1, inclusive of node 1
connected_nodes = graph.get_connected(node_id=1, inclusive=True)
print(f"Nodes connected to 1 (inclusive): {connected_nodes}")
Nodes connected to 1: [5, 2, 4, 3]
Nodes connected to 1 (inclusive): [np.int64(1), 5, 2, 4, 3]