Search code examples
cscipytime-complexitylcs

Analyzing time complexity of a function written in C


I was implementing Longest Common Subsequence problem in C. I wish to compare the time taken for execution of recursive version of the solution and dynamic programming version. How can I find the time taken for running the LCS function in both versions for various inputs? Also can I use SciPy to plot these values on a graph and infer the time complexity?

Thanks in advance,

Razor


Solution

  • For the second part of your question: the short answer is yes, you can. You need to get the two data sets (one for each solution) in a format that is convenient to parse with from Python. Something like:

    x y z

    on each line, where x is the sequence length, y is the time taken by the dynamic solution, z is the time taken by the recursive solution

    Then, in Python:

    # Load these from your data sets.
    sequence_lengths = ...
    recursive_times  = ...
    dynamic_times    = ...
    
    import matplotlib.pyplot as plt
    fig = plt.figure()
    ax = fig.add_subplot(111)
    p1 = ax.plot(sequence_lengths, recursive_times, 'r', linewidth=2)
    p2 = ax.plot(sequence_lengths, dynamic_times,   'b', linewidth=2)
    
    plt.xlabel('Sequence length')
    plt.ylabel('Time')
    plt.title('LCS timing')
    plt.grid(True)
    plt.show()