0% found this document useful (0 votes)
2 views7 pages

Assignment 5

The document discusses three common algorithms: A* search algorithm for pathfinding, QuickSort for sorting data, and Decision Tree for classification and regression. It provides detailed explanations of each algorithm's processes, real-world applications, strengths, and weaknesses. Additionally, it includes a simple implementation of the A* algorithm in Python, along with reflections on the challenges faced and the understanding gained about AI and algorithms.

Uploaded by

shwetatmscbse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

Assignment 5

The document discusses three common algorithms: A* search algorithm for pathfinding, QuickSort for sorting data, and Decision Tree for classification and regression. It provides detailed explanations of each algorithm's processes, real-world applications, strengths, and weaknesses. Additionally, it includes a simple implementation of the A* algorithm in Python, along with reflections on the challenges faced and the understanding gained about AI and algorithms.

Uploaded by

shwetatmscbse
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Identify Common Algorithms:

 Search Algorithms

Example: A Search Algorithm* – Used in pathfinding and graph traversal, it finds the
shortest path from a start node to a goal node using heuristics.

 Sorting Algorithms

Example: QuickSort – A fast sorting algorithm that uses the divide-and-conquer


approach to sort elements efficiently.

 Machine Learning Algorithms

Example: Decision Tree – A supervised learning algorithm used for classification and
regression, where data is split based on feature values.

Research each algorithm:

1. A Search Algorithm*

1. What is the algorithm? And what problem does it solve?

The A (A-star) search algorithm* is an informed search algorithm used for finding the shortest
path in a weighted graph or grid. It is widely used in pathfinding, navigation, and artificial
intelligence for games.

2. Step-by-step process:

1. Initialize: Start with the initial node and add it to an open list (a list of nodes to be
evaluated).
2. Calculate cost: Each node has a cost function: f(n)=g(n)+h(n)f(n) = g(n) + h(n)f(n)=g(n)
+h(n)
o g(n)g(n)g(n) = cost from the start node to the current node.
o h(n)h(n)h(n) = heuristic estimate from the current node to the goal (e.g., straight-
line distance).
3. Expand the best node: Select the node with the lowest f(n)f(n)f(n) and expand its
neighbors.
4. Update costs: If a new path to a neighbor is shorter, update its cost and add it to the open
list.
5. Repeat until goal is reached: Continue expanding nodes until the goal node is found.

3. Real-world applications:

 GPS navigation systems (Google Maps, Waze)


 Robotics (autonomous robots navigating obstacles)
 Video games (NPC pathfinding in games like Pac-Man, chess AI)

4. Strengths and Weaknesses:


✅ Strengths:

 Guarantees the shortest path (if the heuristic is admissible).


 Efficient for large search spaces.

❌ Weaknesses:

 Can be slow for very large graphs.


 Requires a good heuristic function for optimal performance.

2. QuickSort Algorithm

1. What is the algorithm? And what problem does it solve?

QuickSort is a sorting algorithm that efficiently organizes elements in ascending or descending


order. It is widely used due to its speed and efficiency in handling large datasets.

2. Step-by-step process:

1. Choose a pivot: Select an element (often the first, last, or middle) as the pivot.
2. Partition the list: Move all elements smaller than the pivot to its left and all larger
elements to its right.
3. Recursively apply QuickSort: Sort the left and right sublists using the same process.
4. Combine results: Once all elements are sorted, merge them.

3. Real-world applications:

 Databases (indexing and sorting records)


 E-commerce platforms (sorting products by price, rating)
 Search engines (organizing ranked search results)

4. Strengths and Weaknesses:

✅ Strengths:

 Faster than Bubble Sort and Insertion Sort.


 Performs well on large datasets.

❌ Weaknesses:

 Worst-case time complexity is O(n²) (when the pivot is poorly chosen).


 Not stable (relative order of equal elements may change).

3. Decision Tree Algorithm

1. What is the algorithm? And what problem does it solve?


A Decision Tree is a supervised learning algorithm used for classification and regression
tasks. It works like a flowchart, where each internal node represents a decision based on an
attribute, and each leaf node represents a final outcome.

2. Step-by-step process:

1. Start with the dataset: Identify the features (input variables) and the target (output).
2. Choose the best feature: Use criteria like Gini Impurity or Information Gain to find
the best feature to split the data.
3. Split the dataset: Divide the dataset into subsets based on the chosen feature.
4. Repeat recursively: Apply the same logic to each subset until all are classified.
5. Stop when conditions are met: If further splitting doesn’t improve accuracy, stop
growing the tree.

3. Real-world applications:

 Medical diagnosis (predicting diseases based on symptoms)


 Fraud detection (identifying suspicious transactions in banking)
 Customer segmentation (classifying customers based on buying behavior)

