def findMovieTransRelation(self, movA, movB): # finds path between 2 movies using BFS. file = open('outputPS2.txt', 'a+') file.write("\n--------Function findMovieTransRelation --------\n") file.write("Movie A: %s\n" % movA) file.write("Movie B: %s\n" % movB) for i in self.ActMov: if i.getName() == movA: i.visited = True else: i.visited = False q = MyQueue.MyQueue() temp_path = [movA] q.enqueue(temp_path) while q.IsEmpty() == False: tmp_path = q.dequeue() last_node = tmp_path[len(tmp_path) - 1] if last_node == movB: file.write("Related: Yes, %s" % tmp_path.pop(0)) file.writelines([" > %s" % item for item in tmp_path]) file.write("\n----------------------------------------") file.close() return for link_node in self.getNeighbours(last_node): if not link_node.getVisited(): link_node.visited = True new_path = tmp_path + [link_node.getName()] q.enqueue(new_path) file.write("Related: No") file.write("\n-----------------------------------------") file.close()
def main(): maxFrames = 72 colorQueue = MyQueue() grayQueue = MyQueue() videoCapture = cv2.VideoCapture("clip.mp4") threadList = [ Thread(target=extractFrames, args=["clip.mp4", colorQueue, maxFrames]), Thread(target=convertToGray, args=[colorQueue, grayQueue]), Thread(target=displayFrames, args=[grayQueue]) ] for i in threadList: i.start() for i in threadList: i.join()
def squreAllHouse(squre): if queuesDict.has_key(squre): queue = queuesDict[squre] else: queue = MyQueue(40) for house in get_houselist(squre): print house houseObj = getHouse(house, squreDict[squre]) print houseObj.title if houseObj.url not in queue: queue.enqueue(houseObj.url) save(houseObj) if (queue.isfull()): queue.dequeue() queuesDict[squre] = queue
def BreadthFirstSearch(graph, startingNodeData): searchQueue: MyQueue = MyQueue() seen: set = set([]) searchQueue.add(startingNodeData) while searchQueue.isEmpty() == False: currentNodeData = searchQueue.remove() if currentNodeData not in seen: seen.add(currentNodeData) print(currentNodeData) for neighbor in graph[currentNodeData]: if neighbor not in seen: searchQueue.add(neighbor)
def breadth_first_search(b1, bn, U): n = U.shape[0] if b1 == bn: return True previous = [None] * n # previous = [] bfs = [] # Mark all buildings as not visited visited = [False] * n # visited = [False] * 8 # Relations to building B1 rels_b1 = U[:, b1] # Set relationship between building and itself to False rels_b1[b1] = False adjs_b1 = adjacent(rels_b1) # Create a queue for BFS q = MyQueue.MyQueue() # Mark current building as visited and enqueue it visited[b1] = True # Put first in array of adjacent in queue q.enqueue(b1) while not q.isEmpty(): # Deque a building from the top of the queue and print it b = q.dequeue() bfs.append(b) # Get all adjacent buildings of the dequeued building b rels_b = U[:, b] rels_b[b] = False adjs = adjacent(rels_b) for i in range(len(adjs)): # If found destination building, stop curr = adjs[i] # current node if visited[curr] == False: # print "previous["+str(curr)+"]="+str(b) previous[curr] = b visited[curr] = True q.enqueue(curr) path = unique(previous, bn, b1) return path, bfs
def aStar(dicCity,cityA,cityB,h): """ A* search between two city from dicCity with a definned heuristic """ frontier = MyQueue() frontier.put(0,cityA) sumWeight = {cityA: 0} steps = 0 while frontier: current = frontier.get() steps+=1 if current==cityB: return current, steps for coLeaf in current.getcoLeafs(dicCity): tempWeight = sumWeight[current] + current.getWeightOf(coLeaf) if coLeaf not in sumWeight or tempWeight < sumWeight[coLeaf]: sumWeight[coLeaf] = tempWeight coLeaf.parent = current priority = h(coLeaf, cityB) + tempWeight frontier.put(priority,coLeaf) raise Exception("no solution")
def main(): testArr = ['a', 'b', 'c', 'd'] print("NODE") package1: Node = Node(1) print(str(package1)) package2: Node = Node(2) package1.next = package2 print(str(package1)) print(repr(package2)) package2.next = Node(3) print(str(package2)) print() print("LINKED LIST") linkedList :LinkedList = LinkedList('A') linkedList.printLinkedList() linkedList.append('B') linkedList.append('C') linkedList.printLinkedList() linkedList.prepend('Alpha') linkedList.printLinkedList() linkedList.remove('B') linkedList.printLinkedList() linkedList.remove('C') linkedList.printLinkedList() linkedList.remove('Alpha') linkedList.printLinkedList() linkedList.remove('A') linkedList.printLinkedList() linkedList.remove('A') testArr = ['a', 'b', 'c', 'd'] for i in testArr: linkedList.append(i) linkedList.printLinkedList() print() print("STACK") stackOfEmails: Stack = Stack('A') #stackOfEmails.pop() #print(stackOfEmails.isEmpty()) #stackOfEmails.printStack() for i in testArr: stackOfEmails.push(i) stackOfEmails.printStack() #print(stackOfEmails.isEmpty()) while(stackOfEmails.isEmpty() == False): print("The Node with the data '{}' will be deleted".format(stackOfEmails.peek())) stackOfEmails.pop() stackOfEmails.printStack() print(stackOfEmails.isEmpty()) stackOfEmails.push('Z') stackOfEmails.printStack() print() print("QUEUE") myQueue : MyQueue = MyQueue() print(myQueue.isEmpty()) for i in testArr: myQueue.add(i) myQueue.printQueue() while(myQueue.isEmpty()==False): print("The node with the data '{}' will be removed".format(myQueue.remove())) myQueue.printQueue() print(myQueue.isEmpty()) myQueue.remove() print() print("HEAP") testArr2 = [17, 10, 20, 15, 25] myHeap : Heap = Heap() for i in testArr2: myHeap.add(i) myHeap.printHeap() myHeap.poll() myHeap.printHeap() print() print("BINARY SEARCH TREE") testArr3 = [8, 15, 5] binaryTree : BinarySearch = BinarySearch(10) #binaryTree.printInOrder() for i in testArr3: binaryTree.insert(i) binaryTree.printInOrder() print(binaryTree.find(10)) for i in testArr3: print(binaryTree.find(i)) print(binaryTree.find(100)) print() print("TEST") test1 = Test() test2 = Test() print(Test.__dict__) print(test1.__dict__) test1.classVar = 34 print(Test.__dict__) print(test1.__dict__) print(test2.__dict__) print(test2.classVar) print()
import My2_3Tree as T23 import MyAVLTree as TAVL import MyBinaryTree as BST import MyHeap as HP import MyLinkedList as LST import MyQueue as Q import MyStack as S InitDictionary = { '0': None, '1': S.MyStack(), '2': Q.MyQueue(), '3': LST.LinkList(), '4': BST.BST(), '5': TAVL.AVLTree(), '6': HP.Heap(), '7': T23.Tree23() } def InitDict(key): return InitDictionary[str(key)] class ClassObject(object): obj = None name = '' def __init__(self, key): if key == 0: print "No object created.\n"
def testQueue(): q = MyQueue(1) # Initalisierungs Checks assert q.max == 1, "Maximale Größe passt nicht" assert q.size == 0, "Aktuelle Größe passt nicht" is_empty = q.empty() assert is_empty, "Nach Initalisierung sollte die Schlange noch leer sein" is_full = q.full() assert is_full == False, "Schlange ist nach Initalisierung nicht voll" assert q.size == 0, "Größe passt nicht" succeeded = q.enqueue(1) assert succeeded, "Erfolgreich eingefügt" check(q) is_full = q.full() assert is_full, "Schlange ist voll" value = q.dequeue() assert value == 1, "Wert sollte 1 sein" check(q) is_empty = q.empty() assert q.empty(), "Schlange ist wieder leer" check(q) succeeded = q.enqueue(-42) assert succeeded, "Erfolgreich eingefügt" check(q) value = q.dequeue() assert value == -42, "Wert sollte 1 sein" check(q) value = q.dequeue() assert value == None, "Schlange war schon leer" check(q) is_empty = q.empty() assert q.empty(), "Schlange ist wieder leer" check(q) succeeded = q.enqueue(1) assert succeeded, "Erfolgreich eingefügt" succeeded = q.enqueue(2) assert succeeded == False, "Konnte Elelement nicht einfügen" check(q) value = q.dequeue() assert value == 1, "Hätte nicht überschreiben dürfen" check(q) is_empty = q.empty() assert is_empty, "Sollte wieder leer sein" succeeded = q.enqueue(2**31 - 1) assert succeeded, "Einfügen geklappt" value = q.dequeue() assert value == (2**31 - 1), "Zu große Zahl" q = MyQueue(3000) for x in range(2000): #print(x) value = x succeeded = q.enqueue(value) assert succeeded, "Einfügen muss klappen" assert not q.empty(), "Darf nicht leer sein" assert not q.full(), "Darf nicht voll sein" check(q) #print() for x in range(2000): value = q.dequeue() #print(value) iter = x assert (iter == value), "Wert passt nicht" assert not q.full(), "Darf nicht voll sein" check(q) for x in range(2000): #print(x) value = x * -1 succeeded = q.enqueue(value) assert succeeded, "Einfügen muss klappen" assert not q.empty(), "Darf nicht leer sein" assert not q.full(), "Darf nicht voll sein" check(q) #print() for x in range(2000): value = q.dequeue() #print(value) iter = x * -1 assert (iter == value), "Wert passt nicht" assert not q.full(), "Darf nicht voll sein" check(q)