Fix O(n^2) complexity in rule check and group creation#2383
Open
Swapnil-jain wants to merge 1 commit intoexpectedparrot:mainfrom
Open
Fix O(n^2) complexity in rule check and group creation#2383Swapnil-jain wants to merge 1 commit intoexpectedparrot:mainfrom
Swapnil-jain wants to merge 1 commit intoexpectedparrot:mainfrom
Conversation
…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.
Author
|
Would appreciate a review. @johnjosephhorton @zer0dss @rbyh |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Root Cause
Three locations had inefficient nested loops:
add_stop_ruleinrule_manager.py- loop continued even after finding a matchsuggest_dependency_aware_groupsinsurvey.py- nested iteration through group members for each candidatecreate_allowable_groupsinsurvey.py- same nested iteration patternThe Fix
any()which short-circuits on first matchinverted_dag[j]= questions that depend on j)current_group_setincrementally instead of recreating each iterationComplexity Improvement
add_stop_rulesuggest_dependency_aware_groupscreate_allowable_groupsWhere n = number of questions, E = number of dependency edges
Test Plan
tests/surveys/test_group_navigation.pycover the modified methods