Example #1
0
    public static void PrepareTests2()
    {
        int n;
        var rgg = new RandomGraphGenerator();

        cliq_test2 = new Graph[3];
        izo_test2  = new Graph[2, 2];

        cliq_res2 = new int[] { 3, 3, 5 };
        izo_res2  = new bool[] { false, true };

        if (cliq_test2.Length != cliq_res2.Length || izo_test2.GetLongLength(0) != izo_res2.Length)
        {
            throw new ApplicationException("Zle zddefiniowane testy");
        }

        rgg.SetSeed(123);
        cliq_test2[0] = rgg.UndirectedGraph(typeof(AdjacencyListsGraph <HashTableAdjacencyList>), 4000, 0.001);
        rgg.SetSeed(125);
        cliq_test2[1] = rgg.DirectedGraph(typeof(AdjacencyListsGraph <HashTableAdjacencyList>), 3000, 0.05);

        n             = 1500;
        cliq_test2[2] = new AdjacencyListsGraph <HashTableAdjacencyList>(false, n);
        for (int i = 0; i < n; ++i)
        {
            for (int j = 1; j <= 4; ++j)
            {
                cliq_test2[2].AddEdge(i, (i + j) % n);
            }
        }

        n = 50;
        izo_test2[0, 0] = new AdjacencyMatrixGraph(true, n);
        for (int i = 0; i < n; ++i)
        {
            for (int j = 0; j < n; ++j)
            {
                if (i != j)
                {
                    izo_test2[0, 0].AddEdge(i, j);
                }
            }
        }
        izo_test2[0, 1] = izo_test2[0, 0].Clone();
        for (int i = 0; i < n; ++i)
        {
            izo_test2[0, 0].DelEdge(i, (i + 1) % n);
        }
        for (int i = 0; i < n; ++i)
        {
            izo_test2[0, 1].DelEdge(i, (i + 2) % n);
        }

        rgg.SetSeed(1234);
        izo_test2[1, 0] = rgg.DirectedGraph(typeof(AdjacencyMatrixGraph), 2500, 0.95, 1, 999);
        izo_test2[1, 1] = new AdjacencyListsGraph <HashTableAdjacencyList>(izo_test2[1, 0]);
        izo_test2[1, 1] = rgg.Permute(izo_test2[1, 1]);
    }
Example #2
0
 public static void Main(string[] args)
 {
     generator.SetSeed(0);
     BipartiteMatchings();
     Console.WriteLine("*****************");
     ConstrainedMaxFlow();
     Console.WriteLine("*****************");
     MaxIndependentPaths();
 }
Example #3
0
    public static void Main()
    {
        bool   b;
        double mst;
        Graph  g, t;

        Edge[] ep;
        var    rgg = new RandomGraphGenerator();
        var    ge  = new GraphExport();

        // nieskierowany - cykl
        rgg.SetSeed(1111);
        g = rgg.EulerGraph(typeof(AdjacencyMatrixGraph), false, 6, 1, 1, 4);
        b = g.Lab04_Euler(out ep);
        if (b)
        {
            Console.WriteLine("Znaleziono cykl Eulera w grafie 1 - ma {0} krawedzi", ep.Length);
            ge.Export(Construct(g.VerticesCount, ep));
        }
        else
        {
            Console.WriteLine("Nie znaleziono sciezki Eulera w grafie 1");
        }

        // skierowany - cykl
        rgg.SetSeed(2222);
        g = rgg.EulerGraph(typeof(AdjacencyMatrixGraph), true, 5, 1, 1, 3);
        b = g.Lab04_Euler(out ep);
        if (b)
        {
            Console.WriteLine("Znaleziono cykl Eulera w grafie 2 - ma {0} krawedzi", ep.Length);
            //ge.Export(Construct(g.VerticesCount, ep));
        }
        else
        {
            Console.WriteLine("Nie znaleziono sciezki Eulera w grafie 2");
        }

        // nieskierowany - sciezka
        rgg.SetSeed(3333);
        g = rgg.SemiEulerGraph(typeof(AdjacencyMatrixGraph), false, 6, 1, 1, 4);
        b = g.Lab04_Euler(out ep);
        if (b)
        {
            Console.WriteLine("Znaleziono sciezke Eulera w grafie 3 - ma {0} krawedzi", ep.Length);
            //ge.Export(Construct(g.VerticesCount, ep));
        }
        else
        {
            Console.WriteLine("Nie znaleziono sciezki Eulera w grafie 3");
        }

        // skierowany - sciezka
        rgg.SetSeed(4444);
        g = rgg.SemiEulerGraph(typeof(AdjacencyMatrixGraph), true, 5, 1, 1, 3);
        b = g.Lab04_Euler(out ep);
        if (b)
        {
            Console.WriteLine("Znaleziono sciezke Eulera w grafie 4 - ma {0} krawedzi", ep.Length);
            //ge.Export(Construct(g.VerticesCount, ep));
        }
        else
        {
            Console.WriteLine("Nie znaleziono sciezki Eulera w grafie 4");
        }

        // drzewo
        rgg.SetSeed(5555);
        g   = rgg.UndirectedGraph(typeof(AdjacencyMatrixGraph), 20, 0.5, -10, 50);
        mst = g.Lab04_Kruskal(out t);
        Console.WriteLine("Minimalne drzewo ma wage {0}", mst);
        //ge.Export(t);
    }
