예제 #1
0
        public static Result Test(StreamWriter tw, int graphSize, double saturation, bool useBrute)
        {
            Result res   = new Result();
            Graph  graph = RandomGraph.GenerateGraph(graphSize, saturation);

            res.vertices = graphSize;
            res.edges    = graph.EdgesCount;

            int[]     result;
            long      elapsedMs;
            Stopwatch watch;

            if (useBrute)
            {
                watch  = System.Diagnostics.Stopwatch.StartNew();
                result = BruteForce.Solve(ref graph);
                watch.Stop();
                elapsedMs = watch.ElapsedMilliseconds;
                bool tmp = graph.IsColoringValid();
                res.bruteChromaticNum = graph.ChromaticNumber();
                res.bruteTimeMs       = elapsedMs;
                res.bruteValid        = graph.IsColoringValid();
            }

            watch  = System.Diagnostics.Stopwatch.StartNew();
            result = SmallestLast.Solve(ref graph);
            watch.Stop();
            elapsedMs = watch.ElapsedMilliseconds;
            res.smallestlastChromaticNum = graph.ChromaticNumber();
            res.smallestlastTimeMs       = elapsedMs;
            res.smallestlastValid        = graph.IsColoringValid();

            watch  = System.Diagnostics.Stopwatch.StartNew();
            result = DSatur.Solve(ref graph);
            watch.Stop();
            elapsedMs = watch.ElapsedMilliseconds;
            res.dsaturChromaticNum = graph.ChromaticNumber();
            res.dsaturTimeMs       = elapsedMs;
            res.dsaturValid        = graph.IsColoringValid();

            watch  = System.Diagnostics.Stopwatch.StartNew();
            result = BFSColoring.Solve(ref graph);
            watch.Stop();
            elapsedMs           = watch.ElapsedMilliseconds;
            res.bfsChromaticNum = graph.ChromaticNumber();
            res.bfsTimeMs       = elapsedMs;
            res.bfsValid        = graph.IsColoringValid();

            Console.WriteLine(String.Format("{0};{1};{2};{3};{4};{5};{6};{7};",
                                            res.bruteTimeMs, res.bruteChromaticNum,
                                            res.smallestlastTimeMs, res.smallestlastChromaticNum,
                                            res.bfsTimeMs, res.bfsChromaticNum,
                                            res.dsaturTimeMs, res.dsaturChromaticNum));

            tw.WriteLine(String.Format("{0};{1};{2};{3};{4};{5};{6};{7};{8};{9};{10};{11};{12};{13};", res.bruteTimeMs, res.bruteChromaticNum, res.bruteValid,
                                       res.smallestlastTimeMs, res.smallestlastChromaticNum, res.smallestlastValid, res.smallestlastChromaticNum - res.bruteChromaticNum,
                                       res.bfsTimeMs, res.bfsChromaticNum, res.bfsValid, res.bfsChromaticNum - res.bruteChromaticNum,
                                       res.dsaturTimeMs, res.dsaturChromaticNum, res.dsaturValid, res.dsaturChromaticNum - res.bruteChromaticNum));

            return(res);
        }
