Beispiel #1
0
        } // End Sub BaseTestString

        public static void TestMe()
        {
            // ls.Add(new TopologicObject("bar"));
            // ls.Add(new TopologicObject("foo"));
            // ls.Add(new DB.Topology.TopologicObject("foobar").DependsOn("foo").DependsOn("bar") );
            // ls.Add(new TopologicObject("omg").DependsOn("foo"));

            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            sw.Start();
            System.Collections.Generic.List <string> ret = Kahn.Sort(
                new System.Collections.Generic.HashSet <string>(new string[] { "bar", "foo", "foobar", "omg" }),
                new System.Collections.Generic.HashSet <System.Tuple <string, string> >(
                    new[]
            {
                // System.Tuple.Create("foobar", "foo")
                //,System.Tuple.Create("foobar", "bar")
                //,System.Tuple.Create("omg", "foo")

                // System.Tuple.Create(Dependency, DependencyOwner);
                System.Tuple.Create("foo", "foobar")
                , System.Tuple.Create("bar", "foobar")
                , System.Tuple.Create("foo", "omg")
            }
                    )
                );

            sw.Stop();
            long ems = sw.ElapsedMilliseconds;

            System.Console.WriteLine($"Duration Kahn:\t{ems}");
        } // End Sub TestMe
Beispiel #2
0
        } // End Sub BaseTestInteger

        public static void BaseTestString()
        {
            System.Collections.Generic.List <string> ret = Kahn.Sort(
                new System.Collections.Generic.HashSet <string>(new[] { "A", "B", "C", "D", "E", "F", "G", "H" }),
                new System.Collections.Generic.HashSet <System.Tuple <string, string> >(
                    new[]
            {
                System.Tuple.Create("A", "E"),
                System.Tuple.Create("A", "D"),
                System.Tuple.Create("B", "E"),
                System.Tuple.Create("C", "D"),
                System.Tuple.Create("C", "H"),
                System.Tuple.Create("E", "F"),
                System.Tuple.Create("E", "G"),
                System.Tuple.Create("E", "H"),
                System.Tuple.Create("D", "G")
            }
                    )
                );

            System.Console.WriteLine(ret);

            System.Diagnostics.Debug.Assert(ret.SequenceEqual(new[] { "A", "B", "E", "F", "C", "D", "G", "H" }));
            System.Console.WriteLine("Finished");
        } // End Sub BaseTestString
Beispiel #3
0
        public void ThreePeaks()
        {
            WeightedGraphMatrix <int> graph = new WeightedGraphMatrix <int>(true, 3);

            graph.AddArc(1, 0, 2);
            graph.AddArc(1, 2, 3);

            graph.AddArc(0, 2, 4);

            Assert.AreEqual(1, Kahn.Run(new UnweightedGraphWrapper <int>(graph))[0]);
            Assert.AreEqual(0, Kahn.Run(new UnweightedGraphWrapper <int>(graph))[1]);
            Assert.AreEqual(2, Kahn.Run(new UnweightedGraphWrapper <int>(graph))[2]);
        }
Beispiel #4
0
        public void WithoutArcs()
        {
            UnweightedGraphMatrix graph = new UnweightedGraphMatrix(true, 5);

            int[] result = Kahn.Run(graph);

            Assert.AreEqual(5, result.Length);
            Assert.AreEqual(0, result[0]);
            Assert.AreEqual(1, result[1]);
            Assert.AreEqual(2, result[2]);
            Assert.AreEqual(3, result[3]);
            Assert.AreEqual(4, result[4]);
        }
Beispiel #5
0
        public void CycleSix()
        {
            UnweightedGraphMatrix graph = new UnweightedGraphMatrix(true, 6);

            graph.AddArc(0, 3);
            graph.AddArc(0, 2);

            graph.AddArc(1, 3);

            graph.AddArc(3, 4);
            graph.AddArc(3, 5);

            graph.AddArc(4, 1);

            graph.AddArc(5, 1);

            Kahn.Run(graph);
        }
Beispiel #6
0
        public void FivePeaksSeveralScatter()
        {
            // как разобрался, не увидел сложности в таких тестрах, на каждом уровне вершины просто в порядке возрастания
            UnweightedGraphMatrix graph = new UnweightedGraphMatrix(true, 5);

            graph.AddArc(0, 1);
            graph.AddArc(0, 2);

            graph.AddArc(1, 2);

            graph.AddArc(3, 1);

            graph.AddArc(4, 2);
            graph.AddArc(4, 1);

            Assert.AreEqual(0, Kahn.Run(graph)[0]);
            Assert.AreEqual(3, Kahn.Run(graph)[1]);
            Assert.AreEqual(4, Kahn.Run(graph)[2]);
            Assert.AreEqual(1, Kahn.Run(graph)[3]);
            Assert.AreEqual(2, Kahn.Run(graph)[4]);
        }
