Professional Documents
Culture Documents
(Session: - July-2022)
LAB FILE
Subject: - Artificial Intelligence for Robotics
Enrollment No: 19010BTRA05886
(Session: - July-2022)
INDEX
Batch:- B3 Roll no:- 20100BTCSE07611 Name:-Pranav Mathur
S.no. Date of Code of Title Remarks
performing Experiment
01 Write a Program To Implement Depth First Search
1 09/09/2022.
.
• CODE: -
void Graph::DFS(int v)
{
// Mark the current node as visited and
// print it
visited[v] =
true; cout << v
<< " ";
// Driver's
code int
main()
{
// Create a graph given in the above diagram Graph g;
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
cout << "Following is Depth First Traversal" " (starting from vertex 2) \n";
// Function
call
g.DFS(2);
return 0;
}
• Output:
Experiment:02
#include<bits/stdc++.h> using
namespace std;
public:
};
// Pointer to an array containing adjacency
// lists vector<list<int>> adj;
void Graph::BFS(int s)
{
// Mark all the vertices as not visited vector<bool> visited;
visited.resize(V,false);
while(!queue.empty())
{
// Dequeue a vertex from queue and print it s = queue.front();
cout << s << " ";
queue.pop_front();
{
if (!visited[adjecent])
{
visited[adjecent] = true; queue.push_back(adjecent);
}
}
}
}
• Output
Experiment 03
• CODE: -
econd));
}
pq.push(make_pair(graph[x][i].first,graph[x][i].s
}
}
}
alphabets) are
addedge(0, 2, 6);
addedge(0, 3, 5);
addedge(1, 4, 9);
addedge(1, 5, 8);
addedge(2, 6, 12);
addedge(2, 7, 14);
addedge(3, 8, 7);
addedge(8, 9, 5);
addedge(8, 10, 6);
addedge(9, 11, 1);
addedge(9, 12, 10);
addedge(9, 13, 2);
int source
= 0; int
target = 9;
return 0;
}
• Output
Experiment 04
• CODE: -
{
// Returns true if row number and column number
// is in range
return (row >= 0) && (row < ROW) && (col >= 0) && (col < COL);
}
// A Utility Function to calculate the 'h' heuristics. double calculateHValue(int row, int col, Pair
dest)
{
stack<Pair> Path;
return;
}
== true) {
printf("We are already at the destination\n"); return;
}
cellDetails[i][j].h = 0.0;
cellDetails[i][j].parent_i = i;
cellDetails[i][j].parent_j = j;
/*
Create an open list having information as-
<f, <i, j>>
where f = g
+ h,
and i, j are the row and column index of that cell Note that 0 <= i <= ROW-1 & 0 <= j
<= COL-1 This open list is implemented as a set of pair of pair.*/
set<pPair> openList;
// Put the starting cell on the open list and set its
// 'f' as 0
openList.insert(make_pair(0.0, make_pair(i, j)));
closedList[i][j] = true;
/*
Generating all the 8 successor of this cell
N.W N N.E
\|/
\|/
W----Cell--- E
/|\
/|\
S.W S S.E
(i-1, j)
S --> South (i+1, j)
E --> East (i, j+1)
W --> West (i, j-1)
N.E--> North-East (i-1, j+1)
N.W--> North-West (i-1, j-1) S.E--> South-East
(i+1, j+1) S.W--> South-West (i+1, j-1)*/
// To store the 'g', 'h' and 'f' of the 8 successors double gNew, hNew, fNew;
// current successor
if (isDestination(i - 1, j, dest) == true) {
// Set the Parent of the destination cell cellDetails[i - 1][j].parent_i =
i; cellDetails[i - 1][j].parent_j = j; printf("The destination cell is
found\n"); tracePath(cellDetails, dest);
foundDest =
true; return;
}
// If the successor is already on the closed
&& isUnBlocked(grid, i + 1, j)
== true) {
gNew = cellDetails[i][j].g + 1.0;
hNew = calculateHValue(i + 1, j, dest); fNew = gNew + hNew;
return;
}
// Driver program to test above function int main()
{
/* Description of the Grid-
return (0);
}
• Output: -
Experiment 05
• CODE: -
// C++ program to demonstrate
// working of Alpha-Beta Pruning
#include<bits/stdc++.h>
using namespace std;
// Initial values of
// Alpha and Beta const
int MAX = 1000; const
int MIN = -1000;
if (maximizingPlayer)
{
int best = MIN;
// Alpha Beta
Pruning if (beta <=
alpha)
break;
}
}
else
{
return best;
int best = MAX;
// Recur for left and
// right children
for (int i = 0; i < 2; i++)
{
int val = minimax(depth + 1, nodeIndex * 2 + i,
true, values, alpha, beta);
best = min(best, val); beta = min(beta, best);
}
// Driver Code
int main()
{
int values[8] = { 3, 5, 6, 9, 1, 2, 0, -1 };
cout <<"The optimal value is : "<< minimax(0, 0, true, values, MIN, MAX);;
return 0;
}
OUTPUT:
Experiment: 06
• CODE: -
// A simple C++ program to find
// maximum score that
// maximizing player can get.
#include<bits/stdc++.h> using
namespace std;
//
Driv
er
code
int
main
()
{
// The number of elements in scores must be
// a power of 2.
int scores[] = {3, 5, 2, 9, 12, 5, 23, 23};
int n = sizeof(scores)/sizeof(scores[0]); int h = log2(n);
int res = minimax(0, 0, true, scores, h);
cout << "The optimal value is : " << res << endl; return 0;
}
OUTPUT:
Experiment: 07
• CODE: -
#include <bits/stdc++.h>
using namespace std;
#define N 3
//
stores
matrix
int
mat[N]
[N];
return node;
}
// print path from root node to destination node void printPath(Node* root)
{
if (root ==
N
U
L
L)
ret
ur
n;
printPath(root->parent);
printMatrix(root->mat);
printf("\n");
}
//
Driv
er
code
int
main
()
{
// Initial configuration
// Value 0 is used for empty space int
initial[N][N] =
{
{1, 2, 3},
{5, 6, 0},
{7, 8, 4}
};
solve(initial, x, y, final);
return 0;
}
OUTPUT