Ai Algo Reference
Ai Algo Reference
2. A Search Algorithm*
Aim:
To find the shortest path from a source to a destination in a grid using
the A* search algorithm.
Algorithm:
1. Initialize source and destination nodes and create an open list
(min-heap).
2. Calculate the f = g + h value for each cell.
3. Select the node with the lowest f from the open list.
4. Explore valid, unblocked neighbors and update their parent, g,
h, f values.
5. If destination is reached, trace the path using parent pointers.
6. Repeat until the destination is found or open list is empty.
7. Print the path or indicate if no path is found.
8. N-Queens Problem
Aim:
To place N queens on an N×N chessboard such that no two queens
attack each other.
Algorithm:
1. Initialize an N×N board with zeros.
2. Start from column 0 and recursively try placing a queen in each
row.
3. Check if placement is safe (row, left diagonal, right diagonal).
4. If safe, place queen and proceed to next column.
5. If all queens are placed, save the solution.
6. If no placement is possible, backtrack.
7. Print the last found solution.