public void Init()
 {
     _generator = new RandomGraphGenerator();
     _locations = new HashSet <Location>
     {
         new Location
         (
             id: 1.ToString(),
             longitude: 3,
             latitude: -4
         ),
         new Location
         (
             id: 2.ToString(),
             longitude: -2,
             latitude: -3
         ),
         new Location
         (
             id: 3.ToString(),
             longitude: 0,
             latitude: 3
         )
     };
 }
Example #2
0
        static void Main(string[] args)
        {
            RandomGraphGenerator generator = new RandomGraphGenerator(0);
            Graph g = generator.EulerGraph(typeof(AdjacencyListsGraph <AVLAdjacencyList>), true, 12, 0.75, 1, 100);

            var(weight, cycle) = g.BacktrackingTSP();
            Console.WriteLine(weight);
        }
Example #3
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]);
    }
        protected override void SetupHandler()
        {
            RandomGraphGenerator rg = new RandomGraphGenerator();

            Handler.AddInt(NumberOfNodes, rg.NodeCount);
            Handler.AddInt(NumberOfEdges, rg.EdgeCount);

            Handler.AddBool(AllowCycles, rg.AllowCycles);
            Handler.AddBool(AllowSelfloops, rg.AllowSelfLoops);
            Handler.AddBool(AllowMultipleEdges, rg.AllowMultipleEdges);
        }
Example #5
0
        private (Graph, Graph) FullGraph(RandomGraphGenerator rgg, int n)
        {
            Graph g = rgg.UndirectedGraph(typeof(AdjacencyMatrixGraph), n, 1);
            Graph t = new AdjacencyListsGraph <SimpleAdjacencyList>(false, n);

            for (int i = 1; i < n; ++i)
            {
                t.AddEdge(0, i);
            }
            return(g, t);
        }
Example #6
0
        public static MuseumTestCase RandomTest(int size, int seed, double time, int solution)
        {
            Random random            = new Random(seed);
            RandomGraphGenerator rgg = new RandomGraphGenerator(seed);

            Graph g = rgg.UndirectedGraph(typeof(AdjacencyListsGraph <SimpleAdjacencyList>), size, .3, 1, 1);

            int[] cLevel    = Enumerable.Range(0, size).Select(x => random.Next(20)).ToArray();
            int[] entrances = Enumerable.Range(0, random.Next(size / 2)).Select(x => random.Next(size)).ToArray();
            int[] exits     = Enumerable.Range(0, random.Next(size / 2)).Select(x => random.Next(size)).ToArray();
            return(new MuseumTestCase(time, g, cLevel, entrances, exits, solution));
        }
Example #7
0
        static long PerformBoilerplateTask()
        {
            RandomGraphGenerator rgg = new RandomGraphGenerator();
            var   stopwatch          = System.Diagnostics.Stopwatch.StartNew();
            Graph boilerplateGraph   = rgg.UndirectedGraph(typeof(AdjacencyMatrixGraph), 100, 0.9);
            int   cc;

            for (int i = 0; i < 500; i++)
            {
                boilerplateGraph.GeneralSearchAll <EdgesStack>(null, null, null, out cc);
            }

            stopwatch.Stop();
            return(stopwatch.ElapsedMilliseconds);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ShortestPathWindow"/> class.
        /// </summary>
        public ShortestPathWindow()
        {
            InitializeComponent();

            sourceNodes = new List <INode>();
            targetNodes = new List <INode>();

            InitializeInputModes();
            InitializeStyles();
            InitializeGraph();

            // initialize random graph generator
            randomGraphGenerator = new RandomGraphGenerator
            {
                AllowCycles        = true,
                AllowMultipleEdges = true,
                AllowSelfLoops     = false,
                EdgeCount          = 40,
                NodeCount          = 30
            };
        }
Example #9
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);
        }
        protected override Task RunModule()
        {
            RandomGraphGenerator rg = new RandomGraphGenerator
            {
                NodeCount          = (int)Handler[NumberOfNodes].Value,
                EdgeCount          = (int)Handler[NumberOfEdges].Value,
                AllowCycles        = (bool)Handler[AllowCycles].Value,
                AllowMultipleEdges = (bool)Handler[AllowMultipleEdges].Value,
                AllowSelfLoops     = (bool)Handler[AllowSelfloops].Value
            };

            rg.Generate(CurrentIGraph);
            int i = 0;

            foreach (INode node in CurrentIGraph.Nodes)
            {
                CurrentIGraph.AddLabel(node, "" + ++i);
            }
            RandomizeGraph();
            GraphControl.Invalidate();
            return(Task.FromResult <object>(null));
        }
Example #11
0
        public static void Main()
        {
            Random rng = new Random();
            RandomGraphGenerator rgg = new RandomGraphGenerator();

            for (int i = 1; i <= testRnds; i++)
            {
                foreach (Type type in types)
                {
                    TestGraph(rgg.UndirectedGraph(type, size, rng.NextDouble()), "Random undirected graph");
                    TestGraph(rgg.DirectedGraph(type, size, rng.NextDouble()), "Random directed graph");
                    TestGraph(rgg.UndirectedGraph(type, size, rng.NextDouble() * 2 / size, 0, 100), "Sparse undirected graph");
                    int k = rng.Next() % (size - 1) + 1;
                    TestGraph(rgg.BipariteGraph(type, k, size - k, rng.NextDouble()), "Bipartite graph");
                }
            }
            Console.WriteLine("Test summary:\n Reverse graphs:\t{0}/{3}, \n Bipartite graphs:\t{1}/{3},\n Kruskal's algorithm:\t{2}/{3}",
                              reverseOk,
                              bipartiteOk,
                              kruskalOk,
                              totalTestCount);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ShortestPathForm"/> class.
        /// </summary>
        public ShortestPathForm()
        {
            InitializeComponent();

            sourceNodes = new List <INode>();
            targetNodes = new List <INode>();

            InitializeInputModes();
            InitializeStyles();
            InitializeGraph();

            // initialize random graph generator
            randomGraphGenerator = new RandomGraphGenerator
            {
                AllowCycles        = true,
                AllowMultipleEdges = true,
                AllowSelfLoops     = false,
                EdgeCount          = 40,
                NodeCount          = 30
            };

            // load description
            descriptionTextBox.LoadFile(new MemoryStream(Resources.description), RichTextBoxStreamType.RichText);
        }
Example #13
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 #14
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 #15
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);
        }
