Skip to content

Fix O(n^2) complexity in rule check and group creation#2383

Open
Swapnil-jain wants to merge 1 commit intoexpectedparrot:mainfrom
Swapnil-jain:fix/issue-2376-optimize-rule-check-complexity
Open

Fix O(n^2) complexity in rule check and group creation#2383
Swapnil-jain wants to merge 1 commit intoexpectedparrot:mainfrom
Swapnil-jain:fix/issue-2376-optimize-rule-check-complexity

Conversation

@Swapnil-jain
Copy link

@Swapnil-jain Swapnil-jain commented Feb 2, 2026

Summary

Root Cause

Three locations had inefficient nested loops:

  1. add_stop_rule in rule_manager.py - loop continued even after finding a match
  2. suggest_dependency_aware_groups in survey.py - nested iteration through group members for each candidate
  3. create_allowable_groups in survey.py - same nested iteration pattern

The Fix

  1. add_stop_rule: Changed to any() which short-circuits on first match
  2. suggest_dependency_aware_groups & create_allowable_groups:
    • Pre-compute an inverted DAG (inverted_dag[j] = questions that depend on j)
    • Use set intersection instead of iterating through all group members
    • Maintain current_group_set incrementally instead of recreating each iteration

Complexity Improvement

Method Before After
add_stop_rule O(n) always O(1) to O(n) with early exit
suggest_dependency_aware_groups O(n² × |group|) O(n + E)
create_allowable_groups O(n² × |group|) O(n + E)

Where n = number of questions, E = number of dependency edges

Test Plan

  • Existing tests in tests/surveys/test_group_navigation.py cover the modified methods
  • Logic verified to be equivalent to original implementation

…t#2376)

The issue reported O(n^2) complexity in rule check logic where n is the
number of questions. This fix addresses three locations:

1. add_stop_rule: Changed loop to any() for early termination on match
2. suggest_dependency_aware_groups: Pre-compute inverted DAG and use
   set intersection instead of nested iteration
3. create_allowable_groups: Same optimization as above

Complexity improves from O(n^2) to O(n + E) where E is dependency edges.
@Swapnil-jain
Copy link
Author

Swapnil-jain commented Feb 2, 2026

Would appreciate a review. @johnjosephhorton @zer0dss @rbyh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

There is an O(n^2) logic in rule check. (n = number of questions)

1 participant