Пример #1
0
 def test_for_add_and_get_size(self):
     p = PriorityQueue()
     p.add(999)
     p.add(222)
     p.add(33)
     p.add(33)
     self.assertTrue(p.size(), 5)
Пример #2
0
def prim(G,start):
    pq = PriorityQueue()
    inf = float("infinity")
    for i in G.get_vertices():
        v = G.get_vertex(i)
        v.set_distance(inf)
        v.set_previous(None)

    s = G.get_vertex(start)
    s.set_distance(0)
    for v in G:
        pq.add(v.get_distance(), v.get_vertex_ID())
        
    MST = []
    
    while not pq.empty():
        t = pq.extract_min()
        currentVert = G.get_vertex(t[1])
        MST.append((currentVert.get_previous(), currentVert.get_vertex_ID()))
        for nextVert in currentVert.get_connections():
          newCost = currentVert.get_weight(nextVert) + currentVert.get_distance()
          if nextVert in pq and newCost<nextVert.get_distance():
              nextVert.set_previous(currentVert)
              nextVert.set_distance(newCost)
              pq.replace_key(nextVert,newCost)

    print MST
Пример #3
0
def dijkstra(G, s):

    #create a priority queue
    pq = PriorityQueue()
    
    #set distance for all other vertices to "infinity"
    inf = float("infinity")
    for i in G.get_vertices():
        v = G.get_vertex(i)
        v.set_distance(inf)
    
    #set distance of source to zero
    source = G.get_vertex(s)
    source.set_distance(0)

    #insert all vertices into the priority queue (distance is priority)
    for v in G:
        pq.add(v.get_distance(), v.id)

    #loop while priority queue is not empty
    while not(pq.empty()):
        #remove vertex with smallest distance from priority queue
        t = pq.extract_min()
        v = G.get_vertex(t[1])

        #for each vertex w adjacent to v
        for w in v.get_connections():
            if w.get_distance() > (v.get_distance() + v.get_weight(w)):
                w.set_previous(v)
                w.set_distance(v.get_distance() + v.get_weight(w))

        #loop over all nodes and print their distances
    for v in G:
        print "Node", v.get_vertex_ID(), "with distance", v.get_distance()
Пример #4
0
def queue_dict(dict):
    term_queue = PriorityQueue()
    for t in dict:
        this_q_item = Q_item(t, dict[t])
        term_queue.add(this_q_item)

    return term_queue
Пример #5
0
def queue_dict(dict):
    term_queue = PriorityQueue()
    for t in dict:
        this_q_item = Q_item(t, dict[t])
        term_queue.add(this_q_item)

    return term_queue
def sf_compression(file_name):

    # open file to be read
    file = open(file_name, "r")
    text = file.read()
    file.close()

    #check for empty text file
    if text == "":
        print("Error: Empty Text")
    else:
        letter_dict = {text[0]: 1}

    # place each letter into dictionary and count each letter
    for x in range(1, len(text)):
        if text[x] in letter_dict:
            letter_dict[text[x]] = letter_dict[text[x]] + 1

        else:
            letter_dict[text[x]] = 1

    priorityq = PriorityQueue()

    # insert everything into priority queue
    for x in letter_dict:
        priorityq.add([x, letter_dict[x], ""])

    # generate shannon-fano code
    split_equal_list(priorityq, letter_dict)

    print(letter_dict)

    # create compressed version in text format
    file = open(file_name[0:-4] + "_SFcompressed.txt", "w")

    result = ""

    for x in text:
        result = result + letter_dict[x]
        file.write(letter_dict[x])

    file.close()

    # create compressed version in bin format
    file = open(file_name[0:-4] + "_SFcompressed.bin", "wb")
    file.write(_to_Bytes(result))
    file.close()

    # create compression data(bitcount dictionary) in text format
    file = open(file_name[0:-4] + "_SFcode.txt", "w")
    file.write(str(len(result)))
    file.write(str(letter_dict))
    file.close()

    print(result)
Пример #7
0
 def sift_down_test(self):
     p = PriorityQueue()
     p.add(999)
     p.add(222)
     p.add(33)
     p.add(33)
     self.assertTrue(p.sift_down, 3)
     p.add(45)
     self.assertTrue(p.sift_down, 2)
     p.add(22)
Пример #8
0
 def test_for_priority_removal(self):
     p = PriorityQueue()
     p.add(10)
     p.add(20)
     p.add(1)
     p.add(30)
     p.add(5)
     self.assertTrue(p.peek(), 1)
     p.remove()
     self.assertTrue(p.peek(), 5)
     p.remove()
     self.assertTrue(p.peek(), 10)
Пример #9
0
def priority_queue_test():
    priority_queue = PriorityQueue()

    priority_queue.add(1)
    priority_queue.add(8)
    priority_queue.add(5)
    priority_queue.add(3)
    priority_queue.add(4)
    priority_queue.add(2)
    priority_queue.add(6)
    priority_queue.add(7)
    priority_queue.add(0)

    i = 0

    # this effectively tests heap sort as well...
    while not priority_queue.is_empty():
        number_polled = priority_queue.poll()
        assert (
            number_polled == i
        ), "Expected %s got %s from priority queue" % (i, number_polled)
        i = i + 1
Пример #10
0
 def test_we_can_add_five_elements(self):
     p = PriorityQueue()
     p.add(10)
     p.add(20)
     p.add(30)
     p.add(40)
     p.add(50)
     self.assertTrue(p.size(), 5)
     self.assertTrue(p.peek(), 10)
def split_equal_list(priorityq, letter_dict):

    # check if more than 1 node in priority queue
    if len(priorityq.return_all()) > 1:

        priorityq1 = PriorityQueue()
        priorityq2 = PriorityQueue()

        # add code '0' for priority queue 1
        temp1 = priorityq.pop()
        temp1[2] += "0"
        priorityq1.add(temp1)
        count0 = temp1[1]

        # add code '1' for priority queue 2
        temp1 = priorityq.pop()
        temp1[2] += "1"
        priorityq2.add(temp1)
        count1 = temp1[1]

        # place all nodes from priorityq into correct place
        while not priorityq.empty():
            temp1 = priorityq.pop()

            # compare where to place next node
            if count0 + temp1[1] - count1 < count1 + temp1[1] - count0:
                temp1[2] += "0"
                priorityq1.add(temp1)
                count0 += temp1[1]

            else:
                temp1[2] += "1"
                priorityq2.add(temp1)
                count1 += temp1[1]

        # call itself until base case is reached
        return [
            split_equal_list(priorityq1, letter_dict),
            split_equal_list(priorityq2, letter_dict)
        ]

    # base case
    else:
        # save code into dictionary
        letter_dict[priorityq.return_all()[0]
                    [0]] = priorityq.return_all()[0][2]
        return priorityq.return_all()
Пример #12
0
def q_test():
    alan = Q_item('alan', 3)
    brad = Q_item('brad', 2)
    carl = Q_item('carl', 1)
    alanbrad = alan + brad

    tesco = PriorityQueue()
    tesco.add(brad)
    tesco.add(alan)
    tesco.add(carl)
    tesco.add(alanbrad)
    print tesco

    return 'q_test done'
Пример #13
0
def q_test():
    alan = Q_item('alan', 3)
    brad = Q_item('brad', 2)
    carl = Q_item('carl', 1)
    alanbrad = alan + brad

    tesco = PriorityQueue()
    tesco.add(brad)
    tesco.add(alan)
    tesco.add(carl)
    tesco.add(alanbrad)
    print tesco

    return 'q_test done'