Example #16
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 #17
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}");
                }
            }
        }
        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);
        }
Example #19
0
        public override void PrepareTestSets()
        {
            var rgg = new RandomGraphGenerator(12345);
            int n   = 6;

            Graph[]         graphs     = new Graph[n];
            Graph[]         squares    = new Graph[n];
            Graph[]         linegraphs = new Graph[n];
            List <string[]> names      = new List <string[]>(); //description of edges
            List <int[]>    colors     = new List <int[]>();

            int[]   chn        = new int[n]; //chromatic number
            Graph[] sec_graphs = new Graph[n];
            int[]   schi       = new int[n]; //strong chromatic index

            graphs[0] = new AdjacencyMatrixGraph(false, 5);
            graphs[0].AddEdge(0, 1);
            graphs[0].AddEdge(0, 3);
            graphs[0].AddEdge(1, 2);
            graphs[0].AddEdge(2, 0);
            graphs[0].AddEdge(2, 3);
            graphs[0].AddEdge(3, 4);

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

//            graphs[2] = new AdjacencyListsGraph<SimpleAdjacencyList>(false, 9);
            graphs[2] = new AdjacencyMatrixGraph(false, 9);
            graphs[2].AddEdge(0, 3);
            graphs[2].AddEdge(1, 0);
            graphs[2].AddEdge(6, 0);
            graphs[2].AddEdge(1, 5);
            graphs[2].AddEdge(5, 4);
            graphs[2].AddEdge(5, 2);
            graphs[2].AddEdge(5, 6);
            graphs[2].AddEdge(6, 7);
            graphs[2].AddEdge(7, 8);

            graphs[3] = new AdjacencyMatrixGraph(false, 5);
            graphs[3].AddEdge(0, 1);
            graphs[3].AddEdge(1, 2);
            graphs[3].AddEdge(2, 3);
            graphs[3].AddEdge(3, 4);
            graphs[3].AddEdge(4, 0);

            graphs[4] = rgg.UndirectedGraph(typeof(AdjacencyMatrixGraph), 5, 1.0);

            int g5vc = 9000;

            graphs[5] = new AdjacencyListsGraph <SimpleAdjacencyList>(true, g5vc);
            for (int i = 1; i < g5vc; ++i)
            {
                graphs[5].AddEdge(i - 1, i);
            }
            graphs[5].AddEdge(g5vc - 1, 0);

            squares[0] = graphs[0].Clone();
            squares[0].AddEdge(0, 4);
            squares[0].AddEdge(1, 3);
            squares[0].AddEdge(2, 4);

            squares[1] = graphs[1].Clone();
            squares[1].AddEdge(0, 2);
            squares[1].AddEdge(1, 3);
            squares[1].AddEdge(2, 4);
            squares[1].AddEdge(3, 5);

            squares[2] = graphs[2].Clone();
            squares[2].AddEdge(0, 5);
            squares[2].AddEdge(0, 6);
            squares[2].AddEdge(0, 7);
            squares[2].AddEdge(1, 2);
            squares[2].AddEdge(1, 3);
            squares[2].AddEdge(1, 4);
            squares[2].AddEdge(1, 6);
            squares[2].AddEdge(2, 4);
            squares[2].AddEdge(2, 6);
            squares[2].AddEdge(3, 6);
            squares[2].AddEdge(4, 6);
            squares[2].AddEdge(5, 7);
            squares[2].AddEdge(6, 8);

            squares[3] = rgg.UndirectedGraph(typeof(AdjacencyMatrixGraph), 5, 1.0);
            squares[4] = rgg.UndirectedGraph(typeof(AdjacencyMatrixGraph), 5, 1.0);

            squares[5] = new AdjacencyListsGraph <SimpleAdjacencyList>(true, g5vc);
            for (int i = 0; i < g5vc; ++i)
            {
                squares[5].AddEdge(i, (i + 1) % g5vc);
                squares[5].AddEdge(i, (i + 2) % g5vc);
            }

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

            linegraphs[1] = new AdjacencyMatrixGraph(false, 5);
            linegraphs[1].AddEdge(0, 1);
            linegraphs[1].AddEdge(1, 2);
            linegraphs[1].AddEdge(2, 3);
            linegraphs[1].AddEdge(3, 4);

//            linegraphs[2] = new AdjacencyListsGraph<SimpleAdjacencyList>(false, 9);
            linegraphs[2] = new AdjacencyMatrixGraph(false, 9);
            linegraphs[2].AddEdge(0, 1);
            linegraphs[2].AddEdge(0, 2);
            linegraphs[2].AddEdge(0, 3);
            linegraphs[2].AddEdge(1, 2);
            linegraphs[2].AddEdge(2, 6);
            linegraphs[2].AddEdge(2, 7);
            linegraphs[2].AddEdge(3, 4);
            linegraphs[2].AddEdge(3, 5);
            linegraphs[2].AddEdge(3, 6);
            linegraphs[2].AddEdge(4, 5);
            linegraphs[2].AddEdge(4, 6);
            linegraphs[2].AddEdge(5, 6);
            linegraphs[2].AddEdge(6, 7);
            linegraphs[2].AddEdge(7, 8);

            linegraphs[3] = new AdjacencyMatrixGraph(false, 5);
            linegraphs[3].AddEdge(0, 1);
            linegraphs[3].AddEdge(0, 2);
            linegraphs[3].AddEdge(1, 4);
            linegraphs[3].AddEdge(2, 3);
            linegraphs[3].AddEdge(3, 4);

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

            linegraphs[5] = new AdjacencyListsGraph <SimpleAdjacencyList>(false, g5vc);
            for (int i = 0; i < g5vc; ++i)
            {
                linegraphs[5].AddEdge(i, (i + 1) % g5vc);
            }

            names.Add(new string[6]);
            names[0][0] = ("[0, 1]");
            names[0][1] = ("[0, 2]");
            names[0][2] = ("[0, 3]");
            names[0][3] = ("[1, 2]");
            names[0][4] = ("[2, 3]");
            names[0][5] = ("[3, 4]");

            names.Add(new string[5]);
            names[1][0] = ("[0, 1]");
            names[1][1] = ("[1, 2]");
            names[1][2] = ("[2, 3]");
            names[1][3] = ("[3, 4]");
            names[1][4] = ("[4, 5]");

            names.Add(new string[9]);
            names[2][0] = ("[0, 1]");
            names[2][1] = ("[0, 3]");
            names[2][2] = ("[0, 6]");
            names[2][3] = ("[1, 5]");
            names[2][4] = ("[2, 5]");
            names[2][5] = ("[4, 5]");
            names[2][6] = ("[5, 6]");
            names[2][7] = ("[6, 7]");
            names[2][8] = ("[7, 8]");

            names.Add(new string[5]);
            names[3][0] = ("[0, 1]");
            names[3][1] = ("[0, 4]");
            names[3][2] = ("[1, 2]");
            names[3][3] = ("[2, 3]");
            names[3][4] = ("[3, 4]");

            names.Add(new string[10]);
            names[4][0] = ("[0, 1]");
            names[4][1] = ("[0, 2]");
            names[4][2] = ("[0, 3]");
            names[4][3] = ("[0, 4]");
            names[4][4] = ("[1, 2]");
            names[4][5] = ("[1, 3]");
            names[4][6] = ("[1, 4]");
            names[4][7] = ("[2, 3]");
            names[4][8] = ("[2, 4]");
            names[4][9] = ("[3, 4]");

            names.Add(new string[g5vc]);
            for (int i = 0; i < g5vc; ++i)
            {
                names[5][i] = string.Format("[{0}, {1}]", i, (i + 1) % g5vc);
            }

            colors.Add(new int[5]);
            colors[0][0] = 0;
            colors[0][1] = 1;
            colors[0][2] = 2;
            colors[0][3] = 1;
            colors[0][4] = 0;
            chn[0]       = 3;

            colors.Add(new int[6]);
            colors[1][0] = 0;
            colors[1][1] = 1;
            colors[1][2] = 0;
            colors[1][3] = 1;
            colors[1][4] = 0;
            colors[1][5] = 1;
            chn[1]       = 2;

            colors.Add(new int[9]);
            colors[2][0] = 0;
            colors[2][1] = 1;
            colors[2][2] = 0;
            colors[2][3] = 1;
            colors[2][4] = 0;
            colors[2][5] = 2;
            colors[2][6] = 1;
            colors[2][7] = 0;
            colors[2][8] = 1;
            chn[2]       = 3;

            colors.Add(new int[5]);
            colors[3][0] = 0;
            colors[3][1] = 1;
            colors[3][2] = 0;
            colors[3][3] = 1;
            colors[3][4] = 2;
            chn[3]       = 3;

            colors.Add(new int[5]);
            colors[4][0] = 0;
            colors[4][1] = 1;
            colors[4][2] = 2;
            colors[4][3] = 3;
            colors[4][4] = 4;
            chn[4]       = 5;

            colors.Add(new int[g5vc]);  // nie bedzie uzywana

            sec_graphs[0] = new AdjacencyMatrixGraph(false, 5);
            sec_graphs[0].AddEdge(0, 1, 0);
            sec_graphs[0].AddEdge(0, 3, 2);
            sec_graphs[0].AddEdge(1, 2, 3);
            sec_graphs[0].AddEdge(2, 0, 1);
            sec_graphs[0].AddEdge(2, 3, 4);
            sec_graphs[0].AddEdge(3, 4, 5);
            schi[0] = 6;

            sec_graphs[1] = new AdjacencyMatrixGraph(false, 6);
            sec_graphs[1].AddEdge(0, 1, 0);
            sec_graphs[1].AddEdge(1, 2, 1);
            sec_graphs[1].AddEdge(2, 3, 2);
            sec_graphs[1].AddEdge(3, 4, 0);
            sec_graphs[1].AddEdge(4, 5, 1);
            schi[1] = 3;

//            sec_graphs[2] = new AdjacencyListsGraph<SimpleAdjacencyList>(false, 9);
            sec_graphs[2] = new AdjacencyMatrixGraph(false, 9);
            sec_graphs[2].AddEdge(0, 3, 1);
            sec_graphs[2].AddEdge(1, 0, 0);
            sec_graphs[2].AddEdge(6, 0, 2);
            sec_graphs[2].AddEdge(1, 5, 3);
            sec_graphs[2].AddEdge(5, 4, 4);
            sec_graphs[2].AddEdge(5, 2, 1);
            sec_graphs[2].AddEdge(5, 6, 5);
            sec_graphs[2].AddEdge(6, 7, 6);
            sec_graphs[2].AddEdge(7, 8, 0);
            schi[2] = 7;

            sec_graphs[3] = new AdjacencyMatrixGraph(false, 5);
            sec_graphs[3].AddEdge(0, 1, 0);
            sec_graphs[3].AddEdge(1, 2, 2);
            sec_graphs[3].AddEdge(2, 3, 3);
            sec_graphs[3].AddEdge(3, 4, 4);
            sec_graphs[3].AddEdge(4, 0, 1);
            schi[3] = 5;

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

            sec_graphs[5] = new AdjacencyListsGraph <SimpleAdjacencyList>(true, g5vc);
            for (int i = 0; i < g5vc; ++i)
            {
                sec_graphs[5].AddEdge(i, (i + 1) % g5vc, i % 3);
            }
            schi[5] = 3;

            TestSets["LabSquareOfGraphTests"]      = new TestSet(new Lab03Helper(), "Part 1 - square of graph [0.5 pkt]", null, false);
            TestSets["LabLineGraphTests"]          = new TestSet(new Lab03Helper(), "Part 2 - line graph [2 pkt]", null, false);
            TestSets["LabVertexColoringTests"]     = new TestSet(new Lab03Helper(), "Part 3 - vertex coloring [1 pkt]", null, false);
            TestSets["LabStrongEdgeColoringTests"] = new TestSet(new Lab03Helper(), "Part 4 - strong edge coloring [0.5 pkt]", null, false);

            for (int i = 0; i < n; ++i)
            {
                TestSets["LabSquareOfGraphTests"].TestCases.Add(new SquareofGraphTestCase(1, null, "", graphs[i], squares[i]));
                TestSets["LabLineGraphTests"].TestCases.Add(new LineGraphTestCase(1, null, "", graphs[i], linegraphs[i], names[i]));
                TestSets["LabVertexColoringTests"].TestCases.Add(new VertexColoringTestCase(1, graphs[i].Directed?new ArgumentException():null, "", graphs[i], chn[i], colors[i]));
                TestSets["LabStrongEdgeColoringTests"].TestCases.Add(new StrongEdgeColoringTestCase(1, null, "", graphs[i], sec_graphs[i], schi[i]));
            }
        }
