示例#1
0
文件: test.py 项目: waynewe/sandbox
def test_node_heaping():
    '''
    Test priority queues with nodes as elements.
    
    '''
    a = Node(label='a', msg="boom!", priority=1)
    b = Node(label='b', msg="hi", priority=2)
    c = Node(label='c', msg="ok", priority=3)
    d = Node(label='d', msg="oh", priority=4)

    q = PriorityQueue([b, c, d])
    assert q.min == b
    assert q.min.msg == 'hi'
    assert q.min.label == 'b'
    assert q == [b, c, d]

    q.insert(a)
    assert q.min == a
    assert q.min.msg is 'boom!'
    assert q.min.label == 'a'
    assert q == [a, b, d, c]

    assert q.delete('c') == c
    assert q.sort() == [a, b, d]
    assert q.min == a
    assert q.min.label == 'a'

    min = q.shift()
    assert min == a
    assert min.label == 'a'
    assert q.sort() == [b, d]
    assert q.min == b
    assert q.min.label == 'b'

    q = PriorityQueue([d, c, b, a])
    assert [a, b, c, d] == q.sort()
    assert [a, b, c, d] == [q.shift() for x in range(q.size)]
    assert q.size == 0
    assert q == []

    from itertools import permutations
    nodes = [a, b, c, d]
    for perm in permutations(nodes):
        q = PriorityQueue(perm)
        assert [a, b, c, d] == q.sort()
        assert [a, b, c, d] == [q.shift() for x in range(q.size)]
        assert q.size == 0
        assert q == []
示例#2
0
文件: test.py 项目: waynewe/sandbox
def test_sort():
    '''
    Test sorting (heap sort) of priority queues.
    
    '''
    q = PriorityQueue([5, 1, 2, 4, 6, 3])
    assert q == [1, 4, 2, 5, 6, 3]
    assert q.sort() == [1, 2, 3, 4, 5, 6]
示例#3
0
def huffman(C):
    n = len(C)
    Q = PriorityQueue(C, MinHeap)
    for i in range(n - 1):
        x = Q.extract_min()
        y = Q.extract_min()
        z = Node(x.key() + y.key())
        z.left = x
        z.right = y
        Q.insert(z)
    return Q.extract_min()
    def main(self, s):
        self.dists[s] = 0

        pq = PriorityQueue()
        for u in range(self.n):
            pq.insert(float('inf'), u)

        pq.decrease_key(s, 0)
        while pq:
            _, u = pq.extract()
            for v in self.G[u]:
                if self.relax(u, v, self.G[u][v]):
                    pq.decrease_key(v, self.dists[v])
示例#5
0
    def __init__(self, input_maze=None):
        is_text = type(input_maze) == str
        if is_text:
            converter = TextToMatrixMazeConverter(input_maze)
            converter = MazeToGraphConverter(converter.maze,
                                             converter.start_cell)
        else:
            converter = MazeToGraphConverter(input_maze)

        self.vertices = converter.graph_vertices
        self.edge_weights = converter.graph_edge_weights
        self.adjacency_list = converter.graph_adjacency_list
        self.edge_weights = converter.graph_edge_weights
        self.start_v = converter.start_cell
        self.exit_v = converter.exit_cell
        self.dist = dict()
        self.prev = dict()
        self.queue = PriorityQueue()
        self.shortest_path = self.shortest_exit_path()
示例#6
0
def prim(G):
    n = G.num_vertices()
    parents = [-1] * n

    pq = PriorityQueue()
    for u in G:
        pq.insert(float('inf'), u)

    pq.decrease_key(u, 0)
    while pq:
        _, u = pq.extract()
        for v in G[u]:
            if v in pq and G[u][v] < pq[v]:
                parents[v] = u
                pq.decrease_key(v, G[u][v])

    mst = set()
    for u, v in enumerate(parents):
        if v >= 0:
            mst.add((u, v))
    return mst
示例#7
0
        self.g = g  # list of event secuences Ids


class PriorityQueue_tuple(object):
    def __init__(self, l, c_, ci, cj):
        self.l = l
        self.c_ = c_
        self.ci = ci
        self.cj = cj


###############	containers	#####################3
cluster_list = dict()  ### Stores the clusters formed
priorityQueue_dict = dict(
)  ### Maps the priorityQueue elements to Tuple objects
q = PriorityQueue([])  ### Priority Queue
#############//////////////##################


def add_cluster(c):
    global counter_cluster_id
    cluster_list[counter_cluster_id] = c
    counter_cluster_id += 1
    return counter_cluster_id - 1


def remove_cluster(ckey):
    cluster_list.pop(ckey)  ## check


def getNextLabel():
示例#8
0
#@ <int>  cj: Id of Cluster j
class PriorityQueue_tuple(object):
    def __init__(self, l, c_, ci, cj):
        self.l = l
        self.c_ = c_
        self.ci = ci
        self.cj = cj


###############	containers	#####################
# Maps Ids to clusters
cluster_list = dict()
# Maps PriorityQueue elements to PriorityQueue_Tuple Objects
priorityQueue_dict = dict()
# Main Priority Queue for MinDL algorithm <heap>
q = PriorityQueue([])


# -- Adds a new cluster (c) to cluster_list --
#@<Cluster> c: new cluster object
#@<int>     returns: new cluster's id
def add_cluster(c):
    global counter_cluster_id
    cluster_list[counter_cluster_id] = c
    counter_cluster_id += 1
    return counter_cluster_id - 1


# -- Removes the cluster identified by Id cKey from cluster_list --
#@<int> ckey: Id or key of cluster c
def remove_cluster(ckey):
示例#9
0
文件: test.py 项目: waynewe/sandbox
from heap import Node, PriorityQueue

q = PriorityQueue([5, 4, 3])


def test_basic():
    assert q.min == 3
    assert q.size == 3
    assert q == [3, 5, 4]


def test_insert():
    '''
    Testing `insert` method, which inserts a new element into the queue
    and reorders the underlying MinHeap if necessary.

    '''
    q.insert(2)
    assert q.min == 2
    assert q.size == 4
    assert q == [2, 3, 4, 5]

    q.insert(1)
    assert q.min == 1
    assert q.size == 5
    assert q == [1, 2, 4, 5, 3]


def test_relations():
    '''
    Testing parent and child relations among elements.