Example #4
0
    public static void Main()
    {
        int[] backtrackingColors;
        int[] greedyColors;
        int   n, i, j, mb, mg;
        long  counter0, counter1, counter2;

        string[] message1 = { "Zwykly maly graf:",
                              "Maly dwudzielny:",
                              "Mala klika:" };
        int[]    bestColorsNumbers1 = { 4, 2, 9 };
        string[] message2           = { "Zwykly graf:",
                                        "Graf dwudzielny:",
                                        "Cykl parzysty:",
                                        "Klika:" };
        int[]    bestColorsNumbers2 = { 6, 2, 2, 200 };
        string[] message3           = { "Zwykly duzy graf:",
                                        "Duzy dwudzielny:",
                                        "Duza klika:" };
        int[]    bestColorsNumbers3 = { 59, 2, 4000 };
        Graph[]  g1  = new Graph[message1.Length];
        Graph[]  g2  = new Graph[message2.Length];
        Graph[]  g3  = new Graph[message3.Length];
        var      rgg = new RandomGraphGenerator();

        Console.WriteLine();
        Console.WriteLine("Generowanie grafow");
        Console.WriteLine();

        rgg.SetSeed(101);
        g1[0] = rgg.UndirectedGraph(typeof(AdjacencyMatrixGraph), 8, 0.5);
        rgg.SetSeed(102);
        g1[1] = rgg.BipariteGraph(typeof(AdjacencyMatrixGraph), 5, 3, 0.75);
        n     = 9;
        g1[2] = new AdjacencyMatrixGraph(false, n);
        for (i = 0; i < n; ++i)
        {
            for (j = i + 1; j < n; ++j)
            {
                g1[2].AddEdge(i, j);
            }
        }

        rgg.SetSeed(103);
        g2[0] = rgg.UndirectedGraph(typeof(AdjacencyMatrixGraph), 20, 0.5);
        rgg.SetSeed(104);
        g2[1] = rgg.BipariteGraph(typeof(AdjacencyMatrixGraph), 30, 20, 0.25);
        n     = 50;
        g2[2] = new AdjacencyListsGraph <SimpleAdjacencyList>(false, n);
        for (i = 1; i < n; ++i)
        {
            g2[2].AddEdge(i - 1, i);
        }
        g2[2].AddEdge(n - 1, 0);
        rgg.SetSeed(105);
        g2[2] = rgg.Permute(g2[2]);
        n     = 200;
        g2[3] = new AdjacencyMatrixGraph(false, n);
        for (i = 0; i < n; ++i)
        {
            for (j = i + 1; j < n; ++j)
            {
                g2[3].AddEdge(i, j);
            }
        }

        rgg.SetSeed(106);
        g3[0] = rgg.UndirectedGraph(typeof(AdjacencyMatrixGraph), 75, 0.99);
        rgg.SetSeed(107);
        g3[1] = rgg.BipariteGraph(typeof(AdjacencyMatrixGraph), 2000, 2000, 0.55);
        n     = 5000;
        g3[2] = new AdjacencyMatrixGraph(false, n);
        for (i = 0; i < n; ++i)
        {
            for (j = i + 1; j < n; ++j)
            {
                g3[2].AddEdge(i, j);
            }
        }

        Console.WriteLine("Grafy za 1 pkt");
        Console.WriteLine();
        for (i = 0; i < g1.Length; ++i)
        {
            counter0 = (long)Graph.Counter;
            mb       = g1[i].BacktrackingColor(out backtrackingColors);
            counter1 = (long)Graph.Counter;
            mg       = g1[i].GreedyColor(out greedyColors);
            counter2 = (long)Graph.Counter;
            Console.WriteLine("{0,-17}  liczba wierzcholkow  {1,4},  optymalna liczba kolorow  {2,4}", message1[i], g1[i].VerticesCount, bestColorsNumbers1[i]);
            Console.WriteLine("  Backtracking:    liczba kolorow  {0,4},  zlozonosc  {1,8}", mb, counter1 - counter0);
            Console.WriteLine("  Greedy:          liczba kolorow  {0,4},  zlozonosc  {1,8}", mg, counter2 - counter1);
            Console.WriteLine();
        }

        Console.WriteLine("Grafy za 2 pkt");
        Console.WriteLine();
        for (i = 0; i < g2.Length; ++i)
        {
            counter0 = (long)Graph.Counter;
            mb       = g2[i].BacktrackingColor(out backtrackingColors);
            counter1 = (long)Graph.Counter;
            mg       = g2[i].GreedyColor(out greedyColors);
            counter2 = (long)Graph.Counter;
            Console.WriteLine("{0,-17}  liczba wierzcholkow  {1,4},  optymalna liczba kolorow  {2,4}", message2[i], g2[i].VerticesCount, bestColorsNumbers2[i]);
            Console.WriteLine("  Backtracking:    liczba kolorow  {0,4},  zlozonosc  {1,8}", mb, counter1 - counter0);
            Console.WriteLine("  Greedy:          liczba kolorow  {0,4},  zlozonosc  {1,8}", mg, counter2 - counter1);
            Console.WriteLine();
        }

        Console.WriteLine("Grafy za 3 pkt");
        Console.WriteLine();
        for (i = 0; i < g3.Length; ++i)
        {
            counter0 = (long)Graph.Counter;
            mb       = g3[i].BacktrackingColor(out backtrackingColors);
            counter1 = (long)Graph.Counter;
            mg       = g3[i].GreedyColor(out greedyColors);
            counter2 = (long)Graph.Counter;
            Console.WriteLine("{0,-17}  liczba wierzcholkow  {1,4},  optymalna liczba kolorow  {2,4}", message3[i], g3[i].VerticesCount, bestColorsNumbers3[i]);
            Console.WriteLine("  Backtracking:    liczba kolorow  {0,4},  zlozonosc  {1,8}", mb, counter1 - counter0);
            Console.WriteLine("  Greedy:          liczba kolorow  {0,4},  zlozonosc  {1,8}", mg, counter2 - counter1);
            Console.WriteLine();
        }

        Console.WriteLine("Koniec");
        Console.WriteLine();
    }
Example #5
0
    public static void Main()
    {
        var ge  = new GraphExport();
        var rgg = new RandomGraphGenerator();

        Graph g1 = new AdjacencyMatrixGraph(true, 15);

        g1.AddEdge(0, 2);
        g1.AddEdge(0, 7);
        g1.AddEdge(1, 0);
        g1.AddEdge(2, 1);
        g1.AddEdge(2, 3);
        g1.AddEdge(2, 7);
        g1.AddEdge(3, 5);
        g1.AddEdge(4, 3);
        g1.AddEdge(5, 4);
        g1.AddEdge(5, 6);
        g1.AddEdge(6, 7);
        g1.AddEdge(7, 6);
        g1.AddEdge(7, 8);
        g1.AddEdge(8, 9);
        g1.AddEdge(10, 0);
        g1.AddEdge(12, 13);
        g1.AddEdge(13, 14);
        g1.AddEdge(14, 12);
        //ge.Export(g1,null, "g1");

        int[] scc;
        int   n;

        n = g1.StronglyConnectedComponents(out scc);
        Console.WriteLine("\nLiczba silnie spojnych skladowych: {0} (powinno byc 8)", n);
        for (int c = 0; c < n; ++c)
        {
            Console.WriteLine();
            Console.Write("skladowa {0}:", c);
            for (int v = 0; v < g1.VerticesCount; ++v)
            {
                if (scc[v] == c)
                {
                    Console.Write(" {0}", v);
                }
            }
        }
        Console.WriteLine();

        Graph k1 = g1.Kernel();

        //ge.Export(k1, null, "k1");

        rgg.SetSeed(500);
        Graph g2 = rgg.DirectedGraph(typeof(AdjacencyMatrixGraph), 1000, 0.005);

        n = g2.StronglyConnectedComponents(out scc);
        Console.WriteLine("\nLiczba silnie spojnych skladowych: {0} (powinno byc 17)", n);

        Graph g3 = rgg.UndirectedGraph(typeof(AdjacencyMatrixGraph), 1000, 0.1);

        try
        {
            n = g3.StronglyConnectedComponents(out scc);
            Console.WriteLine("\nBlad - powinien byc wyjatek");
        }
        catch (ArgumentException e)
        {
            Console.WriteLine("\n" + e.Message + "  - dobrze");
        }

        Console.WriteLine("\nSciezka maksymalnie powiekszajaca");
        PathsInfo[] d;
        bool        b;
        Graph       m1 = new AdjacencyMatrixGraph(true, 7);

        m1.AddEdge(0, 2, 10);
        m1.AddEdge(2, 3, 4);
        m1.AddEdge(2, 5, 3);
        m1.AddEdge(2, 4, 7);
        m1.AddEdge(3, 1, 2);
        m1.AddEdge(4, 1, 1);
        m1.AddEdge(5, 6, 5);
        m1.AddEdge(6, 1, 4);
        //ge.Export(m1, null, "m1");

        b = m1.MaxFlowPathsLab05(2, out d);
        Console.WriteLine("graf m1");
        for (int v = 0; v < m1.VerticesCount; ++v)
        {
            Console.WriteLine("przepustowosc od 2 do {0} wynosi {1}", v, d[v].Dist ?? 0);
        }

        int[] p = { 55, 33, 65, 73, 0, 73, 84, 76, 45, 78 };
        rgg.SetSeed(200);
        Graph m2 = rgg.UndirectedGraph(typeof(AdjacencyMatrixGraph), 200, 0.02, 1, 99);

        b = m2.MaxFlowPathsLab05(5, out d);
        Console.WriteLine("graf m2");
        for (int v = 10; v < 20; ++v)
        {
            Console.WriteLine("przepustowosc od 5 do do {0} wynosi {1} ({2})", v, d[v].Dist ?? 0, (d[v].Dist ?? 0) == p[v - 10]?"OK":"blad");
        }

        Console.WriteLine("\nTesty Acyklicznosci\n");
        Graph a1, a2, a3, a4, a5;
        bool  b1, b2, b3, b4, b5;

        rgg.SetSeed(101);
        a1 = rgg.TreeGraph(typeof(AdjacencyListsGraph <SimplyAdjacencyList>), 10, 1);
//        ge.Export(a1,"a1");
        b1 = a1.IsUndirectedAcyclic();
        Console.WriteLine("Czy graf a1 jest acykliczny ? : {0} (powinno byc True)", b1);

        rgg.SetSeed(102);
        a2 = rgg.TreeGraph(typeof(AdjacencyListsGraph <SimplyAdjacencyList>), 15, 1);
        a2.DelEdge(1, 7);
        a2.DelEdge(6, 12);
//        ge.Export(a2,"a2");
        b2 = a2.IsUndirectedAcyclic();
        Console.WriteLine("Czy graf a2 jest acykliczny ? : {0} (powinno byc True)", b2);

        rgg.SetSeed(103);
        a3 = rgg.UndirectedGraph(typeof(AdjacencyMatrixGraph), 10, 0.3);
//        ge.Export(a3,"a3");
        b3 = a3.IsUndirectedAcyclic();
        Console.WriteLine("Czy graf a3 jest acykliczny ? : {0} (powinno byc False)", b3);

        rgg.SetSeed(104);
        a4 = rgg.DirectedGraph(typeof(AdjacencyMatrixGraph), 1000, 0.1);
        try
        {
            b4 = a4.IsUndirectedAcyclic();
            Console.WriteLine("Blad - powinien byc wyjatek");
        }
        catch (ArgumentException e)
        {
            Console.WriteLine(e.Message);
        }
        Console.WriteLine("Czy graf a4 jest acykliczny ? (przed chwila powinien byc wyjatek)");

        rgg.SetSeed(105);
        a5 = rgg.UndirectedGraph(typeof(AdjacencyMatrixGraph), 2000, 0.8);
        b5 = a5.IsUndirectedAcyclic();
        Console.WriteLine("Czy graf a5 jest acykliczny ? : {0} (powinno byc False)", b5);

        Console.WriteLine("KONIEC !!!\n");
    }