Example #20
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}");
                }
            }
        }
Example #21
0
    public static void Main()
    {
        Graph[]  g = new Graph[4];
        string[] m = new string[] { "Matrix    ",
                                    "HashTable ",
                                    "SimplyList",
                                    "AVL tree  " };
        int      s, n = 2000;
        ulong    c1, c2;
        DateTime t1, t2;

        RandomGraphGenerator rgg = new RandomGraphGenerator(12345);

// Elementy tabgicy g zawierają ten sam graf w różnych reprezentacjach
        g[0] = rgg.DirectedGraph(typeof(AdjacencyMatrixGraph), n, 0.05, -99, 99);
        g[1] = new AdjacencyListsGraph <HashTableAdjacencyList>(g[0]);
        g[2] = new AdjacencyListsGraph <SimplyAdjacencyList>(g[0]);
        g[3] = new AdjacencyListsGraph <AVLAdjacencyList>(g[0]);

// Liczymy sumę wag wszystkich krawędzi grafu

// Sposób 1 - najlepszy, korzystamy jedynie z metody OutEdges
        Console.WriteLine();
        for (int i = 0; i < g.Length; ++i)
        {
            c1 = Graph.Counter;
            t1 = DateTime.Now;
            s  = 0;
            for (int v = 0; v < n; ++v)
            {
                foreach (var e in g[i].OutEdges(v))
                {
                    s += e.Weight;
                }
            }
            t2 = DateTime.Now;
            c2 = Graph.Counter;
            Console.WriteLine(" A {0} # suma {1}  -  licznik:  {2,11} , czas:  {3}", m[i], s, c2 - c1, t2 - t1);
        }

// Sposób 2 - gorszy, korzystamy z metod OutEdges i GetEdgeWeight
        Console.WriteLine();
        for (int i = 0; i < g.Length; ++i)
        {
            c1 = Graph.Counter;
            t1 = DateTime.Now;
            s  = 0;
            for (int v = 0; v < n; ++v)
            {
                foreach (var e in g[i].OutEdges(v))
                {
                    s += (int)g[i].GetEdgeWeight(e.From, e.To);
                }
            }
            t2 = DateTime.Now;
            c2 = Graph.Counter;
            Console.WriteLine(" B {0} # suma {1}  -  licznik:  {2,11} , czas:  {3}", m[i], s, c2 - c1, t2 - t1);
        }

// Sposób 3 - najgorszy, korzystamy jedynie z metody GetEdgeWeight - nie robić tak!
        Console.WriteLine();
        int?w;

        for (int i = 0; i < g.Length; ++i)
        {
            c1 = Graph.Counter;
            t1 = DateTime.Now;
            s  = 0;
            for (int v = 0; v < n; ++v)
            {
                for (int vv = 0; vv < n; ++vv)
                {
                    w = g[i].GetEdgeWeight(v, vv);
                    if (w != null)
                    {
                        s += (int)w;
                    }
                }
            }
            t2 = DateTime.Now;
            c2 = Graph.Counter;
            Console.WriteLine(" C {0} # suma {1}  -  licznik:  {2,11} , czas:  {3}", m[i], s, c2 - c1, t2 - t1);
        }

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

        int[]    order;
        string[] desc;
        int[]    ec = new int[3];

        Graph g = new AdjacencyMatrixGraph(false, 5);

        g.AddEdge(0, 1);
        g.AddEdge(0, 4);
        g.AddEdge(1, 2);
        g.AddEdge(2, 0);
        g.AddEdge(2, 3);
        g.AddEdge(3, 4);
        g.AddEdge(4, 3);
        ge.Export(g, null, "G");

        Graph lg = Lab03.LineGraph(g, out desc);

        ge.Export(lg, desc, "LG");

        Graph[] g2 = new Graph[3];
        g2[0] = new AdjacencyMatrixGraph(true, 4);
        g2[0].AddEdge(0, 1);
        g2[0].AddEdge(0, 3);
        g2[0].AddEdge(1, 2);
        g2[0].AddEdge(3, 2);

        g2[1] = rgg.DAG(typeof(AdjacencyMatrixGraph), 100, 0.9, 1, 1);
        g2[2] = rgg.DAG(typeof(AdjacencyListsGraph <SimplyAdjacencyList>), 1000, 0.2, 1, 1);

        ec[0] = g2[0].EdgesCount;
        ec[1] = g2[1].EdgesCount;
        ec[2] = g2[2].EdgesCount;

        Console.WriteLine("Sortowanie topologiczne - DFS");
        for (int i = 0; i < 3; ++i)
        {
            order = Lab03.TopologicalSort_DFS(g2[i]);
            Console.WriteLine("  test {0} : {1}", i, TopologicalSortTest(g2[i], order));
        }

        if (ec[0] != g2[0].EdgesCount || ec[1] != g2[1].EdgesCount || ec[2] != g2[2].EdgesCount)
        {
            Console.WriteLine("  Blad - zmieniono graf");
        }

        ec[0] = g2[0].EdgesCount;
        ec[1] = g2[1].EdgesCount;
        ec[2] = g2[2].EdgesCount;

        Console.WriteLine("Sortowanie topologiczne - zrodla 1");
        for (int i = 0; i < 3; ++i)
        {
            order = Lab03.TopologicalSort_V0(g2[i]);
            Console.WriteLine("  test {0} : {1}", i, TopologicalSortTest(g2[i], order));
        }

        if (ec[0] != g2[0].EdgesCount || ec[1] != g2[1].EdgesCount || ec[2] != g2[2].EdgesCount)
        {
            Console.WriteLine("  Blad - zmieniono graf");
        }

        Graph[] g3 = new Graph[3];
        g3[0] = g2[0].Clone();
        g3[0].AddEdge(1, 0);
        g3[1] = rgg.DirectedGraph(typeof(AdjacencyMatrixGraph), 100, 0.9, 1, 1);
        g3[2] = rgg.DirectedGraph(typeof(AdjacencyListsGraph <SimplyAdjacencyList>), 1000, 0.2, 1, 1);

        ec[0] = g3[0].EdgesCount;
        ec[1] = g3[1].EdgesCount;
        ec[2] = g3[2].EdgesCount;

        Console.WriteLine("Sortowanie topologiczne - zrodla 2");
        for (int i = 0; i < 3; ++i)
        {
            order = Lab03.TopologicalSort_V0(g3[i]);
            Console.WriteLine("  test {0} : {1}", i, order == null);
        }

        if (ec[0] != g3[0].EdgesCount || ec[1] != g3[1].EdgesCount || ec[2] != g3[2].EdgesCount)
        {
            Console.WriteLine("  Blad - zmieniono graf");
        }
    }
        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 #24
