public void Constructor()
        {
            var graph     = new AdjacencyGraph <int, Edge <int> >();
            var algorithm = new LayeredTopologicalSortAlgorithm <int, Edge <int> >(graph);

            AssertAlgorithmProperties(algorithm, graph);

            graph = new AdjacencyGraph <int, Edge <int> >();
            graph.AddVerticesAndEdgeRange(new[]
            {
                new Edge <int>(1, 2),
                new Edge <int>(1, 3),
                new Edge <int>(2, 3)
            });
            algorithm = new LayeredTopologicalSortAlgorithm <int, Edge <int> >(graph);
            AssertAlgorithmProperties(algorithm, graph);

            #region Local function

            void AssertAlgorithmProperties <TVertex, TEdge>(
                LayeredTopologicalSortAlgorithm <TVertex, TEdge> algo,
                IVertexAndEdgeListGraph <TVertex, TEdge> g)
                where TEdge : IEdge <TVertex>
            {
                AssertAlgorithmState(algo, g);
                Assert.Zero(algo.LayerCount);
                CollectionAssert.IsEmpty(algo.LayerIndices);
                CollectionAssert.IsEmpty(algo.Layers);
            }

            #endregion
        }
        public void SimpleGraph()
        {
            var graph = new BidirectionalGraph <string, Edge <string> >();

            const int nbVertices = 3;
            var       vertices   = new string[nbVertices];

            for (int i = 1; i <= nbVertices; ++i)
            {
                vertices[i - 1] = i.ToString();
                graph.AddVertex(i.ToString());
            }
            graph.AddEdge(new Edge <string>(vertices[0], vertices[1]));
            graph.AddEdge(new Edge <string>(vertices[1], vertices[2]));

            var algorithm             = new LayeredTopologicalSortAlgorithm <string, Edge <string> >(graph);
            int layerFinished         = 0;
            var verticesPerLayer      = new[] { new[] { vertices[0] }, new[] { vertices[1] }, new[] { vertices[2] } };
            var verticesPerLayerStack = new Stack <string[]>(verticesPerLayer.Reverse());

            algorithm.LayerFinished += (sender, args) =>
            {
                Assert.AreEqual(layerFinished, args.LayerIndex);
                CollectionAssert.AreEquivalent(verticesPerLayerStack.Pop(), args.Vertices);
                ++layerFinished;
            };
            algorithm.Compute();

            Assert.AreEqual(3, layerFinished);
            CheckAlgorithmResults(algorithm, verticesPerLayer);
        }
        public void MultipleSource()
        {
            var g  = new BidirectionalGraph <string, Edge <string> >();
            var vs = new string[5];

            for (int i = 1; i < 6; i++)
            {
                vs[i - 1] = i.ToString();
                g.AddVertex(i.ToString());
            }

            g.AddEdge(new Edge <string>(vs[0], vs[1]));
            g.AddEdge(new Edge <string>(vs[1], vs[2]));
            g.AddEdge(new Edge <string>(vs[3], vs[1]));
            g.AddEdge(new Edge <string>(vs[4], vs[2]));

            var lts = new LayeredTopologicalSortAlgorithm <string, Edge <string> >(g);

            lts.Compute();

            Assert.AreEqual(0, lts.LayerIndices[vs[0]]);
            Assert.AreEqual(1, lts.LayerIndices[vs[1]]);
            Assert.AreEqual(2, lts.LayerIndices[vs[2]]);
            Assert.AreEqual(0, lts.LayerIndices[vs[3]]);
            Assert.AreEqual(0, lts.LayerIndices[vs[4]]);
        }
        public void NonAcyclic()
        {
            var g  = new BidirectionalGraph <string, Edge <string> >();
            var vs = new string[4];

            for (int i = 1; i < 5; i++)
            {
                vs[i - 1] = i.ToString();
                g.AddVertex(i.ToString());
            }

            g.AddEdge(new Edge <string>(vs[0], vs[1]));
            g.AddEdge(new Edge <string>(vs[1], vs[2]));
            g.AddEdge(new Edge <string>(vs[2], vs[0]));
            g.AddEdge(new Edge <string>(vs[3], vs[0]));

            try
            {
                var lts = new LayeredTopologicalSortAlgorithm <string, Edge <string> >(g);
                lts.Compute();

                Assert.Fail("It does not throw exception for non acyclic graphs.");
            }
            catch (NonAcyclicGraphException ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }
		public void NonAcyclic()
		{
			var g = new BidirectionalGraph<string, Edge<string>>( );
			var vs = new string[ 4 ];
			for ( int i = 1; i < 5; i++ )
			{
				vs[ i - 1 ] = i.ToString( );
				g.AddVertex( i.ToString( ) );
			}
			g.AddEdge( new Edge<string>( vs[ 0 ], vs[ 1 ] ) );
			g.AddEdge( new Edge<string>( vs[ 1 ], vs[ 2 ] ) );
			g.AddEdge( new Edge<string>( vs[ 2 ], vs[ 0 ] ) );
			g.AddEdge( new Edge<string>( vs[ 3 ], vs[ 0 ] ) );

			try
			{
				var lts = new LayeredTopologicalSortAlgorithm<string, Edge<string>>( g );
				lts.Compute( );

				Assert.Fail( "It does not throw exception for non acyclic graphs." );
			}
			catch ( NonAcyclicGraphException ex )
			{
				Debug.WriteLine( ex.Message );
			}
		}
        private void CreateInitialLayering()
        {
            var layerTopologicalSort = new LayeredTopologicalSortAlgorithm <SugiVertex, SugiEdge>(_graph);

            layerTopologicalSort.Compute();

            for (int i = 0; i < layerTopologicalSort.LayerCount; ++i)
            {
                // Set the layer
                _layers.Add(layerTopologicalSort.Layers[i].ToList());

                // Assign the layer indexes
                foreach (SugiVertex vertex in _layers[i])
                {
                    ThrowIfCancellationRequested();

                    vertex.LayerIndex = i;
                }
            }

            // Minimize edge length
            if (Parameters.MinimizeEdgeLength)
            {
                MinimizeInitialLayersEdgeLength();
            }
        }
        /// <summary>
        /// Creates the layering of the graph. (Assigns every vertex to a layer.)
        /// </summary>
        protected void AssignLayers()
        {
            var lts = new LayeredTopologicalSortAlgorithm <SugiVertex, SugiEdge>(_graph);

            lts.Compute();

            for (int i = 0; i < lts.LayerCount; i++)
            {
                var vl = new VertexLayer(_graph, i, lts.Layers[i].ToList());
                _layers.Add(vl);
            }
        }
        private void CreateInitialLayering()
        {
            var lts = new LayeredTopologicalSortAlgorithm <SugiVertex, SugiEdge>(_graph);

            lts.Compute();

            // we need at least one layer to prevent IndexOutOfBoundsExceptions
            if (lts.LayerCount == 0)
            {
                _layers.Add(new List <SugiVertex>());
            }

            for (int i = 0; i < lts.LayerCount; i++)
            {
                //set the layer
                _layers.Add(lts.Layers[i].ToList());

                //assign the layerindex
                foreach (var v in _layers[i])
                {
                    v.LayerIndex = i;
                }
            }

            //minimize edge length
            if (Parameters.MinimizeEdgeLength)
            {
                for (int i = _layers.Count - 1; i >= 0; i--)
                {
                    var layer = _layers[i];
                    foreach (var v in layer.ToList())
                    {
                        if (_graph.OutDegree(v) == 0)
                        {
                            continue;
                        }

                        //put the vertex above the descendant on the highest layer
                        int newLayerIndex = _graph.OutEdges(v).Min(edge => edge.Target.LayerIndex - 1);

                        if (newLayerIndex != v.LayerIndex)
                        {
                            //we're changing layer
                            layer.Remove(v);
                            _layers[newLayerIndex].Add(v);
                            v.LayerIndex = newLayerIndex;
                        }
                    }
                }
            }
        }
        public void NonAcyclic()
        {
            var graph = new BidirectionalGraph <int, Edge <int> >();

            graph.AddVerticesAndEdgeRange(new[]
            {
                new Edge <int>(1, 2),
                new Edge <int>(2, 3),
                new Edge <int>(3, 1),
                new Edge <int>(4, 1)
            });

            var algorithm = new LayeredTopologicalSortAlgorithm <int, Edge <int> >(graph);

            Assert.Throws <NonAcyclicGraphException>(() => algorithm.Compute());
        }
        private static void CheckAlgorithmResults <TVertex, TEdge>(
            [NotNull] LayeredTopologicalSortAlgorithm <TVertex, TEdge> algorithm,
            [NotNull, ItemNotNull] TVertex[][] verticesPerLayer)
            where TEdge : IEdge <TVertex>
        {
            Assert.AreEqual(verticesPerLayer.Length, algorithm.LayerCount);

            for (var i = 0; i < verticesPerLayer.Length; ++i)
            {
                foreach (TVertex vertex in verticesPerLayer[i])
                {
                    Assert.AreEqual(i, algorithm.LayerIndices[vertex]);
                }

                CollectionAssert.AreEquivalent(
                    verticesPerLayer[i],
                    algorithm.Layers[i]);
            }
        }
		public void Simple()
		{
			var g = new BidirectionalGraph<string, Edge<string>>( );
			var vs = new string[ 3 ];
			for ( int i = 1; i < 4; i++ )
			{
				vs[ i - 1 ] = i.ToString( );
				g.AddVertex( i.ToString( ) );
			}
			g.AddEdge( new Edge<string>( vs[ 0 ], vs[ 1 ] ) );
			g.AddEdge( new Edge<string>( vs[ 1 ], vs[ 2 ] ) );

			var lts = new LayeredTopologicalSortAlgorithm<string, Edge<string>>( g );
			lts.Compute( );

			Assert.AreEqual( lts.LayerIndices[ vs[ 0 ] ], 0 );
			Assert.AreEqual( lts.LayerIndices[ vs[ 1 ] ], 1 );
			Assert.AreEqual( lts.LayerIndices[ vs[ 2 ] ], 2 );
		}
Пример #12
0
        public void Simple()
        {
            var g  = new BidirectionalGraph <string, Edge <string> >( );
            var vs = new string[3];

            for (int i = 1; i < 4; i++)
            {
                vs[i - 1] = i.ToString( );
                g.AddVertex(i.ToString( ));
            }
            g.AddEdge(new Edge <string>(vs[0], vs[1]));
            g.AddEdge(new Edge <string>(vs[1], vs[2]));

            var lts = new LayeredTopologicalSortAlgorithm <string, Edge <string> >(g);

            lts.Compute( );

            Assert.AreEqual(lts.LayerIndices[vs[0]], 0);
            Assert.AreEqual(lts.LayerIndices[vs[1]], 1);
            Assert.AreEqual(lts.LayerIndices[vs[2]], 2);
        }
		public void MultipleSource()
		{
			var g = new BidirectionalGraph<string, Edge<string>>( );
			var vs = new string[ 5 ];
			for ( int i = 1; i < 6; i++ )
			{
				vs[ i - 1 ] = i.ToString( );
				g.AddVertex( i.ToString( ) );
			}
			g.AddEdge( new Edge<string>( vs[ 0 ], vs[ 1 ] ) );
			g.AddEdge( new Edge<string>( vs[ 1 ], vs[ 2 ] ) );
			g.AddEdge( new Edge<string>( vs[ 3 ], vs[ 1 ] ) );
			g.AddEdge( new Edge<string>( vs[ 4 ], vs[ 2 ] ) );

			var lts = new LayeredTopologicalSortAlgorithm<string, Edge<string>>( g );
			lts.Compute( );

			Assert.AreEqual( 0, lts.LayerIndices[ vs[ 0 ] ] );
			Assert.AreEqual( 1, lts.LayerIndices[ vs[ 1 ] ] );
			Assert.AreEqual( 2, lts.LayerIndices[ vs[ 2 ] ] );
			Assert.AreEqual( 0, lts.LayerIndices[ vs[ 3 ] ] );
			Assert.AreEqual( 0, lts.LayerIndices[ vs[ 4 ] ] );
		}