Example #6
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);
        }
        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]);

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

            FindCycleTestCase.ResultOnly = true;
            Console.WriteLine("\nDirected Graphs - result only");
            findCycleDirected.PreformTests(verbose: true, checkTimeLimit: false);
            Console.WriteLine("\nUndirected Graphs - result only");
            findCycleUndirected.PreformTests(verbose: true, checkTimeLimit: false);

            FindCycleTestCase.ResultOnly = false;
            Console.WriteLine("\nDirected Graphs - full funcionality");
            findCycleDirected.PreformTests(verbose: true, checkTimeLimit: false, forceExecution: false);
            Console.WriteLine("\nUndirected Graphs - full funcionality");
            findCycleUndirected.PreformTests(verbose: true, checkTimeLimit: false, forceExecution: false);

            Console.WriteLine("\nTree Center\n");
            TreeCenterTestCase.ResultOnly = true;
            Console.WriteLine("\nResult only");
            treeCenter.PreformTests(verbose: true, checkTimeLimit: false);
            Console.WriteLine("\nFull funcionality");
            TreeCenterTestCase.ResultOnly = false;
            treeCenter.PreformTests(verbose: true, checkTimeLimit: false, forceExecution: false);
        }
        public override void PrepareTestSets()
        {
            var rgg = new RandomGraphGenerator();
            int n1  = 12;
            int n2  = 9;
            int n2t = 7;

            int[][]  sequences     = new int[n1][];
            bool[]   is_graphic    = new bool[n1];
            string[] descriptions1 = new string[n1];

            int n3 = 13;

            Graph[]  graphs          = new Graph[n3];
            string[] descriptions3   = new string[n3];
            double[] expected_weight = new double[n3];
            double[] limits          = Enumerable.Repeat(1.0, n3).ToArray();
            limits[7]  = 2;    // cykl
            limits[10] = 2;    // niech bedzie z zapasem
            limits[12] = 300;  // duzy rzadki graf

            //Testy ciagu grafowego
            sequences[0]     = new int[] { 0 };
            is_graphic[0]    = true;
            descriptions1[0] = "{ 0 }";

            sequences[1]     = new int[] { 1, 1, 1, 1 };
            is_graphic[1]    = true;
            descriptions1[1] = "{ 1, 1, 1, 1 }";

            sequences[2]     = new int[] { 5, 5, 5, 5, 5, 5 };
            is_graphic[2]    = true;
            descriptions1[2] = "{ 5, 5, 5, 5, 5, 5 }";

            sequences[3]     = new int[] { 1, 3, 5, 2, 3, 3, 3, 1, 3, 5, 2, 3, 3, 3 };
            is_graphic[3]    = true;
            descriptions1[3] = "{ 1, 3, 5, 2, 3, 3, 3, 1, 3, 5, 2, 3, 3, 3 }";

            sequences[4]     = new int[] { 5, 5, 5, 0, 5, 5, 5 };
            is_graphic[4]    = true;
            descriptions1[4] = "{ 5, 5, 5, 0, 5, 5, 5 }";

            sequences[5]     = new int[] { 4, 4, 3, 3, 2 };
            is_graphic[5]    = true;
            descriptions1[5] = "{ 4, 4, 3, 3, 2 }";

            sequences[6]     = new int[] { 2, 3, 2, 3, 2 };
            is_graphic[6]    = true;
            descriptions1[6] = "{ 2, 3, 2, 3, 2 }";

            sequences[7]     = new int[] { 1, 1, 1, 1, 3 };
            is_graphic[7]    = false;
            descriptions1[7] = "{ 1, 1, 1, 1, 3 }";

            sequences[8]     = new int[] { 1, 1, -1, 1 };
            is_graphic[8]    = false;
            descriptions1[8] = "{ 1, 1, -1, 1 }";

            sequences[9]     = new int[] { 1, 1, 1, 1, 6 };
            is_graphic[9]    = false;
            descriptions1[9] = "{ 1, 1, 1, 1, 6 }";

            sequences[10]     = new int[] { 4, 4, 3, 2, 1 };
            is_graphic[10]    = false;
            descriptions1[10] = "{ 4, 4, 3, 2, 1 }";

            sequences[11]     = new int[] { 1, 3, 1, 2, 4 };
            is_graphic[11]    = false;
            descriptions1[11] = "{ 1, 3, 1, 2, 4 }";



            //Testy drzewa rozpinajacego
            //przyklady z zajec

            //graf o jednym wierzcholku izolowanym
            graphs[0]          = new AdjacencyMatrixGraph(false, 1);
            descriptions3[0]   = "Graf o jednym wierzcholku izolowanym";
            expected_weight[0] = 0;

            //graf bez krawedzi
            graphs[1]          = new AdjacencyListsGraph <SimpleAdjacencyList>(false, 10);
            descriptions3[1]   = "Graf bez krawedzi";
            expected_weight[1] = 0;

            //pewien maly graf
            graphs[2] = new AdjacencyMatrixGraph(false, 5);
            graphs[2].AddEdge(0, 1, 1);
            graphs[2].AddEdge(0, 3, -1);
            graphs[2].AddEdge(1, 2, -3);
            graphs[2].AddEdge(2, 0, 2);
            graphs[2].AddEdge(2, 3, 2);
            graphs[2].AddEdge(3, 4, 10);
            descriptions3[2]   = "Pewien maly graf";
            expected_weight[2] = 7;

            //sciezka P6
            graphs[3] = new AdjacencyMatrixGraph(false, 6);
            graphs[3].AddEdge(0, 1, 1);
            graphs[3].AddEdge(1, 2, 2);
            graphs[3].AddEdge(2, 3, 3);
            graphs[3].AddEdge(3, 4, 4);
            graphs[3].AddEdge(4, 5, 5);
            descriptions3[3]   = "Sciezka P6";
            expected_weight[3] = 15;

            //klika K5
            rgg.SetSeed(12345);
            graphs[4]          = rgg.UndirectedGraph(typeof(AdjacencyMatrixGraph), 5, 1.0);
            descriptions3[4]   = "Klika K5";
            expected_weight[4] = 4;

            //cykl C5
            graphs[5] = new AdjacencyMatrixGraph(false, 5);
            graphs[5].AddEdge(0, 1, 1.5);
            graphs[5].AddEdge(1, 2, -1.5);
            graphs[5].AddEdge(2, 3, 3.2);
            graphs[5].AddEdge(3, 4, 4.8);
            graphs[5].AddEdge(4, 0, 2.3);
            descriptions3[5]   = "Cykl C5";
            expected_weight[5] = 5.5;

            //cykl
            int g6vc = 500;

            graphs[6] = new AdjacencyListsGraph <SimpleAdjacencyList>(false, g6vc);
            for (int i = 1; i < g6vc; ++i)
            {
                graphs[6].AddEdge(i - 1, i);
            }
            graphs[6].AddEdge(g6vc - 1, 0);
            descriptions3[6]   = "Cykl";
            expected_weight[6] = 499;

            //graf niespojny
            graphs[7] = new AdjacencyMatrixGraph(false, 12);
            graphs[7].AddEdge(0, 3, 3);
            graphs[7].AddEdge(0, 7, 2);
            graphs[7].AddEdge(3, 8, 3);
            graphs[7].AddEdge(7, 11, 2);
            graphs[7].AddEdge(11, 8, 4);
            graphs[7].AddEdge(7, 8, 5);
            graphs[7].AddEdge(1, 2, 1);
            graphs[7].AddEdge(2, 10, 5);
            graphs[7].AddEdge(1, 10, 3);
            graphs[7].AddEdge(2, 9, 4);
            graphs[7].AddEdge(4, 5, 1);
            graphs[7].AddEdge(5, 6, 2);
            graphs[7].AddEdge(4, 6, 3);
            descriptions3[7]   = "Graf niespojny";
            expected_weight[7] = 21;

            //grafy losowe

            int g8vc = 200;

            rgg.SetSeed(123451);
            graphs[8]          = rgg.UndirectedGraph(typeof(AdjacencyMatrixGraph), g8vc, 0.1, -100, -1);
            descriptions3[8]   = "Graf losowy o wagach ujemnych";
            expected_weight[8] = -18599;

            int g9vc = 200;

            rgg.SetSeed(123452);
            graphs[9]          = rgg.UndirectedGraph(typeof(AdjacencyListsGraph <SimpleAdjacencyList>), g9vc, 0.05, 1, 30);
            descriptions3[9]   = "Graf losowy o wagach dodatnich";
            expected_weight[9] = 867;

            int g10vc = 300;

            rgg.SetSeed(123453);
            graphs[10]          = rgg.UndirectedGraph(typeof(AdjacencyListsGraph <HashTableAdjacencyList>), g10vc, 0.02, -10, 40);
            descriptions3[10]   = "Graf losowy o wagach calkowitych";
            expected_weight[10] = 238;

            //graf skierowany
            graphs[11] = new AdjacencyMatrixGraph(true, 5);
            graphs[11].AddEdge(0, 1, 1);
            graphs[11].AddEdge(0, 3, -1);
            graphs[11].AddEdge(1, 2, -3);
            graphs[11].AddEdge(2, 0, 2);
            graphs[11].AddEdge(2, 3, 2);
            graphs[11].AddEdge(3, 4, 10);
            descriptions3[11]   = "Pewien maly graf skierowany";
            expected_weight[11] = 0;

            // graf dodatkowy
            int g12vc = 20000;

            graphs[12] = new AdjacencyListsGraph <SimpleAdjacencyList>(false, g12vc);
            for (int i = 0; i < g12vc - 1; i += 4)
            {
                graphs[12].AddEdge(i, i + 1);
                graphs[12].AddEdge(i + 1, i + 2);
            }
            descriptions3[12]   = "duzy rzadki graf";
            expected_weight[12] = 10000;


            TestSets["LabIsGraphic"]           = new TestSet(new Lab03(), "Część 1 - czy ciąg jest grafowy (0.5 pkt)");
            TestSets["LabConstructGraph"]      = new TestSet(new Lab03(), "Część 2 - skonstruowanie grafu (1.5 pkt)");
            TestSets["LabMinimumSpanningTree"] = new TestSet(new Lab03(), "Część 3 - drzewo rozpinające, algorytm Kruskala (2 pkt)");

            for (int k = 0; k < n1; ++k)
            {
                TestSets["LabIsGraphic"].TestCases.Add(new IsGraphicTestCase(1, null, descriptions1[k], sequences[k], is_graphic[k]));
                if (k < n2)
                {
                    TestSets["LabConstructGraph"].TestCases.Add(new ConstructGraph(1, null, descriptions1[k], sequences[k], k < n2t));
                }
            }
            for (int k = 0; k < n3; ++k)
            {
                TestSets["LabMinimumSpanningTree"].TestCases.Add(new MinimumSpanningTreeTestCase(limits[k], graphs[k].Directed ? new ArgumentException("") : null, descriptions3[k], graphs[k], expected_weight[k]));
            }
        }