0
        static void Main(string[] args)
        {
            Graph roads, paths, paths0, paths1, paths2;

            int[]              cityCosts;
            string[]           names;
            List <List <int> > listOfResults0 = new List <List <int> >();
            List <List <int> > listOfResults1 = new List <List <int> >();
            List <List <int> > listOfResults2 = new List <List <int> >();
            List <int>         result0        = new List <int>();
            List <int>         result1        = new List <int>();
            List <int>         result2        = new List <int>();
            List <Graph>       graphs         = new List <Graph>();
            List <Graph>       listOfPaths0   = new List <Graph>();
            List <Graph>       listOfPaths1   = new List <Graph>();
            List <Graph>       listOfPaths2   = new List <Graph>();
            List <int[]>       costs          = new List <int[]>();
            List <int>         bounds         = new List <int>();
            List <int>         expCosts       = new List <int>();
            ulong              cr;

            ulong [,] cgg = new ulong[3, 5];
            cgg[0, 0]     = 325;
            cgg[0, 1]     = 211;
            cgg[0, 2]     = 169;
            cgg[0, 3]     = 393;
            cgg[0, 4]     = 33523661;
            cgg[1, 0]     = 946;
            cgg[1, 1]     = 638;
            cgg[1, 2]     = 550;
            cgg[1, 3]     = 1090;
            cgg[1, 4]     = 62343794;
            cgg[2, 0]     = 1030;
            cgg[2, 1]     = 758;
            cgg[2, 2]     = 582;
            cgg[2, 3]     = 1231;
            cgg[2, 4]     = 76762096;


            Test0(out roads, out cityCosts, out result0, out result1, out result2, out paths0, out paths1, out paths2);
            graphs.Add(roads);
            costs.Add(cityCosts);
            listOfPaths0.Add(paths0);
            listOfPaths1.Add(paths1);
            listOfPaths2.Add(paths2);
            listOfResults0.Add(result0);
            listOfResults1.Add(result1);
            listOfResults2.Add(result2);


            Test1(out roads, out cityCosts, out result0, out result1, out result2, out paths0, out paths1, out paths2);
            graphs.Add(roads);
            costs.Add(cityCosts);
            listOfPaths0.Add(paths0);
            listOfPaths1.Add(paths1);
            listOfPaths2.Add(paths2);
            listOfResults0.Add(result0);
            listOfResults1.Add(result1);
            listOfResults2.Add(result2);


            Test2(out roads, out cityCosts, out result0, out result1, out result2, out paths0, out paths1, out paths2);
            graphs.Add(roads);
            costs.Add(cityCosts);
            listOfPaths0.Add(paths0);
            listOfPaths1.Add(paths1);
            listOfPaths2.Add(paths2);
            listOfResults0.Add(result0);
            listOfResults1.Add(result1);
            listOfResults2.Add(result2);


            Test3(out roads, out cityCosts, out result0, out result1, out result2, out paths0, out paths1, out paths2);
            graphs.Add(roads);
            costs.Add(cityCosts);
            listOfPaths0.Add(paths0);
            listOfPaths1.Add(paths1);
            listOfPaths2.Add(paths2);
            listOfResults0.Add(result0);
            listOfResults1.Add(result1);
            listOfResults2.Add(result2);

            int n = 200;
            RandomGraphGenerator rgg = new RandomGraphGenerator(12345);

            roads = rgg.UndirectedGraph(typeof(AdjacencyMatrixGraph), n, 0.75, 1, 99);
            Random rnd = new Random(1234);

            cityCosts = new int[n];
            for (int i = 0; i < n; i++)
            {
                cityCosts[i] = rnd.Next(1, 9);
            }
            graphs.Add(roads);
            costs.Add(cityCosts);

            Pathfinder pf;
            int        minCost = 0;

            int[] path;
            bool  pass;

            Console.WriteLine("\nCzesc 0\n");
            for (int testno = 0; testno < graphs.Count - 1; ++testno)
            {
                roads     = graphs[testno];
                cityCosts = costs[testno];
                result0   = listOfResults0.ElementAt(testno);

                names = new string[cityCosts.Length];
                for (int i = 0; i < cityCosts.Length; ++i)
                {
                    names[i] = String.Format("{0} ({1})", i, cityCosts[i]);
                }

                pf      = new Pathfinder(roads, cityCosts);
                minCost = 0;
                cr      = Graph.Counter;
                path    = pf.FindBestLocationWithoutCityCosts(out minCost, out paths);
                cr      = Graph.Counter - cr;

                pass = path != null && path.SequenceEqual(result0) && minCost == result0.Min();
                Console.WriteLine("Test {0} koszty  - {1}", testno, pass ? "Dobrze" : "Zle");
                if (paths != null)
                {
                    pass = paths.IsEqual(listOfPaths0.ElementAt(testno));
                }
                else
                {
                    pass = false;
                }
                Console.WriteLine("Test {0} sciezki - {1}", testno, pass ? "Dobrze" : "Zle");
                Console.WriteLine("Test {0} wydajnosc: {1} ( wzorcowa: {2} )", testno, cr, cgg[0, testno]);
            }

            roads     = graphs[graphs.Count - 1];
            cityCosts = costs[graphs.Count - 1];
            pf        = new Pathfinder(roads, cityCosts);
            minCost   = 0;
            cr        = Graph.Counter;
            path      = pf.FindBestLocationWithoutCityCosts(out minCost, out paths);
            cr        = Graph.Counter - cr;
            Console.WriteLine("\nTest wydajnosci : {0} ( wzorcowo: {1} )", cr, cgg[0, 4]);

            Console.WriteLine("\nCzesc 1\n");
            for (int testno = 0; testno < graphs.Count - 1; ++testno)
            {
                roads     = graphs[testno];
                cityCosts = costs[testno];
                result1   = listOfResults1.ElementAt(testno);

                names = new string[cityCosts.Length];
                for (int i = 0; i < cityCosts.Length; ++i)
                {
                    names[i] = String.Format("{0} ({1})", i, cityCosts[i]);
                }

                pf      = new Pathfinder(roads, cityCosts);
                minCost = 0;
                cr      = Graph.Counter;
                path    = pf.FindBestLocation(out minCost, out paths);
                cr      = Graph.Counter - cr;

                pass = path != null && path.SequenceEqual(result1) && minCost == result1.Min();
                Console.WriteLine("Test {0} koszty  - {1}", testno, pass ? "Dobrze" : "Zle");
                if (paths != null)
                {
                    pass = paths.IsEqual(listOfPaths1.ElementAt(testno));
                }
                else
                {
                    pass = false;
                }
                Console.WriteLine("Test {0} sciezki - {1}", testno, pass ? "Dobrze" : "Zle");
                Console.WriteLine("Test {0} wydajnosc: {1} ( wzorcowa: {2} )", testno, cr, cgg[1, testno]);
            }

            roads     = graphs[graphs.Count - 1];
            cityCosts = costs[graphs.Count - 1];
            pf        = new Pathfinder(roads, cityCosts);
            minCost   = 0;
            cr        = Graph.Counter;
            path      = pf.FindBestLocation(out minCost, out paths);
            cr        = Graph.Counter - cr;
            Console.WriteLine("\nTest wydajnosci : {0} ( wzorcowo: {1} )", cr, cgg[1, 4]);

            Console.WriteLine("\nCzesc 2\n");
            for (int testno = 0; testno < graphs.Count - 1; ++testno)
            {
                roads     = graphs[testno];
                cityCosts = costs[testno];
                result2   = listOfResults2.ElementAt(testno);

                names = new string[cityCosts.Length];
                for (int i = 0; i < cityCosts.Length; ++i)
                {
                    names[i] = String.Format("{0} ({1})", i, cityCosts[i]);
                }

                minCost = 0;
                pf      = new Pathfinder(roads, cityCosts);
                cr      = Graph.Counter;
                path    = pf.FindBestLocationSecondMetric(out minCost, out paths);
                cr      = Graph.Counter - cr;

                pass = path != null && path.SequenceEqual(result2) && minCost == result2.Min();
                Console.WriteLine("Test {0} koszty  - {1}", testno, pass ? "Dobrze" : "Zle");
                if (paths != null)
                {
                    pass = paths.IsEqual(listOfPaths2.ElementAt(testno));
                }
                else
                {
                    pass = false;
                }
                Console.WriteLine("Test {0} sciezki - {1}", testno, pass ? "Dobrze" : "Zle");
                Console.WriteLine("Test {0} wydajnosc: {1} ( wzorcowa: {2} )", testno, cr, cgg[2, testno]);
            }

            roads     = graphs[graphs.Count - 1];
            cityCosts = costs[graphs.Count - 1];
            pf        = new Pathfinder(roads, cityCosts);
            minCost   = 0;
            cr        = Graph.Counter;
            path      = pf.FindBestLocationSecondMetric(out minCost, out paths);
            cr        = Graph.Counter - cr;
            Console.WriteLine("\nTest wydajnosci : {0} ( wzorcowo: {1} )", cr, cgg[2, 4]);

            Console.WriteLine();
        }
        private static List <CirculationTestCase> GetTestCases()
        {
            List <CirculationTestCase> testCases = new List <CirculationTestCase>();

            IGraph testGraph1 = Graph.IsolatedVerticesGraph(true, 6, typeof(AdjacencyMatrixGraph));

            testGraph1.AddEdge(0, 1, 10);
            testGraph1.AddEdge(0, 2, 3);
            testGraph1.AddEdge(1, 2, 6);
            testGraph1.AddEdge(3, 2, 7);
            testGraph1.AddEdge(1, 4, 7);
            testGraph1.AddEdge(4, 3, 4);
            testGraph1.AddEdge(3, 5, 9);
            testGraph1.AddEdge(4, 5, 4);

            int[] demands1 = new int[6] {
                -7, -8, 10, -6, 0, 11
            };

            testCases.Add(new CirculationTestCase()
            {
                TestGraph = testGraph1, Demands = demands1, ExpectedResult = true
            });

            var rgg    = new RandomGraphGenerator();
            var random = new Random(0);

            var testGraph2 = rgg.DirectedGraph(typeof(AdjacencyMatrixGraph), 10, 0.7, 1, 20);

            int[] demands2 = new[] { -4, -6, 2, 7, 0, -5, 8, 0, -2, 0 };
            int[] demands3 = new[] { -4, -6, 2, 7, 0, -5, 8, 0, -3, -5 };

            testCases.Add(new CirculationTestCase()
            {
                TestGraph = testGraph2, Demands = demands2, ExpectedResult = true
            });
            testCases.Add(new CirculationTestCase()
            {
                TestGraph = testGraph2, Demands = demands3, ExpectedResult = false
            });

            var lowerBounds1 = testGraph1.IsolatedVerticesGraph();

            lowerBounds1.AddEdge(0, 1, 8);

            testCases.Add(new CirculationTestCase()
            {
                TestGraph = testGraph1, Demands = demands1, LowerBounds = lowerBounds1, ExpectedResult = false
            });

            var lowerBounds2 = testGraph1.IsolatedVerticesGraph();

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

            testCases.Add(new CirculationTestCase()
            {
                TestGraph = testGraph1, Demands = demands1, LowerBounds = lowerBounds2, ExpectedResult = true
            });

            var lowerBounds3 = testGraph2.Clone();

            for (int v = 0; v < lowerBounds3.VerticesCount; v++)
            {
                foreach (var e in lowerBounds3.OutEdges(v))
                {
                    lowerBounds3.ModifyEdgeWeight(e.From, e.To, -e.Weight + 1);
                }
            }

            testCases.Add(new CirculationTestCase()
            {
                TestGraph = testGraph2, Demands = demands2, LowerBounds = lowerBounds3, ExpectedResult = true
            });


            return(testCases);
        }
