Esempio n. 1
0
        public static void WeightedTest()
        {
            Console.Out.WriteLine("Grafy ważone");

            Graph grid3 = new AdjacencyListsGraph <SimpleAdjacencyList>(false, 9);

            for (int i = 0; i < 9; i += 3)
            {
                grid3.AddEdge(i, i + 1, 3);
                grid3.AddEdge(i + 1, i + 2, 2);
            }

            for (int i = 0; i < 3; i++)
            {
                grid3.AddEdge(i + 0, i + 3, 2);
                grid3.AddEdge(i + 3, i + 6, 1);
            }

            RandomGraphGenerator rgg = new RandomGraphGenerator(240044);
            Graph eCycle24           = rgg.UndirectedCycle(typeof(AdjacencyListsGraph <SimpleAdjacencyList>), 24, 1, 5, true);
            Graph oCycle23           = rgg.UndirectedCycle(typeof(AdjacencyListsGraph <SimpleAdjacencyList>), 23, 1, 5, true);
            Graph g3 = rgg.UndirectedGraph(typeof(AdjacencyListsGraph <SimpleAdjacencyList>), 20, 0.2, 1, 11, true);


            TestSet set2 = new TestSet();

            set2.TestCases.Add(new MatchingTestCase(10, grid3, 0, 4, 5));
            set2.TestCases.Add(new MatchingTestCase(10, grid3, 1, 5, 7));
            set2.TestCases.Add(new MatchingTestCase(10, grid3, 2 * grid3.EdgesCount - grid3.VerticesCount, grid3.EdgesCount, 3 + 6 + 6 + 9));
            set2.TestCases.Add(new MatchingTestCase(10, grid3, 2 * grid3.EdgesCount - grid3.VerticesCount - 1, grid3.EdgesCount - 1, 3 + 6 + 6 + 6));
            set2.TestCases.Add(new MatchingTestCase(10, eCycle24, 0, 12, 33));
            set2.TestCases.Add(new MatchingTestCase(10, oCycle23, 0, 11, 30));
            set2.TestCases.Add(new MatchingTestCase(10, eCycle24, 1, 12, 24));
            set2.TestCases.Add(new MatchingTestCase(10, oCycle23, 1, 12, 32));
            set2.TestCases.Add(new MatchingTestCase(10, g3, 0, 10, 45));
            set2.TestCases.Add(new MatchingTestCase(10, g3, 2, 11, 43));
            set2.TestCases.Add(new MatchingTestCase(10, g3, 3, 11, 35));

            set2.PreformTests(true, false);
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            Graph g1, g2, g3, g4, g5, g6;

            // test 1 -- graf pełny
            g1 = new AdjacencyMatrixGraph(false, 20);
            for (int i = 0; i < 20; ++i)
            {
                for (int j = i + 1; j < 20; ++j)
                {
                    g1.AddEdge(i, j);
                }
            }

            //test 2 -- graf pusty
            g2 = new AdjacencyMatrixGraph(false, 20);

            // test 3
            g3 = new AdjacencyMatrixGraph(false, 8);

            for (int i = 1; i < 6; ++i)
            {
                g3.AddEdge(i - 1, i);
            }
            g3.AddEdge(6, 1);
            g3.AddEdge(7, 4);

            // test 4 -- K_n,n - skojarzenie doskonałe, nieparzyste i parzyste w osobnych klasach dwudzielności

            g4 = new AdjacencyMatrixGraph(false, 20);
            for (int i = 0; i < 10; ++i)
            {
                for (int j = 0; j < 10; ++j)
                {
                    if (i != j)
                    {
                        g4.AddEdge(2 * i, 2 * j + 1);
                    }
                }
            }

            // test 5 -- prismoid - przypadek dla SL

            g5 = new AdjacencyMatrixGraph(false, 8);

            g5.AddEdge(0, 1);
            g5.AddEdge(0, 2);
            g5.AddEdge(0, 4);
            g5.AddEdge(0, 6);
            g5.AddEdge(1, 2);
            g5.AddEdge(1, 3);
            g5.AddEdge(1, 7);
            g5.AddEdge(2, 3);
            g5.AddEdge(2, 4);
            g5.AddEdge(3, 5);
            g5.AddEdge(3, 7);
            g5.AddEdge(4, 5);
            g5.AddEdge(4, 6);
            g5.AddEdge(5, 6);
            g5.AddEdge(5, 7);
            g5.AddEdge(6, 7);

            //http://cs.stackexchange.com/questions/42973/counter-example-to-graph-coloring-heuristic-using-bfs
            g6 = new AdjacencyMatrixGraph(false, 6);

            g6.AddEdge(0, 1);
            g6.AddEdge(0, 2);
            g6.AddEdge(1, 3);
            g6.AddEdge(3, 4);
            g6.AddEdge(3, 5);
            g6.AddEdge(2, 4);
            g6.AddEdge(2, 5);
            g6.AddEdge(4, 5);

            RandomGraphGenerator rgg = new RandomGraphGenerator();

            rgg.SetSeed(111);
            Graph g7 = rgg.UndirectedCycle(typeof(AdjacencyMatrixGraph), 100);

            rgg.SetSeed(222);
            Graph g8 = rgg.UndirectedGraph(typeof(AdjacencyMatrixGraph), 100, 0.3);

            rgg.SetSeed(333);
            Graph g9 = rgg.UndirectedGraph(typeof(AdjacencyMatrixGraph), 100, 0.5);

            rgg.SetSeed(444);
            Graph g10 = rgg.UndirectedGraph(typeof(AdjacencyMatrixGraph), 100, 0.7);

            rgg.SetSeed(666);
            Graph g11 = rgg.UndirectedGraph(typeof(AdjacencyListsGraph <SimpleAdjacencyList>), 17000, 0.001);

            Graph[] graphs = { g1, g2, g3, g4, g5, g6, g7, g8, g9, g10, g11 };

            DataContractSerializer dcs    = new DataContractSerializer(typeof(ProperResult[]));
            FileStream             greedy = new FileStream("greedy.dat", FileMode.Open);

            ProperResult[] greedyProperResults = (ProperResult[])dcs.ReadObject(greedy);
            greedy.Close();
            FileStream bfs = new FileStream("bfs.dat", FileMode.Open);

            ProperResult[] bfsProperResults = (ProperResult[])dcs.ReadObject(bfs);
            bfs.Close();
            FileStream back = new FileStream("back.dat", FileMode.Open);

            ProperResult[] backProperResults = (ProperResult[])dcs.ReadObject(back);
            back.Close();
            FileStream color = new FileStream("color.dat", FileMode.Open);

            ProperResult[] colorProperResults = (ProperResult[])dcs.ReadObject(color);
            color.Close();
            FileStream incremental = new FileStream("incremental.dat", FileMode.Open);

            ProperResult[] incrementalProperResults = (ProperResult[])dcs.ReadObject(incremental);
            incremental.Close();

            TestSet setSG = new TestSet();

            setSG.TestCases.Add(new GreedyColoringGraphColoringTestCase(5, g1, greedyProperResults[0]));
            setSG.TestCases.Add(new GreedyColoringGraphColoringTestCase(5, g2, greedyProperResults[1]));
            setSG.TestCases.Add(new GreedyColoringGraphColoringTestCase(5, g3, greedyProperResults[2]));
            setSG.TestCases.Add(new GreedyColoringGraphColoringTestCase(5, g4, greedyProperResults[3]));
            setSG.TestCases.Add(new GreedyColoringGraphColoringTestCase(5, g5, greedyProperResults[4]));
            setSG.TestCases.Add(new GreedyColoringGraphColoringTestCase(5, g6, greedyProperResults[5]));
            setSG.TestCases.Add(new GreedyColoringGraphColoringTestCase(5, g7, greedyProperResults[6]));
            setSG.TestCases.Add(new GreedyColoringGraphColoringTestCase(5, g8, greedyProperResults[7]));
            setSG.TestCases.Add(new GreedyColoringGraphColoringTestCase(5, g9, greedyProperResults[8]));
            setSG.TestCases.Add(new GreedyColoringGraphColoringTestCase(5, g10, greedyProperResults[9]));
            setSG.TestCases.Add(new GreedyColoringGraphColoringTestCase(5, g11, greedyProperResults[10]));
            Console.WriteLine("\nGreedy Coloring");
            setSG.PreformTests(verbose: true, checkTimeLimit: false);

            TestSet setBFS = new TestSet();

            setBFS.TestCases.Add(new BFSGraphColoringTestCase(5, g1, bfsProperResults[0]));
            setBFS.TestCases.Add(new BFSGraphColoringTestCase(5, g2, bfsProperResults[1]));
            setBFS.TestCases.Add(new BFSGraphColoringTestCase(5, g3, bfsProperResults[2]));
            setBFS.TestCases.Add(new BFSGraphColoringTestCase(5, g4, bfsProperResults[3]));
            setBFS.TestCases.Add(new BFSGraphColoringTestCase(5, g5, bfsProperResults[4]));
            setBFS.TestCases.Add(new BFSGraphColoringTestCase(5, g6, bfsProperResults[5]));
            setBFS.TestCases.Add(new BFSGraphColoringTestCase(5, g7, bfsProperResults[6]));
            setBFS.TestCases.Add(new BFSGraphColoringTestCase(5, g8, bfsProperResults[7]));
            setBFS.TestCases.Add(new BFSGraphColoringTestCase(5, g9, bfsProperResults[8]));
            setBFS.TestCases.Add(new BFSGraphColoringTestCase(5, g10, bfsProperResults[9]));
            setBFS.TestCases.Add(new BFSGraphColoringTestCase(5, g11, bfsProperResults[10]));
            Console.WriteLine("\nBFS Coloring");
            setBFS.PreformTests(verbose: true, checkTimeLimit: false);

            TestSet setLBD = new TestSet();

            setLBD.TestCases.Add(new LargestBackDegreeGraphColoringTestCase(5, g1, backProperResults[0]));
            setLBD.TestCases.Add(new LargestBackDegreeGraphColoringTestCase(5, g2, backProperResults[1]));
            setLBD.TestCases.Add(new LargestBackDegreeGraphColoringTestCase(5, g3, backProperResults[2]));
            setLBD.TestCases.Add(new LargestBackDegreeGraphColoringTestCase(5, g4, backProperResults[3]));
            setLBD.TestCases.Add(new LargestBackDegreeGraphColoringTestCase(5, g5, backProperResults[4]));
            setLBD.TestCases.Add(new LargestBackDegreeGraphColoringTestCase(5, g6, backProperResults[5]));
            setLBD.TestCases.Add(new LargestBackDegreeGraphColoringTestCase(5, g7, backProperResults[6]));
            setLBD.TestCases.Add(new LargestBackDegreeGraphColoringTestCase(5, g8, backProperResults[7]));
            setLBD.TestCases.Add(new LargestBackDegreeGraphColoringTestCase(5, g9, backProperResults[8]));
            setLBD.TestCases.Add(new LargestBackDegreeGraphColoringTestCase(5, g10, backProperResults[9]));
            setLBD.TestCases.Add(new LargestBackDegreeGraphColoringTestCase(5, g11, backProperResults[10]));
            Console.WriteLine("\nLargest Back Degree");
            setLBD.PreformTests(verbose: true, checkTimeLimit: false);

            TestSet setDS = new TestSet();

            setDS.TestCases.Add(new ColorDegreeOrderingGraphColoringTestCase(5, g1, colorProperResults[0]));
            setDS.TestCases.Add(new ColorDegreeOrderingGraphColoringTestCase(5, g2, colorProperResults[1]));
            setDS.TestCases.Add(new ColorDegreeOrderingGraphColoringTestCase(5, g3, colorProperResults[2]));
            setDS.TestCases.Add(new ColorDegreeOrderingGraphColoringTestCase(5, g4, colorProperResults[3]));
            setDS.TestCases.Add(new ColorDegreeOrderingGraphColoringTestCase(5, g5, colorProperResults[4]));
            setDS.TestCases.Add(new ColorDegreeOrderingGraphColoringTestCase(5, g6, colorProperResults[5]));
            setDS.TestCases.Add(new ColorDegreeOrderingGraphColoringTestCase(5, g7, colorProperResults[6]));
            setDS.TestCases.Add(new ColorDegreeOrderingGraphColoringTestCase(5, g8, colorProperResults[7]));
            setDS.TestCases.Add(new ColorDegreeOrderingGraphColoringTestCase(5, g9, colorProperResults[8]));
            setDS.TestCases.Add(new ColorDegreeOrderingGraphColoringTestCase(5, g10, colorProperResults[9]));
            setDS.TestCases.Add(new ColorDegreeOrderingGraphColoringTestCase(5, g11, colorProperResults[10]));
            Console.WriteLine("\nColor Degree Ordering");
            setDS.PreformTests(verbose: true, checkTimeLimit: false);

            TestSet setInc = new TestSet();

            setInc.TestCases.Add(new IncrementalGraphColoringTestCase(5, g1, incrementalProperResults[0]));
            setInc.TestCases.Add(new IncrementalGraphColoringTestCase(5, g2, incrementalProperResults[1]));
            setInc.TestCases.Add(new IncrementalGraphColoringTestCase(5, g3, incrementalProperResults[2]));
            setInc.TestCases.Add(new IncrementalGraphColoringTestCase(5, g4, incrementalProperResults[3]));
            setInc.TestCases.Add(new IncrementalGraphColoringTestCase(5, g5, incrementalProperResults[4]));
            setInc.TestCases.Add(new IncrementalGraphColoringTestCase(5, g6, incrementalProperResults[5]));
            setInc.TestCases.Add(new IncrementalGraphColoringTestCase(5, g7, incrementalProperResults[6]));
            setInc.TestCases.Add(new IncrementalGraphColoringTestCase(5, g8, incrementalProperResults[7]));
            setInc.TestCases.Add(new IncrementalGraphColoringTestCase(5, g9, incrementalProperResults[8]));
            setInc.TestCases.Add(new IncrementalGraphColoringTestCase(5, g10, incrementalProperResults[9]));
            setInc.TestCases.Add(new IncrementalGraphColoringTestCase(5, g11, incrementalProperResults[10]));
            Console.WriteLine("\nIncremental");
            setInc.PreformTests(verbose: true, checkTimeLimit: false);
        }
