def key_of(self, i): """ Returns the key associated with index i. :param i: the index of the key to return :return: the key associated with index i :raises IllegalArgumentException: unless 0 <= i < max_n :raises NoSuchElementException: if no key is associated with index i """ if i < 0 or i >= self.max_n: raise IllegalArgumentException("index is out of range") if not self.contains(i): raise IllegalArgumentException("index is not on the priority queue") return self.keys[i]
def __init__(self, G, s): """Computes a shortest-paths tree from the source vertex s to every other vertex in the edge-weighted graph G. :param G: the edge-weighted graph :param s: the source vertex :raises IllegalArgumentException: if an edge weight is negative :raises IllegalArgumentException: unless 0 <= s < V """ for e in G.edges(): if e.weight() < 0: raise IllegalArgumentException( "edge {} has negative weight".format(e)) self._dist_to = [float("inf")] * G.V() self._edge_to = [None] * G.V() self._dist_to[s] = 0.0 self._validate_vertex(s) self._pq = IndexMinPQ(G.V()) self._pq.insert(s, 0) while not self._pq.is_empty(): v = self._pq.del_min() for e in G.adj(v): self._relax(e, v)
def put(self, key, val): if key is None: raise IllegalArgumentException("called put() with None key") if val is None: self._st.pop(key, None) else: self._st[key] = val
def from_stream(stream): """Initializes an edge-weighted graph from an input stream. The format is the number of vertices V, followed by the number of edges E, followed by E pairs of vertices and edge weights, with each entry separated by whitespace. :param stream: the input stream :raises IllegalArgumentException: if the endpoints of any edge are not in prescribed range :raises IllegalArgumentException: if the number of vertices or edges is negative :return: the edge-weighted graph :rtype: EdgeWeightedGraph """ g = EdgeWeightedGraph(stream.readInt()) E = stream.readInt() if E < 0: raise IllegalArgumentException("Number of edges must be nonnegative") for _ in range(E): v = stream.readInt() w = stream.readInt() g._validate_vertex(v) g._validate_vertex(w) weight = stream.readFloat() e = Edge(v, w, weight) g.add_edge(e) return g
def insert(self, i, key): """ Associates key with index i :param i: an index :param key: the key to associate with index i :raises IllegalArgumentException: unless 0 <= i < max_n :raises IllegalArgumentException: if there already is an item associated with index i """ if i < 0 or i >= self.max_n: raise IllegalArgumentException("index is not within range") if self.contains(i): raise IllegalArgumentException("index is already in the priority queue") self.n += 1 self.qp[i] = self.n self.pq[self.n] = i self.keys[i] = key self._swim(self.n)
def decrease_key(self, i, key): """ Decrease the key associated with index i to the specified value. :param i: the index of the key to decrease :param key: decrease the key associated with index i to this key :raises IllegalArgumentException: unless 0 <= i < max_n :raises IllegalArgumentException: if key >= key_of(i) :raises NoSuchElementException: if no key is associated with index i """ if i < 0 or i >= self.max_n: raise IllegalArgumentException("index is not within range") if not self.contains(i): raise IllegalArgumentException("index is not in the priority queue") if self.keys[i] <= key: raise IllegalArgumentException("calling decrease_key() with given argument would not strictly decrease the key") self.keys[i] = key self._swim(self.qp[i])
def _validate_vertex(self, v): """ Raises an IllegalArgumentException unluess 0 <= v < V :param v: the vertex to validate """ if v < 0 or v >= self._V: raise IllegalArgumentException( "vertex {} is not between 0 and {}".format(v, self._V - 1))
def union(self, that): if that is None: raise IllegalArgumentException("called union() with a None argument") c = set() for x in self: c.add(x) for x in that: c.add(x) return c
def _validate_vertex(self, v): """ Raises an IllegalArgumentException unless 0 <= v < V :param v: the vertex to be validated """ V = len(self._dist_to) if v < 0 or v >= V: raise IllegalArgumentException( "vertex {} is not between 0 and {}".format(v, V - 1))
def __init__(self, v, w, weight): """Initializes an edge between vertices v and w of the given weight. :param v: one vertex :param w: the other vertex :param weight: the weight of this edge :raises IllegalArgumentException: if either v or w is a negative integer :raises IllegalArgumentException: if weight is NaN """ if v < 0: raise IllegalArgumentException("vertex index must be a nonnegative integer") if w < 0: raise IllegalArgumentException("vertex index must be a nonnegative integer") if math.isnan(weight): raise IllegalArgumentException("Weight is NaN") self._v = v self._w = w self._weight = weight
def floor(self, key): if key is None: raise IllegalArgumentException("called floor() with None key") floor = None for k in self: if (floor is None and k <= key) or (floor is not None and k<=key and k>floor): floor = k if floor is None: raise NoSuchElementException("all keys are greater than " + str(key)) return floor
def ceiling(self, key): if key is None: raise IllegalArgumentException("called ceiling() with None key") ceiling = None for k in self: if (ceiling is None and k >= key) or (ceiling is not None and k>=key and k<ceiling): ceiling = k if ceiling is None: raise NoSuchElementException("all keys are less than " + str(key)) return ceiling
def contains(self, i): """ Is i an index on this priority queue? :param i: an index :return: True if i is an index on this priority queue False otherwise :rtype: bool :raises IllegalArgumentException: unless 0 <= i < max_n """ if i < 0 or i >= self.max_n: raise IllegalArgumentException("index is not within range") return self.qp[i] != -1
def __init__(self, v, w, weight): """ Initializes a directed edge from vertex v to vertex w with the given weight. :param v: the tail vertex :param w: the head vertex :param weight: the weight of the directed edge :raises IllegalArgumentException: if either v or w is a negative integer :raises IllegalArgumentException: if weight is NaN """ if v < 0: raise IllegalArgumentException( "Vertex names must be nonnegative integers") if w < 0: raise IllegalArgumentException( "Vertex names must be nonnegative integers") if math.isnan(weight): raise IllegalArgumentException("Weight is NaN") self._v = v self._w = w self._weight = weight
def intersects(self, that): if that is None: raise IllegalArgumentException("called intersects() with a null argument") c = set() if self.size() < that.size(): for x in self: if that.contains(x): c.add(x) else: for x in that: if self.contains(x): c.add(x) return c
def __init__(self, V): """ Initializes an empty edge-weighted graph with V vertices and 0 edges. :param V: the number of vertices :raises IllegalArgumentException: if V < 0 """ if V < 0: raise IllegalArgumentException( "Number of vertices must be nonnegative") self._V = V self._E = 0 self._adj = [None] * V for v in range(V): self._adj[v] = Bag()
def other(self, vertex): """ Returns the endpoint of this edge that is different from the given vertex. :param vertex: one endpoint of this edge :return: the other endpoint of this edge :rtype: int :raises IllegalArgumentException: if the vertex is not one of the endpoints of this edge """ if vertex == self._v: return self._w elif vertex == self._w: return self._v else: raise IllegalArgumentException("Illegal endpoint")
def change_key(self, i, key): """ Change the key associated with index i to the specified value. :param i: the index of the key to change :param key: change the key associated with index i to this key :raises IllegalArgumentException: unless 0 <= i < max_n :raises NoSuchElementException: if no key is associated with index i """ if i < 0 or i >= self.max_n: raise IllegalArgumentException("index is not within range") if not self.contains(i): raise NoSuchElementException("index is not in the priority queue") self.keys[i] = key self._swim(self.qp[i]) self._sink(self.qp[i])
def delete(self, i): """ Remove the key associated with index i :param i: the index of the key to remove :raises IllegalArgumentException: unless 0 <= i < max_n :raises NoSuchElementException: if no key is associated with index i """ if i < 0 or i >= self.max_n: raise IllegalArgumentException("index is not in range") if not self.contains(i): raise NoSuchElementException("index is not in the priority queue") index = self.qp[i] self._exch(index, self.n) self.n -= 1 self._sink(index) self.keys[i] = None self.qp[i] = -1
def _validate_vertex(self, v): V = len(self._marked) if v < 0 or v >= V: raise IllegalArgumentException( "vertex {} is not between 0 and {}".format(v, V - 1))
def get(self, key): if key is None: raise IllegalArgumentException("called get() with None key") return self._st.get(key)
def delete(self, key): if key is None: raise IllegalArgumentException("called delete() with None key") self._st.pop(key, None)
def add(self, key): if key is None: raise IllegalArgumentException("called add() with a None key") self._set.add(key)
def _validate_column_index(self, col): if col < 0 or col >= self.width(): raise IllegalArgumentException()
def _validate_row_index(self, row): if row < 0 or row >= self.height(): raise IllegalArgumentException()
def contains(self, key): if key is None: raise IllegalArgumentException("called contains() with a None key") return key in self._set
def delete(self, key): if key is None: raise IllegalArgumentException("called delete() with a None key") if self.contains(key): self._set.remove(key)