One of the optimized solutions is the use of dynamic programming, but still very expensive O (2 ** n), which is not very possible if you do not use any clustering and distribution of calculations, a ruby or one server will not be very useful for you.
I would recommend that you come up with greedy criteria instead of using DP or brute force, which would be easier to implement.
Once your program is finished, you can take a note and save the results somewhere for later searches, which can also save you a few cycles.
in terms of code, you will need to implement vertices, edges that have weight.
i.e.: a vertex class having edges with weights, recursive. than the class of the graph that will fill the data.
Darthvader
source share