Esempio n. 3
0
        private static void TestAcyclic()
        {
            var rgg = new RandomGraphGenerator(12345);

            Graph[] g   = new Graph[AcyclicTestSize];
            bool?[] res = { true, false, null, false, true };
            Graph   gg;
            bool    r;
            ulong   cr;

            ulong[] cgg = { 73, 3724, 1, 300000, 1 };
            g[0] = rgg.TreeGraph(typeof(AdjacencyMatrixGraph), 7, 1.0, -99, 99);
            g[1] = rgg.UndirectedGraph(typeof(AdjacencyListsGraph <HashTableAdjacencyList>), 100, 0.1, -999, 999);
            g[2] = rgg.DirectedGraph(typeof(AdjacencyMatrixGraph), 10, 0.5);
            g[3] = rgg.UndirectedCycle(typeof(AdjacencyListsGraph <SimplyAdjacencyList>), 50000, -99, 99);
            g[4] = new AdjacencyListsGraph <AVLAdjacencyList>(false, 50000);

            for (int i = 0; i < AcyclicTestSize; ++i)
            {
                Console.Write($"  Test {i} - ");
                gg = g[i].Clone();
                try
                {
                    cr = Graph.Counter;
                    r  = g[i].Lab03IsUndirectedAcyclic();
                    cr = Graph.Counter - cr;
                    if (res[i] == null)
                    {
                        Console.WriteLine("Failed : exception Lab03Exception expected");
                        continue;
                    }
                    if (!g[i].IsEqual(gg))
                    {
                        Console.WriteLine("Failed : graph was destroyed");
                        continue;
                    }
                    if (r != res[i])
                    {
                        Console.WriteLine("Failed : bad result");
                        continue;
                    }
                    if (cr > 1.5 * cgg[i])
                    {
                        Console.WriteLine($"Failed : poor efficiency {cr} (should be {cgg[i]})");
                        continue;
                    }
                    Console.WriteLine("Passed");
                }
                catch (Lab03Exception e)
                {
                    if (res[i] == null)
                    {
                        Console.WriteLine("Passed");
                    }
                    else
                    {
                        Console.WriteLine($"Failed : {e.GetType()} : {e.Message}");
                    }
                }
                catch (System.Exception e) when(maskExceptions)
                {
                    Console.WriteLine($"Failed : {e.GetType()} : {e.Message}");
                }
            }
        }