Example #9
0
    public static void Main()
    {
        int[] backtrackingColors;
        int[] greedyColors;
        int n,i,j,mb,mg;
        long counter0, counter1, counter2;
        string[] message1 = { "Zwykly maly graf:",
                              "Maly dwudzielny:",
                              "Mala klika:" };
        int[] bestColorsNumbers1 = { 4, 2, 9 };
        string[] message2 = { "Zwykly graf:",
                              "Graf dwudzielny:",
                              "Cykl parzysty:",
                              "Klika:" };
        int[] bestColorsNumbers2 = { 6, 2, 2, 200 };
        string[] message3 = { "Zwykly duzy graf:",
                              "Duzy dwudzielny:",
                              "Duza klika:" };
        int[] bestColorsNumbers3 = { 59, 2, 4000 };
        IGraph[] g1 = new IGraph[message1.Length];
        IGraph[] g2 = new IGraph[message2.Length];
        IGraph[] g3 = new IGraph[message3.Length];
        var rgg = new RandomGraphGenerator();
        //GraphExport ge = new GraphExport(true, "C:\\Users\\polgrabiat\\Desktop\\Graphviz2.26.3\\Graphviz2.26.3\\bin\\dot.exe");

        Console.WriteLine();
        Console.WriteLine("Generowanie grafow");
        Console.WriteLine();

        rgg.SetSeed(101);
        g1[0] = rgg.UndirectedGraph(typeof(AdjacencyMatrixGraph),8,0.5);
        rgg.SetSeed(102);
        g1[1] = rgg.BipariteGraph(typeof(AdjacencyMatrixGraph),5,3,0.75);
        n=9;
        g1[2] = new AdjacencyMatrixGraph(false,n);
        for ( i=0 ; i<n ; ++i )
            for ( j=i+1 ; j<n ; ++ j )
                g1[2].AddEdge(i,j);

        rgg.SetSeed(103);
        g2[0] = rgg.UndirectedGraph(typeof(AdjacencyMatrixGraph), 20, 0.5);
        rgg.SetSeed(104);
        g2[1] = rgg.BipariteGraph(typeof(AdjacencyMatrixGraph), 30, 20, 0.25);
        n = 50;
        g2[2] = new AdjacencyListsGraph(false, n);
        for (i = 1; i < n; ++i)
            g2[2].AddEdge(i - 1, i);
        g2[2].AddEdge(n - 1, 0);
        rgg.SetSeed(105);
        g2[2] = rgg.Permute(g2[2]);
        n = 200;
        g2[3] = new AdjacencyMatrixGraph(false, n);
        for (i = 0; i < n; ++i)
        {
            for (j = i + 1; j < n; ++j)
                g2[3].AddEdge(i, j);
        }

        rgg.SetSeed(106);
        g3[0] = rgg.UndirectedGraph(typeof(AdjacencyMatrixGraph), 75, 0.99);
        rgg.SetSeed(107);
        g3[1] = rgg.BipariteGraph(typeof(AdjacencyMatrixGraph), 2000, 2000, 0.55);
        n = 5000;
        g3[2] = new AdjacencyMatrixGraph(false, n);
        for (i = 0; i < n; ++i)
        {
            for (j = i + 1; j < n; ++j)
                g3[2].AddEdge(i, j);
        }

        //Console.WriteLine("{0}", g3[2].EdgesCount);

        Console.WriteLine("Grafy za 1 pkt");
        Console.WriteLine();
        for ( i=0 ; i<g1.Length ; ++i )
            {
           //     ge.Export(g1[i], "ala");
            counter0=Graph.Counter;
            mb=g1[i].BacktrackingColor(out backtrackingColors);
            counter1=Graph.Counter;
            mg=g1[i].GreedyColor(out greedyColors);
            counter2=Graph.Counter;
            Console.WriteLine("{0,-17}  liczba wierzcholkow  {1,4},  optymalna liczba kolorow  {2,4}", message1[i], g1[i].VerticesCount, bestColorsNumbers1[i]);
            Console.WriteLine("  Backtracking:    liczba kolorow  {0,4},  zlozonosc  {1,8}", mb, counter1-counter0);
            Console.WriteLine("  Greedy:          liczba kolorow  {0,4},  zlozonosc  {1,8}", mg, counter2-counter1);
            Console.WriteLine();
            }

        Console.WriteLine("Grafy za 2 pkt");
        Console.WriteLine();
        for (i = 0; i < g2.Length; ++i)
        {
            counter0 = Graph.Counter;
            mb = g2[i].BacktrackingColor(out backtrackingColors);
            counter1 = Graph.Counter;
            mg = g2[i].GreedyColor(out greedyColors);
            counter2 = Graph.Counter;
            Console.WriteLine("{0,-17}  liczba wierzcholkow  {1,4},  optymalna liczba kolorow  {2,4}", message2[i], g2[i].VerticesCount, bestColorsNumbers2[i]);
            Console.WriteLine("  Backtracking:    liczba kolorow  {0,4},  zlozonosc  {1,8}", mb, counter1 - counter0);
            Console.WriteLine("  Greedy:          liczba kolorow  {0,4},  zlozonosc  {1,8}", mg, counter2 - counter1);
            Console.WriteLine();
        }

        Console.WriteLine("Grafy za 3 pkt");
        Console.WriteLine();
        for (i = 0; i < g3.Length; ++i)
        {
            counter0 = Graph.Counter;
            mb = g3[i].BacktrackingColor(out backtrackingColors);
            counter1 = Graph.Counter;
            mg = g3[i].GreedyColor(out greedyColors);
            counter2 = Graph.Counter;
            Console.WriteLine("{0,-17}  liczba wierzcholkow  {1,4},  optymalna liczba kolorow  {2,4}", message3[i], g3[i].VerticesCount, bestColorsNumbers3[i]);
            Console.WriteLine("  Backtracking:    liczba kolorow  {0,4},  zlozonosc  {1,8}", mb, counter1 - counter0);
            Console.WriteLine("  Greedy:          liczba kolorow  {0,4},  zlozonosc  {1,8}", mg, counter2 - counter1);
            Console.WriteLine();
        }

        Console.WriteLine("Koniec");
        Console.WriteLine();
    }