Example #26
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 #27
0
    public static void Main()
    {
        int?   m;
        IGraph g0, g1, g2, g3, g4, g5, g6, g7;

        Edge[] c;

        RandomGraphGenerator gen = new RandomGraphGenerator(1);

        g0 = gen.UndirectedEuclidGraph(typeof(AdjacencyMatrixGraph), 5, 1.0, 0.0, 100.0, 0.0, 100.0);
        g1 = gen.UndirectedEuclidGraph(typeof(AdjacencyMatrixGraph), 100, 1.0, 0.0, 100.0, 0.0, 100.0);
        g2 = gen.UndirectedGraph(typeof(AdjacencyMatrixGraph), 100, 1.0, 1, 99);
        g3 = gen.DirectedGraph(typeof(AdjacencyMatrixGraph), 100, 1.0, 1, 99);
        g4 = gen.UndirectedGraph(typeof(AdjacencyMatrixGraph), 100, 0.9, 1, 99);
        g5 = gen.DirectedGraph(typeof(AdjacencyMatrixGraph), 100, 0.9, 1, 99);
        g6 = gen.UndirectedGraph(typeof(AdjacencyListsGraph), 100, 0.2, 1, 99);
        g7 = gen.DirectedGraph(typeof(AdjacencyListsGraph), 100, 0.2, 1, 99);

        Console.WriteLine("\nAlgorytm \"Kruskalopodobny\"");

        Console.Write("  maly graf euklidesowy     -  ");
        m = g0.TSP_Kruskal(out c);
        Test(g0, c, m);
        if (m == null)
        {
            Console.WriteLine("    Nie znaleziono cyklu Hamiltona");
        }
        else
        {
            Console.Write("    ");
            for (int i = 0; i < g0.VerticesCount; ++i)
            {
                Console.Write("  {0}", c[i]);
            }
            Console.WriteLine();
        }

        Console.Write("  graf pelny euklidesowy    -  ");
        m = g1.TSP_Kruskal(out c);
        Test(g1, c, m);

        Console.Write("  graf pelny nieskierowany  -  ");
        m = g2.TSP_Kruskal(out c);
        Test(g2, c, m);

        Console.Write("  graf pelny skierowany     -  ");
        m = g3.TSP_Kruskal(out c);
        Test(g3, c, m);

        Console.Write("  graf nieskierowany        -  ");
        m = g4.TSP_Kruskal(out c);
        Test(g4, c, m);

        Console.Write("  graf skierowany           -  ");
        m = g5.TSP_Kruskal(out c);
        Test(g5, c, m);

        Console.Write("  graf rzadki nieskierowany -  ");
        m = g6.TSP_Kruskal(out c);
        Test(g6, c, m);

        Console.Write("  graf rzadki skierowany    -  ");
        m = g7.TSP_Kruskal(out c);
        Test(g7, c, m);

        Console.WriteLine("\nAlgorytm na podstawie drzewa");

        Console.Write("  maly graf euklidesowy     -  ");
        m = g0.TSP_TreeBased(out c);
        Test(g0, c, m);
        if (m == null)
        {
            Console.WriteLine("    Nie znaleziono cyklu Hamiltona");
        }
        else
        {
            Console.Write("    ");
            for (int i = 0; i < g0.VerticesCount; ++i)
            {
                Console.Write("  {0}", c[i]);
            }
            Console.WriteLine();
        }

        Console.Write("  graf pelny euklidesowy    -  ");
        m = g1.TSP_TreeBased(out c);
        Test(g1, c, m);

        Console.Write("  graf pelny nieskierowany  -  ");
        m = g2.TSP_TreeBased(out c);
        Test(g2, c, m);

        Console.Write("  graf pelny skierowany     -  ");
        try
        {
            m = g3.TSP_TreeBased(out c);
            Console.WriteLine("BLAD 3  !!!");
        }
        catch (ArgumentException)
        {
            Console.WriteLine("wyjatek (to dobrze)");
        }

        Console.Write("  graf nieskierowany        -  ");
        m = g4.TSP_TreeBased(out c);
        Test(g4, c, m);

        Console.Write("  graf skierowany           -  ");
        try
        {
            m = g5.TSP_TreeBased(out c);
            Console.WriteLine("BLAD 3  !!!");
        }
        catch (ArgumentException)
        {
            Console.WriteLine("wyjatek (to dobrze)");
        }

        Console.Write("  graf rzadki nieskierowany -  ");
        m = g6.TSP_TreeBased(out c);
        Test(g6, c, m);

        Console.Write("  graf rzadki skierowany    -  ");
        try
        {
            m = g7.TSP_TreeBased(out c);
            Console.WriteLine("BLAD 3  !!!");
        }
        catch (ArgumentException)
        {
            Console.WriteLine("wyjatek (to dobrze)");
        }
    }
