The Algorithm Analysis Tool is a Python-based system designed to analyze and visualize the execution behavior of algorithms in real-time. By instrumenting code at runtime using Python’s ast library, this tool tracks key operations such as assignments, indexing, arithmetic, comparisons, function calls, and loop iterations.
It provides both single-execution and multi-execution dashboards, allowing users to profile algorithms across multiple input configurations and visualize operational trends.
Additionally, the tool includes empirical complexity measurement, estimating how algorithm operations scale with input size and comparing them against theoretical complexity classes (e.g., O(n), O(n log n), O(n²)).
Analyzing algorithm performance is a fundamental skill in computer science, but traditional profiling tools often provide only coarse metrics such as execution time or memory usage.
This tool goes deeper, enabling:
- Operation-level profiling (e.g., assignments vs comparisons)
- Multi-run analysis across different input sizes and generation modes
- Empirical complexity measurement and visualization
- Visualizations of algorithm behavior for teaching, research, and optimization purposes
By instrumenting the AST of the algorithm, we avoid modifying source code manually while still collecting detailed execution statistics.
The tool currently supports:
| Category | Algorithms |
|---|---|
| Sorting | bubble_sort(smart/dumb), merge_sort, insertion_sort, quicksort |
| Searching | linear_search, binary_search |
| Graph | DFS, BFS |
| Scheduling | activity_selection |
| Matrix | matrix_addition, matrix_multiplication |
- Graph algorithms track nodes and edges instead of arrays.
- Sorting and searching algorithms track arrays and operations on array elements.
- random — randomly generated arrays
- edge-case — generates common edge-case arrays (sorted, reversed, all_same, few_unique)
- evolution — optimizes/modifies a base array
- Users can define multiple algorithms, input sizes (
n), array lengths, and modes. - The system computes all combinations and queues jobs automatically.
- Jobs can be submitted, canceled, or dropped from the queue before execution.
- Bar charts of operation counts per job: assignments, comparisons, arithmetic operations, function calls, loop iterations
- Optional graphs for node/edge-based algorithms
- Dashboard metrics: total jobs, running, finished
- For single runs animations are available showing the line of code executed on small input sizes
- Estimates the growth rate of total ops as n increases
- Compares measured operation counts against theoretical complexity curves: O(1), O(log n), O(n), O(n log n), O(n²), O(n³)
- Provides a complexity ladder to show each algos approximate class
- Global complexity landscape visualizes multiple algorithms and modes, with dots staggered for readability and colored by algorithm
- Supports mode-level analysis for algorithms, allowing comparisons between random, edge-case, or evolved input scenarios This allows students and researchers to see how algorithms scale and whether empirical performance aligns with theoretical expectations
-
execution_session.py- Maintains counters for each operation type
- Tracks histories of arrays or graph nodes
- Provides methods such as
count_assign,count_index,count_compare,count_arith,count_loop_iteration
-
ast_visitor.py- Traverses AST using
ast.NodeTransformer - Injects calls to the execution session counters before running code
- Traverses AST using
-
streamlit_app/single_execution.py- Dashboard for single-job configuration and execution
- Supports history, job cancellation, and visualization/animating the steps
-
streamlit_app/multi_execution.py- Dashboard for multi-job configuration and execution
- Supports queueing, batch submission, job cancellation, and visualization
- Includes complexity analysis tabs with ladders and global landscape plots
Original code:
x = 5
y = x + 2Instrumented at Runtime
x = SESSION.count_assign(5, arrays=[arr], line_no=1)
y = SESSION.count_arith(SESSION.count_index(x, None), operator.add, 2, arrays=[arr], line_no=2)pip install .streamlit run src/streamlit_app/app.py --client.showSidebarNavigation=False./run_streamlit.shUnit tests are provided to ensure the correct counting of operations
- Assignments, arithmetic and comparisons
- Indexing
- Function calls
- Loop iterations
- Cumulative counts
def test_simple_arithmetic():
session = ExecutionSession()
counters, history = session.run("x = 1 + 2")
assert counters["arithmetic"] == 1
assert counters["assignments"] == 1
assert_last_history(session, "arithmetic", -2)
assert_last_history(session, "assignment", -1)- Large arrays are truncated for visulaisation (MAX_ANIMATION_ARRAY_LENGTH)
- No GPU acceleration so runs on large input sizes may be slow (depends on the hardware)
- Complexity measurement is empirical and depends on observed operation counts, which may vary slightly due to input generation or Python execution overhead
- Current code is limited to predefined algorithms, with expected input formats