/// <inheritdoc />
        public override List <Chain <TNode> > GetChains(IGraph <TNode> graph)
        {
            Initialize(graph);

            if (Faces.Count != 0)
            {
                // Get faces and remove the largest one
                Faces.RemoveAt(Faces.MaxBy(x => x.Count));
            }

            var decomposition = new PartialDecomposition(Faces);

            decomposition = GetFirstComponent(decomposition);

            while (decomposition.GetAllCoveredVertices().Count != Graph.VerticesCount)
            {
                decomposition = ExtendDecomposition(decomposition);
            }

            var chains = decomposition.GetFinalDecomposition();

            logger.WriteLine("Final decomposition:");
            foreach (var chain in chains)
            {
                logger.WriteLine($"[{string.Join(",", chain.Nodes)}]");
            }

            return(chains);
        }
示例#2
0
        /// <inheritdoc />
        public override List <Chain <TNode> > GetChains(IGraph <TNode> graph)
        {
            Initialize(graph);

            if (Faces.Count != 0)
            {
                // Get faces and remove the largest one
                Faces.RemoveAt(Faces.MaxBy(x => x.Count));
            }

            var chains = new List <Chain <TNode> >();

            if (Faces.Count != 0)
            {
                chains.Add(new Chain <TNode>(GetFirstCycle(Faces), chains.Count));
            }

            // Process cycles
            while (Faces.Count != 0)
            {
                var chain       = GetNextCycle();
                var isFromCycle = chain != null;

                if (chain == null)
                {
                    chain = GetNextPath();
                }

                // Must not happen. There must always be a cycle or a path while we have at least one face available.
                if (chain == null)
                {
                    throw new InvalidOperationException();
                }

                chains.Add(new Chain <TNode>(chain, chains.Count)
                {
                    IsFromFace = isFromCycle,
                });
            }

            // Add remaining nodes
            while (Graph.Vertices.Any(x => !IsCovered(x)))
            {
                var chain = GetNextPath();

                // Must not happen. There must always be a path while there are vertices that are not covered. (The graph must be connected)
                if (chain == null)
                {
                    throw new InvalidOperationException();
                }

                chains.Add(new Chain <TNode>(chain, chains.Count)
                {
                    IsFromFace = false,
                });
            }

            return(chains);
        }