Example #10
0
    public static void PrepareTests()
    {
        var rgg = new RandomGraphGenerator();

        cliq_test = new Graph[5];
        izo_test  = new Graph[4, 2];

        cliq_res = new int[] { 4, 20, 19, 19, 9 };
        izo_res  = new bool[] { true, false, true, false };

        if (cliq_test.Length != cliq_res.Length || izo_test.GetLongLength(0) != izo_res.Length)
        {
            throw new ApplicationException("Zle zddefiniowane testy");
        }

        cliq_test[0] = new AdjacencyMatrixGraph(false, 8);
        cliq_test[0].AddEdge(0, 4);
        cliq_test[0].AddEdge(0, 7);
        cliq_test[0].AddEdge(1, 2);
        cliq_test[0].AddEdge(1, 3);
        cliq_test[0].AddEdge(1, 5);
        cliq_test[0].AddEdge(1, 6);
        cliq_test[0].AddEdge(2, 5);
        cliq_test[0].AddEdge(2, 6);
        cliq_test[0].AddEdge(3, 4);
        cliq_test[0].AddEdge(3, 7);
        cliq_test[0].AddEdge(4, 7);
        cliq_test[0].AddEdge(5, 6);

        cliq_test[1] = new AdjacencyMatrixGraph(false, 20);
        for (int i = 0; i < cliq_test[1].VerticesCount; ++i)
        {
            for (int j = i + 1; j < cliq_test[1].VerticesCount; ++j)
            {
                cliq_test[1].AddEdge(i, j);
            }
        }

        cliq_test[2] = cliq_test[1].Clone();
        cliq_test[2].DelEdge(0, 1);

        cliq_test[3] = cliq_test[2].Clone();
        cliq_test[3].DelEdge(0, 2);

        rgg.SetSeed(123);
        cliq_test[4] = rgg.DirectedGraph(typeof(AdjacencyMatrixGraph), 100, 0.7);

        izo_test[0, 0] = cliq_test[0].Clone();
        izo_test[0, 1] = rgg.Permute(izo_test[0, 0]);

        izo_test[1, 0] = izo_test[0, 0].Clone();
        izo_test[1, 1] = izo_test[0, 1].Clone();
        izo_test[1, 0].ModifyEdgeWeight(2, 5, 3);

        rgg.SetSeed(1234);
        izo_test[2, 0] = rgg.DirectedGraph(typeof(AdjacencyMatrixGraph), 50, 0.95, 1, 999);
        izo_test[2, 1] = new AdjacencyListsGraph <HashTableAdjacencyList>(izo_test[2, 0]);
        izo_test[2, 1] = rgg.Permute(izo_test[2, 1]);

        izo_test[3, 0] = rgg.UndirectedGraph(typeof(AdjacencyListsGraph <HashTableAdjacencyList>), 5000, 0.01, 1, 3);
        izo_test[3, 1] = new AdjacencyListsGraph <HashTableAdjacencyList>(true, 5000);
        for (int v = 0; v < 5000; ++v)
        {
            foreach (Edge e in izo_test[3, 0].OutEdges(v))
            {
                izo_test[3, 1].AddEdge(e);
            }
        }
    }
