def prims(g, node): """ ------------------------------------------------------- an exmplar of prims algorithum ------------------------------------------------------- Preconditions: g - a graph to be navigated node - a node to start on, in the graph Postconditions: prints: final - a list of how to navigate the graph at the lowest distance ------------------------------------------------------- """ final = [] activated =[] pq = PriorityQueue() done = False first_node = g.get_edges(node) for i in range(len(first_node)): pq.insert(first_node[i]) while not done: lowest = pq.remove() if lowest.end not in activated or lowest.start not in activated: activated.append(lowest.start) activated.append(lowest.end) final.append(lowest) node = lowest.end next_node = g.get_edges(node) for i in range(len(next_node)): pq.insert(next_node[i]) if pq.is_empty() == True: done = True for i in range(len(final)): print(final[i])
def priority_queue_test(a): """ ------------------------------------------------------- Tests priority queue implementation. Use: pq_test(a) ------------------------------------------------------- Preconditions: a - list of data (list of ?) Postconditions: the methods of PriorityQueue are tested for both empty and non-empty priority queues using the data in a: empty, insert, remove, peek ------------------------------------------------------- """ pq = PriorityQueue() # tests for the priority queue methods go here # print the results of the method calls and verify by hand print("Empty pqueue: {}".format(pq.is_empty())) for i in a: pq.insert(i) print("Items inserted into pqueue:") for j in pq: print("{}".format(j)) print() print("Remove: {}".format(pq.remove())) print() if not pq.is_empty(): print("Peek: {}".format(pq.peek())) else: print("Pqueue is empty.") return
def pq_combine(pq1, pq2): """ ------------------------------------------------------- Combines contents of two priority queues into a new priority queue. Alternate the values from pq1 and pq2. Use: q3 = pq_combine(pq1, pq2) ------------------------------------------------------- Preconditions: pq1 - a priority queue (PriorityQueue) pq2 - a priority queue (PriorityQueue) Postconditions: returns pq3 - Contents of pq1 and pq2 are moved into pq3 (PriorityQueue) ------------------------------------------------------- """ pq3 = PriorityQueue() while len(pq1) > 0 or len(pq2) > 0: if len(pq1) > 0: pq3.insert(pq1.remove()) if len(pq2) > 0: pq3.insert(pq2.remove()) return pq3
def priority_queue_test(a): """ ------------------------------------------------------- Tests priority queue implementation. Use: pq_test(a) ------------------------------------------------------- Preconditions: a - list of data (list of ?) Postconditions: the methods of PriorityQueue are tested for both empty and non-empty priority queues using the data in a: empty, insert, remove, peek ------------------------------------------------------- """ pq = PriorityQueue() for i in a: pq.insert(i) print("Is the queue empty: {}".format(pq.is_empty())) print("Removed: {}".format(pq.remove())) print("Peek {}".format(pq.peek())) return
""" ---------------------------------------------------- q4.py pq_combine ---------------------------------------------------- Author: Qadeer Assan ID: 160257370 Email: [email protected] _updated_="2018-02-01" ---------------------------------------------------- """ from asgn04 import pq_combine from priority_queue_array import PriorityQueue pq1 = PriorityQueue() pq2 = PriorityQueue() pq1.insert(0) pq1.insert(2) pq2.insert(1) pq2.insert(3) pq3 = pq_combine(pq1, pq2) for i in pq3: print(i)
""" ---------------------------------------------------- q3.py ---------------------------------------------------- Author: Qadeer Assan ID: 160257370 Email: [email protected] _updated_="2018-02-01" ---------------------------------------------------- """ from priority_queue_array import PriorityQueue pq1 = PriorityQueue() key = 3 pq1.insert(1) pq1.insert(2) pq1.insert(3) pq1.insert(4) pq2, pq3 = pq1.split(key) for i in pq2: print(i, 'pq2') for i in pq3: print(i, 'pq3')
""" ---------------------------------------------------- q2.py Tests combine() in priority queue. ---------------------------------------------------- Author: Qadeer Assan ID: 160257370 Email: [email protected] _updated_="2018-01-31" ---------------------------------------------------- """ from priority_queue_array import PriorityQueue pq1 = PriorityQueue() pq2 = PriorityQueue() pq1.insert(0) pq2.insert(1) pq1.insert(2) pq2.insert(3) for i in pq1: print(i, "pq1") for i in pq2: print(i, "pq2") pq3 = pq1.combine(pq2) for i in pq3: print(i, "pq3")