Skip to content

⚡️ Speed up function find_node_with_highest_degree by 3,600% #31

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

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/dsa/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,21 @@ def sort_chat_inputs_first(self, vertices_layers: list[list[str]]) -> list[list[
def find_node_with_highest_degree(
nodes: list[str], connections: dict[str, list[str]]
) -> str:
# Precompute incoming degree for each node.
incoming_degree = {}
for targets in connections.values():
for target in targets:
incoming_degree[target] = incoming_degree.get(target, 0) + 1

max_degree = -1
max_degree_node = None

for node in nodes:
degree = 0
# Count outgoing connections
degree += len(connections.get(node, []))

out_deg = len(connections.get(node, []))
# Count incoming connections
for src, targets in connections.items():
if node in targets:
degree += 1
in_deg = incoming_degree.get(node, 0)
degree = out_deg + in_deg

if degree > max_degree:
max_degree = degree
Expand Down