I came up with the following implementation for the Greedy Set Cover after much discussion of my original question here . From the help I received, I coded the problem into the βGreedy Cover Kitβ and after receiving additional help here , I came up with the following implementation. I am grateful to everyone for helping me with this. The following implementation works fine, but I want to make it scalable / fast.
Scalable / faster, I mean that:
- My dataset contains about 50K-100K sets in S
- The number of elements in U itself is very small in the order of 100-500
- The size of each set in S can be from 0 to 40
And here is my attempt:
U = set([1,2,3,4]) R = U S = [set([1,2]), set([1]), set([1,2,3]), set([1]), set([3,4]), set([4]), set([1,2]), set([3,4]), set([1,2,3,4])] w = [1, 1, 2, 2, 2, 3, 3, 4, 4] C = [] costs = [] def findMin(S, R): minCost = 99999.0 minElement = -1 for i, s in enumerate(S): try: cost = w[i]/(len(s.intersection(R))) if cost < minCost: minCost = cost minElement = i except:
I am not an expert in Python, but any Python optimization for this code would be very enjoyable.
performance optimization python algorithm scalability
Legend
source share