我编写了这个程序来测试“解决”集合覆盖问题需要多长时间。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using MoreLinq;
namespace SetCover
{
class Program
{
const int maxNumItems = 10000;
const int numSets = 5000;
const int maxItemsPerSet = 300;
static void Main(string[] args)
{
var rand = new Random();
var sets = new List<HashSet<int>>(numSets);
var cover = new List<HashSet<int>>(numSets);
var universe = new HashSet<int>();
HashSet<int> remaining;
var watch = new Stopwatch();
Console.Write("Generating sets...");
for (int i = 0; i < numSets; ++i)
{
int numItemsInSet = rand.Next(1, maxItemsPerSet);
sets.Add(new HashSet<int>());
for (int j = 0; j < numItemsInSet; ++j)
{
sets[i].Add(rand.Next(maxNumItems));
}
}
Console.WriteLine("Done!");
Console.Write("Computing universe...");
foreach (var set in sets)
foreach (var item in set)
universe.Add(item);
Console.WriteLine("Found {0} items.", universe.Count);
watch.Start();
//Console.Write("Removing subsets...");
//int numSetsRemoved = sets.RemoveAll(subset => sets.Any(superset => subset != superset && subset.IsSubsetOf(superset)));
//Console.WriteLine("Removed {0} subsets.", numSetsRemoved);
//Console.Write("Sorting sets...");
//sets = sets.OrderByDescending(s => s.Count).ToList();
//Console.WriteLine("{0} elements in largest set.", sets[0].Count);
Console.WriteLine("Computing cover...");
remaining = universe.ToHashSet();
while (remaining.Any())
{
Console.Write(" Finding set {0}...", cover.Count + 1);
var nextSet = sets.MaxBy(s => s.Intersect(remaining).Count());
remaining.ExceptWith(nextSet);
cover.Add(nextSet);
Console.WriteLine("{0} elements remaining.", remaining.Count);
}
Console.WriteLine("{0} sets in cover.", cover.Count);
watch.Stop();
Console.WriteLine("Computed cover in {0} seconds.", watch.Elapsed.TotalSeconds);
Console.ReadLine();
}
}
public static class Extensions
{
public static HashSet<TValue> Clone<TValue>(this HashSet<TValue> set)
{
var tmp = new TValue[set.Count];
set.CopyTo(tmp, 0);
return new HashSet<TValue>(tmp);
}
public static HashSet<TSource> ToHashSet<TSource>(this IEnumerable<TSource> source)
{
return new HashSet<TSource>(source);
}
}
}
这只是一个贪婪的次优解决方案,但仍然需要 147 秒才能运行。然而,我认为这个解决方案应该非常接近最优,所以它应该足以满足我的目的。我怎样才能加快速度呢?
我注释掉了几行,因为它们弊大于利。编辑:计算宇宙实际上不应该是时间的一部分……这可以事先知道。