public void DirectedDFS_Test() { DirectedDFS DFS = new DirectedDFS(baseicDigraph, 0); Assert.IsTrue(DFS.HasPathTo(5)); Assert.IsFalse(DFS.HasPathTo(6)); }
private static void DiGraph() { IDiGraph G = new DiGraph.DiGraph(new Scanner(new StreamReader(File.OpenRead("tinyDG.txt")))); Bag <int> sources = new Bag <int>(); StdOut.Println("搜索几个结点:"); int i = StdIn.ReadInt(); while (i-- > 0) { sources.Add(StdIn.ReadInt()); } DirectedDFS reachable = new DirectedDFS(G, sources); for (int v = 0; v < G.V; v++) { if (reachable.Marked(v)) { StdOut.Print(v + " "); } } //string filename = "jobs.txt"; //文件有问题 //string seperator = "/"; //SymbolDiGraph sg = new SymbolDiGraph(filename, seperator); //Topological topological = new Topological(sg.G); //foreach (int v in topological.Order) // StdOut.Println(sg.Name(v)); StdOut.Println(); StdOut.Println("强连通分量的数量"); ISCC scc = new KosarajuSCC(G); StdOut.Println(scc.Count); }
void DigraphTest() { Digraph r = new Digraph(7); r.AddEdge(0, 1); r.AddEdge(1, 2); r.AddEdge(3, 1); r.AddEdge(1, 4); r.AddEdge(4, 5); r.AddEdge(4, 6); r.AddEdge(6, 0); //r.Display(); Digraph reverse = r.Reverse(); //reverse.Display(); DirectedDFS d = new DirectedDFS(r, 1); //d.Display(); DirectedCycle c = new DirectedCycle(r); //Debug.Log(c.HasCycle()); //c.Cycle(); //DepthFirstOrder o = new DepthFirstOrder(r); //o.Pre(); KosarajuSCC scc = new KosarajuSCC(r); Debug.Log(scc.Connected(0, 4)); }
private readonly DirectedDFS[] _tc; // tc[v] = reachable from v /// <summary> /// Computes the transitive closure of the digraph <tt>G</tt>. /// </summary> /// <param name="g">g the digraph</param> public TransitiveClosure(Digraph g) { _tc = new DirectedDFS[g.V]; for (var v = 0; v < g.V; v++) { _tc[v] = new DirectedDFS(g, v); } }
public TransitiveClosure(Digraph g) { _all = new DirectedDFS[g.V()]; for (int s = 0; s < g.V(); s++) { _all[s] = new DirectedDFS(g, s); } }
public TransitiveClosure(DiGraph G) { all = new DirectedDFS[G.V]; for (int v = 0; v < G.V; v++) { all[v] = new DirectedDFS(G, v); } }
public TransitiveClosure(Digraph G) { _dfs = new DirectedDFS[G.V()]; for (int i = 0; i < G.V(); i++) { _dfs[i] = new DirectedDFS(G, i); } }
private DirectedDFS[] tc; // tc[v] = reachable from v public TransitiveClosure(Digraph G) { tc = new DirectedDFS[G.V()]; for (int v = 0; v < G.V(); v++) { tc[v] = new DirectedDFS(G, v); } }
public TransitiveClosure(DirectedGraph g) { m_all = new DirectedDFS[g.V()]; for (int v = 0; v < g.V(); ++v) { m_all[v] = new DirectedDFS(g, v); } }
/// <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 virtual bool recognizes(string str) { DirectedDFS directedDFS = new DirectedDFS(this.G, 0); Bag bag = new Bag(); for (int i = 0; i < this.G.V(); i++) { if (directedDFS.marked(i)) { bag.add(Integer.valueOf(i)); } } for (int i = 0; i < java.lang.String.instancehelper_length(str); i++) { Bag bag2 = new Bag(); Iterator iterator = bag.iterator(); while (iterator.hasNext()) { int num = ((Integer)iterator.next()).intValue(); if (num != this.M) { if (java.lang.String.instancehelper_charAt(this.regexp, num) == java.lang.String.instancehelper_charAt(str, i) || java.lang.String.instancehelper_charAt(this.regexp, num) == '.') { bag2.add(Integer.valueOf(num + 1)); } } } directedDFS = new DirectedDFS(this.G, bag2); bag = new Bag(); for (int j = 0; j < this.G.V(); j++) { if (directedDFS.marked(j)) { bag.add(Integer.valueOf(j)); } } if (bag.Size == 0) { return(false); } } Iterator iterator2 = bag.iterator(); while (iterator2.hasNext()) { int num2 = ((Integer)iterator2.next()).intValue(); if (num2 == this.M) { return(true); } } return(false); }
// checks if the given text matches the regexp // if it does return true (its possible to reach state M in the NFA) // Logic: // 1) create a list of states that are reachable from 0 : ps // 2) create a list of states that are reachable after processing text.CharAt(i) : matches // 3) update ps with list of states that are reachable from matches // 4) if ever PS has a state that is the last state, then the given text matches the regexp public bool Matches(string text) { List <int> ps = new List <int>(); DirectedDFS dfs = new DirectedDFS(G, 0); // states reachable from start by e-transitions for (int i = 0; i < G.Vertices(); i++) { if (dfs.marked(i)) { ps.Add(i); } } for (int i = 0; i < text.Length; i++) { List <int> match = new List <int>(); // states reachable after scanning past txt.charAt(i) foreach (int v in ps) { if (v == M) { return(true); } if (re[v] == text[i] || re[v] == '.') // since '.' indicates any character, it is considered a match { match.Add(v + 1); } } dfs = new DirectedDFS(G, match); ps = new List <int>(); for (int v = 0; v < G.Vertices(); v++) { if (dfs.marked(v)) { ps.Add(v); } } } foreach (int v in ps) { if (v == M) { return(true); } } return(false); }
public bool Recognizes(string txt) { List <int> pc = new List <int>(); DirectedDFS dfs = new DirectedDFS(m_g, 0); for (int v = 0; v < m_g.V(); v++) { if (dfs.Marked(v)) { pc.Add(v); } } for (int i = 0; i < txt.Length; ++i) { List <int> match = new List <int>(); foreach (var v in pc) { if (v < m_m) { if (m_re[v] == txt[i] || m_re[v] == '.') { match.Add(v + 1); } } } pc = new List <int>(); dfs = new DirectedDFS(m_g, match); for (int v = 0; v < m_g.V(); ++v) { if (dfs.Marked(v)) { pc.Add(v); } } } foreach (var v in pc) { if (v == m_m) { return(true); } } return(false); }
public bool Recognizes(string txt) { Bag <int> pc = new Bag <int>(); DirectedDFS dfs = new DirectedDFS(G, 0); for (int v = 0; v < G.V; v++) { if (dfs.Marked(v)) { pc.Add(v); } } for (int i = 0; i < txt.Length; i++) { //计算txt[i+1]可能到达的所有MFA状态 var match = new Bag <int>(); foreach (int v in pc) { if (v < M) { if (Re[v] == txt[i] || Re[v] == '.') { match.Add(v + 1); } } } pc = new Bag <int>(); dfs = new DirectedDFS(G, match); for (int v = 0; v < G.V; v++) { if (dfs.Marked(v)) { pc.Add(v); } } } foreach (int v in pc) { if (v == M) { return(true); } } return(false); }
/**/ public static void main(string[] strarr) { In i = new In(strarr[0]); Digraph digraph = new Digraph(i); Bag bag = new Bag(); for (int j = 1; j < strarr.Length; j++) { int k = Integer.parseInt(strarr[j]); bag.add(Integer.valueOf(k)); } DirectedDFS directedDFS = new DirectedDFS(digraph, bag); for (int k = 0; k < digraph.V(); k++) { if (directedDFS.marked(k)) { StdOut.print(new StringBuilder().append(k).append(" ").toString()); } } StdOut.println(); }
void Start() { Digraph G = new Digraph(txt); Bag <int> sources = new Bag <int>(); sources.Add(1); sources.Add(2); sources.Add(6); DirectedDFS dfs = new DirectedDFS(G, sources); string str = null; for (int v = 0; v < G.V(); v++) { if (dfs.Marked(v)) { str += (v + " --- "); } } print(str); }
private readonly DirectedDFS[] _tc; // tc[v] = reachable from v #endregion Fields #region Constructors /// <summary> /// Computes the transitive closure of the digraph <tt>G</tt>. /// </summary> /// <param name="g">g the digraph</param> public TransitiveClosure(Digraph g) { _tc = new DirectedDFS[g.V]; for (var v = 0; v < g.V; v++) _tc[v] = new DirectedDFS(g, v); }
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(); }