public void HaNoCycleTest() { var graph = new Digraph(13, 13, new[] { "0 5", "4 3", "0 1", "9 12", "6 4", "5 4", "0 2", "11 12", "9 10", "0 6", "7 8", "9 11", "5 3" }); var directedCycle = new DirectedCycle(graph); Assert.AreEqual(false, directedCycle.HasCycle()); var cycle = directedCycle.Cycle(); }
public BreadthFirstDirectedPaths(Digraph graph, int s) { marked = new bool[graph.V()]; edgeTo = new int[graph.V()]; this.s = s; bfs(graph, s); }
public void BreadthFirstDirectedPathsTest() { var graph = new Digraph(13, 22, new[] { "4 2", "2 3", "3 2", "6 0", "0 1", "2 0", "11 12", "12 9", "9 10", "9 11", "8 9", "10 12", "11 4", "4 3", "3 5", "7 8", "8 7", "5 4", "0 5", "6 4", "6 9", "7 6" }); var path = new DirectedCycle(graph); Assert.AreEqual(true, path.HasCycle()); var cycle = path.Cycle(); }
public DepthFirstDirectedPath(Digraph g, int s) { marked = new bool[g.V()]; edgeTo = new int[g.V()]; this.s = s; dfs(g, s); }
private void dfs(Digraph g, int v) { onStack[v] = true; marked[v] = true; foreach (int w in g.Adj(v)) { if (this.HasCycle()) return; else if (!marked[w]) { edgeTo[w] = v; dfs(g, w); } else if (onStack[w]) { cycle = new Stack<int>(); for (int x = v; x != w; x = edgeTo[x]) cycle.Push(x); cycle.Push(w); cycle.Push(v); } } onStack[v] = false; }
private void dfs(Digraph graph, int v) { foreach (int w in graph.Adj(v)) { if (!marked[w]) { dfs(graph, w); } } }
public DirectedCycle(Digraph g) { onStack = new bool[g.V()]; edgeTo = new int[g.V()]; marked = new bool[g.V()]; for (int v = 0; v < g.V(); v++) { if (!marked[v]) dfs(g, v); } }
public DirectDfs(Digraph graph, IEnumerable<int> sources) { marked = new bool[graph.V()]; for (int i = 0; i < sources.Count(); i++) { if (!marked[i]) { dfs(graph, i); } } }
public Digraph Reverse() { Digraph r = new Digraph(versus); for (int v = 0; v < V(); v++) { foreach (int w in Adj(v)) { r.AddEdge(w, v); } } return r; }
private void dfs(Digraph graph, int v) { pre.Enqueue(v); marked[v] = true; foreach (int w in graph.Adj(v)) { if (!marked[w]) { edgeTo[w] = v; dfs(graph, w); } } post.Enqueue(v); reversPost.Push(v); }
private void bfs(Digraph graph, int s) { Queue<int> queue = new Queue<int>(); marked[s] = true; queue.Enqueue(s); while (queue.Any()) { int v = queue.Dequeue(); foreach (int w in graph.Adj(v)) { if (!marked[w]) { edgeTo[w] = v; marked[w] = true; queue.Enqueue(w); } } } }
public void MarkedTest() { var graph = new Digraph(13, 22, new[] { "4 2", "2 3", "3 2", "6 0", "0 1", "2 0", "11 12", "12 9", "9 10", "9 11", "8 9", "10 12", "11 4", "4 3", "3 5", "7 8", "8 7", "5 4", "0 5", "6 4", "6 9", "7 6" }); int[] sources = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 }; var reachable = new DirectDfs(graph, sources); StringBuilder builder = new StringBuilder(); for (int v = 0; v < graph.V(); v++) { if (reachable.Marked(v)) { builder.AppendLine(v + " "); } } }
public void HasPathToTest() { var graph = new Digraph(6, 8, new[] { "0 5", "2 4", "2 3", "1 2", "0 1", "3 4", "3 5", "0 2" }); var path = new DepthFirstDirectedPath(graph, 0); Assert.AreEqual(path.HasPathTo(1), true); Assert.AreEqual(path.HasPathTo(2), true); Assert.AreEqual(path.HasPathTo(3), true); Assert.AreEqual(path.HasPathTo(4), true); Assert.AreEqual(path.HasPathTo(5), true); var builder = new StringBuilder(); foreach (int i in path.PathTo(4)) { builder.AppendFormat(" -> {0}", i); } builder.AppendLine(); }
public DirectDfs(Digraph graph, int s) { marked = new bool[graph.V()]; dfs(graph, s); }