/// <summary> /// Returns all Keys in the symbol table in the given range, /// as an <tt>Iterable</tt>. /// </summary> /// <param name="lo"></param> /// <param name="hi"></param> /// <returns>all Keys in the sybol table between <tt>lo</tt> (inclusive) and <tt>hi</tt> (exclusive) as an <tt>Iterable</tt></returns> /// <exception cref="NullReferenceException">if either <tt>lo</tt> or <tt>hi</tt> is <tt>null</tt></exception> public IEnumerable <TKey> Keys(TKey lo, TKey hi) { var queue = new Collections.Queue <TKey>(); Keys(_root, queue, lo, hi); return(queue); }
/// <summary> /// Returns all keys in the symbol table as an <tt>Iterable</tt>. /// To iterate over all of the keys in the symbol table named <tt>st</tt>, /// use the foreach notation: <tt>for (Key key : st.keys())</tt>. /// </summary> /// <returns>all keys in the sybol table as an <tt>Iterable</tt></returns> public IEnumerable <string> Keys() { var queue = new Collections.Queue <string>(); Collect(_root, new StringBuilder(), queue); return(queue); }
/// <summary> /// breadth-first search from a single source /// </summary> /// <param name="g"></param> /// <param name="s"></param> private void Bfs(Graph g, int s) { var q = new Collections.Queue <Integer>(); for (var v = 0; v < g.V; v++) { _distTo[v] = INFINITY; } _distTo[s] = 0; _marked[s] = true; q.Enqueue(s); while (!q.IsEmpty()) { int v = q.Dequeue(); foreach (int w in g.Adj(v)) { if (_marked[w]) { continue; } _edgeTo[w] = v; _distTo[w] = _distTo[v] + 1; _marked[w] = true; q.Enqueue(w); } } }
/// <summary> /// breadth-first search from multiple sources /// </summary> /// <param name="g"></param> /// <param name="sources"></param> private void Bfs(Graph 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]) { continue; } _edgeTo[w] = v; _distTo[w] = _distTo[v] + 1; _marked[w] = true; q.Enqueue(w); } } }
/// <summary> /// Returns all of the keys in the symbol table that match <tt>pattern</tt>, /// where . symbol is treated as a wildcard character. /// </summary> /// <param name="pattern">pattern the pattern</param> /// <returns>all of the keys in the symbol table that match <tt>pattern</tt>, as an iterable, where . is treated as a wildcard character.</returns> public IEnumerable <string> KeysThatMatch(string pattern) { var queue = new Collections.Queue <string>(); Collect(_root, new StringBuilder(), 0, pattern, queue); return(queue); }
/// <summary> /// Returns all keys in the symbol table in the given range, /// as an <tt>Iterable</tt>. /// </summary> /// <param name="lo"></param> /// <param name="hi"></param> /// <returns>all keys in the sybol table between <tt>lo</tt> (inclusive) and <tt>hi</tt> (exclusive)</returns> /// <exception cref="NullReferenceException">if either <tt>lo</tt> or <tt>hi</tt> is <tt>null</tt></exception> public IEnumerable <TKey> Keys(TKey lo, TKey hi) { var queue = new Collections.Queue <TKey>(); // if (lo == null && hi == null) return queue; if (lo == null) { throw new NullReferenceException("lo is null in keys()"); } if (hi == null) { throw new NullReferenceException("hi is null in keys()"); } if (lo.CompareTo(hi) > 0) { return(queue); } for (var i = Rank(lo); i < Rank(hi); i++) { queue.Enqueue(_keys[i]); } if (Contains(hi)) { queue.Enqueue(_keys[Rank(hi)]); } return(queue); }
/// <summary> /// Returns all keys in the symbol table in the given range, /// as an <tt>Iterable</tt>. /// </summary> /// <param name="lo"></param> /// <param name="hi"></param> /// <returns>all keys in the sybol table between <tt>lo</tt> (inclusive) and <tt>hi</tt> (exclusive) as an <tt>Iterable</tt></returns> /// <exception cref="NullReferenceException">if either <tt>lo</tt> or <tt>hi</tt> is <tt>null</tt></exception> public IEnumerable <TKey> Keys(TKey lo, TKey hi) { var queue = new Collections.Queue <TKey>(); // if (isEmpty() || lo.compareTo(hi) > 0) return queue; Keys(_root, queue, lo, hi); return(queue); }
/// <summary> /// Returns all of the keys in the set that match <tt>pattern</tt>, /// where . symbol is treated as a wildcard character. /// </summary> /// <param name="pattern">pattern the pattern</param> /// <returns>all of the keys in the set that match <tt>pattern</tt>, as an iterable, where . is treated as a wildcard character.</returns> public IEnumerable <string> KeysThatMatch(string pattern) { var results = new Collections.Queue <string>(); var prefix = new StringBuilder(); Collect(_root, prefix, pattern, results); return(results); }
/// <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 results = new Collections.Queue <string>(); var x = Get(_root, prefix, 0); Collect(x, new StringBuilder(prefix), results); return(results); }
/// <summary> /// Returns all keys in the symbol table as an <tt>Iterable</tt>. /// To iterate over all of the keys in the symbol table named <tt>st</tt>, /// use the foreach notation /// </summary> /// <returns>all keys in the sybol table as an <tt>IEnumerable</tt></returns> public IEnumerable <TKey> Keys() { var queue = new Collections.Queue <TKey>(); for (var x = _first; x != null; x = x.Next) { queue.Enqueue(x.Key); } return(queue); }
/// <summary> /// Returns all keys in the symbol table as an <tt>Iterable</tt>. /// To iterate over all of the keys in the symbol table named <tt>st</tt>, /// use the foreach notation /// </summary> /// <returns>all keys in the sybol table as an <tt>IEnumerable</tt></returns> public IEnumerable <TKey> Keys() { var queue = new Collections.Queue <TKey>(); for (var i = 0; i < _m; i++) { if (_keys[i] != null) { queue.Enqueue(_keys[i]); } } return(queue); }
/// <summary> /// return keys in symbol table as an IEnumerable /// </summary> /// <returns></returns> public IEnumerable <TKey> Keys() { var queue = new Collections.Queue <TKey>(); for (var i = 0; i < _m; i++) { foreach (var key in _st[i].Keys()) { queue.Enqueue(key); } } return(queue); }
/// <summary> /// Returns the edges in a minimum spanning tree (or forest). /// </summary> /// <returns>the edges in a minimum spanning tree (or forest) as an iterable of edges</returns> public IEnumerable <EdgeW> Edges() { var mst = new Collections.Queue <EdgeW>(); for (var v = 0; v < _edgeTo.Length; v++) { var e = _edgeTo[v]; if (e != null) { mst.Enqueue(e); } } return(mst); }
private void Bfs(Graph g, int s) { var q = new Collections.Queue <Integer>(); _color[s] = WHITE; _marked[s] = true; q.Enqueue(s); while (!q.IsEmpty()) { int v = q.Dequeue(); foreach (int w in g.Adj(v)) { if (!_marked[w]) { _marked[w] = true; _edgeTo[w] = v; _color[w] = !_color[v]; q.Enqueue(w); } else if (_color[w] == _color[v]) { _isBipartite = false; // to form odd cycle, consider s-v path and s-w path // and let x be closest node to v and w common to two paths // then (w-x path) + (x-v path) + (edge v-w) is an odd-length cycle // Note: distTo[v] == distTo[w]; _cycle = new Collections.Queue <Integer>(); var stack = new Collections.Stack <Integer>(); int x = v, y = w; while (x != y) { stack.Push(x); _cycle.Enqueue(y); x = _edgeTo[x]; y = _edgeTo[y]; } stack.Push(x); while (!stack.IsEmpty()) { _cycle.Enqueue(stack.Pop()); } _cycle.Enqueue(w); return; } } } }
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); }
private readonly MinPQ <EdgeW> _pq; // edges with one endpoint in tree /// <summary> /// Compute a minimum spanning tree (or forest) of an edge-weighted graph. /// </summary> /// <param name="g">g the edge-weighted graph</param> public LazyPrimMST(EdgeWeightedGraph g) { _mst = new Collections.Queue <EdgeW>(); _pq = new MinPQ <EdgeW>(); _marked = new bool[g.V]; for (var v = 0; v < g.V; v++) // run Prim from all vertices to { if (!_marked[v]) { Prim(g, v); // get a minimum spanning forest } } // check optimality conditions //assert check(G); }
/// <summary> /// Determines a depth-first order for the edge-weighted digraph <tt>G</tt>. /// </summary> /// <param name="g">g the edge-weighted digraph</param> public DepthFirstOrder(EdgeWeightedDigraph g) { _pre = new int[g.V]; _post = new int[g.V]; _postorder = new Collections.Queue <Integer>(); _preorder = new Collections.Queue <Integer>(); _marked = new bool[g.V]; for (var v = 0; v < g.V; v++) { if (!_marked[v]) { Dfs(g, v); } } }
// 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); }
/// <summary> /// Computes the maximum flow between <see cref="MaximumFlowAlgorithm{TVertex,TEdge}.Source"/> /// and <see cref="MaximumFlowAlgorithm{TVertex,TEdge}.Sink"/>. /// </summary> protected override void InternalCompute() { if (Services.CancelManager.IsCancelling) { return; } var graph = VisitedGraph; foreach (TVertex vertex in graph.Vertices) { foreach (TEdge edge in graph.OutEdges(vertex)) { double capacity = Capacities(edge); if (capacity < 0) { throw new NegativeCapacityException(); } ResidualCapacities[edge] = capacity; } } VerticesColors[Sink] = GraphColor.Gray; while (VerticesColors[Sink] != GraphColor.White) { var verticesPredecessors = new VertexPredecessorRecorderObserver <TVertex, TEdge>(Predecessors); var queue = new Collections.Queue <TVertex>(); var bfs = new BreadthFirstSearchAlgorithm <TVertex, TEdge>( ResidualGraph, queue, VerticesColors); using (verticesPredecessors.Attach(bfs)) bfs.Compute(Source); if (VerticesColors[Sink] != GraphColor.White) { Augment(Source, Sink); } } MaxFlow = 0; foreach (TEdge edge in graph.OutEdges(Source)) { MaxFlow += (Capacities(edge) - ResidualCapacities[edge]); } }
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); } }
/// <summary> /// BFS from single source /// </summary> /// <param name="g"></param> /// <param name="s"></param> private void Bfs(Digraph g, int s) { var q = new Collections.Queue <Integer>(); _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); } } } }
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); } }
private void SetCookie(string val) { // Console.WriteLine("in set cookie 1 - got value : " + val); string[] parts = null; Collections.Queue options = null; Cookie cookie = null; options = new Collections.Queue(val.Split(';')); parts = SplitValue((string)options.Dequeue()); // NAME=VALUE must be first cookie = new Cookie(parts[0], parts[1]); while (options.Count > 0) { parts = SplitValue((string)options.Dequeue()); switch (parts [0]) { case "COMMENT": if (cookie.Comment == null) { cookie.Comment = parts[1]; } break; case "COMMENTURL": if (cookie.CommentUri == null) { cookie.CommentUri = new Uri(parts[1]); } break; case "DISCARD": cookie.Discard = true; break; case "DOMAIN": if (cookie.Domain == "") { cookie.Domain = parts[1]; } break; case "MAX-AGE": // RFC Style Set-Cookie2 if (cookie.Expires == DateTime.MinValue) { cookie.Expires = cookie.TimeStamp.AddSeconds(Int32.Parse(parts[1])); } break; case "EXPIRES": // Netscape Style Set-Cookie if (cookie.Expires == DateTime.MinValue) { //FIXME: Does DateTime parse something like: "Sun, 17-Jan-2038 19:14:07 GMT"? //cookie.Expires = DateTime.ParseExact (parts[1]); cookie.Expires = DateTime.Now.AddDays(1); } break; case "PATH": cookie.Path = parts[1]; break; case "PORT": if (cookie.Port == null) { cookie.Port = parts[1]; } break; case "SECURE": cookie.Secure = true; break; case "VERSION": cookie.Version = Int32.Parse(parts[1]); break; } // switch } // while if (_cookies == null) { _cookies = new CookieCollection(); } if (cookie.Domain == "") { cookie.Domain = _uri.Host; } // Console.WriteLine("adding cookie " + cookie + " to collection"); _cookies.Add(cookie); // Console.WriteLine("exit from method..."); }
private readonly Collections.Stack <Integer> _cycle = new Collections.Stack <Integer>(); // Eulerian cycle; null if no such cycle /// <summary> /// Computes an Eulerian cycle in the specified graph, if one exists. /// </summary> /// <param name="g">g the graph</param> public EulerianCycle(Graph g) { // must have at least one EdgeW if (g.E == 0) { return; } // necessary condition: all vertices have even degree // (this test is needed or it might find an Eulerian path instead of cycle) for (var v = 0; v < g.V; v++) { if (g.Degree(v) % 2 != 0) { return; } } // create local view of adjacency lists, to iterate one vertex at a time // the helper EdgeW data type is used to avoid exploring both copies of an EdgeW v-w var adj = new Collections.Queue <EdgeW> [g.V]; for (var v = 0; v < g.V; v++) { adj[v] = new Collections.Queue <EdgeW>(); } for (var v = 0; v < g.V; v++) { var selfLoops = 0; foreach (int w in g.Adj(v)) { // careful with self loops if (v == w) { if (selfLoops % 2 == 0) { var e = new EdgeW(v, w, 0); adj[v].Enqueue(e); adj[w].Enqueue(e); } selfLoops++; } else if (v < w) { var e = new EdgeW(v, w, 0); adj[v].Enqueue(e); adj[w].Enqueue(e); } } } // initialize Collections.Stack with any non-isolated vertex var s = NonIsolatedVertex(g); var stack = new Collections.Stack <Integer>(); stack.Push(s); // greedily search through EdgeWs in iterative DFS style _cycle = new Collections.Stack <Integer>(); while (!stack.IsEmpty()) { int v = stack.Pop(); while (!adj[v].IsEmpty()) { var edgeW = adj[v].Dequeue(); if (edgeW.IsUsed) { continue; } edgeW.IsUsed = true; stack.Push(v); v = edgeW.Other(v); } // push vertex with no more leaving EdgeWs to cycle _cycle.Push(v); } // check if all EdgeWs are used if (_cycle.Size() != g.E + 1) { _cycle = null; } //assert certifySolution(G); }
/// <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); } } } }
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(); }
private static void Collect(Node <TValue> x, StringBuilder prefix, int i, string pattern, Collections.Queue <string> queue) { if (x == null) { return; } var c = pattern[i]; if (c == '.' || c < x.Ch) { Collect(x.Left, prefix, i, pattern, queue); } if (c == '.' || c == x.Ch) { if (i == pattern.Length - 1 && x.Value != null) { queue.Enqueue(prefix.ToString() + x.Ch); } if (i < pattern.Length - 1) { Collect(x.Mid, prefix.Append(x.Ch), i + 1, pattern, queue); prefix.Remove(prefix.Length - 1, 1); } } if (c == '.' || c > x.Ch) { Collect(x.Right, prefix, i, pattern, queue); } }