From c0f5a8f7f7257a36d7d6e9886753cdfedcd7d7fb Mon Sep 17 00:00:00 2001 From: = <=> Date: Mon, 27 Apr 2020 10:49:30 +0200 Subject: [PATCH] added BFS --- kelner/src/algorithms/BFS/BFS.py | 49 ++++++++++++++++++++++++++++ kelner/src/managers/WaiterManager.py | 3 ++ 2 files changed, 52 insertions(+) create mode 100644 kelner/src/algorithms/BFS/BFS.py diff --git a/kelner/src/algorithms/BFS/BFS.py b/kelner/src/algorithms/BFS/BFS.py new file mode 100644 index 0000000..ddc1d38 --- /dev/null +++ b/kelner/src/algorithms/BFS/BFS.py @@ -0,0 +1,49 @@ +import queue + + +class BFS: + def __init__(self, grid): + self.grid = grid + self.visited = [] + + def get_neighbors(self, node, grid): + dr = [-1, +1, 0, 0] + dc = [0, 0, +1, -1] + x = node[0] + y = node[1] + node = (x, y) + neighbors = [] + + for i in range(4): + new_col = x + dc[i] + new_row = y + dr[i] + node = (new_col, new_row) + + if 0 <= new_row < len(grid) and 0 <= new_col < len(grid[0]): + if (new_col, new_row) not in self.visited: + if grid[new_row][new_col] != 1: + neighbors.append((new_col, new_row)) + + return neighbors + + def find_path(self, start, end): + grid = self.grid + queue = [(start, [])] + + while len(queue) > 0: + node, path = queue.pop(0) + + path.append(node) + self.visited.append(node) + + if node == end: + if len(path) > 1: + path.pop(0) + return path + + adj_nodes = self.get_neighbors(node, grid) + for item in adj_nodes: + if item not in self.visited: + queue.append((item, path[:])) + + return None diff --git a/kelner/src/managers/WaiterManager.py b/kelner/src/managers/WaiterManager.py index 2240637..e75a9d6 100644 --- a/kelner/src/managers/WaiterManager.py +++ b/kelner/src/managers/WaiterManager.py @@ -3,6 +3,7 @@ import time import sys from kelner.src.components.Table import Status from kelner.src.algorithms.AStar.Finder import Finder +from kelner.src.algorithms.BFS.BFS import BFS # creates new thread @@ -20,11 +21,13 @@ class WaiterManager (threading.Thread): if tables: reservedPlaces = self.__drawableManager.getReservedPlaces(waiter) finder = Finder(reservedPlaces) + # bfs = BFS(reservedPlaces) origin = (waiter.getX(), waiter.getY()) for table in tables: targets = finder.getNeighbours((table.getX(), table.getY()), False) for target in targets: if target is not None: + # path = bfs.find_path(origin, target) path = finder.getPath(origin, target, True) if path: result = len(path)