Esempio n. 1
0
        private static SampleStatistics[] BronKerboschTimed(RandomUndirectedGraph graph, int[] funcIndices, int samples)
        {
            List <ImmutableArray <Vertex> >?first = null;

            SampleStatistics[] times = Enumerable.Range(0, Portfolio.FuncNames.Length)
                                       .Select(funcIndex => new SampleStatistics()).ToArray();
            for (var sample = samples == 1 ? 1 : 0; sample <= samples; ++sample)
            {
                foreach (var funcIndex in funcIndices)
                {
                    if (sample == 0)
                    {
                        var reporter = new SimpleReporter();
                        var sw       = Stopwatch.StartNew();
                        Portfolio.Explore(funcIndex, graph.Graph, reporter);
                        sw.Stop();
                        var secs = sw.ElapsedMilliseconds / 1e3;
                        if (secs >= 3.0)
                        {
                            Console.WriteLine($"  {Portfolio.FuncNames[funcIndex],8}: {secs,6:N2}s");
                        }
                        Portfolio.SortCliques(reporter.Cliques);
                        if (first == null)
                        {
                            if (reporter.Cliques.Count != graph.CliqueCount)
                            {
                                throw new ArgumentException(
                                          $"Expected {graph.CliqueCount} cliques, got {reporter.Cliques.Count}");
                            }
                            first = reporter.Cliques;
                        }
                        else
                        {
                            Portfolio.AssertSameCliques(first, reporter.Cliques);
                        }
                    }
                    else
                    {
                        var reporter = new CountingReporter();
                        var sw       = Stopwatch.StartNew();
                        Portfolio.Explore(funcIndex, graph.Graph, reporter);
                        sw.Stop();
                        var secs = sw.ElapsedMilliseconds / 1e3;
                        times[funcIndex].Put(secs);
                    }
                }
            }
            return(times);
        }
Esempio n. 2
0
        private static void Bk(string orderstr, IEnumerable <int> sizes, Func <int, IEnumerable <int> > includedFuncs,
                               int samples)
        {
            const string tmpfname = "tmp.csv";

            using (StreamWriter fo = new StreamWriter(tmpfname))
            {
                fo.Write("Size");
                foreach (string name in Portfolio.FuncNames)
                {
                    fo.Write(",{0} min,{0} mean,{0} max", name);
                }
                fo.WriteLine();
                foreach (var size in sizes)
                {
                    var funcIndices = includedFuncs(size).ToArray();
                    var g           = RandomUndirectedGraph.Read(orderstr, size);
                    var stats       = BronKerboschTimed(g, funcIndices, samples);
                    fo.Write($"{size}");
                    foreach ((var funcIndex, string funcName) in Portfolio.FuncNames.Select((n, i) => (i, n)))
                    {
                        var max  = stats[funcIndex].Max;
                        var min  = stats[funcIndex].Min;
                        var mean = stats[funcIndex].Mean;
                        fo.Write($",{min},{mean},{max}");
                        if (!double.IsNaN(mean))
                        {
                            var reldev = stats[funcIndex].Deviation / mean;
                            Console.WriteLine(
                                $"order {orderstr,4:D} size {size,7:D} {funcName,-8}: {mean,6:N3}s ± {reldev:P0}");
                        }
                    }
                    fo.WriteLine();
                }
            }

            var path = $"..\\bron_kerbosch_csharp_order_{orderstr}.csv";

            if (File.Exists(path))
            {
                File.Delete(path);
            }
            File.Move(tmpfname, path);
        }