Exemplo n.º 1
0
        public static void MainTest(string[] args)
        {
            // read in digraph from command-line argument
            //if (args.Length < 2) throw new ArgumentException("Expecting input file");
            TextInput input = new TextInput(args[0]);
            Digraph   G     = new Digraph(input);

            // read in sources from command-line arguments
            Bag <int> sources = new Bag <int>();

            for (int i = 1; i < args.Length; i++)
            {
                int s = int.Parse(args[i]);
                sources.Add(s);
            }

            // multiple-source reachability
            DirectedDFS dfs = new DirectedDFS(G, sources);

            // print out vertices reachable from sources
            for (int v = 0; v < G.V; v++)
            {
                if (dfs.Marked(v))
                {
                    Console.Write(v + " ");
                }
            }
            Console.WriteLine();
        }
Exemplo n.º 2
0
Arquivo: NFA.cs Projeto: zzhi/Algs4Net
        /// <summary>
        /// Returns true if the text is matched by the regular expression.</summary>
        /// <param name="txt"> txt the text</param>
        /// <returns><c>true</c> if the text is matched by the regular expression,
        /// <c>false</c> otherwise</returns>
        ///
        public bool Recognizes(string txt)
        {
            DirectedDFS dfs = new DirectedDFS(G, 0);
            Bag <int>   pc  = new Bag <int>();

            for (int v = 0; v < G.V; v++)
            {
                if (dfs.Marked(v))
                {
                    pc.Add(v);
                }
            }

            // Compute possible NFA states for txt[i+1]
            for (int i = 0; i < txt.Length; i++)
            {
                if (txt[i] == '*' || txt[i] == '|' || txt[i] == '(' || txt[i] == ')')
                {
                    throw new ArgumentException("text contains the metacharacter '" + txt[i] + "'");
                }

                Bag <int> match = new Bag <int>();
                foreach (int v in pc)
                {
                    if (v == M)
                    {
                        continue;
                    }
                    if ((regexp[v] == txt[i]) || regexp[v] == '.')
                    {
                        match.Add(v + 1);
                    }
                }
                dfs = new DirectedDFS(G, match);
                pc  = new Bag <int>();
                for (int v = 0; v < G.V; v++)
                {
                    if (dfs.Marked(v))
                    {
                        pc.Add(v);
                    }
                }

                // optimization if no states reachable
                if (pc.Count == 0)
                {
                    return(false);
                }
            }

            // check for accept state
            foreach (int v in pc)
            {
                if (v == M)
                {
                    return(true);
                }
            }
            return(false);
        }
Exemplo n.º 3
0
        private DirectedDFS[] tc; // tc[v] = reachable from v

        /// <summary>Computes the transitive closure of the digraph <c>G</c>.</summary>
        /// <param name="G">the digraph</param>
        ///
        public TransitiveClosure(Digraph G)
        {
            tc = new DirectedDFS[G.V];
            for (int v = 0; v < G.V; v++)
            {
                tc[v] = new DirectedDFS(G, v);
            }
        }