Artificial Intelligence Lab File
Artificial Intelligence Lab File
PRACTICAL-1
AIM: WAP of Recursion pseudo code and example.
if A[n/2] >= 0
else
if p<q
print p
else
print q
#include <stdio.h>
int factorial(int);
int main()
{
int num;
int result;
scanf("%d", &num);
if (num < 0)
{
printf("Factorial of negative number not possible\n");
}
Else
{
result = factorial(num);
printf("The Factorial of %d is %d.\n", num, result);
}
return 0;
}
if (num == 0 || num == 1)
{
return 1;
Else
{
return(num * factorial(num - 1));
}}
$ cc pgm5.c
$ a.out
PRACTICAL-2
AIM: WAP of Brute force solution to the knapsack problem.
Example:
#include <stdio.h>
if (i==0 || w==0)
K[i][w] = 0;
}}
return K[n][W];
}
int main()
$ ./knapsack
Value = 220
PRACTICAL-3
AIM: WAP of graph coloring problem.
Example:
// A C++ program to implement greedy algorithm for graph coloring
#include <iostream>
#include <list>
using namespace std;
// A class that represents an undirected graph
class Graph
{
int V; // No. of vertices
list<int> *adj; // A dynamic array of adjacency lists
public:
// Constructor and destructor
Graph(int V) { this->V = V; adj = new list<int>[V]; }
~Graph() { delete [] adj; }
// function to add an edge to graph
void addEdge(int v, int w);
// Prints greedy coloring of the vertices
void greedyColoring();
};
void Graph::addEdge(int v, int w)
{
adj[v].push_back(w);
adj[w].push_back(v); // Note: the graph is undirected
}
// Assigns colors (starting from 0) to all vertices and prints
// the assignment of colors
void Graph::greedyColoring()
{
int result[V];
// Assign the first color to first vertex
result[0] = 0;
// Initialize remaining V-1 vertices as unassigned
for (int u = 1; u < V; u++)
result[u] = -1; // no color is assigned to u
// A temporary array to store the available colors. True
// value of available[cr] would mean that the color cr is
// assigned to one of its adjacent vertices
bool available[V];
for (int cr = 0; cr < V; cr++)
available[cr] = false;
// Assign colors to remaining V-1 vertices
for (int u = 1; u < V; u++)
// Process all adjacent vertices and flag their colors
// as unavailable
list<int>::iterator i;
for (i = adj[u].begin(); i != adj[u].end(); ++i)
if (result[*i] != -1)
available[result[*i]] = true;
// Find the first available color
int cr;
for (cr = 0; cr < V; cr++)
if (available[cr] == false)
break;
result[u] = cr; // Assign the found color
// Reset the values back to false for the next iteration
for (i = adj[u].begin(); i != adj[u].end(); ++i)
if (result[*i] != -1)
available[result[*i]] = false; }
// print the result for (int u = 0; u < V; u++)
cout << "Vertex " << u << " ---> Color "
<< result[u] << endl;
}
// Driver program to test above function
int main()
{
Graph g1(5);
g1.addEdge(0, 1);
g1.addEdge(0, 2);
g1.addEdge(1, 2);
g1.addEdge(1, 3);
g1.addEdge(2, 3);
g1.addEdge(3, 4);
cout << "Coloring of graph 1 \n";
g1.greedyColoring();
Graph g2(5);
g2.addEdge(0, 1);
g2.addEdge(0, 2);
g2.addEdge(1, 2);
g2.addEdge(1, 4);
g2.addEdge(2, 4);
g2.addEdge(4, 3);
cout << "\nColoring of graph 2 \n";
g2.greedyColoring();
return 0;
}
Output:
Coloring of graph 1
Coloring of graph 2
PRACTICAL-4
AIM: WAP of array structure.
#include <stdio.h>
struct Bookinfo
{
char[20] bname;
int pages;
int price;
}
book[3];
int main(int argc, char *argv[])
int i;
for(i=0;i<3;i++)
gets(book[i].bname);
scanf("%d",book[i].pages);
scanf("%f",book[i].price);
{
printf("\nName of Book : %s",book[i].bname);
}
return 0;
}
PRACTICAL-5
AIM: Write A Program on loop detection
h = h->next;
}
return false;
}
/* Drier program to test above function*/
int main()
{
/* Start with the empty list */
struct Node* head = NULL;
push(&head, 20);
push(&head, 4);
push(&head, 15);
push(&head, 10);
/* Create a loop for testing */
head->next->next->next->next = head;
if (detectLoop(head))
cout<<”loop found”;
else
return 0;
PRACTICAL-6
AIM: Write A Program to generate output for A*
;;; ASTAR.CL
;;; A* Search for a shortest path.
;;; Here is the representation of the adjacency distance data,
;;; plus functions for getting at it and at solution path info.
;;; This code is identical to that in UNIFCOST.CL
(let ((distance-info (make- hash-table :size 20))
(path-predecessor-info (make-hash-table :size 20))
) (defun set-distances (x y)
(setf (gethash x distance-info) y) ) (defun
get-distances (x)
(gethash x distance- info) )
(defun set- predecessor (x y)
(setf (gethash x path-predecessor-info)
y) ) (defun get-predecessor (x) (gethash x
path-predecessor-info) )
)
;;; Here are actual inter-city distances from theMichelin map:
(set-distances 'brest '((rennes .244)))
(set-distances 'rennes '((caen . 176)(paris . 348)
(brest . 244)(nantes . 107)))
(set-distances 'caen '((calais . 120)(paris . 241)(rennes . 176)))
(set-distances 'calais '((nancy . 534)(paris . 297)(caen . 120)))
(set-distances 'nancy '((strasbourg . 145)(dijon . 201)
(paris . 372)(calais . 534))) (set-distances 'strasbourg
'((dijon . 335)(nancy . 145))) (set-distances 'dijon
'((strasbourg .
335)(lyon . 192)
(paris . 313)(nancy . 201)))
(set-distances 'lyon '((grenoble . 104)(avignon . 216)
(limoges . 389)(dijon . 192))) (set-distances 'grenoble '((avignon .
227)(lyon . 104))) (set-distances 'avignon '((grenoble .
227)(marseille . 99)
(montpellier . 121)(lyon . 216))) (set-distances
get-longitude (x)
(gethash x longitude-info) ))
;;; The longitude of each city is stored in tenths of a degree.
;;; We again use a local function with a LAMBDA form, since
;;; SET-LONGITUDE takes two arguments but we want a
;;; function that takes one argument for this use with MAPCAR.
(mapcar #'(lambda (pair) (apply #'set-longitude pair))
'((avignon 48)(bordeaux -6)(brest -45)(caen-4)
(calais 18)(dijon51)(grenobl57)(limoges 12) (lyon 48)
(marseille 53)(montpellier 36) (nantes -16)(nancy 62)(nice
73)(paris 23) (rennes -17)(strasbourg 77)(toulouse 14) ) )
Output: