Skip to content

Commit c4fb7bb

Browse files
committed
refactor(BaseGraph): improve performance of get_components
Signed-off-by: Vincent Koppen <[email protected]>
1 parent a732914 commit c4fb7bb

File tree

2 files changed

+4
-6
lines changed

2 files changed

+4
-6
lines changed

src/power_grid_model_ds/_core/model/graphs/models/base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,8 @@ def get_all_paths(self, ext_start_node_id: int, ext_end_node_id: int) -> list[li
250250

251251
def get_components(self, substation_nodes: NDArray[np.int32]) -> list[list[int]]:
252252
"""Returns all separate components when the substation_nodes are removed of the graph as lists"""
253-
internal_components = self._get_components(substation_nodes=self._externals_to_internals(substation_nodes))
253+
with self.tmp_remove_nodes(substation_nodes):
254+
internal_components = self._get_components()
254255
return [self._internals_to_externals(component) for component in internal_components]
255256

256257
def get_connected(

src/power_grid_model_ds/_core/model/graphs/models/rustworkx.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,8 @@ def _get_shortest_path(self, source: int, target: int) -> tuple[list[int], int]:
8484
def _get_all_paths(self, source: int, target: int) -> list[list[int]]:
8585
return list(rx.all_simple_paths(self._graph, source, target))
8686

87-
def _get_components(self, substation_nodes: list[int]) -> list[list[int]]:
88-
no_os_graph = self._graph.copy()
89-
for os_node in substation_nodes:
90-
no_os_graph.remove_node(os_node)
91-
components = rx.connected_components(no_os_graph)
87+
def _get_components(self) -> list[list[int]]:
88+
components = rx.connected_components(self._graph)
9289
return [list(component) for component in components]
9390

9491
def _get_connected(self, node_id: int, nodes_to_ignore: list[int], inclusive: bool = False) -> list[int]:

0 commit comments

Comments
 (0)