Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions algorithms/bfs/maze_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
initial_x, initial_y = -1, -1 #todo revert back to 0,0 to resolve error
initial_x, initial_y = 0, 0


# 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)]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
directions = [(0, 0), (0, 0), (0, 0), (0, 0)] #todo revert back to [(0, -1), (0, 1), (-1, 0), (1, 0)]
directions = [(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)])
Expand All @@ -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

Expand All @@ -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
Expand Down
20 changes: 12 additions & 8 deletions algorithms/search/binary_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if val != query: # todo make this == to resolve error
if val == query:

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
Loading