Esempio n. 1
0
        /// <summary>
        /// Does the NFA recognize txt? 
        /// Returns true if the text is matched by the regular expression.
        /// </summary>
        /// <param name="txt">txt the text</param>
        /// <returns><tt>true</tt> if the text is matched by the regular expression, <tt>false</tt> otherwise</returns>
        public bool Recognizes(string txt)
        {
            var dfs = new DirectedDFS(_g, 0);
            var pc = new Bag<Integer>();
            for (var v = 0; v < _g.V; v++)
                if (dfs.Marked(v)) pc.Add(v);

            // Compute possible NFA states for txt[i+1]
            for (var i = 0; i < txt.Length; i++)
            {
                var match = new Bag<Integer>();
                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<Integer>();
                for (var v = 0; v < _g.V; v++)
                    if (dfs.Marked(v)) pc.Add(v);

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

            // check for accept state
            foreach (int v in pc)
                if (v == _m) return true;
            return false;
        }
        public void Run()
        {
            Console.WriteLine("Choose file:"); // Prompt
            Console.WriteLine("1 - tinyDG.txt"); // Prompt
            Console.WriteLine("or quit"); // Prompt

            var fileNumber = Console.ReadLine();
            string fileName;
            switch (fileNumber)
            {
                case "1":
                    fileName = "tinyDG.txt";
                    break;
                case "quit":
                    return;
                default:
                    return;
            }

            var @in = new In($"Files\\Graphs\\{fileName}");
            var lines = @in.ReadAllLines();

            var lineIterator = 0;
            var v = 0;
            var e = 0;
            var edges = new List<EdgeD>();
            foreach (var line in lines)
            {
                if (lineIterator == 0)
                {
                    v = Convert.ToInt32(line);
                }
                if (lineIterator == 1)
                {
                    e = Convert.ToInt32(line);
                }
                if (lineIterator > 1)
                {
                    var lineSplitted = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    var ve = Convert.ToInt32(lineSplitted[0]);
                    var we = Convert.ToInt32(lineSplitted[1]);
                    var edge = new EdgeD(ve, we);
                    edges.Add(edge);
                }

                lineIterator++;
            }

            var digraph = new Digraph(v, e, edges);
            Console.WriteLine(digraph);
            Console.WriteLine("------------------------------------------------------");

            var bag1 = new Core.Collections.Bag<Integer> { new Integer(1) };
            var bag2 = new Core.Collections.Bag<Integer> { new Integer(2) };
            var bag3 = new Core.Collections.Bag<Integer> { new Integer(1), new Integer(2), new Integer(6) };
            var listSources = new List<Core.Collections.Bag<Integer>> {bag1, bag2, bag3};
            foreach (var sources in listSources)
            {
                // multiple-source reachability
                var dfs = new DirectedDFS(digraph, sources);
                // print out vertices reachable from sources
                for (var i = 0; i < digraph.V; i++)
                {
                    if (dfs.Marked(i)) Console.Write($"{i} ");
                }
                Console.WriteLine();
                Console.WriteLine("---------------------------------------------------------");
            }

            Console.ReadLine();
        }