-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(BaseGraph): improve performance of get_components #23
base: main
Are you sure you want to change the base?
Changes from all commits
f237fe2
1154f96
bcd6870
45780e7
3daad3f
d9521f6
4cae2a4
4cb80a4
369498b
e81346f
c223e83
a732914
a3d3e7e
269182b
9f3bd5b
68adcd2
a40bedd
f7e0085
0351b12
62c6cde
89d6341
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
1.0 | ||
1.1 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
# SPDX-License-Identifier: MPL-2.0 | ||
|
||
import logging | ||
from typing import Generator | ||
|
||
import rustworkx as rx | ||
from rustworkx import NoEdgeBetweenNodes | ||
|
@@ -83,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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As @jaapschoutenalliander already pointed out: Surprisingly, there seems to be no performance increase. Verified it on my own laptop. Seems even slightly slower: 0.36 (this PR) vs. 0.33 (main) at 5000 nodes I still think we can consider merging this because in my opinion it makes the implementation more readable and more consistent with the other methods. Also, our performance test might not be representative enough (only 2 Substation nodes). I could also try a more representative performance comparison with my internal project at Alliander |
||
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]: | ||
|
@@ -99,6 +97,9 @@ def _get_connected(self, node_id: int, nodes_to_ignore: list[int], inclusive: bo | |
|
||
return connected_nodes | ||
|
||
def _in_branches(self, int_node_id: int) -> Generator[tuple[int, int], None, None]: | ||
return ((source, target) for source, target, _ in self._graph.in_edges(int_node_id)) | ||
|
||
def _find_fundamental_cycles(self) -> list[list[int]]: | ||
"""Find all fundamental cycles in the graph using Rustworkx. | ||
|
||
|
@@ -107,6 +108,9 @@ def _find_fundamental_cycles(self) -> list[list[int]]: | |
""" | ||
return find_fundamental_cycles_rustworkx(self._graph) | ||
|
||
def _all_branches(self) -> Generator[tuple[int, int], None, None]: | ||
return ((source, target) for source, target in self._graph.edge_list()) | ||
|
||
|
||
class _NodeVisitor(BFSVisitor): | ||
def __init__(self, nodes_to_ignore: list[int]): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
perhaps we can add the deprecation message for substation_nodes?
something like
warning should say intended use is as follows: