Skip to content

Commit

Permalink
refactor(BaseGraph): improve performance of get_components
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Koppen <[email protected]>
  • Loading branch information
vincentkoppen committed Jan 29, 2025
1 parent a732914 commit c4fb7bb
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/power_grid_model_ds/_core/model/graphs/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,8 @@ def get_all_paths(self, ext_start_node_id: int, ext_end_node_id: int) -> list[li

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

def get_connected(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,8 @@ def _get_shortest_path(self, source: int, target: int) -> tuple[list[int], int]:
def _get_all_paths(self, source: int, target: int) -> list[list[int]]:
return list(rx.all_simple_paths(self._graph, source, target))

def _get_components(self, substation_nodes: list[int]) -> list[list[int]]:
no_os_graph = self._graph.copy()
for os_node in substation_nodes:
no_os_graph.remove_node(os_node)
components = rx.connected_components(no_os_graph)
def _get_components(self) -> list[list[int]]:
components = rx.connected_components(self._graph)
return [list(component) for component in components]

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

0 comments on commit c4fb7bb

Please sign in to comment.