示例#1
0
 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]
示例#2
0
 def keys_range(self, lo, hi):
     """
     Returns all keys in the symbol table in the given range.
     :param lo: minimum endpoint
     :param hi: maximum endpoint
     :return: all keys in the symbol table between lo (inclusive) and hi (inclusive)
     :raises IllegalArgumentException: if either lo or hi is None
     """
     if lo is None:
         raise IllegalArgumentException("first argument to keys() is None")
     if hi is None:
         raise IllegalArgumentException("second argument to keys() is None")
     queue = Queue()
     self._keys(self._root, queue, lo, hi)
     return queue
示例#3
0
 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
示例#4
0
    def from_stream(stream):
        """
        Initializes an edge-weighted digraph from the specified 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 seperated 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 digraph
        :rtype: EdgeWeightedDigraph
        """
        g = EdgeWeightedDigraph(stream.readInt())
        E = stream.readInt()
        if g._E < 0:
            raise IllegalArgumentException(
                "Number of edges must be nonnegative")
        for i in range(E):
            v = stream.readInt()
            w = stream.readInt()
            g._validate_vertex(v)
            g._validate_vertex(w)
            weight = stream.readFloat()
            g.add_edge(DirectedEdge(v, w, weight))
        return g
    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)
示例#6
0
 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))
示例#7
0
 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)
示例#8
0
 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
示例#9
0
 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))
示例#10
0
 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
示例#11
0
 def increase_key(self, i, key):
     """
     Increase the key associated with index i to the specified value.
     :param i: the index of the key to increase
     :param key: increase 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 NoSuchElementException("index is not in the priority queue")
     if self.keys[i] >= key:
         raise IllegalArgumentException(
             "calling increase_key() with given argument would not strictly increase the key"
         )
     self.keys[i] = key
     self._sink(self.qp[i])
示例#12
0
 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
示例#13
0
 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
示例#14
0
 def rank(self, key):
     """
     Returns the number of keys in the symbol table strictly less than key.
     :param key: the key
     :return: the number of keys in the symbol table strictly less than key
     :rtype: int
     :raises IllegalArgumentException: if key is None
     """
     if key is None:
         raise IllegalArgumentException("argument to rank() is None")
     return self._rank(key, self._root)
示例#15
0
 def size_range(self, lo, hi):
     """
     Returns the number of keys in the symbol table in the given range.
     :param lo: minimum endpoint
     :param hi: maximum endpoint
     :return: the number of keys in the symbol table between lo
     (inclusive) and hi (inclusive)
     :rtype: int
     :raises IllegalArgumentException: if either lo or hi is None
     """
     if lo is None:
         return IllegalArgumentException("first argument to size() is None")
     if hi is None:
         return IllegalArgumentException(
             "second argument to size() is None")
     if lo > hi:
         return 0
     if self.contains(hi):
         return self.rank(hi) - self.rank(lo) + 1
     else:
         return self.rank(hi) - self.rank(lo)
示例#16
0
 def select(self, k):
     """
     Return the kth smallest key in the symbol table.
     :param k: the order statistic
     :return: the kth smallest key in the symbol table
     :raises IllegalArgumentException: unless k is between 0 and n-1
     """
     if k < 0 or k >= self.size():
         raise IllegalArgumentException(
             "argument to select() is invalid: {}".format(k))
     x = self._select(self._root, k)
     return x.key
示例#17
0
 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
示例#18
0
 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
示例#19
0
    def get(self, key):
        """
        Returns the value associated with the given key.
        :param key: the key
        :return: the value associated with the given key if the key is in the symbol table
        and None if the key is not in the symbol table
        :raises IllegalArgumentException: if key is None
        """
        if key is None:
            raise IllegalArgumentException("argument to get() is None")

        return self._get(self._root, key)
示例#20
0
 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()
示例#22
0
 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")
示例#23
0
 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])
示例#24
0
 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
示例#25
0
 def ceiling(self, key):
     """
     Returns the smallest key in the symbol table greater than or equal to key.
     :param key: the key
     :return: the smallest key in the symbol table greater than or equal to key
     :raises IllegalArgumentException: if key is None
     :raises NoSuchElementException: if there is no such key
     """
     if key is None:
         raise IllegalArgumentException("argument to ceiling is None")
     if self.is_empty():
         raise NoSuchElementException(
             "calls ceiling() with empty symbol table")
     x = self._ceiling(self._root, key)
     if x is None:
         return None
     return x.key
示例#26
0
 def delete(self, key):
     """
     Removes the specified key and its associated value from this symbol table
     (if the key is in this symbol table).
     :param key: the key
     :raises IllegalArgumentException: if key is None
     """
     if key is None:
         raise IllegalArgumentException("argument to delete() is None")
     if not self.contains(key):
         return
     if not self._is_red(self._root.left) and not self._is_red(
             self._root.right):
         self._root.color = self.RED
     self._root = self._delete(self._root, key)
     if not self.is_empty():
         self._root.color = self.BLACK
示例#27
0
 def floor(self, key):
     """
     Returns the largest key in the symbol table less than or equal to key.
     :param key: the key
     :return: the largest key in the symbol table less than er equal to key
     :raises IllegalArgumentException: if key is None
     :raises NoSuchElementException: if there is no such key
     """
     if key is None:
         raise IllegalArgumentException("argument to floor() is None")
     if self.is_empty():
         raise NoSuchElementException(
             "calls floor() with empty symbol table")
     x = self._floor(self._root, key)
     if x is None:
         return None
     return x.key
示例#28
0
    def put(self, key, val):
        """
        Inserts the specified key-value pair into the symbol table, overwriting the old
        value with the new value if the symbol table already contains the specified key.
        Deletes the specified key (and its associated value) from this symbol table
        if the specified value is None.
        :param key: the key
        :param val: the value
        :raises IllegalArgumentException: if key is None
        """
        if key is None:
            raise IllegalArgumentException("first argument to put() is None")
        if val is None:
            self.delete(key)
            return

        self._root = self._put(self._root, key, val)
        self._root.color = self.BLACK
示例#29
0
 def contains(self, key):
     if key is None:
         raise IllegalArgumentException("called contains() with a None key")
     return key in self._set
示例#30
0
 def delete(self, key):
     if key is None:
         raise IllegalArgumentException("called delete() with a None key")
     if self.contains(key):
         self._set.remove(key)