예제 #2
0
        public static void benchmarkGraph(Graph graph, int avg_iterations, bool usebrute)
        {
            Results results = new Results();

            Console.WriteLine("Benchmarking graph with {0} vertices and {1} edges.", graph.VerticesCount, graph.EdgesCount);

            for (int i = 0; i < avg_iterations; i++)
            {
                Result res = new Result();
                res.vertices = graph.VerticesCount;
                res.edges    = graph.EdgesCount;

                int[]     result;
                long      elapsedMs;
                Stopwatch watch;

                Console.WriteLine("Starting iteration " + i);

                if (usebrute)
                {
                    watch  = System.Diagnostics.Stopwatch.StartNew();
                    result = BruteForce.Solve(ref graph);
                    watch.Stop();
                    elapsedMs = watch.ElapsedMilliseconds;
                    bool tmp = graph.IsColoringValid();
                    res.bruteChromaticNum = graph.ChromaticNumber();
                    res.bruteTimeMs       = elapsedMs;
                    res.bruteValid        = graph.IsColoringValid();
                }

                watch  = System.Diagnostics.Stopwatch.StartNew();
                result = SmallestLast.Solve(ref graph);
                watch.Stop();
                elapsedMs = watch.ElapsedMilliseconds;
                res.smallestlastChromaticNum = graph.ChromaticNumber();
                res.smallestlastTimeMs       = elapsedMs;
                res.smallestlastValid        = graph.IsColoringValid();

                watch  = System.Diagnostics.Stopwatch.StartNew();
                result = DSatur.Solve(ref graph);
                watch.Stop();
                elapsedMs = watch.ElapsedMilliseconds;
                res.dsaturChromaticNum = graph.ChromaticNumber();
                res.dsaturTimeMs       = elapsedMs;
                res.dsaturValid        = graph.IsColoringValid();

                watch  = System.Diagnostics.Stopwatch.StartNew();
                result = BFSColoring.Solve(ref graph);
                watch.Stop();
                elapsedMs           = watch.ElapsedMilliseconds;
                res.bfsChromaticNum = graph.ChromaticNumber();
                res.bfsTimeMs       = elapsedMs;
                res.bfsValid        = graph.IsColoringValid();

                results.Add(res);

                //Console.WriteLine(String.Format("{0};{1};{2};{3};{4};{5};{6};{7};",
                //   res.bruteTimeMs, res.bruteChromaticNum,
                //   res.smallestlastTimeMs, res.smallestlastChromaticNum,
                //   res.bfsTimeMs, res.bfsChromaticNum,
                //   res.dsaturTimeMs, res.dsaturChromaticNum));
            }
            Console.WriteLine("Brute ms;XBrute;BFS ms;Xbfs;DS ms;Xds;SL ms;Xsl");
            Console.WriteLine(String.Format("{0};{1};{2};{3};{4};{5};{6};{7};"
                                            , results.AvgBruteMs(), results.AvgBruteChromaticNum()
                                            , results.AvgBFSMs(), results.AvgBFSChromaticNum()
                                            , results.AvgDSMs(), results.AvgDSChromaticNum()
                                            , results.AvgSLMs(), results.AvgSLChromaticNum()));

            Console.WriteLine("Benchmark done. Press key to exit...");
            Console.ReadKey();
        }
