def priority_queue_test(a): """ ------------------------------------------------------- Tests priority queue implementation. Test the methods of Priority_Queue are tested for both empty and non-empty priority queues using the data in a: is_empty, insert, remove, peek Use: pq_test(a) ------------------------------------------------------- Parameters: a - list of data (list of ?) Returns: None ------------------------------------------------------- """ pq = Priority_Queue() array_to_pq(pq, a) print(pq.is_empty()) print(pq.peek()) print(pq.remove()) #print(pq.insert(1)) # print the results of the method calls and verify by hand return
def prims(graph, start_node): """ ------------------------------------------------------- Applies Prim's Algorithm to a graph. Use: edges, total = prims(graph, node) ------------------------------------------------------- Parameters: graph - graph to evaluate (Graph) start_node - name of node to start evaluation from (str) Returns: edges - the list of the edges traversed (list of Edge) total - total distance of all edges traversed (int) ------------------------------------------------------- """ pq = Priority_Queue() node_names = graph.node_names() distance = 0 visited_nodes = [] edges = [] for node_name in node_names: node_edges = graph.edges_by_node(node_name) for edge in node_edges: pq.insert(edge) return pq
def main(): pq = Priority_Queue() pq.insert(99) print(pq._values) target1, target2 = pq.split_key(100) print(target1._values) print(target2._values)
def pq_split_alt(source): """ ------------------------------------------------------- Splits a priority queue into two with values going to alternating priority queues. The source priority queue is empty when the method ends. The order of the values in source is preserved. Use: target1, target2 = pq_split_alt(source) ------------------------------------------------------- Parameters: source - a priority queue (Priority_Queue) Returns: target1 - a priority queue that contains alternating values from source (Priority_Queue) target2 - priority queue that contains alternating values from source (Priority_Queue) ------------------------------------------------------- """ target1 = Priority_Queue() target2 = Priority_Queue() while source.is_empty() != True: temp = source.remove() target1.insert(temp) if source.is_empty(): temp = source.remove() target2.insert(temp) return target1, target2
def main(): pq = Priority_Queue() pq.insert(10) pq.insert(1) pq.insert(4) pq.insert(7) pq.insert(11) target1, target2 = pq_split_key(pq, 6) for t in target1: print(t) print() for v in target2: print(v)
def pq_split_key(source, key): """ ------------------------------------------------------- Splits a priority queue into two depending on an external priority key. The source priority queue is empty when the method ends. Use: target1, target2 = pq_split_key(source, key) ------------------------------------------------------- Parameters: source - a priority queue (Priority_Queue) key - a data object (?) Returns: target1 - a priority queue that contains all values with priority higher than key (Priority_Queue) target2 - priority queue that contains all values with priority lower than or equal to key (Priority_Queue) ------------------------------------------------------- """ target1 = Priority_Queue() target2 = Priority_Queue() while len(source) != 0: y = source.remove() if y > key: target1.insert(y) else: target2.insert(y) return target1, target2
def prims(graph, start_node): """ ------------------------------------------------------- Applies Prim's Algorithm to a graph. Use: edges, total = prims(graph, node) ------------------------------------------------------- Parameters: graph - graph to evaluate (Graph) start_node - name of node to start evaluation from (str) Returns: edges - the list of the edges traversed (list of Edge) total - total distance of all edges traversed (int) ------------------------------------------------------- """ pq = Priority_Queue() total = 0 edges = [] nodes = [] nodes.append(start_node) for edge in graph.edges_by_node(start_node): pq.insert(edge) while len(nodes) < len(graph): edge = pq.remove() node_name = edge.end if node_name not in nodes: nodes.append(node_name) edges.append(edge) total += edge.distance for edge in graph.edges_by_node(node_name): if edge.end not in nodes: pq.insert(edge) return edges, total
def main(): pq = Priority_Queue() pq.insert(2) pq.insert(5) pq.insert(26) pq.insert(22) pq.insert(3) print(pq._values) target1, target2 = pq_split_alt(pq) print(target1._values) print(target2._values)
def prims(graph, start_node): """ ------------------------------------------------------- Applies Prim's Algorithm to a graph. Use: edges, total = prims(graph, node) ------------------------------------------------------- Parameters: graph - graph to evaluate (Graph) start_node - name of node to start evaluation from (str) Returns: edges - the list of the edges traversed (list of Edge) total - total distance of all edges traversed (int) ------------------------------------------------------- """ pq = Priority_Queue() edges = [] total = 0 removed = [start_node] names = graph.node_names() current_edges = [] while len(removed) != len(names): current_edges = graph.edges_by_node(start_node) for edge in current_edges: if edge.end() not in removed: pq.insert(edge) edge = pq.remove() if edge.end() not in removed: edges.append(edge) total += edge.distance removed.append(edge.end()) start_node = edge.end() return edges, total
def priority_queue_test(a): """ ------------------------------------------------------- Tests priority queue implementation. Test the methods of Priority_Queue are tested for both empty and non-empty priority queues using the data in a: is_empty, insert, remove, peek Use: pq_test(a) ------------------------------------------------------- Parameters: a - list of data (list of ?) Returns: None ------------------------------------------------------- """ pq = Priority_Queue() print(pq.is_empty()) print("inserting 5:", pq.insert(5)) print("removing", pq.remove()) print("Peek:", pq.peek()) # tests for the priority queue methods go here # print the results of the method calls and verify by hand return None
def priority_queue_test(a): """ ------------------------------------------------------- Tests priority queue implementation. Use: pq_test(a) ------------------------------------------------------- Parameters: a - list of data (list of ?) Returns: the methods of Priority_Queue are tested for both empty and non-empty priority queues using the data in a: is_empty, insert, remove, peek ------------------------------------------------------- """ pq = Priority_Queue() dummy = [] if pq.is_empty() == True: print('pq is empty.') array_to_pq(pq, a) print('Converting a into a pq...') if pq.is_empty() == False: print('a has been transferred into pq!') print('\nRemoving pq...') while pq.is_empty() == False: temp = pq.remove() print(temp) dummy.append(temp) print('\pq is empty. Inserting values back into queue...') while dummy != []: temp = dummy.pop() print(temp) pq.insert(temp) print('\nPushing complete! Peeking...') print(pq.peek()) print('\npq is {} objects long!'.format(len(pq))) return
def prims(graph, start_node): """ ------------------------------------------------------- Applies Prim's Algorithm to a graph. Use: edges, total = prims(graph, node) ------------------------------------------------------- Parameters: graph - graph to evaluate (Graph) start_node - name of node to start evaluation from (str) Returns: edges - the list of the edges traversed (list of Edge) total - total distance of all edges traversed (int) ------------------------------------------------------- """ edges = [] total = 0 nodes_visited = [] pq = Priority_Queue() current_node = start_node while len(nodes_visited) != len(graph): if current_node not in nodes_visited: nodes_visited.append(current_node) node_edges = graph.edges_by_node(current_node) for e in node_edges: if not (e.start() in nodes_visited and e.end() in nodes_visited): pq.insert(e) edge_to_add = pq.remove() already_in = edge_to_add.start() in nodes_visited and edge_to_add.end( ) in nodes_visited if not already_in: edges.append(edge_to_add) total += edge_to_add.distance current_node = edge_to_add.end() return edges, total
def prims(graph, start_node): """ ------------------------------------------------------- Applies Prim's Algorithm to a graph. Use: edges, total = prims(graph, node) ------------------------------------------------------- Parameters: graph - graph to evaluate (Graph) start_node - name of node to start evaluation from (str) Returns: edges - the list of the edges traversed (list of Edge) total - total distance of all edges traversed (int) ------------------------------------------------------- """ total = 0 edges = [] all_edges = Priority_Queue() done_nodes = [start_node] node_names = graph.node_names() while len(done_nodes) != len(node_names): done_nodes.append(start_node) node_edges = graph.edges_by_node(start_node) for edge in node_edges: if edge.end() not in done_nodes: all_edges.insert(edge) ed = all_edges.remove() while ed.end() in done_nodes and all_edges.is_empty() == False: ed = all_edges.remove() if all_edges.is_empty() == False: edges.append(ed) start_node = ed.end() for i in edges: total += i.distance return edges, total
def main(): pq = Priority_Queue() pq.insert(3) pq.insert(2) r = pq.remove() print(r)
def priority_queue_test(a): """ ------------------------------------------------------- Tests priority queue implementation. Use: Priority_Queue_test(a) ------------------------------------------------------- Parameters: a - list of data (list of ?) Returns: the methods of Priority_Queue are tested for both is_empty and non-is_empty priority queues using the data in a: is_empty, push, pop, peek ------------------------------------------------------- """ pq = Priority_Queue() print("Priority Queue Initialised") print() print(SEP) print("Priority Queue is_empty (expect True): {}".format(pq.is_empty())) print("Priority Queue size (expect 0): {}".format(len(pq))) print(SEP) print("Add one item to Priority Queue") pq.insert(a[0]) print("Front of Priority Queue (peek):") print(pq.peek()) print(SEP) print("Priority Queue is_empty (expect False): {}".format(pq.is_empty())) print("Priority Queue size (expect 1): {}".format(len(pq))) print(SEP) print("Priority Queue remove") v = pq.remove() print(v) print(SEP) print("Priority Queue is_empty (expect True): {}".format(pq.is_empty())) print("Priority Queue size (expect 0): {}".format(len(pq))) print(SEP) print("Copy all data to Priority Queue") array_to_pq(pq, a) print("Priority Queue is_empty (expect False): {}".format(pq.is_empty())) print("Priority Queue size (expect > 0): {}".format(len(pq))) print(SEP) print("Front of Priority Queue (peek):") print(pq.peek()) print(SEP) print("Remove all elements from Priority Queue") while not pq.is_empty(): v = pq.remove() print(v) print() print(SEP) print("Priority Queue is_empty (expect True): {}".format(pq.is_empty())) print("Priority Queue size (expect 0): {}".format(len(pq))) print() return
def levelorder(self): """ ------------------------------------------------------- Copies the contents of the tree in levelorder order to a list. Use: values = bst.levelorder() ------------------------------------------------------- Returns: values - a list containing the values of bst in levelorder. (list of ?) ------------------------------------------------------- """ node = self._root if not node: return queue = Priority_Queue() queue.insert(node) traversal = [] while not queue.is_empty(): traversal.append(queue.peek()) node = queue.remove() if node._left: queue.insert(node._left) if node._right: queue.insert(node._right) return traversal
''' ................................................... CP164 - Data Structures Author: Laith Adi ID: 170265190 Email: [email protected] Updated: 2019-02-10 ................................................... ''' from functions import pq_split_key from Priority_Queue_array import Priority_Queue source = Priority_Queue() source.insert(1) source.insert(2) source.insert(3) source.insert(4) print(source._values) key = 2 print(key) target1, target2 = pq_split_key(source, key) print(target1._values, target2._values)
''' ................................................... CP164 - Data Structures Author: Laith Adi ID: 170265190 Email: [email protected] Updated: 2019-02-10 ................................................... ''' from Priority_Queue_array import Priority_Queue pq = Priority_Queue() pq.insert(1) pq.insert(2) pq.insert(3) pq.insert(4) print(pq._values) key = 2 print(key) target1, target2 = pq.split_key(key) print(target1._values, target2._values)
""" ------------------------------------------------------------------------ [program description] ------------------------------------------------------------------------ Author: Nicolas Mills ID: 180856100 Email: [email protected] __updated__ = 2019-01-28 ------------------------------------------------------------------------ """ from Priority_Queue_array import Priority_Queue pq = Priority_Queue() values = [66, 55, 44, 33, 22, 11] for i in values: pq.insert(i) removed = pq.remove() print("removed: {}\tpeeked: {}".format(removed, pq.peek())) assert pq.peek() == values[-2]
""" ------------------------------------------------------- [program 4] ------------------------------------------------------- Author: Anshul Khatri ID: 193313680 Email: [email protected] Section: CP164 Winter 2020 __updated__ = "2020-02-06" ------------------------------------------------------- """ from Priority_Queue_array import Priority_Queue from functions import pq_split_alt source = Priority_Queue() source.insert(15) source.insert(85) source.insert(35) source.insert(25) source.insert(45) source.insert(65) print(source._values) t_1, t_2 = pq_split_alt(source) print(t_1._values) print(t_2._values)
def priority_queue_test(a): """ ------------------------------------------------------- Tests priority queue implementation. Use: pq_test(a) ------------------------------------------------------- Parameters: a - list of data (list of ?) Returns: the methods of Priority_Queue are tested for both empty and non-empty priority queues using the data in a: is_empty, insert, remove, peek ------------------------------------------------------- """ pq = Priority_Queue() print(pq.is_empty()) print(array_to_pq(pq, a)) print(pq.is_empty()) print(pq.peek()) a = pq.remove() print(pq.peek()) pq.insert(a) print(pq.peek()) print(pq.is_empty()) return
""" ------------------------------------------------------- [program 3] ------------------------------------------------------- Author: Anshul Khatri ID: 193313680 Email: [email protected] Section: CP164 Winter 2020 __updated__ = "2020-02-06" ------------------------------------------------------- """ from Priority_Queue_array import Priority_Queue source = Priority_Queue() source.insert(11) source.insert(22) source.insert(44) source.insert(33) source.insert(66) source.insert(5) print(source._values) target1, target2 = source.split_key(33) for k in target1: print(k) print() for y in target2: print(y)
""" ------------------------------------------------------- [program 5] ------------------------------------------------------- Author: Anshul Khatri ID: 193313680 Email: [email protected] Section: CP164 Winter 2020 __updated__ = "2020-02-06" ------------------------------------------------------- """ from Priority_Queue_array import Priority_Queue source = Priority_Queue() source.insert(15) source.insert(85) source.insert(35) source.insert(25) source.insert(45) source.insert(65) source.insert(55) print(source._values) t_1, t_2 = source.split_alt() print(t_1._values) print(t_2._values)
def priority_queue_test(a): """ ------------------------------------------------------- Tests priority queue implementation. Use: pq_test(a) ------------------------------------------------------- Parameters: a - list of data (list of ?) Returns: the methods of Priority_Queue are tested for both empty and non-empty priority queues using the data in a: is_empty, insert, remove, peek ------------------------------------------------------- """ pq = Priority_Queue() # tests for the priority priority queue methods go here # print the results of the method calls and verify by hand # Test if priority queue is empty (Expected: TRUE) print("priority queue contents: ") for i in pq: print(i, end=' ') print("\tEmpty? {}".format(pq.is_empty())) # Load elements from a into priority queue print(">> Insert elements into a onto priority queue") print(">> Num elements to insert: {}".format(len(a))) for elem in a: pq.insert(elem) # Check if the priority queue is empty again (T/F depending on a contents) print("priority queue contents: ") for i in pq: print(i, end=' ') print("\tEmpty? {}".format(pq.is_empty())) # Peek the top of the priority queue top = None try: top = pq.peek() except: print(">>! Peeked at an empty priority queue, assertion caught, continuing...") # Check if top of priority queue is the end element of a print("Top of priority queue should be same as beginning of list") highest_priority = 0 if pq.is_empty(): print("List empty so no check") else: for i in range(len(a)): if a[i] < a[highest_priority]: highest_priority = i priority_same = (top == a[highest_priority]) print("Highest priority same as smallest in list?") print("Top: \n{}\tFront of list: \n{}".format(top, a[highest_priority])) print("Same? {}".format(priority_same)) # Testing pop #try: print(">> Remove top of priority queue") popped = pq.remove() print("Removed value from pq: \n{}\tLowest value of list: \n{}".format(popped, a[highest_priority])) print("Removed value should be same as lowest value of list.\tSame? {}".format(popped == a[highest_priority])) #except: #print(">>! Removed from an empty priority queue, exception caught, continuing...") return
""" ------------------------------------------------------------------------ [program description] ------------------------------------------------------------------------ Author: Nicolas Mills ID: 180856100 Email: [email protected] __updated__ = 2019-02-05 ------------------------------------------------------------------------ """ from functions import pq_split_key from Priority_Queue_array import Priority_Queue pq = Priority_Queue() l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] for i in l: pq.insert(i) list1, list2 = pq_split_key(pq, 5) list3 = [] print("List 1: ") for i in list1: print(i) list3.append(i) print("\nList 2: ") for i in list2: print(i) list3.append(i)