0% found this document useful (0 votes)
23 views4 pages

AI FILE manjot 4 & 5

The document outlines the implementation of Best First Search and A* Search algorithms, highlighting their key features, advantages, and disadvantages. Best First Search uses a priority queue based on heuristic values to explore nodes, while A* combines Dijkstra’s algorithm with a heuristic to ensure optimal pathfinding. Both algorithms have real-world applications in areas like GPS navigation, AI in games, and network routing.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views4 pages

AI FILE manjot 4 & 5

The document outlines the implementation of Best First Search and A* Search algorithms, highlighting their key features, advantages, and disadvantages. Best First Search uses a priority queue based on heuristic values to explore nodes, while A* combines Dijkstra’s algorithm with a heuristic to ensure optimal pathfinding. Both algorithms have real-world applications in areas like GPS navigation, AI in games, and network routing.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Practical No.

:- 04

Aim: Write a program to implement Best Frist Search.

Best First Search :-


It's a search algorithm that explores a graph by expanding the most promising node chosen according to a
specified heuristic. Unlike algorithms like BFS or DFS, Best-First uses a priority queue where nodes are
ordered based on their heuristic value. The main goal is to reach the target as quickly as possible by
prioritizing nodes that seem closer to the goal.

🔹Key Features of Best-First Search


1.Uses a Priority Queue (Min-Heap) → The node with the lowest heuristic value gets expanded first.
2.Heuristic-Based Search → Uses a function h(n) that estimates the cost to the goal.
3.Greedy Approach → It selects the best node at each step without considering past costs.
4.Fast but Not Always Optimal → It doesn’t guarantee finding the shortest path.
5.Used in AI, Pathfinding, and Game Development → Efficient in navigating large search spaces.

🔹
Real-World Applications

1.Google Maps & GPS → Finds the best routes.


2.AI in Games → Helps NPCs navigate efficiently.
3.Network Routing → Optimizes internet traffic.
4.Medical Diagnosis → Finds the best treatment options.

Advantages:
1.Efficient for problems with good heuristics
2.Typically faster than BFS/DFS when guided by an accurate heuristic
3.More goal-directed than blind search algorithms
4.Avoids exploring non-promising paths

Disadvantages:
1.Not guaranteed to find the optimal path (not complete)
2.Heuristic quality directly impacts performance
3.Can get stuck in infinite loops without proper cycle checking
4.Memory intensive for large state spaces

Algorithm Explanation:
1.Initialization:
Create a priority queue (min-heap) ordered by heuristic values
Create a set to track visited nodes
Add start node to queue with its heuristic value
2.Main Loop:
Dequeue node with lowest heuristic value
Check if it's the goal node
Expand its neighbors and add unvisited ones to the queue
3.Termination:
Success if goal is found
Failure if queue is empty and goal isn't found

Manjot Singh 2220979 D1


Program :-

#include <iostream>
#include <queue>
#include <vector>
#include <map>
#include <set>
using namespace std;
void bestFirstSearch(map<char, vector<char>>& graph, map<char, int>& heuristic, char start, char goal) {
priority_queue<pair<int, char>, vector<pair<int, char>>, greater<pair<int, char>>> pq;
set<char> visited;
pq.push({heuristic[start], start});
visited.insert(start);
cout << "Search Path: ";
while (!pq.empty()) {
char current = pq.top().second;
int current_heuristic = pq.top().first;
pq.pop();
cout << current << " ";
if (current == goal) {
cout << "\nGoal node '" << goal << "' found!" << endl;
return;
}
f o r ( c h a r n e i g h b o r : g r a p h [ c u r r e n t ] ) {
if (visited.find(neighbor) == visited.end()) {
visited.insert(neighbor);
pq.push({heuristic[neighbor], neighbor});
} } }
cout << "\nGoal node '" << goal << "' not reachable from start node '" << start << "'." << endl;
}
int main() {
map<char, vector<char>> graph = {
{'A', {'B', 'C'}},
{'B', {'D', 'E'}},
{'C', {'F', 'G'}},
{'D', {}}, {'E',
{'H'}}, {'F',
{}}, {'G', {'I'}},
{'H', {}}, {'I',
{}}

};
map<char, int> heuristic = {
{'A', 9}, {'B', 7}, {'C', 8},
{'D', 5}, {'E', 6}, {'F', 3},
{'G', 4}, {'H', 2}, {'I', 1}
};

char start = 'A';


char goal = 'I';
bestFirstSearch(graph, heuristic, start, goal);
return 0;
}