Example #11
0
        public override void PrepareTestSets()
        {
            var rgg = new RandomGraphGenerator(2018);
            int n   = 14;
            int n1  = 8;

            Graph[]  graphs       = new Graph[n];
            string[] descriptions = new string[n];

            // do czesci 1
            int[]  sizes        = new int[n];
            bool[] bool_results = new bool[n];
            int[]  limits1      = new int[n];
            // do czesci 2
            double[] maximal_results = new double[n];
            int[]    limits2         = new int[n];


            //przyklady z zajec
            //sciezka P10
            graphs[0] = new AdjacencyMatrixGraph(false, 10);
            graphs[0].AddEdge(0, 1, 5);
            graphs[0].AddEdge(1, 2, 4);
            graphs[0].AddEdge(2, 3, 3);
            graphs[0].AddEdge(3, 4, 2);
            graphs[0].AddEdge(4, 5, 3);
            graphs[0].AddEdge(5, 6, 1);
            graphs[0].AddEdge(6, 7, 6);
            graphs[0].AddEdge(7, 8, 2);
            graphs[0].AddEdge(8, 9, 1);
            descriptions[0]    = "Sciezka P10";
            sizes[0]           = 3;
            bool_results[0]    = true;
            maximal_results[0] = 13;

            //cykl C5
            graphs[1] = new AdjacencyMatrixGraph(false, 5);
            graphs[1].AddEdge(0, 1, 1);
            graphs[1].AddEdge(1, 2, 2);
            graphs[1].AddEdge(2, 3, 3);
            graphs[1].AddEdge(3, 4, 4);
            graphs[1].AddEdge(4, 0, 0);
            descriptions[1]    = "Cykl C5";
            sizes[1]           = 2;
            bool_results[1]    = false;
            maximal_results[1] = 4;

            //cykl C10
            graphs[2] = new AdjacencyMatrixGraph(false, 10);
            graphs[2].AddEdge(0, 1, 1.08);
            graphs[2].AddEdge(1, 2, 6.52);
            graphs[2].AddEdge(2, 3, 3);
            graphs[2].AddEdge(3, 4, 4);
            graphs[2].AddEdge(4, 5, 0);
            graphs[2].AddEdge(5, 6, 1.99);
            graphs[2].AddEdge(6, 7, 2.56);
            graphs[2].AddEdge(7, 8, 3.9);
            graphs[2].AddEdge(8, 9, 4.12);
            graphs[2].AddEdge(9, 0, 0);
            descriptions[2]    = "Cykl C10";
            sizes[2]           = 2;
            bool_results[2]    = true;
            maximal_results[2] = 12.63;

            //klika K5
            graphs[3]          = rgg.UndirectedGraph(typeof(AdjacencyMatrixGraph), 5, 1, -9, -1);
            descriptions[3]    = "Klika K5";
            sizes[3]           = 1;
            bool_results[3]    = true;
            maximal_results[3] = 0;

            //prawie klika K10 (bez kilku krawedzi)
            graphs[4]       = rgg.UndirectedGraph(typeof(AdjacencyMatrixGraph), 10, 1, -9, 99);
            descriptions[4] = "Klika K10 bez kilku krawedzi";
            graphs[4].DelEdge(0, 1);
            graphs[4].DelEdge(0, 2);
            graphs[4].DelEdge(0, 3);
            graphs[4].DelEdge(4, 5);
            graphs[4].DelEdge(4, 6);
            graphs[4].DelEdge(4, 7);
            sizes[4]           = 1;
            bool_results[4]    = true;
            maximal_results[4] = 98;

            //jakis sobie graf
            graphs[5] = new AdjacencyListsGraph <SimpleAdjacencyList>(false, 9);
            graphs[5].AddEdge(0, 1, 0.33);
            graphs[5].AddEdge(1, 3, 1.5);
            graphs[5].AddEdge(1, 6, 10.1);
            graphs[5].AddEdge(2, 3, 1);
            graphs[5].AddEdge(2, 4, 1);
            graphs[5].AddEdge(2, 5, 1);
            graphs[5].AddEdge(2, 8, 3);
            graphs[5].AddEdge(3, 7, 3.33);
            graphs[5].AddEdge(4, 5, 3.7);
            graphs[5].AddEdge(7, 8, 0.7);
            descriptions[5]    = "Pewien graf o 9 wierzcholkach";
            sizes[5]           = 3;
            bool_results[5]    = true;
            maximal_results[5] = 14.5;

            //graf dwudzielny
            graphs[6] = new AdjacencyListsGraph <SimpleAdjacencyList>(false, 10);
            graphs[6].AddEdge(0, 5, 3);
            graphs[6].AddEdge(0, 6, 3);
            graphs[6].AddEdge(1, 7, 4);
            graphs[6].AddEdge(1, 8, 4);
            graphs[6].AddEdge(2, 6, 1);
            graphs[6].AddEdge(2, 8, 1);
            graphs[6].AddEdge(2, 9, 1);
            graphs[6].AddEdge(3, 9, 2);
            graphs[6].AddEdge(4, 9, 3);
            descriptions[6]    = "Graf dwudzielny o 10 wierzcholkach";
            sizes[6]           = 3;
            bool_results[6]    = true;
            maximal_results[6] = 10;

            //gwiazda
            int g7vc = 200;

            graphs[7] = new AdjacencyListsGraph <AVLAdjacencyList>(false, g7vc);
            for (int i = 1; i < g7vc; ++i)
            {
                graphs[7].AddEdge(0, i, i);
            }
            descriptions[7]    = "Nieskierowana gwiazda";
            sizes[7]           = 2;
            bool_results[7]    = false;
            maximal_results[7] = 199;


            //Wieksze testy, z wydajnoscia
            //gesty duzy graf losowy nr 1
            int g8vc = 250;

            rgg.SetSeed(123451);
            graphs[8]          = rgg.UndirectedGraph(typeof(AdjacencyMatrixGraph), g8vc, 0.9, -9, 9);
            descriptions[8]    = "Gesty graf losowy nr 1";
            sizes[8]           = 3;
            bool_results[8]    = true;
            maximal_results[8] = 21;
            limits1[8]         = 3;
            limits2[8]         = 60;

            //gesty duzy graf losowy nr 2
            int g9vc = 200;

            rgg.SetSeed(123452);
            graphs[9]          = rgg.UndirectedGraph(typeof(AdjacencyMatrixGraph), g9vc, 0.95, -9, 99);
            descriptions[9]    = "Gesty graf losowy nr 2";
            sizes[9]           = 3;
            bool_results[9]    = false;
            maximal_results[9] = 194;
            limits1[9]         = 12;
            limits2[9]         = 12;

            //rzadki graf losowy nr 1
            int g10vc = 45;

            rgg.SetSeed(123453);
            graphs[10]          = rgg.UndirectedGraph(typeof(AdjacencyListsGraph <SimpleAdjacencyList>), g10vc, 0.1, -9, 99);
            descriptions[10]    = "Rzadki graf losowy nr 1";
            sizes[10]           = 5;
            bool_results[10]    = true;
            maximal_results[10] = 743;
            limits1[10]         = 1;
            limits2[10]         = 65;

            //rzadki graf losowy nr 2
            int g11vc = 45;

            rgg.SetSeed(123454);
            graphs[11]          = rgg.UndirectedGraph(typeof(AdjacencyListsGraph <SimpleAdjacencyList>), g11vc, 0.04, -9, 99);
            descriptions[11]    = "Rzadki graf losowy nr 2";
            sizes[11]           = 7;
            bool_results[11]    = true;
            maximal_results[11] = 675;
            limits1[11]         = 1;
            limits2[11]         = 12;

            //cykl C35
            int g12vc = 35;

            graphs[12] = new AdjacencyListsGraph <HashTableAdjacencyList>(false, g12vc);
            for (int i = 1; i < g12vc; ++i)
            {
                graphs[12].AddEdge(i - 1, i, i);
            }
            graphs[12].AddEdge(g12vc - 1, 0, 1);
            descriptions[12]    = "Cykl C35";
            sizes[12]           = 11;
            bool_results[12]    = true;
            maximal_results[12] = 209;
            limits1[12]         = 1;
            limits2[12]         = 22;

            //duza klika K200
            int g13vc = 200;

            rgg.SetSeed(123455);
            graphs[13]          = rgg.UndirectedGraph(typeof(AdjacencyMatrixGraph), g13vc, 1, -9, 99);
            descriptions[13]    = "Klika K200";
            sizes[13]           = 100;
            bool_results[13]    = false;
            maximal_results[13] = 99;
            limits1[13]         = 10;
            limits2[13]         = 10;

            TestSets["LabInducedMatchingTests"]                   = new TestSet(new Lab09(), "Part 1 - induced matching with given size [1.5]");
            TestSets["LabMaximalInducedMatchingTests"]            = new TestSet(new Lab09(), "Part 2 - maximal induced matching [1.5]");
            TestSets["LabInducedMatchingPerformanceTests"]        = new TestSet(new Lab09(), "Part 1 - induced matching with given size - performance tests[0.5]");
            TestSets["LabMaximalInducedMatchingPerformanceTests"] = new TestSet(new Lab09(), "Part 2 - maximal induced matching - performance tests[0.5]");

            for (int i = 0; i < n; ++i)
            {
                if (i < n1)
                {
                    TestSets["LabInducedMatchingTests"].TestCases.Add(new InducedMatchingTestCase(1, null, descriptions[i], graphs[i], sizes[i], bool_results[i]));
                    TestSets["LabMaximalInducedMatchingTests"].TestCases.Add(new MaximalInducedMatchingTestCase(1, null, descriptions[i], graphs[i], maximal_results[i]));
                }
                else
                {
                    TestSets["LabInducedMatchingPerformanceTests"].TestCases.Add(new InducedMatchingTestCase(limits1[i], null, descriptions[i], graphs[i], sizes[i], bool_results[i]));
                    TestSets["LabMaximalInducedMatchingPerformanceTests"].TestCases.Add(new MaximalInducedMatchingTestCase(limits2[i], null, descriptions[i], graphs[i], maximal_results[i]));
                }
            }
        }
