Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To all those who wonder about the use of learning theory, this question is a good illustration. The right question is not about a "fast way to bounce between lists and tuples in python" — the reason for the slowness is something deeper.</p> <p>What you're trying to solve here is known as the <a href="http://en.wikipedia.org/wiki/Assignment_problem" rel="noreferrer">assignment problem</a>: given two lists of n elements each and n&times;n values (the value of each pair), how to assign them so that the total "value" is maximized (or equivalently, minimized). There are several algorithms for this, such as the <a href="http://en.wikipedia.org/wiki/Hungarian_algorithm" rel="noreferrer">Hungarian algorithm</a> (<a href="http://www.clapper.org/software/python/munkres/" rel="noreferrer">Python implementation</a>), or you could solve it using more general min-cost flow algorithms, or even cast it as a linear program and use an LP solver. Most of these would have a running time of O(n<sup>3</sup>).</p> <p>What your algorithm above does is to try each possible way of pairing them. (The memoisation only helps to avoid recomputing answers for pairs of subsets, but you're still looking at all pairs of subsets.) This approach is at least Ω(n<sup>2</sup>2<sup>2n</sup>). For n=16, n<sup>3</sup> is 4096 and n<sup>2</sup>2<sup>2n</sup> is 1099511627776. There are constant factors in each algorithm of course, but see the difference? :-) (The approach in the question is still better than the naive O(n!), which would be much worse.) Use one of the O(n^3) algorithms, and I predict it should run in time for up to n=10000 or so, instead of just up to n=15. </p> <p>"Premature optimization is the root of all evil", as Knuth said, but so is delayed/overdue optimization: you should first carefully consider an appropriate algorithm before implementing it, not pick a bad one and then wonder what parts of it are slow. :-) Even badly implementing a good algorithm in Python would be orders of magnitude faster than fixing all the "slow?" parts of the code above (e.g., by rewriting in C).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload