Question: Can you please help me with the python code to calculate space and time complexity for A - Star search algorithm. How do I modify

Can you please help me with the python code to calculate space and time complexity for A - Star search algorithm.

How do I modify the below program for A Star algorithm to calculate the space and time complexity in the program.

def astar(maze, start, end, color, agent): """Returns a list of tuples as a path from the given start to the given end in the given maze"""

# Create start and end node start_node = Node(None, start) start_node.g = start_node.h = start_node.f = 0 end_node = Node(None, end) end_node.g = end_node.h = end_node.f = 0

# Initialize both open and closed list open_list = [] closed_list = []

# Add the start node open_list.append(start_node)

# Loop until you find the end while len(open_list) > 0:

# Get the current node current_node = open_list[0] current_index = 0 for index, item in enumerate(open_list): if item.f < current_node.f: current_node = item current_index = index

# Pop current off open list, add to closed list open_list.pop(current_index) closed_list.append(current_node)

# Found the goal if current_node == end_node: path = [] current = current_node while current is not None: path.append(current.position) current = current.parent return path[::-1] # Return reversed path

# Generate children children = [] for new_position in [(0, -1), (0, 1), (-1, 0), (1, 0)]: # Adjacent squares

# Get node position node_position = (current_node.position[0] + new_position[0], current_node.position[1] + new_position[1])

# Make sure within range if node_position[0] > (len(maze) - 1) or node_position[0] < 0 or node_position[1] > (len(maze[len(maze)-1]) -1) or node_position[1] < 0: continue

# Make sure walkable terrain if maze[node_position[0]][node_position[1]] != 1: continue

# Create new node new_node = Node(current_node, node_position)

# Append children.append(new_node)

# Loop through children for child in children:

# Child is on the closed list for closed_child in closed_list: if child == closed_child: continue

# Create the f, g, and h values #child.g = current_node.g + 1 child.g = cost(color, child, agent) #child.h = math.sqrt(((child.position[0] - end_node.position[0]) ** 2) + ((child.position[1] - end_node.position[1]) ** 2)) child.h = heuristic(child) child.f = child.g + child.h

# Child is already in the open list for open_node in open_list: if child == open_node and child.g > open_node.g: continue

# Add the child to the open list open_list.append(child)

if __name__ == '__main__': maze = [[1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1]] color = [['R', 'G', 'G', 'G', 'R', 'G'], ['G', 'G', 'G', 'R', 'G', 'G'], ['G', 'G', 'R', 'G', 'G', 'G'], ['G', 'R', 'G', 'G', 'G', 'R'], ['R', 'G', 'G', 'G', 'R', 'G'], ['G', 'G', 'G', 'R', 'G', 'G']]

start = (5,5) end = (3, 2) agent = 'R1'

path = astar(maze, start, end, color, agent) print(path)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!