⚡️ Speed up function find_cycle_vertices by 175%
#37
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.
📄 175% (1.75x) speedup for
find_cycle_verticesinsrc/dsa/nodes.py⏱️ Runtime :
87.7 milliseconds→31.9 milliseconds(best of130runs)📝 Explanation and details
Great! The biggest bottleneck is
nx.simple_cycles, which is known to be slow for large graphs since it finds all simple cycles. However, your code only wants the set of vertices involved in any cycle, not the cycles themselves.Optimization insight:
We only care about the vertices contained in any cycles, not the cycles themselves.
Therefore, we can use
networkx.simple_cyclesonly as a last resort.But:
(v,v)) is a cycle by definition.So, the code can be rewritten to avoid enumerating all cycles and instead just.
This avoids listing all cycles.
Here's the optimized code.
Runtime::
Now, the runtime is dominated by the search for SCCs and checking self-loops, which is much more efficient than enumerating all simple cycles.
Behavior:
The result is the same as your original code for all test cases. No changes to the function signature.
All comments are preserved except for omitted inapplicable one regarding cycle enumeration, which is now unnecessary.
Let me know if you want further low-level tuning or Cython rewrite.
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
test_dsa_nodes.py::test_complex_graphtest_dsa_nodes.py::test_cycle_with_extra_nodes_edgestest_dsa_nodes.py::test_figure_eighttest_dsa_nodes.py::test_multiple_disjoint_cyclestest_dsa_nodes.py::test_multiple_overlapping_cyclestest_dsa_nodes.py::test_no_cycles_dagtest_dsa_nodes.py::test_self_looptest_dsa_nodes.py::test_simple_triangle_cycletest_dsa_nodes.py::test_simple_two_node_cycletest_dsa_nodes.py::test_string_vertices🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-find_cycle_vertices-mce0k6pjand push.