예제 #3
0
        static void Main(string[] args)
        {
            if (args.Count() >= 3)
            {
                int i = 0;
                string[] output = new string[5];
                int graphSize = Convert.ToInt32(args[0]);
                double saturation = Convert.ToDouble(args[1]);
                bool useBrute = Convert.ToBoolean(args[2]);
                Console.WriteLine("Begin program with params: n={0}, saturation={1}, use brute={2}", graphSize, saturation, useBrute);
                string path = "";
                if (args.Count() == 4)
                {
                    path = args[3];
                }
                else
                {
                    path = "output.txt";
                }
                Graph graph = RandomGraph.GenerateGraph(graphSize, saturation);
                int[] result;
                long elapsedMs;
                Stopwatch watch;
                output[i] = String.Format("Generated graph with {0} vertices and {1} edges.", graph.VerticesCount, graph.EdgesCount);
                Console.WriteLine(output[i]);
                i++;

                watch = System.Diagnostics.Stopwatch.StartNew();
                result = SmallestLast.Solve(ref graph);
                watch.Stop();
                elapsedMs = watch.ElapsedMilliseconds;
                output[i] = String.Format("SL finished in {0} ms. Valid coloring={1} X(G)={2}. Exact coloring={3}", elapsedMs, graph.IsColoringValid(), graph.ColorsMatrix.Distinct().Count(), string.Join(" ", result));
                Console.WriteLine(output[i]);
                i++;

                watch = System.Diagnostics.Stopwatch.StartNew();
                result = DSatur.Solve(ref graph);
                watch.Stop();
                elapsedMs = watch.ElapsedMilliseconds;
                output[i] = String.Format("DSATUR finished in {0} ms. Valid coloring={1} X(G)={2}. Exact coloring={3}", elapsedMs, graph.IsColoringValid(), graph.ColorsMatrix.Distinct().Count(), string.Join(" ", result));
                Console.WriteLine(output[i]);
                i++;

                watch = System.Diagnostics.Stopwatch.StartNew();
                result = BFSColoring.Solve(ref graph);
                watch.Stop();
                elapsedMs = watch.ElapsedMilliseconds;
                output[i] = String.Format("BFS finished in {0} ms. Valid coloring={1} X(G)={2}. Exact coloring={3}", elapsedMs, graph.IsColoringValid(), graph.ColorsMatrix.Distinct().Count(), string.Join(" ", result));
                Console.WriteLine(output[i]);

                if (useBrute)
                {
                    watch = System.Diagnostics.Stopwatch.StartNew();
                    result = BruteForce.Solve(ref graph);
                    watch.Stop();
                    elapsedMs = watch.ElapsedMilliseconds;
                    bool tmp = graph.IsColoringValid();

                    output[i] = String.Format("Brute force finished in {0} ms. Valid coloring={1} X(G)={2}. Exact coloring={3}", elapsedMs, graph.IsColoringValid(), graph.ColorsMatrix.Distinct().Count(), string.Join(" ", result));
                    Console.WriteLine(output[i]);
                    i++;
                }

                using (var tw = new StreamWriter(path, false))
                {
                    foreach (string s in output)
                    {
                            tw.WriteLine(s);
                    }
                }
                Console.WriteLine("Finished program. Press key to exit...");
                Console.ReadKey();
            }
            else
            {
                while (true)
                {
                    Console.WriteLine("Input graph file name:");
                    var filename = Console.ReadLine();
                    Console.WriteLine("Reading from file: " + filename);
                    Graph graph = null;
                    try
                    {
                        graph = new Graph(filename);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Encoutered exception: " + e.Message);
                        Console.ReadKey();
                        Environment.Exit(0);
                    }


                    int[] result;
                    long elapsedMs;
                    Stopwatch watch;
                    Console.WriteLine("Created graph with " + graph.VerticesCount + " vertices and " + graph.EdgesCount + " edges.");

                    Console.WriteLine("Should I run brute force? (y/n)");
                    var bruteanswer = Console.ReadLine();
                    bool usebrute = bruteanswer == "y";

                    if (usebrute)
                    {
                        Console.WriteLine("------------------------------------------------------");
                        Console.WriteLine("Solving with BruteForce");
                        watch = System.Diagnostics.Stopwatch.StartNew();
                        result = BruteForce.Solve(ref graph);
                        watch.Stop();
                        elapsedMs = watch.ElapsedMilliseconds;

                        PrintResult(result, graph, elapsedMs, "BruteForce");
                    }
                    Console.WriteLine("------------------------------------------------------");
                    Console.WriteLine("Solving with SmallestLast");
                    watch = System.Diagnostics.Stopwatch.StartNew();
                    result = SmallestLast.Solve(ref graph);
                    watch.Stop();
                    elapsedMs = watch.ElapsedMilliseconds;
                    PrintResult(result, graph, elapsedMs, "SmallestLast");
                    Console.WriteLine("------------------------------------------------------");

                    Console.WriteLine("Solving with DSatur");
                    watch = System.Diagnostics.Stopwatch.StartNew();
                    result = DSatur.Solve(ref graph);
                    watch.Stop();
                    elapsedMs = watch.ElapsedMilliseconds;
                    PrintResult(result, graph, elapsedMs, "DSatur");
                    Console.WriteLine("------------------------------------------------------");

                    Console.WriteLine("Solving with BFS");
                    watch = System.Diagnostics.Stopwatch.StartNew();
                    result = BFSColoring.Solve(ref graph);
                    watch.Stop();
                    elapsedMs = watch.ElapsedMilliseconds;
                    PrintResult(result, graph, elapsedMs, "BFS");
                    Console.WriteLine("######################################################\n\n");

                }
            }
        }