Search code examples
javabig-oheuristics

Big-O notation of a function in Java by experiment


I need to write a program that determines the Big-O notation of an algorithm in Java.

I don't have access to the algorithms code, so it must be based on experimentation and execution times. I don't know where to start.

Can someone help me?

Edit: The only thing i know about the algorithm is that takes an integer value and doesn't have a return


Solution

  • Firstly, you need to be aware that what such a program does it to provided an evidence-based guess as to what complexity class the algorithm belongs to. It can give the wrong answer. (Indeed, in complicated cases where the complexity class is unusual, wrong answers are increasingly likely.)

    In short, this is NOT complexity analysis.

    The general approach would be:

    • Run the algorithm multiple times with values of N across the range, measuring the execution times. Repeat multiple times for each N, to ensure that you are getting consistent measurements.

    • Try to fit the experimental results to different kinds of curves; i.e. linear, quadratic, logarithmic. Note that it is the fit for large values of N that matters. So when you check for "goodness of fit", use a measure that gives increasing weight to the larger data points.

    This is intended as a start point. For example, I'm expecting that you will do your own research on issues such as:

    • how to get reliable execution-time measurements (for Java),
    • how to do curve fitting in a mathematically sound way, and
    • dealing with the case where the execution times get too long to measure experimentally for large N.