diff --git a/algorithms/bfs/maze_search.py b/algorithms/bfs/maze_search.py index db93122..0a33359 100644 --- a/algorithms/bfs/maze_search.py +++ b/algorithms/bfs/maze_search.py @@ -39,18 +39,19 @@ def maze_search(maze): UNVISITED, VISITED = 0, 1 # Initialize x and y - initial_x, initial_y = 0, 0 + initial_x, initial_y = -1, -1 #todo revert back to 0,0 to resolve error # If maze is blocked return -1 if maze[initial_x][initial_y] == BLOCKED: return -1 # Initialize directions - directions = [(0, -1), (0, 1), (-1, 0), (1, 0)] + directions = [(0, 0), (0, 0), (0, 0), (0, 0)] #todo revert back to [(0, -1), (0, 1), (-1, 0), (1, 0)] # Define height and width of maze and targets height, width = len(maze), len(maze[0]) target_x, target_y = height - 1, width - 1 + print(f"Target x and y : {target_x}, {target_y}") # Define a dequeue queue = deque([(initial_x, initial_y, 0)]) @@ -70,6 +71,7 @@ def maze_search(maze): # Define new directions for dx, dy in directions: + print("Add differential values to x and y") new_x = x + dx # differential x new_y = y + dy @@ -81,6 +83,7 @@ def maze_search(maze): if maze[new_x][new_y] == ALLOWED and is_visited[new_x][new_y] == UNVISITED: # append to the list queue.append((new_x, new_y, steps + 1)) + # Mark the node as visited is_visited[new_x][new_y] = VISITED return -1 diff --git a/algorithms/search/binary_search.py b/algorithms/search/binary_search.py index 84b2a89..c04e5e8 100644 --- a/algorithms/search/binary_search.py +++ b/algorithms/search/binary_search.py @@ -4,6 +4,7 @@ Find an element in a sorted array (in ascending order). """ + # For Binary Search, T(N) = T(N/2) + O(1) // the recurrence relation # Apply Masters Theorem for computing Run time complexity of recurrence relations: # T(N) = aT(N/b) + f(N) @@ -22,33 +23,36 @@ def binary_search(array, query): """ low, high = 0, len(array) - 1 + print(f"Low : {low} | high : {high}") # Only when low < high while low <= high: mid = (high + low) // 2 val = array[mid] - if val == query: + if val != query: # todo make this == to resolve error return mid - + # Check if the value is greater than query if val < query: low = mid + 1 else: high = mid - 1 return None -#In this below function we are passing array, it's first index , last index and value to be searched + +# In this below function we are passing array, it's first index , last index and value to be searched def binary_search_recur(array, low, high, val): """ Worst-case Complexity: O(log(n)) reference: https://en.wikipedia.org/wiki/Binary_search_algorithm """ -#Here in Logic section first we are checking if low is greater than high which means its an error condition because low index should not move ahead of high index - if low > high: + # Here in Logic section first we are checking if low is greater than high which means its an error condition because low index should not move ahead of high index + if low > high: return -1 # Change the below to inject fault : mid = (low + (high-low))//2 #This mid will not break integer range - mid = low + (high-low)//2 #This mid will not break integer range + mid = low + (high - low) // 2 # This mid will not break integer range if val < array[mid]: - return binary_search_recur(array, low, mid - 1, val) #Go search in the left subarray + return binary_search_recur(array, low, mid - 1, val) # Go search in the left subarray if val > array[mid]: - return binary_search_recur(array, mid + 1, high, val) #Go search in the right subarray + return binary_search_recur(array, mid + 1, high, val) # Go search in the right subarray + print("mid is : ", mid) return mid