Esempio n. 4
0
        private static void TestKruskal()
        {
            var rgg = new RandomGraphGenerator(12345);

            Graph[] g = new Graph[KruskalTestSize];
            bool[]  ex = { false, false, true, false, false };
            Graph   r, gg;
            ulong   cr, cgg;
            int     mstwr, mstwgg;

            g[0] = rgg.UndirectedGraph(typeof(AdjacencyMatrixGraph), 5, 0.7, -99, 99);
            g[1] = rgg.UndirectedGraph(typeof(AdjacencyListsGraph <HashTableAdjacencyList>), 100, 0.1, -999, 999);
            g[2] = rgg.DirectedGraph(typeof(AdjacencyMatrixGraph), 10, 0.5);
            g[3] = rgg.UndirectedCycle(typeof(AdjacencyListsGraph <SimplyAdjacencyList>), 50000, -99, 99);
            g[4] = new AdjacencyListsGraph <AVLAdjacencyList>(false, 50000);

            for (int i = 0; i < KruskalTestSize; ++i)
            {
                Console.Write($"  Test {i} - ");
                gg = g[i].Clone();
                try
                {
                    cr = Graph.Counter;
                    r  = g[i].Lab03Kruskal(out mstwr);
                    cr = Graph.Counter - cr;
                    if (ex[i])
                    {
                        Console.WriteLine("Failed : exception Lab03Exception expected");
                        continue;
                    }
                    if (r == null)
                    {
                        Console.WriteLine("Failed : null returned");
                        continue;
                    }
                    if (r.Directed)
                    {
                        Console.WriteLine("Failed : returned graph is directed");
                        continue;
                    }
                    if (r.GetType() != g[i].GetType())
                    {
                        Console.WriteLine("Failed : invalid graph representation");
                        continue;
                    }
                    if (!g[i].IsEqual(gg))
                    {
                        Console.WriteLine("Failed : graph was destroyed");
                        continue;
                    }
                    cgg    = Graph.Counter;
                    mstwgg = g[i].Kruskal(out gg);
                    cgg    = Graph.Counter - cgg;
                    if (mstwr != mstwgg || !IsSubtree(g[i], r))
                    {
                        Console.WriteLine("Failed : bad result");
                        continue;
                    }
                    if (cr > 1.5 * cgg)
                    {
                        Console.WriteLine($"Failed : poor efficiency {cr} (should be {cgg})");
                        continue;
                    }
                    Console.WriteLine("Passed");
                }
                catch (Lab03Exception e)
                {
                    if (ex[i])
                    {
                        Console.WriteLine("Passed");
                    }
                    else
                    {
                        Console.WriteLine($"Failed : {e.GetType()} : {e.Message}");
                    }
                }
                catch (System.Exception e) when(maskExceptions)
                {
                    Console.WriteLine($"Failed : {e.GetType()} : {e.Message}");
                }
            }
        }
