コード例 #1
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()
コード例 #2
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 == []
コード例 #3
0
ファイル: test.py プロジェクト: joyrexus/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 == []
コード例 #4
0
class DijkstrasMazeSolver(object):
    """Gets shortest maze exit path with help of Dijkstras algorithm.
    It converts 0/1 matrix maze to edge weighted graph representation
    (due to MazeToGraphConverter) and then finds shortest path.
    Also it uses Heap data structure to quickly get minimums.
    """
    infinity = 10**10

    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()

    def shortest_exit_path(self):
        self.count_shortest_paths(self.start_v, self.exit_v)
        length = self.dist[self.exit_v]
        path = self.get_path(self.exit_v)
        return path, length

    def get_path(self, v, path=()):
        path = (v, ) + path
        if self.prev[v] and v != self.start_v:
            return self.get_path(self.prev[v], path)
        else:
            return path

    def count_shortest_paths(self, source, dest):
        self.dist[source] = 0
        self.prev[source] = None

        for v in self.vertices:
            if v != source:
                self.dist[v] = self.infinity
                self.prev[v] = None
            else:
                self.queue.insert((v, self.dist[v]))

        while self.queue:
            u = self.queue.pop_min()[0]
            if u == dest:
                break
            for v in self.adjacency_list[u]:
                edge = frozenset((u, v))
                alt = self.dist[u] + self.edge_weights[edge]
                if alt < self.dist[v]:
                    self.dist[v] = alt
                    self.prev[v] = u
                    if v not in self.queue:
                        self.queue.insert((v, alt))
コード例 #5
0
class DijkstrasMazeSolver(object):
    """Gets shortest maze exit path with help of Dijkstras algorithm.
    It converts 0/1 matrix maze to edge weighted graph representation
    (due to MazeToGraphConverter) and then finds shortest path.
    Also it uses Heap data structure to quickly get minimums.
    """

    infinity = 10 ** 10

    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()

    def shortest_exit_path(self):
        self.count_shortest_paths(self.start_v, self.exit_v)
        length = self.dist[self.exit_v]
        path = self.get_path(self.exit_v)
        return path, length

    def get_path(self, v, path=()):
        path = (v,) + path
        if self.prev[v] and v != self.start_v:
            return self.get_path(self.prev[v], path)
        else:
            return path

    def count_shortest_paths(self, source, dest):
        self.dist[source] = 0
        self.prev[source] = None

        for v in self.vertices:
            if v != source:
                self.dist[v] = self.infinity
                self.prev[v] = None
            else:
                self.queue.insert((v, self.dist[v]))

        while self.queue:
            u = self.queue.pop_min()[0]
            if u == dest:
                break
            for v in self.adjacency_list[u]:
                edge = frozenset((u, v))
                alt = self.dist[u] + self.edge_weights[edge]
                if alt < self.dist[v]:
                    self.dist[v] = alt
                    self.prev[v] = u
                    if v not in self.queue:
                        self.queue.insert((v, alt))