Sie sind auf Seite 1von 25

Practical File for Master in Computer

Application
(5 Year Integrated)
(Through Distance Education)

Directorate of Distance Education


Guru Jambeshwar University of
Science & Technology, Hisar

Submitted by-Mansi Maheshwari


Roll No.-14031135004
LAB- (MCA-408)

Artificial
Intelligence
Index
S.no. Programs Page No.
1 Turbo Prolog features and format. 1
2 Write a program for usage of rules in 2-3
prolog.
3 Write a program for using Input, 4-5
Output and fail predicates in prolog.
4 Write a program for studying usage if 6-8
arithmetic operators in prolog.
5 Write a program to study usage of Cut, 9-10
Not, Fail predicates in prolog.
6 Write a program to study usage of 11
recursion in prolog.
7 Write a program to implement DFS/ 12-14
BFS.
8 Write a program to implement A* 15-18
algorithm.
9 Write a program to solve 8 queens 19-20
problem.
10 Write a program to solve travelling 21-22
salesman problem.
1. Turbo Prolog features and format.
Procedure:- Write, verification, domain, predicate
and clauses section.

1
2. Write a program for usage of rules in prolog.
Procedure:- Write a prolong program contaning facts
related to following predicates
a) Location (city, state)
b) Stays (person, city) in clauses
section.
Display (i) list of person, state and city
Example:-
Output:- Person= ram City=anand State=Gujarat
Person= Lakshman City=anand State=Gujarat
Person= Seeta City=Baroda State=Gujarat
Person= Ravan City=Columbo State=Srilanka
(ii) Given person staying in which state .
Example:-
Output:- Person= ram State=Gujarat

Procedure:-->Create a family tree program following rules –


 M is the mother if she is a parent of P and is female
 F is the father of P if he is a parent of P and is male
 X is a sibling of Y if they both have the same parent.
 Then add rules for grandparents, uncle-aunt, sister and
brother.

2
Based on the facts , define goals to answer questions related
to family tree.
Example:- Suppose family tree is as follows-
Bharat- Shakuntlal

Dashrath-kaushalya Sheena-harsh

Ram-Seeta Lakshman- Urmila

Luv
Commented [1]:

Kush

Output:-
goal :-grandfather(Luv,X) goal:uncle(luv,X)
X=Dashrath X=lakshman
Goal:-father(Luv,X)
X=Ram
Goal:-sibling(luv,X)
X=kush
Goal:-aunt(luv,X)
X=Urmila
3
3. Write a program for using Input, Output and fail
predicates in prolog.
Procedure:- Write program using write, writef, readint,
readchar, readln, readreal predicates
 Store facts of employee name, age, job location,
marital status and gender.
Display (i) List of married and unmarried employees.
(ii) List of male and female employees.
(iii) List of employees for given job locations.
Example:- Output:-
******Options*******
1) Display list of married employees.
2) Display list of unmarried employees.
3) Display list of male employees.
4) Display list of female employees.
5) Display list of employees at a given location.
Enter your choice:-
5
Enter location:-
Anand
List of employees staying at “Anand” are
Sr no of employees
4
1. Ram
2. Lakshman
 Create a set of facts and rules on the ancestor of
whom.
Display 1) Who is ancestor of given person.
2) Complete list i.e. who is ancestor of whom.
Example:-
I. Input person's name- Luv
Output:- Ancestors of “Luv” is-
Ram, Lakshman
II. Output:- person ancestors
Luv Ram, Lakshman
Ram Dashrath, sheena
Lakshman Dashrath, Sheena
Dashrath Bharat

5
4.Write a program for studying usage if arithmetic
operators in prolog.
Procedure:- Write programs to-
 Accept names of student, roll no, his subject name,