Esempio n. 5
0
        private static void TestBipartite()
        {
            var rgg = new RandomGraphGenerator(12345);

            Graph[] g   = new Graph[BipartiteTestSize];
            bool?[] res = { true, false, null, false, true };
            Graph   gg;
            bool    r;

            int[] part;
            ulong cr;

            ulong[] cgg = { 77, 457, 1, 2500007, 1 };
            g[0] = rgg.BipariteGraph(typeof(AdjacencyMatrixGraph), 4, 3, 0.4, -99, 99);
            g[1] = rgg.UndirectedGraph(typeof(AdjacencyListsGraph <HashTableAdjacencyList>), 100, 0.1, -999, 999);
            g[2] = rgg.DirectedGraph(typeof(AdjacencyMatrixGraph), 10, 0.5);
            g[3] = rgg.UndirectedCycle(typeof(AdjacencyListsGraph <SimpleAdjacencyList>), 50001, -99, 99);
            g[4] = new AdjacencyListsGraph <AVLAdjacencyList>(false, 50000);

            for (int i = 0; i < BipartiteTestSize; ++i)
            {
                Console.Write($"  Test {i} - ");
                gg = g[i].Clone();
                try
                {
                    cr = Graph.Counter;
                    r  = g[i].Lab03IsBipartite(out part);
                    cr = Graph.Counter - cr;
                    if (res[i] == null)
                    {
                        Console.WriteLine("Failed : exception Lab03Exception expected");
                        continue;
                    }
                    if (!g[i].IsEqual(gg))
                    {
                        Console.WriteLine("Failed : graph was destroyed");
                        continue;
                    }
                    if (r != res[i])
                    {
                        Console.WriteLine("Failed : bad result");
                        continue;
                    }
                    if (r && !IsProperPartition(g[i], part))
                    {
                        Console.WriteLine("Failed : invalid partition");
                        continue;
                    }
                    if (!r && part != null)
                    {
                        Console.WriteLine("Failed : part==null expected");
                        continue;
                    }
                    if (cr > 1.5 * cgg[i])
                    {
                        Console.WriteLine($"Failed : poor efficiency {cr} (should be {cgg[i]})");
                        continue;
                    }
                    Console.WriteLine("Passed");
                }
                catch (Lab03Exception e)
                {
                    if (res[i] == null)
                    {
                        Console.WriteLine("Passed");
                    }
                    else
                    {
                        Console.WriteLine($"Failed : {e.GetType()} : {e.Message}");
                    }
                }
                catch (System.Exception e) when(maskExceptions)
                {
                    Console.WriteLine($"Failed : {e.GetType()} : {e.Message}");
                }
            }
        }