Beispiel #7
0
        public void FivePeaks()
        {
            UnweightedGraphMatrix graph = new UnweightedGraphMatrix(true, 5);

            graph.AddArc(0, 1);
            graph.AddArc(0, 2);
            graph.AddArc(0, 3);
            graph.AddArc(0, 4);

            graph.AddArc(1, 3);

            graph.AddArc(2, 3);
            graph.AddArc(2, 4);

            graph.AddArc(3, 4);

            Assert.AreEqual(0, Kahn.Run(graph)[0]);
            Assert.AreEqual(1, Kahn.Run(graph)[1]);
            Assert.AreEqual(2, Kahn.Run(graph)[2]);
            Assert.AreEqual(3, Kahn.Run(graph)[3]);
            Assert.AreEqual(4, Kahn.Run(graph)[4]);
        }
Beispiel #8
0
        private static void BaseTestInteger()
        {
            //
            // digraph G {
            //   "7"  -> "11"
            //   "7"  -> "8"
            //   "5"  -> "11"
            //   "3"  -> "8"
            //   "3"  -> "10"
            //   "11" -> "2"
            //   "11" -> "9"
            //   "11" -> "10"
            //   "8"  -> "9"
            // }

            System.Collections.Generic.List <int> ret = Kahn.Sort(
                new System.Collections.Generic.HashSet <int>(new[] { 7, 5, 3, 8, 11, 2, 9, 10 }),
                new System.Collections.Generic.HashSet <System.Tuple <int, int> >(
                    new[]
            {
                System.Tuple.Create(7, 11),
                System.Tuple.Create(7, 8),
                System.Tuple.Create(5, 11),
                System.Tuple.Create(3, 8),
                System.Tuple.Create(3, 10),
                System.Tuple.Create(11, 2),
                System.Tuple.Create(11, 9),
                System.Tuple.Create(11, 10),
                System.Tuple.Create(8, 9)
            }
                    )
                );

            System.Console.WriteLine(ret);
            System.Diagnostics.Debug.Assert(ret.SequenceEqual(new[] { 7, 5, 11, 2, 3, 8, 9, 10 }));
        } // End Sub BaseTestInteger
Beispiel #9
0
 public void NullCheck()
 {
     Kahn.Run(null);
 }
Beispiel #10
0
        public void InputData()
        {
            UnweightedGraphList graph = new UnweightedGraphList(false);

            Kahn.Run(graph);
        }
Beispiel #11
0
        } // End Sub TestMe

        public static void TestWithWrapper()
        {
            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            sw.Start();

            System.Collections.Generic.Dictionary <string, int> dict =
                new System.Collections.Generic.Dictionary <string, int>(
                    System.StringComparer.InvariantCultureIgnoreCase
                    );

            System.Collections.Generic.List <TopologicObject> ls =
                new System.Collections.Generic.List <TopologicObject>();

            ls.Add(new TopologicObject("bar"));
            ls.Add(new TopologicObject("foo"));

            ls.Add(new TopologicObject("foobar")
                   .DependsOn("foo").DependsOn("bar")
                   );

            ls.Add(new TopologicObject("omg").DependsOn("foo"));


            for (int i = 0; i < ls.Count; ++i)
            {
                ls[i].Index = i;
                dict.Add(ls[i].Name, i);
            } // Next i


            for (int i = 0; i < ls.Count; ++i)
            {
                for (int j = 0; j < ls[i].Dependencies.Count; ++j)
                {
                    int key = dict[ls[i].Dependencies[j].Name];
                    ls[i].Dependencies[j].Index = key;
                } // Next j
            }     // Next i



            System.Collections.Generic.HashSet <int> hsAllObjects =
                new System.Collections.Generic.HashSet <int>();

            System.Collections.Generic.HashSet <System.Tuple <int, int> > hsDependence =
                new System.Collections.Generic.HashSet <System.Tuple <int, int> >();


            for (int i = 0; i < ls.Count; ++i)
            {
                hsAllObjects.Add(i);
            } // Next i

            for (int i = 0; i < ls.Count; ++i)
            {
                for (int j = 0; j < ls[i].Dependencies.Count; ++j)
                {
                    hsDependence.Add(System.Tuple.Create(ls[i].Dependencies[j].Index, i));
                } // Next j
            }     // Next i

            System.Collections.Generic.List <int> ret = Kahn.Sort(hsAllObjects, hsDependence);
            string[] sret = new string[ret.Count];
            for (int i = 0; i < ret.Count; ++i)
            {
                sret[i] = ls[ret[i]].Name;
            } // Next i


            sw.Stop();
            long durKahn = sw.ElapsedMilliseconds;

            // sret = sret.Reverse().ToArray();
            System.Console.WriteLine(sret);

            // And the winner is: NOT KAHN !
            System.Console.WriteLine("{0}, {1}", durKahn);
        } // End Sub TestWithWrapper