maximum marks and obtained marks
in the subject. (Take marks of atleast 6 subjects).
Compute the percentage of a student.
Display his result with other information. Use variables,
arithmetic operators, I/O predicates appropriately.
Example:-Output:-
Enter name of student:- “ram”
Enter roll number of student-IT1
Enter subject information for 6 subjects :-
Subject name- “daa”
Max marks:-150 Obtained marks:-120
Subject name- “ oopd”
Max marks:-150 Obtained marks:-110
Subject name- “ cn”
Max marks:-150 Obtained marks:-100
Subject name- “ dc”
Max marks:-100 Obtained marks:-60
Subject name- “ amp”
Max marks:-150 Obtained marks:-140
Subject name- “ cpi”
Max marks:-150 Obtained marks:-30
**********RESULT***************
Student name:-“ram” Roll no-“IT1”
6
Subject names-“daa” ,”oopd”, “cn”,”dc”,”amp”,”cpi”
Total max marks-….
Total Obtained marks- ….
% percentage-
Accept department, designation, name, age, basic
salary, house rent allowance(HRA) of an employee .
Compute dearness allowance (DA) which is
15% of basic salary . Determine the gross salary
( basic salary + HRA + DA ) of the
employee. Display all information of the employee.
(Use variables, rules, I/O predicates, arithmetic operator
s as per needed).
Example:-Output:-
Enter employee name:- xyz
Enter department :-IT
Enter age:-43
Enter basic salary-50,000
Enter HRA:- 2000-----------------------
PAYSLIP
Employee Name is –xyz
Department-IT
BA-50,000
7
DA – 7500
HRA-2000-2000
Gross salary- 59500/-

8
5.Write a program to study usage of Cut, Not,
Fail predicates in prolog.
Procedure:- Write a prolog program having facts and
clauses section for predicate student (studentname,
branchname). Use cut !, fail, not predicate to
 Diaplay list of all students
 Display list of students for given specific branch
 Display list of students excluding specific branch
Example:- Output:- **********Menu**********
1) Display list of all students
2) Display list of students for given specific branch
3) Display list of students excluding specific branch
Enter your choice-
3
Enter branch name to be excluded from result-
CE
List of all students except from CE branch are-
Student name – Department
Harsh – IT
Deep – EC
Heena – IC
Do you want to continue?
“Y”
***************Menu***************
1) Display list of all students
2) Display list of students for given specific branch
9
3) Display list of students excluding specific branch
Enter your choice-
1
Student name – Department
Harsh – IT
Deep – EC
Heena – IC
Amardeep – CE
Anand – CE

10
6. Write a program to study usage of recursion in
prolog.
Procedure:-
 Write predicate fact(n), which finds and display
factorial of a given number .
Example:- Output:-
Goal :- fact(5)
5 != 5*4*3*2*1= 120 .
Write predicate fibbonacci (n) ,which finds the series for first
“n” values .
Example:- Output:-Total number of numbers needed in
output?
6
Fibonacci series of first ‘6’ numbers:-
“1,1,2,3,5,8”

11
7. Write a program to implement DFS/ BFS.
DFS
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
int cost[10][10],i,j,k,n,stk[10],top,v,visit[10],visited[10];
main()
{
int m;
cout <<"enterno of vertices";
cin >> n;
cout <<"ente no of edges";
cin >> m;
cout <<"\nEDGES \n";
for(k=1;k<=m;k++)
{
cin >>i>>j;
cost[i][j]=1;
}
cout <<"enter initial vertex";
cin >>v;
cout <<"ORDER OF VISITED VERTICES";
cout << v <<" ";
visited[v]=1;
k=1;
while(k<n)
{
for(j=n;j>=1;j--)
12
if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1)
{
visit[j]=1;
stk[top]=j;
top++;
}
v=stk[--top];
cout<<v << " ";
k++;
visit[v]=0; visited[v]=1;
}
}

BFS
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
int cost[10][10],i,j,k,n,qu[10],front,rare,v,visit[10],visited[10];

main()
{
int m;
cout <<"enterno of vertices";
cin >> n;
cout <<"ente no of edges";
cin >> m;
cout <<"\nEDGES \n";
for(k=1;k<=m;k++)
13
cin >>i>>j
cost[i][j]=1;
}

cout <<"enter initial vertex";


cin >>v;
cout <<"Visitied vertices\n";
cout << v;
visited[v]=1;
k=1;
while(k<n)
{
for(j=1;j<=n;j++)
if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1)
{
visit[j]=1;
qu[rare++]=j;
}
v=qu[front++];
cout<<v << " ";
k++;
visit[v]=0; visited[v]=1;
}
}

14
8. Write a program to implement A* algorithm.
#include<bits/stdc++.h>
using namespace std;
#define ROW 9
#define COL 10
// Creating a shortcut for int, int pair type
typedef pair<int, int> Pair;
// Creating a shortcut for pair<int, pair<int, int>> type
typedef pair<double, pair<int, int>> pPair;
// A structure to hold the neccesary parameters
struct cell
{
// Row and Column index of its parent
// Note that 0 <= i <= ROW-1 & 0 <= j <= COL-1
int parent_i, parent_j;
// f = g + h
double f, g, h;
}

