Professional Documents
Culture Documents
AI Lab Student Sample File With Pages Removed
AI Lab Student Sample File With Pages Removed
Theory:
An uninformed search is a searching technique that has no additional information about the distance
from the current state to the goal.
Informed Search is another technique that has additional information about the estimate distance from
the current state to the goal.
Performance Finds the solution more Speed is slower than the informed
quickly. search.
Aim: Write a program to conduct Breadth First Search (Uninformed search) for a graph.
Program
#include<iostream>
#include <list>
using namespace std;
// This class represents a directed graph using
// adjacency list representation class
Graph
{
int V; // No. of vertices
Graph::Graph(int V)
{
this->V = V;
adj = new list<int>[V];
}
void Graph::BFS(int s)
{
// Mark all the vertices as not visited bool
*visited = new bool[V];
for(int i = 0; i < V; i++) visited[i]
= false;
while(!queue.empty())
{
// Dequeue a vertex from queue and print it s =
queue.front();
cout << s << " ";
queue.pop_front();
return 0;
}
Output
Experiment-1.2
Program
#include<bits/stdc++.h> using
namespace std;
// Graph class represents a directed graph
// using adjacency list representation class Graph
{
int V; // No. of vertices
public:
Graph(int V); // Constructor
// function to add an edge to graph
Graph::Graph(int V)
{
this->V = V;
adj = new list<int>[V];
}
return 0;
}
Output
Experiment-1.3
Program
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pi;
while (!pq.empty()) {
int x = pq.top().second;
// Displaying the path having lowest cost
cout << x << " ";
pq.pop();
if (x == target) break;
return 0;
}
Output
Experiment-1.4
Program
#include <iostream>
#include <cmath>
#include <list>
#include <vector>
#include <algorithm>
class Vector2
{
int x, y;
public:
Vector2(int _x, int _y) : x(_x), y(_y) {}
Vector2() = default;
Vector2 operator +(const Vector2& other)
{
Vector2 temp;
temp.x = this->x + other.x;
temp.y = this->y + other.y;
return temp;
}
int getX() const { return x; }
int getY() const { return y; }
struct Node
{
Vector2 position;
int G, H, F;
Node* parent = nullptr;
Node() = default;
Node(const Node& other) = default;
Node(Vector2 pos):position(pos) {};
class Map
{
std::vector<char> data;
int size;
public:
Map() = default;
Map(int _size) : size(_size) {
data.resize(size * size);
for (int i = 0; i < size * size; ++i) data[i] = '.';
}
void display() const{
for (int i = 1; i <= size * size; ++i) {
std::cout << data[i - 1] << " ";
if (!(i % size)) std::cout << "\n";
}
}
bool getIfInDanger(Vector2 position) const {
if (position.y < 0) position.y = 0;
if (position.x < 0) position.x = 0;
if (position.y >= 20) position.y = size - 1;
if (position.x >= 20) position.x = size - 1;
return(data[position.getX() + (position.getY() * size)] == 'X');
}
void setElement(char&& asda, Vector2 position) {
data[position.getX() + (position.getY() * size)] = asda;
}
};
class Solver
{
Vector2 startPos, endPos;
std::vector<Vector2> directions;
Map map;
public:
Solver(Vector2 _startPos, Vector2 _endPos, int size) : startPos(_startPos),
endPos(_endPos){
Map temp(size);
map = temp;
directions.resize(8);
directions[0] = Vector2(-1, 1);
directions[1] = Vector2(-1, 0);
directions[2] = Vector2(-1, -1);
directions[3] = Vector2(0, 1);
directions[4] = Vector2(0, -1);
directions[5] = Vector2(1, 1);
directions[6] = Vector2(1, 0);
directions[7] = Vector2(1, -1);
}
bool aStar() {
Node startNode(startPos);
Node goalNode(Vector2(endPos.getX(), endPos.getY()));
if (map.getIfInDanger(startNode.position) || map.getIfInDanger(goalNode.position)) {
std::cout << "Either the start of this map is obstructed or so is the end.";
return false;
}
std::list<Node> openList;
std::list<Node> closedList;
startNode.calc(endPos);
openList.push_back(startNode);
while (!openList.empty()) {
auto current = Node(*std::min_element(openList.begin(), openList.end()));
current.calc(endPos);
closedList.push_back(current);
openList.remove(current);
if (current == goalNode) break;
successor.calc(endPos);
auto inOpen = std::find(openList.begin(), openList.end(), successor);
if (inOpen == openList.end()) {
successor.parent = &closedList.back();
successor.calc(endPos);
openList.push_back(successor);
}
else
if (successor.G < inOpen->G) successor.parent = &closedList.back();
}
}
if (!openList.size()) {
std::cout << "No path has been found\n";
return false;
}
map.display();
return true;
}
};
int main()
{
Solver solve(Vector2(0,0),Vector2(19,19), 20);
solve.aStar();
}
Output