Esempio n. 6
0
        public static void UnweightedTest()
        {
            Console.Out.WriteLine("Grafy nieważone");
            Graph grid3 = new AdjacencyListsGraph <SimpleAdjacencyList>(false, 9);

            for (int i = 0; i < 9; i += 3)
            {
                grid3.AddEdge(i, i + 1);
                grid3.AddEdge(i + 1, i + 2);
            }

            for (int i = 0; i < 3; i++)
            {
                grid3.AddEdge(i + 0, i + 3);
                grid3.AddEdge(i + 3, i + 6);
            }

            Graph path20 = new AdjacencyListsGraph <SimpleAdjacencyList>(false, 20);

            for (int i = 1; i < path20.VerticesCount; i++)
            {
                path20.AddEdge(i - 1, i);
            }

            Graph shiftedPath20 = new AdjacencyListsGraph <SimpleAdjacencyList>(false, 20);

            shiftedPath20.AddEdge(18, 19);
            shiftedPath20.AddEdge(0, 1);
            shiftedPath20.AddEdge(19, 1);
            for (int i = 2; i < shiftedPath20.VerticesCount; i++)
            {
                shiftedPath20.AddEdge(i - 1, i);
            }

            RandomGraphGenerator rgg = new RandomGraphGenerator(240044);
            Graph eCycle24           = rgg.UndirectedCycle(typeof(AdjacencyListsGraph <SimpleAdjacencyList>), 24, 1, 1, true);
            Graph oCycle23           = rgg.UndirectedCycle(typeof(AdjacencyListsGraph <SimpleAdjacencyList>), 23, 1, 1, true);
            Graph iso = new AdjacencyListsGraph <SimpleAdjacencyList>(false, 1024);

            TestSet set1 = new TestSet();

            set1.TestCases.Add(new MatchingTestCase(10, path20, 0, 10, 10));
            set1.TestCases.Add(new MatchingTestCase(10, shiftedPath20, 0, 10, 10));
            set1.TestCases.Add(new MatchingTestCase(10, eCycle24, 0, 12, 12));
            set1.TestCases.Add(new MatchingTestCase(10, oCycle23, 0, 11, 11));
            set1.TestCases.Add(new MatchingTestCase(10, grid3, 0, 4, 4));
            set1.TestCases.Add(new MatchingTestCase(10, grid3, 1, 5, 5));
            set1.TestCases.Add(new MatchingTestCase(10, grid3, 2 * grid3.EdgesCount - grid3.VerticesCount, grid3.EdgesCount, grid3.EdgesCount));
            set1.TestCases.Add(new MatchingTestCase(10, path20, 0, 10, 10));
            set1.TestCases.Add(new MatchingTestCase(10, shiftedPath20, 0, 10, 10));
            set1.TestCases.Add(new MatchingTestCase(10, path20, 1, 10, 10));
            set1.TestCases.Add(new MatchingTestCase(10, shiftedPath20, 1, 10, 10));
            set1.TestCases.Add(new MatchingTestCase(10, eCycle24, 0, 12, 12));
            set1.TestCases.Add(new MatchingTestCase(10, oCycle23, 0, 11, 11));
            set1.TestCases.Add(new MatchingTestCase(10, eCycle24, 1, 12, 12));
            set1.TestCases.Add(new MatchingTestCase(10, oCycle23, 1, 12, 12));
            set1.TestCases.Add(new MatchingTestCase(10, iso, 0, 0, 0));
            set1.TestCases.Add(new MatchingTestCase(10, iso, 10000, 0, 0));

            set1.PreformTests(true, false);
        }