4. Strengths and Weaknesses:

✅ Strengths:

 Easy to understand and interpret.


 Works well with categorical and numerical data.

❌ Weaknesses:

 Overfitting (complex trees may memorize training data instead of generalizing).


 Sensitive to noise (small changes in data can drastically change the structure).

Implement a simple version:


import heapq

class Node:
def __init__(self, position, parent=None, g=0, h=0):
self.position = position # (x, y) coordinates
self.parent = parent # Previous node
self.g = g # Cost from start to current node
self.h = h # Heuristic cost to goal
self.f = g + h # Total cost
def __lt__(self, other):
return self.f < other.f

def heuristic(a, b):


"""Calculate Manhattan distance heuristic (for a grid)."""
return abs(a[0] - b[0]) + abs(a[1] - b[1])

def a_star_search(grid, start, goal):


open_list = []
closed_set = set()

start_node = Node(start, None, 0, heuristic(start, goal))


heapq.heappush(open_list, start_node)

while open_list:
current_node = heapq.heappop(open_list) # Get node with lowest f(n)

if current_node.position == goal:
path = []
while current_node:
path.append(current_node.position)
current_node = current_node.parent
return path[::-1] # Return reversed path

closed_set.add(current_node.position)

for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]: # Possible moves
neighbor_pos = (current_node.position[0] + dx, current_node.position[1] + dy)
if neighbor_pos in closed_set or not (0 <= neighbor_pos[0] < len(grid) and 0 <=
neighbor_pos[1] < len(grid[0])):
continue # Ignore if out of bounds or already visited

if grid[neighbor_pos[0]][neighbor_pos[1]] == 1: # 1 represents obstacles


continue

g_cost = current_node.g + 1
h_cost = heuristic(neighbor_pos, goal)
neighbor_node = Node(neighbor_pos, current_node, g_cost, h_cost)

heapq.heappush(open_list, neighbor_node)

return None # No path found

# Example grid (0 = free space, 1 = obstacle)


grid = [
[0, 0, 0, 0, 0],
[1, 1, 0, 1, 0],
[0, 0, 0, 1, 0],
[0, 1, 1, 1, 0],
[0, 0, 0, 0, 0]
]

start = (0, 0) # Start position


goal = (4, 4) # Goal position

path = a_star_search(grid, start, goal)

if path:
print("Shortest Path:", path)
else:
print("No path found")

Explanation of the Code:


Node Class: Represents each position on the grid with properties like cost values.
Heuristic Function: Uses Manhattan distance to estimate cost.
A Search Algorithm*:
Uses a priority queue (min-heap) for efficient node selection.
Expands the node with the lowest cost
𝑓
(
𝑛
)
f(n).
Tracks visited nodes in a closed set.
Moves in four directions (up, down, left, right).
Grid Representation:
0 → Walkable path
1 → Obstacle
Outputs the shortest path from start to goal.
Analyze and Reflect

Challenges Faced While Understanding & Writing the Algorithm

1. Balancing Efficiency & Accuracy


o A* search needs an effective heuristic function to perform well. I had to decide
between the Manhattan distance and Euclidean distance for estimating the cost.
o Manhattan distance works well for grid-based movement, but in cases where
diagonal movement is allowed, Euclidean distance might be more appropriate.

2. Understanding Data Structures


o A* requires priority queues to efficiently select the best node to expand. Python’s
heapq module helps with this, but it only supports min-heaps by default. I had to
structure the Node class to make comparisons based on the f(n) cost.

3. Handling Edge Cases


o I had to ensure that the algorithm didn’t attempt to move into obstacles or out of
bounds.
o Checking if a node was already in the closed set was crucial to prevent infinite
loops.

How This Exercise Helped Me Understand AI in Algorithms

1. AI is Based on Core Algorithms


o Even complex AI systems (like self-driving cars or AI-driven gaming bots) rely
on foundational algorithms like A* for decision-making and navigation.
o A* is a perfect example of an informed search algorithm, which prioritizes paths
intelligently rather than blindly searching.

2. AI Requires Problem-Solving & Optimization


o Choosing the right heuristic is key in AI. Many AI applications optimize paths,
schedules, or recommendations similarly to how A* finds the best route.
o This highlights how AI involves heuristics, optimization, and efficiency—all
necessary to make decisions in real-world scenarios.

3. Real-World Relevance of AI Algorithms


o A* isn’t just an academic concept—it’s used in robotics, gaming, and even GPS
navigation.
o This reinforces the idea that AI isn’t magic—it’s built on structured, logical steps
that solve problems efficiently.

You might also like