Пример #1
0
        public IList <T> GetDirectedPath(T startPoint)
        {
            // we assume the graph is eularian
            var(source, sink) = GetSourceAndSink();

            // if source exists, sink has to exist. Or both have to be null.
            // In that case we get an eularian circuit
            if (source != null ^ sink != null)
            {
                return(null);
            }
            var eularianPath = new LinkedList <GraphNode <T> >();

            _directedGraph.ClearEdgeColors();
            // if source is null, we have an eularian circuit.
            // If a start point is suggested, we use that, otherwise, we can start anywhere
            if (!_directedGraph.Nodes.TryGetValue(new GraphNode <T>(startPoint), out var startNode))
            {
                startNode = _directedGraph.Nodes.First();
            }
            startNode = source ?? startNode;

            while (startNode != null)
            {
                var partialPath = GetPartialPath(startNode);
                MergePaths(eularianPath, partialPath, startNode);
                startNode = GetNodeWithUnusedEdges(eularianPath);
            }

            return(_directedGraph.Edges.Any(x => x.Color == Color.Uncolored) ? null : eularianPath.Select(x => x.Label).ToList());
        }