Esempio n. 7
0
        static void Main(string[] args)
        {
            GraphExport          ge  = new GraphExport();
            RandomGraphGenerator rgg = new RandomGraphGenerator();

            Graph[] directedGraphs = new Graph[8];
            directedGraphs[0] = new AdjacencyListsGraph <AVLAdjacencyList>(true, 3)
            {
                new Edge(0, 1), new Edge(1, 2), new Edge(2, 0)
            };
            directedGraphs[1] = new AdjacencyListsGraph <SimpleAdjacencyList>(true, 3)
            {
                new Edge(0, 1), new Edge(1, 2), new Edge(2, 1)
            };
            directedGraphs[2] = new AdjacencyMatrixGraph(true, 4)
            {
                new Edge(0, 1), new Edge(0, 2), new Edge(1, 3), new Edge(2, 3)
            };
            directedGraphs[3] = new AdjacencyListsGraph <HashTableAdjacencyList>(true, 10);
            directedGraphs[4] = new AdjacencyMatrixGraph(true, 10)
            {
                new Edge(0, 1), new Edge(0, 2), new Edge(0, 3), new Edge(2, 4), new Edge(2, 5), new Edge(2, 6),
                new Edge(5, 7), new Edge(5, 8), new Edge(5, 9), new Edge(6, 5), new Edge(7, 8), new Edge(8, 2)
            };
            rgg.SetSeed(111);
            directedGraphs[5] = rgg.DirectedGraph(typeof(AdjacencyMatrixGraph), 100, 0.2);
            rgg.SetSeed(222);
            directedGraphs[6] = rgg.DirectedCycle(typeof(AdjacencyListsGraph <SimpleAdjacencyList>), 1000);
            rgg.SetSeed(333);
            directedGraphs[7] = rgg.DAG(typeof(AdjacencyMatrixGraph), 200, 0.2, 1, 1);

            TestSet findCycleDirected = new TestSet();

            findCycleDirected.TestCases.Add(new FindCycleTestCase(5, null, directedGraphs[0], true));
            findCycleDirected.TestCases.Add(new FindCycleTestCase(5, null, directedGraphs[1], true));
            findCycleDirected.TestCases.Add(new FindCycleTestCase(5, null, directedGraphs[2], false));
            findCycleDirected.TestCases.Add(new FindCycleTestCase(5, null, directedGraphs[3], false));
            findCycleDirected.TestCases.Add(new FindCycleTestCase(5, null, directedGraphs[4], true));
            findCycleDirected.TestCases.Add(new FindCycleTestCase(5, null, directedGraphs[5], true));
            findCycleDirected.TestCases.Add(new FindCycleTestCase(5, null, directedGraphs[6], true));
            findCycleDirected.TestCases.Add(new FindCycleTestCase(5, null, directedGraphs[7], false));

            Graph[] undirectedGraphs = new Graph[6];
            undirectedGraphs[0] = new AdjacencyListsGraph <AVLAdjacencyList>(false, 3)
            {
                new Edge(0, 1), new Edge(1, 2), new Edge(2, 0)
            };
            undirectedGraphs[1] = new AdjacencyListsGraph <SimpleAdjacencyList>(false, 4)
            {
                new Edge(0, 1), new Edge(1, 2), new Edge(2, 3), new Edge(3, 1)
            };
            undirectedGraphs[2] = new AdjacencyListsGraph <HashTableAdjacencyList>(false, 10);
            undirectedGraphs[3] = new AdjacencyMatrixGraph(false, 10)
            {
                new Edge(0, 1), new Edge(0, 2), new Edge(0, 3), new Edge(2, 4), new Edge(2, 5), new Edge(2, 6),
                new Edge(5, 7), new Edge(5, 8), new Edge(5, 9), new Edge(8, 2)
            };
            rgg.SetSeed(444);
            undirectedGraphs[4] = rgg.UndirectedGraph(typeof(AdjacencyMatrixGraph), 100, 0.2);
            rgg.SetSeed(555);
            undirectedGraphs[5] = rgg.TreeGraph(typeof(AdjacencyListsGraph <SimpleAdjacencyList>), 1000, 1.0);

            TestSet findCycleUndirected = new TestSet();

            findCycleUndirected.TestCases.Add(new FindCycleTestCase(5, null, undirectedGraphs[0], true));
            findCycleUndirected.TestCases.Add(new FindCycleTestCase(5, null, undirectedGraphs[1], true));
            findCycleUndirected.TestCases.Add(new FindCycleTestCase(5, null, undirectedGraphs[2], false));
            findCycleUndirected.TestCases.Add(new FindCycleTestCase(5, null, undirectedGraphs[3], true));
            findCycleUndirected.TestCases.Add(new FindCycleTestCase(5, null, undirectedGraphs[4], true));
            findCycleUndirected.TestCases.Add(new FindCycleTestCase(5, null, undirectedGraphs[5], false));

            Graph[] trees = new Graph[10];
            trees[0] = new AdjacencyListsGraph <AVLAdjacencyList>(false, 3)
            {
                new Edge(0, 1), new Edge(1, 2)
            };
            trees[1] = new AdjacencyListsGraph <SimpleAdjacencyList>(false, 4)
            {
                new Edge(0, 1), new Edge(1, 2), new Edge(2, 3)
            };
            trees[2] = new AdjacencyListsGraph <HashTableAdjacencyList>(false, 1);
            trees[3] = new AdjacencyListsGraph <HashTableAdjacencyList>(false, 2)
            {
                new Edge(0, 1)
            };
            trees[4] = new AdjacencyMatrixGraph(false, 5)
            {
                new Edge(1, 3), new Edge(2, 4)
            };
            trees[5] = new AdjacencyMatrixGraph(true, 3)
            {
                new Edge(0, 1), new Edge(0, 2)
            };
            rgg.SetSeed(777);
            trees[6] = rgg.UndirectedGraph(typeof(AdjacencyMatrixGraph), 100, 0.2);
            rgg.SetSeed(888);
            trees[7] = rgg.TreeGraph(typeof(AdjacencyListsGraph <SimpleAdjacencyList>), 1000, 1.0);
            rgg.SetSeed(999);
            trees[8] = rgg.TreeGraph(typeof(AdjacencyListsGraph <SimpleAdjacencyList>), 1001, 1.0);
            trees[9] = new AdjacencyListsGraph <SimpleAdjacencyList>(false, 10);
            for (int i = 1; i < 10; ++i)
            {
                trees[9].AddEdge(i - 1, i);
            }

            TestSet treeCenter = new TestSet();

            treeCenter.TestCases.Add(new TreeCenterTestCase(5, null, trees[0], true, new int[] { 1 }));
            treeCenter.TestCases.Add(new TreeCenterTestCase(5, null, trees[1], true, new int[] { 1, 2 }));
            treeCenter.TestCases.Add(new TreeCenterTestCase(5, null, trees[2], true, new int[] { 0 }));
            treeCenter.TestCases.Add(new TreeCenterTestCase(5, null, trees[3], true, new int[] { 0, 1 }));
            treeCenter.TestCases.Add(new TreeCenterTestCase(5, null, trees[4], false, null));
            treeCenter.TestCases.Add(new TreeCenterTestCase(5, new ArgumentException(), trees[5], false, null));
            treeCenter.TestCases.Add(new TreeCenterTestCase(5, null, trees[6], false, null));
            treeCenter.TestCases.Add(new TreeCenterTestCase(5, null, trees[7], true, new int[] { 305, 786 }));
            treeCenter.TestCases.Add(new TreeCenterTestCase(5, null, trees[8], true, new int[] { 60 }));
            treeCenter.TestCases.Add(new TreeCenterTestCase(5, null, trees[9], true, new int[] { 4, 5 }));

            //
            // Odkomentuj odpowiednią linię aby zobaczyć wybrany graf
            // Pamiętaj, że przykłady numerowane są od 1
            //
            //ge.Export(directedGraphs[0]);
            //ge.Export(directedGraphs[1]);
            //ge.Export(directedGraphs[2]);
            //ge.Export(directedGraphs[3]);
            //ge.Export(directedGraphs[4]);
            //ge.Export(directedGraphs[5]);
            //ge.Export(directedGraphs[6]);
            //ge.Export(directedGraphs[7]);
            //ge.Export(undirectedGraphs[0]);
            //ge.Export(undirectedGraphs[1]);
            //ge.Export(undirectedGraphs[2]);
            ge.Export(undirectedGraphs[3]);
            //ge.Export(undirectedGraphs[4]);
            //ge.Export(undirectedGraphs[5]);
            //ge.Export(trees[0]);
            //ge.Export(trees[1]);
            //ge.Export(trees[2]);
            //ge.Export(trees[3]);
            //ge.Export(trees[4]);
            //ge.Export(trees[5]);
            //ge.Export(trees[6]);
            //ge.Export(trees[7]);
            //ge.Export(trees[8]);

            long[]    timeElapsed = new long[9];
            Stopwatch stopwatch   = new Stopwatch();


            Console.WriteLine("\nCycle Finding\n");

            FindCycleTestCase.ResultOnly = true;
            Console.WriteLine("\nDirected Graphs - result only");
            stopwatch.Start();
            findCycleDirected.PreformTests(verbose: true, checkTimeLimit: false);
            stopwatch.Stop();
            timeElapsed[0] = stopwatch.ElapsedMilliseconds;
            Console.WriteLine("\nUndirected Graphs - result only");
            stopwatch.Restart();
            findCycleUndirected.PreformTests(verbose: true, checkTimeLimit: false);
            stopwatch.Stop();
            timeElapsed[1] = stopwatch.ElapsedMilliseconds;

            FindCycleTestCase.ResultOnly = false;
            Console.WriteLine("\nDirected Graphs - full funcionality");
            stopwatch.Restart();
            findCycleDirected.PreformTests(verbose: true, checkTimeLimit: false, forceExecution: false);
            stopwatch.Stop();
            timeElapsed[2] = stopwatch.ElapsedMilliseconds;
            Console.WriteLine("\nUndirected Graphs - full funcionality");
            stopwatch.Restart();
            findCycleUndirected.PreformTests(verbose: true, checkTimeLimit: false, forceExecution: false);
            stopwatch.Stop();
            timeElapsed[3] = stopwatch.ElapsedMilliseconds;

            Console.WriteLine("\nTree Center\n");
            TreeCenterTestCase.ResultOnly = true;
            Console.WriteLine("\nResult only");
            stopwatch.Restart();
            treeCenter.PreformTests(verbose: true, checkTimeLimit: false);
            stopwatch.Stop();
            timeElapsed[4] = stopwatch.ElapsedMilliseconds;
            Console.WriteLine("\nFull funcionality");
            TreeCenterTestCase.ResultOnly = false;

            stopwatch.Restart();
            treeCenter.PreformTests(verbose: true, checkTimeLimit: false, forceExecution: false);
            stopwatch.Stop();
            timeElapsed[5] = stopwatch.ElapsedMilliseconds;

            Console.WriteLine("\n\nPerformance metrics for the initial task tests");
            for (int i = 0; i < 6; i++)
            {
                Console.WriteLine("Testset {0}: {1,5} ms", i + 1, timeElapsed[i]);
            }



            // Nie radzę tych grafów próbować eksportować za pomocą GraphViz (mają sporo wierzchołków)
            // Wyjątek stanowi customTrees[0]
            Console.WriteLine("\n\nCustom tests");
            Console.WriteLine("Generating graphs. This may take a while");

            // Cycle in directed graphs
            Graph[] customDirectedGraphs = new Graph[5];
            customDirectedGraphs[0] = rgg.DirectedGraph(typeof(AdjacencyMatrixGraph), 100, 0.7);
            customDirectedGraphs[1] = rgg.DAG(typeof(AdjacencyListsGraph <AVLAdjacencyList>), 1000, 0.9, 1, 1);
            customDirectedGraphs[2] = rgg.DirectedCycle(typeof(AdjacencyListsGraph <AVLAdjacencyList>), 1000);
            customDirectedGraphs[3] = rgg.DirectedGraph(typeof(AdjacencyListsGraph <AVLAdjacencyList>), 1000, 0.5);
            customDirectedGraphs[4] = rgg.DirectedGraph(typeof(AdjacencyListsGraph <AVLAdjacencyList>), 1000, 0.7);

            TestSet customDirectedGraphsTestSet = new TestSet();

            customDirectedGraphsTestSet.TestCases.Add(new FindCycleTestCase(5, null, customDirectedGraphs[0], true));
            customDirectedGraphsTestSet.TestCases.Add(new FindCycleTestCase(5, null, customDirectedGraphs[1], false));
            customDirectedGraphsTestSet.TestCases.Add(new FindCycleTestCase(5, null, customDirectedGraphs[2], true));
            customDirectedGraphsTestSet.TestCases.Add(new FindCycleTestCase(5, null, customDirectedGraphs[3], true));
            customDirectedGraphsTestSet.TestCases.Add(new FindCycleTestCase(5, null, customDirectedGraphs[4], true));


            // Cycle in undirected graphs
            Graph[] customUndirectedGraphs = new Graph[5];
            customUndirectedGraphs[0] = rgg.UndirectedGraph(typeof(AdjacencyMatrixGraph), 100, 0.7);
            customUndirectedGraphs[1] = rgg.TreeGraph(typeof(AdjacencyListsGraph <AVLAdjacencyList>), 1000, 1);
            customUndirectedGraphs[2] = rgg.UndirectedCycle(typeof(AdjacencyListsGraph <AVLAdjacencyList>), 1000);
            customUndirectedGraphs[3] = rgg.UndirectedGraph(typeof(AdjacencyListsGraph <AVLAdjacencyList>), 1000, 0.5);
            customUndirectedGraphs[4] = rgg.UndirectedGraph(typeof(AdjacencyListsGraph <AVLAdjacencyList>), 1000, 0.7);

            TestSet customUndirectedGraphsTestSet = new TestSet();

            customUndirectedGraphsTestSet.TestCases.Add(new FindCycleTestCase(5, null, customUndirectedGraphs[0], true));
            customUndirectedGraphsTestSet.TestCases.Add(new FindCycleTestCase(5, null, customUndirectedGraphs[1], false));
            customUndirectedGraphsTestSet.TestCases.Add(new FindCycleTestCase(5, null, customUndirectedGraphs[2], true));
            customUndirectedGraphsTestSet.TestCases.Add(new FindCycleTestCase(5, null, customUndirectedGraphs[3], true));
            customUndirectedGraphsTestSet.TestCases.Add(new FindCycleTestCase(5, null, customUndirectedGraphs[4], true));

            // Tree center
            Graph[] customTrees = new Graph[5];
            customTrees[0] = new AdjacencyMatrixGraph(false, 1);
            customTrees[1] = rgg.TreeGraph(typeof(AdjacencyMatrixGraph), 100, 0.5);
            customTrees[2] = rgg.TreeGraph(typeof(AdjacencyListsGraph <SimpleAdjacencyList>), 100, 1);
            customTrees[3] = rgg.TreeGraph(typeof(AdjacencyListsGraph <AVLAdjacencyList>), 1000, 1);
            customTrees[4] = rgg.TreeGraph(typeof(AdjacencyListsGraph <AVLAdjacencyList>), 10000, 1);

            TestSet customTreeSet = new TestSet();

            customTreeSet.TestCases.Add(new TreeCenterTestCase(5, null, customTrees[0], true, new int[] { 0 }));
            customTreeSet.TestCases.Add(new TreeCenterTestCase(5, null, customTrees[1], false, null));
            customTreeSet.TestCases.Add(new TreeCenterTestCase(5, null, customTrees[2], true, new int[] { 77 }));
            customTreeSet.TestCases.Add(new TreeCenterTestCase(5, null, customTrees[3], true, new int[] { 146, 282 }));
            customTreeSet.TestCases.Add(new TreeCenterTestCase(5, null, customTrees[4], true, new int[] { 6780, 8396 }));



            Console.WriteLine("Custom cycle finding in directed graphs");
            stopwatch.Restart();
            customDirectedGraphsTestSet.PreformTests(verbose: true, checkTimeLimit: false, forceExecution: false);
            stopwatch.Stop();
            timeElapsed[6] = stopwatch.ElapsedMilliseconds;

            Console.WriteLine("Custom cycle finding in undirected graphs");
            stopwatch.Restart();
            customUndirectedGraphsTestSet.PreformTests(verbose: true, checkTimeLimit: false, forceExecution: false);
            stopwatch.Stop();
            timeElapsed[7] = stopwatch.ElapsedMilliseconds;

            Console.WriteLine("Custom tree center");
            stopwatch.Restart();
            customTreeSet.PreformTests(verbose: true, checkTimeLimit: false, forceExecution: false);
            stopwatch.Stop();
            timeElapsed[8] = stopwatch.ElapsedMilliseconds;


            Console.WriteLine("\n\nPerformance metrics for the custom tests");
            for (int i = 6; i < timeElapsed.Length; i++)
            {
                Console.WriteLine("Testset {0}: {1,5} ms", i + 1, timeElapsed[i]);
            }
        }