public List <List <TNode> > GetChains(IGraph <TNode> graph)
        {
            var chains       = new List <List <TNode> >();
            var usedVertices = new Dictionary <TNode, bool>();
            var faces        = GraphDecomposer.GetFaces(graph);

            faces.RemoveAt(faces.MaxBy(x => x.Count));
            graph.Vertices.ToList().ForEach(x => usedVertices.Add(x, false));

            if (faces.Count != 0)
            {
                var smallestFaceIndex = faces.MaxBy(x => - x.Count);
                faces[smallestFaceIndex].ForEach(x => usedVertices[x] = true);
                chains.Add(faces[smallestFaceIndex]);
                faces.RemoveAt(smallestFaceIndex);
            }

            // Process all the cycles
            while (faces.Count != 0)
            {
                var chain = GetSmallestNeighbouringCycle(graph, faces, usedVertices);
                chains.Add(chain);
            }

            // Process all the paths
            while (usedVertices.Any(x => !x.Value))
            {
                var chain = GetNeighbouringPath(graph, usedVertices);
                chains.Add(chain);
            }

            return(chains);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Setups graph and gets faces.
        /// </summary>
        /// <param name="graph"></param>
        protected void Initialize(IGraph <TNode> graph)
        {
            if (!GraphUtils.IsConnected(graph))
            {
                throw new ArgumentException("The graph must be connected", nameof(graph));
            }

            Graph           = graph;
            Faces           = GraphDecomposer.GetFaces(Graph);
            CoveredVertices = new Dictionary <TNode, int>();

            // Initialize all vertices to the -1 depth
            graph.Vertices.ToList().ForEach(x => SetDepth(x, -1));
        }