Example #28
0
        private static void TestReverse()
        {
            var rgg = new RandomGraphGenerator(12345);

            Graph[] g = new Graph[ReverseTestSize];
            bool[]  ex = { false, false, true, false, false };
            Graph   r, gg;
            ulong   cr, cgg;

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

            for (int i = 0; i < ReverseTestSize; ++i)
            {
                Console.Write($"  Test {i} - ");
                gg = g[i].Clone();
                try
                {
                    cr = Graph.Counter;
                    r  = g[i].Lab03Reverse();
                    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 undirected");
                        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;
                    gg  = g[i].Reverse();
                    cgg = Graph.Counter - cgg;
                    if (!r.IsEqual(gg))
                    {
                        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}");
                }
            }
        }
Example #29
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 #30
0
        static void Main(string[] args)
        {
            try
            {
                var    rgg = new RandomGraphGenerator(123);
                IGraph h1, h2;
                IGraph g1 = new AdjacencyMatrixGraph(false, 5);
                IGraph g2 = new AdjacencyListsGraph(true, 4);
                Console.WriteLine("# licznik: {0}", Graph.Counter);

                g1.AddEdge(0, 1);
                g1.AddEdge(1, 2);
                g1.AddEdge(0, 2);
                g1.AddEdge(0, 4);
                g1.AddEdge(2, 4);
                g1.AddEdge(2, 3);
                Console.WriteLine("# licznik: {0}", Graph.Counter);
                Console.WriteLine("Graf g1 jest typu: {0} i jest skierowany: {1}", g1.GetType(), g1.Directed);


                h1 = g1.AddVertex();
                Console.WriteLine("\nDodawanie\nGraf h1 ma {0} (powinno być 6) wierzchołków, a ostatni wierzchołek ma stopień {1} (powinno być 0)",
                                  h1.VerticesCount, h1.InDegree(h1.VerticesCount - 1));
                Console.WriteLine("Graf h1 ma {0} (powinno być 6) krawędzi", h1.EdgesCount);
                Console.WriteLine("# licznik: {0}", Graph.Counter);


                h1 = g1.DeleteVertex(2);
                Console.WriteLine("\nUsuwanie\nGraf h1 ma {0} (powinno być 4) wierzchołków, a wierzchołki 1,2,3 mają odpowiednio stopienie {1} (powinno być 1),{2} (powinno być 0),{3} (powinno być 1)",
                                  h1.VerticesCount, h1.InDegree(1), h1.InDegree(2), h1.InDegree(3));
                Console.WriteLine("Graf h1 ma {0} (powinno być 2) krawędzi", h1.EdgesCount);
                Console.WriteLine("# licznik: {0}", Graph.Counter);


                h1 = g1.Complement();
                Console.WriteLine("\nDopełnienie g1 ma {0} (powinno być 4) krawędzi", h1.EdgesCount);
                Console.WriteLine("# licznik: {0}", Graph.Counter);


                h1 = g1.Closure();
                Console.WriteLine("\nDomknięcie g1 ma {0} (powinno być 10) krawędzi", h1.EdgesCount);
                Console.WriteLine("# licznik: {0}", Graph.Counter);

                h1 = (g1.AddVertex()).Closure();
                Console.WriteLine("Domknięcie g1 + K1 ma {0} (powinno być 10) krawędzi", h1.EdgesCount);
                Console.WriteLine("# licznik: {0}", Graph.Counter);


                Console.WriteLine("\nCzy h1 jest dwudzielny ?: {0} (powinno być False)", h1.IsBipartite());
                Console.WriteLine("# licznik: {0}", Graph.Counter);

                h1 = rgg.BipariteGraph(typeof(AdjacencyMatrixGraph), 30, 50, 0.5);
                Console.WriteLine("\nCzy nowy h1 jest dwudzielny ?: {0} (powinno być True)", h1.IsBipartite());
                Console.WriteLine("# licznik: {0}", Graph.Counter);

                Console.WriteLine("\n\n*************************\n\n");
                g2.AddEdge(0, 1);
                g2.AddEdge(2, 1);
                g2.AddEdge(3, 2);
                Console.WriteLine("# licznik: {0}", Graph.Counter);
                Console.WriteLine("Graf g2 jest typu: {0} i jest skierowany: {1}", g2.GetType(), g2.Directed);

                h2 = g2.AddVertex();
                Console.WriteLine("\nDodawanie\nGraf h2 ma {0} (powinno być 5) wierzchołków, a ostatni wierzchołek ma stopień wy: {1} (powinno być 0) i we: {2} (powinno być 0) ",
                                  h2.VerticesCount, h2.InDegree(h2.VerticesCount - 1), h2.OutDegree(h2.VerticesCount - 1));
                Console.WriteLine("Graf h2 ma {0} (powinno być 3) krawędzi", h2.EdgesCount);
                Console.WriteLine("# licznik: {0}", Graph.Counter);


                h2 = g2.DeleteVertex(1);
                Console.WriteLine("\nUsuwanie\nGraf h2 ma {0} (powinno być 3) wierzchołków, a wierzchołek 1 ma stopień wy: {1} (powinno być 0) ", h2.VerticesCount, h2.OutDegree(1));
                Console.WriteLine("Graf h2 ma {0} (powinno być 1) krawędzi", h2.EdgesCount);
                Console.WriteLine("# licznik: {0}", Graph.Counter);


                h2 = g2.Complement();
                Console.WriteLine("\nDopełnienie g2 ma {0} (powinno być 9) krawędzi", h2.EdgesCount);
                Console.WriteLine("# licznik: {0}", Graph.Counter);


                h2 = g2.Closure();
                Console.WriteLine("\nDomknięcie g2 + K1 ma {0} (powinno być 4) krawędzi", h2.EdgesCount);
                Console.WriteLine("# licznik: {0}", Graph.Counter);

                h2 = (g2.AddVertex()).Closure();
                Console.WriteLine("Domknięcie g2 + K1 ma {0} (powinno być 4) krawędzi", h2.EdgesCount);
                Console.WriteLine("# licznik: {0}", Graph.Counter);


                Console.WriteLine("\nCzy h2 jest dwudzielny ?: {0} (powinno być False)", g2.IsBipartite());
                Console.WriteLine("# licznik: {0}", Graph.Counter);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
Example #31
0
        public static void Main(string[] args)
        {
            AdjacencyListsGraph <SimplyAdjacencyList> star
                = new AdjacencyListsGraph <SimplyAdjacencyList>(false, 10);

            for (int i = 0; i < 9; i++)
            {
                star.AddEdge(i, 9);
            }

            var evenCSmall = new AdjacencyListsGraph <SimplyAdjacencyList>(false, 10);

            for (int i = 0; i < evenCSmall.VerticesCount; i++)
            {
                evenCSmall.AddEdge(i, (i + 1) % evenCSmall.VerticesCount);
            }

            var oddCSmall = new AdjacencyListsGraph <SimplyAdjacencyList>(false, 11);

            for (int i = 0; i < oddCSmall.VerticesCount; i++)
            {
                oddCSmall.AddEdge(i, (i + 1) % oddCSmall.VerticesCount);
            }

            RandomGraphGenerator rgg = new RandomGraphGenerator(12345);

            var biparSmall = rgg.BipariteGraph(typeof(AdjacencyMatrixGraph), 9, 9, 1);


            var hypercube = new AdjacencyListsGraph <SimplyAdjacencyList>(false, 32);

            for (int i = 0; i < hypercube.VerticesCount; i++)
            {
                for (int j = 0; j < i; j++)
                {
                    if (NumberOfBits(i ^ j) == 1)
                    {
                        hypercube.AddEdge(i, j);
                    }
                }
            }

            var oddC = new AdjacencyListsGraph <SimplyAdjacencyList>(false, 31);

            for (int i = 0; i < oddC.VerticesCount; i++)
            {
                oddC.AddEdge(i, (i + 2) % oddC.VerticesCount);
            }

            AdjacencyMatrixGraph full = new AdjacencyMatrixGraph(false, 50);

            for (int i = 0; i < full.VerticesCount; i++)
            {
                for (int j = 0; j < i; j++)
                {
                    full.AddEdge(i, j);
                }
            }


            Test[] tests     = { new Test(star, 1, 1), new Test(evenCSmall, 5, 2), new Test(oddCSmall, 6, 11), new Test(biparSmall, 9, 2) };
            Test[] testLarge = { new Test(hypercube, 16, 2), new Test(oddC, 16, 31), new Test(full, 49, 50) };

            Console.Out.WriteLine("Ma³e testy");
            for (int i = 0; i < tests.Length; i++)
            {
                PerformTest(i, tests[i]);
            }

            Console.Out.WriteLine("Du¿e testy");
            for (int i = 0; i < testLarge.Length; i++)
            {
                PerformTest(i, testLarge[i]);
            }
        }