protected internal virtual TransducerGraph BuildMinimizedFA()
        {
            TransducerGraph minimizedFA   = new TransducerGraph();
            TransducerGraph unminimizedFA = GetUnminimizedFA();

            foreach (TransducerGraph.Arc arc in unminimizedFA.GetArcs())
            {
                ICollection <TransducerGraph.Arc> source = ProjectNode(arc.GetSourceNode());
                ICollection <TransducerGraph.Arc> target = ProjectNode(arc.GetTargetNode());
                try
                {
                    if (minimizedFA.CanAddArc(source, target, arc.GetInput(), arc.GetOutput()))
                    {
                        minimizedFA.AddArc(source, target, arc.GetInput(), arc.GetOutput());
                    }
                }
                catch (Exception)
                {
                }
            }
            //throw new IllegalArgumentException();
            minimizedFA.SetStartNode(ProjectNode(unminimizedFA.GetStartNode()));
            foreach (object o in unminimizedFA.GetEndNodes())
            {
                minimizedFA.SetEndNode(ProjectNode(o));
            }
            return(minimizedFA);
        }
        // end static class ExactBlock
        public static void Main(string[] args)
        {
            TransducerGraph fa = new TransducerGraph();

            fa.AddArc(fa.GetStartNode(), "1", "a", string.Empty);
            fa.AddArc(fa.GetStartNode(), "2", "b", string.Empty);
            fa.AddArc(fa.GetStartNode(), "3", "c", string.Empty);
            fa.AddArc("1", "4", "a", string.Empty);
            fa.AddArc("2", "4", "a", string.Empty);
            fa.AddArc("3", "5", "c", string.Empty);
            fa.AddArc("4", "6", "c", string.Empty);
            fa.AddArc("5", "6", "c", string.Empty);
            fa.SetEndNode("6");
            System.Console.Out.WriteLine(fa);
            ExactAutomatonMinimizer minimizer = new ExactAutomatonMinimizer();

            System.Console.Out.WriteLine(minimizer.MinimizeFA(fa));
            System.Console.Out.WriteLine("Starting...");
            Timing.StartTime();
            TransducerGraph randomFA          = TransducerGraph.CreateRandomGraph(100, 10, 1.0, 10, new ArrayList());
            TransducerGraph minimizedRandomFA = minimizer.MinimizeFA(randomFA);

            System.Console.Out.WriteLine(randomFA);
            System.Console.Out.WriteLine(minimizedRandomFA);
            Timing.Tick("done. ( " + randomFA.GetArcs().Count + " arcs to " + minimizedRandomFA.GetArcs().Count + " arcs)");
        }
        // assumes that the path already has EPSILON as the last element.
        public static void AddOnePathToGraph(IList path, double count, int markovOrder, TransducerGraph graph)
        {
            object source = graph.GetStartNode();

            for (int j = 0; j < path.Count; j++)
            {
                object input          = path[j];
                TransducerGraph.Arc a = graph.GetArcBySourceAndInput(source, input);
                if (a != null)
                {
                    // increment the arc weight
                    a.output = ((double)a.output) + count;
                }
                else
                {
                    object target;
                    if (input.Equals(TransducerGraph.EpsilonInput))
                    {
                        target = "END";
                    }
                    else
                    {
                        // to ensure they all share the same end node
                        if (markovOrder == 0)
                        {
                            // we all transition back to the same state
                            target = source;
                        }
                        else
                        {
                            if (markovOrder > 0)
                            {
                                // the state is described by the partial history
                                target = path.SubList((j < markovOrder ? 0 : j - markovOrder + 1), j + 1);
                            }
                            else
                            {
                                // the state is described by the full history
                                target = path.SubList(0, j + 1);
                            }
                        }
                    }
                    double output = count;
                    a = new TransducerGraph.Arc(source, target, input, output);
                    graph.AddArc(a);
                }
                source = a.GetTargetNode();
            }
            graph.SetEndNode(source);
        }