Output:-

Manjot Singh 2220979 D1


Practical No. :- 05

Aim: Write a program to implement A* Search.

A* Search :-
A* (A-Star) is a popular search algorithm used for pathfinding and graph traversal. It is widely used in
artificial intelligence, robotics, and gaming for finding the shortest path between two points efficiently. A*
combines the advantages of Dijkstra’s algorithm and Greedy Best-First Search by using a cost function:
f(n) = g(n) + h(n)
Where:
g(n) = Cost from the start node to node n.
h(n) = Heuristic estimate of the cost from node n to the goal.
f(n) = Total estimated cost of the cheapest solution path.

🔹 Key Features of Best-First Search


1. Combines Best-First Search and Dijkstra’s Algorithm - A* uses a heuristic function to prioritize nodes,
improving efficiency over traditional shortest path algorithms.
2. Uses f(n) = g(n) + h(n) - The total cost function combines the actual cost to reach a node (g(n)) and the
estimated cost to the goal (h(n)).
3. Optimal and Complete - A* guarantees the shortest path if the heuristic function is admissible (i.e., never
overestimates the cost to reach the goal).
4. Admissible and Consistent Heuristics - The heuristic function must be well-designed to ensure optimal
performance.
5. Widely Used in AI and Game Development - Commonly applied in pathfinding for games, robotics, and
network routing.

🔹 Real-World Applications
1.Google Maps & GPS → Finds the best routes.
2.AI in Games → Helps NPCs navigate efficiently.
3.Network Routing → Optimizes internet traffic.
4.Medical Diagnosis → Finds the best treatment options.

Advantages:
Finds the shortest path efficiently.
Uses heuristics to guide the search, reducing unnecessary calculations.
Works well with weighted graphs and real-world applications like GPS navigation.

Disadvantages:
Performance depends on the quality of the heuristic function.
Can be computationally expensive with poor heuristics.
Memory-intensive as it stores all possible paths.

Algorithm Explanation:
1. Initialize the open list with the start node.
2. Initialize the closed list (visited nodes) as empty.
3. Loop until the open list is empty:
Select the node with the lowest f(n) value.
If this node is the goal, return the path.
Otherwise, move it to the closed list.
Generate all its successors and calculate f(n) for each.
Add unvisited successors to the open list.
4. If the goal is not found, return failure.

Manjot Singh 2220979 D1


Program :-

#include <iostream>
#include <queue>
#include <vector>
#include <cmath>
#include <unordered_map>
using namespace std;
struct Node {
int x, y, cost, heuristic;
bool operator>(const Node& other) const {
return (cost + heuristic) > (other.cost + other.heuristic);
}
};
int heuristic(int x1, int y1, int x2, int y2) {
return abs(x1 - x2) + abs(y1 - y2);
}
void aStarSearch(vector<vector<int>>& grid, Node start, Node goal) {
priority_queue<Node, vector<Node>, greater<Node>> openList;
unordered_map<int, bool> closedList;
openList.push(start);
while (!openList.empty()) {
Node current = openList.top();
openList.pop();
if (current.x == goal.x && current.y == goal.y) {
cout << "Goal reached!" << endl;
return;
}
closedList[current.x * grid[0].size() + current.y] = true;
// Generate successors and continue search...
}
cout << "Path not found" << endl;
}
int main() {
vector<vector<int>> grid = {{0, 0, 0}, {0, 1, 0}, {0, 0, 0}};
Node start = {0, 0, 0, heuristic(0, 0, 2, 2)};
Node goal = {2, 2, 0, 0};
aStarSearch(grid, start, goal);
return 0;
}

Output:-

Manjot Singh 2220979 D1

You might also like