AI FILE manjot 4 & 5
AI FILE manjot 4 & 5
:- 04
🔹
Real-World Applications
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
#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}
};
Output:-
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.
🔹 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.
#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:-