private readonly int[] _rank; // rank[v] = order where vertex v appers in order /// <summary> /// Determines whether the digraph <tt>G</tt> has a topological order and, if so, /// finds such a topological order. /// </summary> /// <param name="g">g the digraph</param> public TopologicalX(Digraph g) { // indegrees of remaining vertices var indegree = new int[g.V]; for (var v = 0; v < g.V; v++) { indegree[v] = g.Indegree(v); } // initialize _rank = new int[g.V]; _order = new Collections.Queue <Integer>(); var count = 0; // initialize queue to contain all vertices with indegree = 0 var queue = new Collections.Queue <Integer>(); for (var v = 0; v < g.V; v++) { if (indegree[v] == 0) { queue.Enqueue(v); } } for (var j = 0; !queue.IsEmpty(); j++) { int v = queue.Dequeue(); _order.Enqueue(v); _rank[v] = count++; foreach (int w in g.Adj(v)) { indegree[w]--; if (indegree[w] == 0) { queue.Enqueue(w); } } } // there is a directed cycle in subgraph of vertices with indegree >= 1. if (count != g.V) { _order = null; } //assert check(G); }
// all keys in subtrie rooted at x with given prefix private static void Collect(Node <TValue> x, StringBuilder prefix, Collections.Queue <string> queue) { if (x == null) { return; } Collect(x.Left, prefix, queue); if (x.Value != null) { queue.Enqueue(prefix.ToString() + x.Ch); } Collect(x.Mid, prefix.Append(x.Ch), queue); prefix.Remove(prefix.Length - 1, 1); Collect(x.Right, prefix, queue); }
/// <summary> /// Returns all of the keys in the set that start with <tt>prefix</tt>. /// </summary> /// <param name="prefix">prefix the prefix</param> /// <returns>all of the keys in the set that start with <tt>prefix</tt>, as an iterable</returns> public IEnumerable <string> KeysWithPrefix(string prefix) { var queue = new Collections.Queue <string>(); var x = Get(_root, prefix, 0); if (x == null) { return(queue); } if (x.Value != null) { queue.Enqueue(prefix); } Collect(x.Mid, new StringBuilder(prefix), queue); return(queue); }
private void Collect(Node <TValue> x, StringBuilder prefix, Collections.Queue <string> results) { if (x == null) { return; } if (x.Value != null) { results.Enqueue(prefix.ToString()); } for (var c = 0; c < R; c++) { var ch = (char)c; prefix.Append(ch); Collect(x.Next[c], prefix, results); prefix.Remove(prefix.Length - 1, 1); } }
/// <summary> /// Returns the Keys in the BST in level order (for debugging). /// </summary> /// <returns>the Keys in the BST in level order traversal, as an Iterable</returns> public IEnumerable <TKey> LevelOrder() { var keys = new Collections.Queue <TKey>(); var queue = new Collections.Queue <Node <TKey, TValue> >(); queue.Enqueue(_root); while (!queue.IsEmpty()) { var x = queue.Dequeue(); if (x == null) { continue; } keys.Enqueue(x.Key); queue.Enqueue(x.Left); queue.Enqueue(x.Right); } return(keys); }
/// <summary> /// /// </summary> /// <param name="x"></param> /// <param name="queue"></param> /// <param name="lo"></param> /// <param name="hi"></param> private void Keys(Node <TKey, TValue> x, Collections.Queue <TKey> queue, TKey lo, TKey hi) { if (x == null) { return; } var cmplo = lo.CompareTo(x.Key); var cmphi = hi.CompareTo(x.Key); if (cmplo < 0) { Keys(x.Left, queue, lo, hi); } if (cmplo <= 0 && cmphi >= 0) { queue.Enqueue(x.Key); } if (cmphi > 0) { Keys(x.Right, queue, lo, hi); } }
private IEnumerable <DirectedEdge> _cycle; // negative cycle (or null if no such cycle) /// <summary> /// Computes a shortest paths tree from <tt>s</tt> to every other vertex in /// the edge-weighted digraph <tt>G</tt>. /// </summary> /// <param name="g">g the acyclic digraph</param> /// <param name="s">s the source vertex</param> /// <exception cref="ArgumentException">unless 0 le; <tt>s</tt> le; <tt>V</tt> - 1</exception> public BellmanFordSP(EdgeWeightedDigraph g, int s) { _distTo = new double[g.V]; _edgeTo = new DirectedEdge[g.V]; _onQueue = new bool[g.V]; for (var v = 0; v < g.V; v++) { _distTo[v] = double.PositiveInfinity; } _distTo[s] = 0.0; // Bellman-Ford algorithm _queue = new Collections.Queue <Integer>(); _queue.Enqueue(s); _onQueue[s] = true; while (!_queue.IsEmpty() && !HasNegativeCycle()) { int v = _queue.Dequeue(); _onQueue[v] = false; Relax(g, v); } }
private void Collect(Node <TValue> x, StringBuilder prefix, string pattern, Collections.Queue <string> results) { if (x == null) { return; } var d = prefix.Length; if (d == pattern.Length && x.Value != null) { results.Enqueue(prefix.ToString()); } if (d == pattern.Length) { return; } var c = pattern[d]; if (c == '.') { for (var ch = 0; ch < R; ch++) { var chr = (char)ch; prefix.Append(chr); Collect(x.Next[ch], prefix, pattern, results); prefix.Remove(prefix.Length - 1, 1); } } else { prefix.Append(c); Collect(x.Next[c], prefix, pattern, results); prefix.Remove(prefix.Length - 1, 1); } }
/// <summary> /// run Prim's algorithm /// </summary> /// <param name="g"></param> /// <param name="s"></param> private void Prim(EdgeWeightedGraph g, int s) { Scan(g, s); while (!_pq.IsEmpty()) { // better to stop when mst has V-1 edges var e = _pq.DelMin(); // smallest edge on pq int v = e.Either(), w = e.Other(v); // two endpoints //assert marked[v] || marked[w]; if (_marked[v] && _marked[w]) { continue; // lazy, both v and w already scanned } _mst.Enqueue(e); // add e to MST _weight += e.Weight; if (!_marked[v]) { Scan(g, v); // v becomes part of tree } if (!_marked[w]) { Scan(g, w); // w becomes part of tree } } }
private readonly Collections.Stack <Integer> _cycle; // the directed cycle; null if digraph is acyclic public DirectedCycleX(Digraph g) { // indegrees of remaining vertices var indegree = new int[g.V]; for (var v = 0; v < g.V; v++) { indegree[v] = g.Indegree(v); } // initialize queue to contain all vertices with indegree = 0 var queue = new Collections.Queue <Integer>(); for (var v = 0; v < g.V; v++) { if (indegree[v] == 0) { queue.Enqueue(v); } } for (var j = 0; !queue.IsEmpty(); j++) { int v = queue.Dequeue(); foreach (int w in g.Adj(v)) { indegree[w]--; if (indegree[w] == 0) { queue.Enqueue(w); } } } // there is a directed cycle in subgraph of vertices with indegree >= 1. var edgeTo = new int[g.V]; var root = -1; // any vertex with indegree >= -1 for (var v = 0; v < g.V; v++) { if (indegree[v] == 0) { continue; } root = v; foreach (int w in g.Adj(v)) { if (indegree[w] > 0) { edgeTo[w] = v; } } } if (root != -1) { // find any vertex on cycle var visited = new bool[g.V]; while (!visited[root]) { visited[root] = true; root = edgeTo[root]; } // extract cycle _cycle = new Collections.Stack <Integer>(); var v = root; do { _cycle.Push(v); v = edgeTo[v]; } while (v != root); _cycle.Push(root); } //assert check(); }
/// <summary> /// Log a diagnostic level message. /// </summary> /// <param name="category">The category of the message.</param> /// <param name="message">The message format string.</param> /// <param name="args">Arguments for the message format string.</param> public static void Diag(int category, string message, params object[] args) { _logQueue.Enqueue(new Entry { Level = LogLevel.Diagnostic, Category = category, Message = string.Format(message, args), Except = null }); }
/// <summary> /// BFS from multiple sources /// </summary> /// <param name="g"></param> /// <param name="sources"></param> private void Bfs(Digraph g, IEnumerable<Integer> sources) { var q = new Collections.Queue<Integer>(); foreach (int s in sources) { _marked[s] = true; _distTo[s] = 0; q.Enqueue(s); } while (!q.IsEmpty()) { int v = q.Dequeue(); foreach (int w in g.Adj(v)) { if (!_marked[w]) { _edgeTo[w] = v; _distTo[w] = _distTo[v] + 1; _marked[w] = true; q.Enqueue(w); } } } }