Example #12
0
    public static void Main()
    {
        Graph  g;
        double m, w;

        Edge[]   c;
        int      N = 8;
        DateTime t1, t2;

        // "Odkomentuj" wybrany przykład i "zakomentuj" pozostałe
        // Możesz też podać własne inne przykłady
        // Obliczenia przykładów dla grafów o 200 wierzchołkach mogą chwilę potrwać

        RandomGraphGenerator gen = new RandomGraphGenerator(123);

        g = gen.UndirectedGraph(typeof(AdjacencyMatrixGraph), 20, 1.0, 1, 99);
        //g = gen.UndirectedGraph(typeof(AdjacencyMatrixGraph), 80, 1.0, 1, 99);
        //g = gen.UndirectedGraph(typeof(AdjacencyMatrixGraph), 200, 1.0, 1, 99);
        //g = gen.UndirectedEuclidGraph(typeof(AdjacencyMatrixGraph), 30, 1.0, 0.0, 100.0, 0.0, 100.0);
        //g = gen.UndirectedEuclidGraph(typeof(AdjacencyMatrixGraph), 200, 1.0, 0.0, 100.0, 0.0, 100.0);
        gen.SetSeed(3333);
        //g = gen.UndirectedGraph(typeof(AdjacencyMatrixGraph),8,1.0,10,99);

        Console.WriteLine();
        if (g.VerticesCount <= N)
        {
            for (int i = 0; i < g.VerticesCount; ++i)
            {
                for (int j = 0; j < g.VerticesCount; ++j)
                {
                    w = g.GetEdgeWeight(i, j);
                    Console.Write(" {0}", w.IsNaN() ? "**" : w.ToString());
                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }

        if (g.VerticesCount <= 30)
        {
            t1     = DateTime.Now;
            (m, c) = g.BranchAndBoundTSP();
            t2     = DateTime.Now;
            Console.WriteLine("BranchAndBound: {0}  {1}  {2}", m, m == HamiltonTest(g, c), t2 - t1);
            if (g.VerticesCount <= N)
            {
                WriteCycle(c);
            }
            Console.WriteLine();
        }
        else
        {
            Console.WriteLine("BranchAndBound: nie liczymy - zbyt dlugo by trwalo\n");
        }

        if (g.VerticesCount <= 10)
        {
            t1     = DateTime.Now;
            (m, c) = g.BacktrackingTSP();
            t2     = DateTime.Now;
            Console.WriteLine("Backtracking: {0}  {1}  {2}", m, m == HamiltonTest(g, c), t2 - t1);
            if (g.VerticesCount <= N)
            {
                WriteCycle(c);
            }
            Console.WriteLine();
        }
        else
        {
            Console.WriteLine("Backtracking: nie liczymy - zbyt dlugo by trwalo\n");
        }

        t1     = DateTime.Now;
        (m, c) = g.SimpleGreedyTSP();
        t2     = DateTime.Now;
        Console.WriteLine("SimpleGreedy: {0}  {1}  {2}", m, m == HamiltonTest(g, c), t2 - t1);
        if (g.VerticesCount <= N)
        {
            WriteCycle(c);
        }
        Console.WriteLine();

        t1     = DateTime.Now;
        (m, c) = g.KruskalTSP();
        t2     = DateTime.Now;
        Console.WriteLine("Kruskal: {0}  {1}  {2}", m, m == HamiltonTest(g, c), t2 - t1);
        if (g.VerticesCount <= N)
        {
            WriteCycle(c);
        }
        Console.WriteLine();

        if (!g.Directed)
        {
            t1     = DateTime.Now;
            (m, c) = g.TreeBasedTSP();
            t2     = DateTime.Now;
            Console.WriteLine("TreeBased: {0}  {1}  {2}", m, m == HamiltonTest(g, c), t2 - t1);
            if (g.VerticesCount <= N)
            {
                WriteCycle(c);
            }
            Console.WriteLine();

            t1     = DateTime.Now;
            (m, c) = g.TreeBasedTSP(TSPTreeBasedVersion.Simple);
            t2     = DateTime.Now;
            Console.WriteLine("TreeBased 0: {0}  {1}  {2}", m, m == HamiltonTest(g, c), t2 - t1);
            if (g.VerticesCount <= N)
            {
                WriteCycle(c);
            }
            Console.WriteLine();

            t1     = DateTime.Now;
            (m, c) = g.TreeBasedTSP(TSPTreeBasedVersion.Christofides);
            t2     = DateTime.Now;
            Console.WriteLine("TreeBased 1: {0}  {1}  {2}", m, m == HamiltonTest(g, c), t2 - t1);
            if (g.VerticesCount <= N)
            {
                WriteCycle(c);
            }
            Console.WriteLine();

            t1     = DateTime.Now;
            (m, c) = g.TreeBasedTSP(TSPTreeBasedVersion.ModifiedChristofides);
            t2     = DateTime.Now;
            Console.WriteLine("TreeBased 2: {0}  {1}  {2}", m, m == HamiltonTest(g, c), t2 - t1);
            if (g.VerticesCount <= N)
            {
                WriteCycle(c);
            }
            Console.WriteLine();
        }

        t1     = DateTime.Now;
        (m, c) = g.IncludeTSP();
        t2     = DateTime.Now;
        Console.WriteLine("Include: {0}  {1}  {2}", m, m == HamiltonTest(g, c), t2 - t1);
        if (g.VerticesCount <= N)
        {
            WriteCycle(c);
        }
        Console.WriteLine();

        t1     = DateTime.Now;
        (m, c) = g.IncludeTSP(TSPIncludeVertexSelectionMethod.Sequential);
        t2     = DateTime.Now;
        Console.WriteLine("Include 0: {0}  {1}  {2}", m, m == HamiltonTest(g, c), t2 - t1);
        if (g.VerticesCount <= N)
        {
            WriteCycle(c);
        }
        Console.WriteLine();

        if (!g.Directed)
        {
            t1     = DateTime.Now;
            (m, c) = g.IncludeTSP(TSPIncludeVertexSelectionMethod.Nearest);
            t2     = DateTime.Now;
            Console.WriteLine("Include 1: {0}  {1}  {2}", m, m == HamiltonTest(g, c), t2 - t1);
            if (g.VerticesCount <= N)
            {
                WriteCycle(c);
            }
            Console.WriteLine();

            t1     = DateTime.Now;
            (m, c) = g.IncludeTSP(TSPIncludeVertexSelectionMethod.Furthest);
            t2     = DateTime.Now;
            Console.WriteLine("Include 2: {0}  {1}  {2}", m, m == HamiltonTest(g, c), t2 - t1);
            if (g.VerticesCount <= N)
            {
                WriteCycle(c);
            }
            Console.WriteLine();
        }

        t1     = DateTime.Now;
        (m, c) = g.IncludeTSP(TSPIncludeVertexSelectionMethod.MinimalCost);
        t2     = DateTime.Now;
        Console.WriteLine("Include 3: {0}  {1}  {2}", m, m == HamiltonTest(g, c), t2 - t1);
        if (g.VerticesCount <= N)
        {
            WriteCycle(c);
        }
        Console.WriteLine();

        t1     = DateTime.Now;
        (m, c) = g.ThreeOptTSP();
        t2     = DateTime.Now;
        Console.WriteLine("ThreeOpt: {0}  {1}  {2}", m, m == HamiltonTest(g, c), t2 - t1);
        if (g.VerticesCount <= N)
        {
            WriteCycle(c);
        }
        Console.WriteLine();

        t1     = DateTime.Now;
        (m, c) = g.ThreeOptTSPParallel();
        t2     = DateTime.Now;
        Console.WriteLine("ThreeOptParallel: {0}  {1}  {2}", m, m == HamiltonTest(g, c), t2 - t1);
        if (g.VerticesCount <= N)
        {
            WriteCycle(c);
        }
        Console.WriteLine();
    }
Example #13
0
    public static void PrepareTests()
    {
        var rgg = new RandomGraphGenerator();

        cliq_test = new Graph[6];
        izo_test  = new Graph[4, 2];

        cliq_res = new int[] { 4, 20, 19, 19, 9, 3 };
        izo_res  = new bool[] { true, false, true, false };

        if (cliq_test.Length != cliq_res.Length || izo_test.GetLongLength(0) != izo_res.Length)
        {
            throw new ApplicationException("Zle zddefiniowane testy");
        }

        cliq_test[0] = new AdjacencyMatrixGraph(false, 8);
        cliq_test[0].AddEdge(0, 4);
        cliq_test[0].AddEdge(0, 7);
        cliq_test[0].AddEdge(1, 2);
        cliq_test[0].AddEdge(1, 3);
        cliq_test[0].AddEdge(1, 5);
        cliq_test[0].AddEdge(1, 6);
        cliq_test[0].AddEdge(2, 5);
        cliq_test[0].AddEdge(2, 6);
        cliq_test[0].AddEdge(3, 4);
        cliq_test[0].AddEdge(3, 7);
        cliq_test[0].AddEdge(4, 7);
        cliq_test[0].AddEdge(5, 6);

        cliq_test[1] = new AdjacencyMatrixGraph(false, 20);
        for (int i = 0; i < cliq_test[1].VerticesCount; ++i)
        {
            for (int j = i + 1; j < cliq_test[1].VerticesCount; ++j)
            {
                cliq_test[1].AddEdge(i, j);
            }
        }

        cliq_test[2] = cliq_test[1].Clone();
        cliq_test[2].DelEdge(0, 1);

        cliq_test[3] = cliq_test[2].Clone();
        cliq_test[3].DelEdge(0, 2);

        rgg.SetSeed(123);
        cliq_test[4] = rgg.DirectedGraph(typeof(AdjacencyMatrixGraph), 100, 0.7);

        cliq_test[5] = rgg.UndirectedGraph(typeof(AdjacencyListsGraph <SimplyAdjacencyList>), 5000, 0.001);

        izo_test[0, 0] = cliq_test[0].Clone();
        izo_test[0, 1] = rgg.Permute(izo_test[0, 0]);

        int n = 50;

        izo_test[1, 0] = new AdjacencyMatrixGraph(true, n);
        for (int i = 0; i < n; ++i)
        {
            for (int j = 0; j < n; ++j)
            {
                if (i != j)
                {
                    izo_test[1, 0].AddEdge(i, j);
                }
            }
        }
        izo_test[1, 1] = izo_test[1, 0].Clone();
        for (int i = 0; i < n; ++i)
        {
            izo_test[1, 0].DelEdge(i, (i + 1) % n);
        }
        for (int i = 0; i < n; i += 2)
        {
            izo_test[1, 1].DelEdge(i, (i + 2) % n);
            izo_test[1, 1].DelEdge(i + 1, (i + 3) % n);
        }

        rgg.SetSeed(1234);
        izo_test[2, 0] = rgg.DirectedGraph(typeof(AdjacencyMatrixGraph), 50, 0.95, 1, 999);
        izo_test[2, 1] = new AdjacencyListsGraph <SimplyAdjacencyList>(izo_test[2, 0]);
        izo_test[2, 1] = rgg.Permute(izo_test[2, 1]);

        izo_test[3, 0] = rgg.UndirectedGraph(typeof(AdjacencyListsGraph <SimplyAdjacencyList>), 15, 0.01, 1, 3);
        izo_test[3, 0].AddEdge(izo_test[3, 0].VerticesCount / 2, izo_test[3, 0].VerticesCount / 2 + 1, 2);
        izo_test[3, 1] = izo_test[3, 0].Clone();
        izo_test[3, 1].ModifyEdgeWeight(izo_test[3, 0].VerticesCount / 2, izo_test[3, 0].VerticesCount / 2 + 1, 1);
    }
Example #14
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]);
            }
        }
