def Best_First(self,source,dest,Flag,traversed,path): if self.is_valid(source): shortest_path={} #stores button number as key and its parent as value heap=min_heap() for i in range(0,self.row*self.col): if self.is_valid(i): heap.push(i,float("inf"),None) source_cost=eucledian_distance(source,dest,self.col) heap.decrease_key(source,source_cost,None) while(not heap.isempty()): node=heap.extract_min() if node.val<float("inf"): traversed.append(node.key) shortest_path[node.key]=node.parent if node.key==dest: Flag[0]=True break moves=self.movement(node.key) for i in moves: if self.is_valid(i) and heap.contains(i) : cost=eucledian_distance(i,dest,self.col) if cost<heap.get_val(i): heap.decrease_key(i,cost,node.key) if Flag[0]==True: stack=[] stack.append(dest) temp=shortest_path.get(dest) while(temp): stack.append(temp) temp=shortest_path.get(temp) while(stack): temp=stack.pop() path.append(temp)
def Dijkstras(self, source, Flag, dest, traversed, path): if self.is_valid(source): short_path = { } #stores button number as key and its parent as value heap = min_heap() for i in range(0, self.row * self.col): if self.is_valid(i): heap.push(i, float("infinity"), None) heap.decrease_key(source, 0, None) while (not heap.isempty()): node = heap.extract_min() if node.val < float("inf"): traversed.append(node.key) short_path[node.key] = node.parent moves = self.movement(node.key) for i in moves: if self.is_valid(i) and heap.contains(i): if node.val + 1 < heap.get_val(i): heap.decrease_key(i, node.val + 1, node.key) if short_path.get(dest) != None: Flag[0] = True temp = short_path.get(dest) stack = [] stack.append(dest) while (temp): stack.append(temp) temp = short_path.get(temp) while (stack): temp = stack.pop() path.append(temp)