// A Utility Function to check whether given cell (row, col)


// is a valid cell or not.
bool isValid(int row, int col)
15
{
// Returns true if row number and column number
// is in range
return (row >= 0) && (row < ROW) &&
(col >= 0) && (col < COL);
}

// A Utility Function to check whether the given cell is


// blocked or not
bool isUnBlocked(int grid[][COL], int row, int col)
{
// Returns true if the cell is not blocked else false
if (grid[row][col] == 1)
return (true);
else
return (false);
}

// A Utility Function to check whether destination cell has


// been reached or not
bool isDestination(int row, int col, Pair dest)
{
16
if (row == dest.first && col == dest.second)
return (true);
else
return (false);
}

// A Utility Function to calculate the 'h' heuristics.


double calculateHValue(int row, int col, Pair dest)
{
// Return using the distance formula
return ((double)sqrt ((row-dest.first)*(row-dest.first)
+ (col-dest.second)*(col-dest.second)));
}

// A Utility Function to trace the path from the source


// to destination
void tracePath(cell cellDetails[][COL], Pair dest)
{
printf ("\nThe Path is ");
int row = dest.first;
int col = dest.second;
stack<Pair> Path;
17
while (!(cellDetails[row][col].parent_i == row
&& cellDetails[row][col].parent_j == col ))
{
Path.push (make_pair (row, col));
int temp_row = cellDetails[row][col].parent_i;
int temp_col = cellDetails[row][col].parent_j;
row = temp_row;
col = temp_col;
}

Path.push (make_pair (row, col));


while (!Path.empty())
{
pair<int,int> p = Path.top();
Path.pop();
printf("-> (%d,%d) ",p.first,p.second);
}
return;
}

18
9. Write a program to solve 8 queens problem.
#include<stdio.h>
#include<stdlib.h>
int t[8] = {-1};
int sol = 1;

void printsol()
{
int i,j;
char crossboard[8][8];
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
crossboard[i][j]='_';
}
}
for(i=0;i<8;i++)
{
crossboard[i][t[i]]='q';
}

for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
printf("%c ",crossboard[i][j]);
}
printf("\n");
}
19
}
int empty(int i)
{
int j=0;
while((t[i]!=t[j])&&(abs(t[i]-t[j])!=(i-j))&&j<8)j++;
return i==j?1:0;
}

void queens(int i)
{
for(t[i] = 0;t[i]<8;t[i]++)
{
if(empty(i))
{
if(i==7){
printsol();
printf("\n solution %d\n",sol++);
}
else
queens(i+1);
}
}
}

int main()
{
queens(0);
return 0;
}

20
10. Write a program to solve travelling salesman
problem.
#include <vector>
#include <iostream>
using namespace std;
/** \brief Given a complete, undirected, weighted graph in
the form of an adjacency matrix,
returns the smallest tour that visits all nodes and
starts and ends at the same
node. This dynamic programming solution runs in
O(n * 2^n).
\return the minimum cost to complete the tour
*/
int tsp(const vector<vector<int>>& cities, int pos, int
visited, vector<vector<int>>& state)
{
if(visited == ((1 << cities.size()) - 1))
return cities[pos][0]; // return to starting city

if(state[pos][visited] != INT_MAX)
return state[pos][visited];
for(int i = 0; i < cities.size(); ++i)
{
// can't visit ourselves unless we're ending & skip if already
visited
21
if(i == pos || (visited & (1 << i)))
continue;
int distance = cities[pos][i] + tsp(cities, i, visited | (1 << i),
state);
if(distance < state[pos][visited])
state[pos][visited] = distance;
}
return state[pos][visited];
}
int main()
{
vector<vector<int>> cities = { { 0, 20, 42, 35 },
{ 20, 0, 30, 34 },
{ 42, 30, 0, 12 },
{ 35, 34, 12, 0 }
};
vector<vector<int>> state(cities.size());
for(auto& neighbors : state)
neighbors = vector<int>((1 << cities.size()) - 1,
INT_MAX);
cout << "minimum: " << tsp(cities, 0, 1, state) << endl;
return 0;
}

22