Example #15
0
    public static void Main()
    {
        var ge  = new GraphExport();
        var rgg = new RandomGraphGenerator();

        int[] scc;
        int   n, w;

        string[] desc;
        int[]    sccc = { 8, 17, 1000 };
        IGraph   mst;

        int[] mstw = { 47, 3998, 40533 };


        IGraph g1 = new AdjacencyMatrixGraph(true, 16);

        g1.AddEdge(0, 5);
        g1.AddEdge(1, 0);
        g1.AddEdge(1, 2);
        g1.AddEdge(1, 7);
        g1.AddEdge(2, 3);
        g1.AddEdge(3, 1);
        g1.AddEdge(3, 7);
        g1.AddEdge(4, 15);
        g1.AddEdge(5, 4);
        g1.AddEdge(5, 6);
        g1.AddEdge(6, 7);
        g1.AddEdge(7, 6);
        g1.AddEdge(7, 8);
        g1.AddEdge(8, 9);
        g1.AddEdge(10, 3);
        g1.AddEdge(12, 13);
        g1.AddEdge(13, 14);
        g1.AddEdge(14, 12);
        g1.AddEdge(15, 0);

        IGraph g2 = new AdjacencyMatrixGraph(false, 15);

        g2.AddEdge(0, 1, 3);
        g2.AddEdge(0, 4, 5);
        g2.AddEdge(1, 4, 6);
        g2.AddEdge(1, 5, 4);
        g2.AddEdge(2, 6, 5);
        g2.AddEdge(4, 7, 8);
        g2.AddEdge(4, 8, 2);
        g2.AddEdge(4, 10, 12);
        g2.AddEdge(5, 8, 1);
        g2.AddEdge(5, 9, 7);
        g2.AddEdge(6, 11, 4);
        g2.AddEdge(6, 14, 5);
        g2.AddEdge(7, 8, 9);
        g2.AddEdge(8, 12, 3);
        g2.AddEdge(10, 12, 2);
        g2.AddEdge(10, 13, 3);

        IGraph[] scc_test = new IGraph[3];
        scc_test[0] = g1;
        rgg.SetSeed(500);
        scc_test[1] = rgg.DirectedGraph(typeof(AdjacencyListsGraph), 1000, 0.005);
        scc_test[2] = rgg.DAG(typeof(AdjacencyMatrixGraph), 1000, 0.3, 1, 1);

        IGraph[] mst_test = new IGraph[3];
        mst_test[0] = g2;
        mst_test[1] = rgg.UndirectedGraph(typeof(AdjacencyMatrixGraph), 100, 0.3, 1, 1000);
        mst_test[2] = rgg.UndirectedGraph(typeof(AdjacencyListsGraph), 1000, 0.03, 1, 1000);

        Console.WriteLine();
        for (int i = 0; i < scc_test.Length; ++i)
        {
            n = scc_test[i].Tarjan(out scc);
            Console.WriteLine("Liczba silnie spojnych skladowych: {0,4},    powinno byc {1,4}", n, sccc[i]);
            if (i > 0)
            {
                continue;
            }
            desc = new string[scc_test[i].VerticesCount];
            for (int v = 0; v < scc_test[i].VerticesCount; ++v)
            {
                desc[v] = string.Format("{0}:{1}", v, scc[v]);
            }
            ge.Export(scc_test[i], desc, "scc");
        }

        Console.WriteLine();
        for (int i = 0; i < mst_test.Length; ++i)
        {
            w = mst_test[i].Boruvka(out mst);
            Console.WriteLine("Waga minimalnego drzewa rozpinajacego: {0,5},    powinno byc {1,5}", w, mstw[i]);
            if (i > 0)
            {
                continue;
            }
            ge.Export(mst_test[i], null, "graph");
            ge.Export(mst, null, "mst");
        }

        Console.WriteLine();
    }