예제 #1
0
        public void DepthFirstTraversalIsLazy()
        {
            var guid       = Guid.NewGuid();
            var descriptor = GraphDescriptor.Create(n => n, Function(new Dictionary <int, IEnumerable <int> >
            {
                { 0, A(1, 2) },
                { 1, A(3, 4) },
                { 2, EnumerableEx.Throw <int>(new Exception(guid.ToString())) }
            }));

            var enumerator = Graph.DepthFirstTraversal(A(0), descriptor).GetEnumerator();

            Assert.IsTrue(enumerator.MoveNext());
            Assert.AreEqual(0, enumerator.Current);
            Assert.IsTrue(enumerator.MoveNext());
            Assert.AreEqual(1, enumerator.Current);
            Assert.IsTrue(enumerator.MoveNext());
            Assert.AreEqual(3, enumerator.Current);
            Assert.IsTrue(enumerator.MoveNext());
            Assert.AreEqual(4, enumerator.Current);
            Assert.IsTrue(enumerator.MoveNext());
            Assert.AreEqual(2, enumerator.Current);
            try
            {
                enumerator.MoveNext();
            }
            catch (Exception e)
            {
                Assert.AreEqual(guid.ToString(), e.Message);
                return;
            }
            Assert.Fail();
        }
예제 #2
0
        public static IEnumerable <IGraphDescriptor <T, T> > AllDirectedAcyclicGraphs <T>(IList <T> nodes)
        {
            var E = new Combinations <T>(nodes, 2).Where(e => e.Count == 2).ToList();

            foreach (var F in Enumerable.Range(0, E.Count + 1).SelectMany(k => new Combinations <IList <T> >(E, k)))
            {
                yield return(GraphDescriptor.Create(u => u, Function(F.GroupBy(e => e[0]).ToDictionary(g => g.Key, g => g.Select(e => e[1])))));
            }
        }