Sunteți pe pagina 1din 12

Experiment 3

Aim: To implement the Best First Search and Least Cost search.

Description of Aim and Related Theory:


Best First Search :

Best-first search is a search algorithm which explores a graph by expanding the most
promising node chosen according to a specified rule.

Judea Pearl described best-first search as estimating the promise of node n by a "heuristic
evaluation function { f(n)} which, in general, may depend on the description of n, the
description of the goal, the information gathered by the search up to that point, and most
important, on any extra knowledge about the problem domain."

Least Cost Search:

The search for an answer node can often be speeded by using an “intelligent” ranking
function, also called an approximate cost function to avoid searching in sub-trees that do
not contain an answer node. It is similar to backtracking technique but uses BFS-like search

FOR BEST FIRST SEARCH:


Algorithm:
Best-First-Search(Grah g, Node start)

1) Create an empty PriorityQueue

PriorityQueue pq;

2) Insert "start" in pq.

pq.insert(start)

3) Until PriorityQueue is empty

u = PriorityQueue.DeleteMin

If u is the goal

Exit

Else

For each neighbour v of u


If v "Unvisited"

Mark v "Visited"

pq.insert(v)

Mark v "Examined"

End procedure

Code:

#include <stdio.h>
#include <stdlib.h>
int difference(int A[][3], int B[][3]);
int alter(int A[][3], int B[][3]);
int diffup(int A[][3], int B[][3]);
int diffdown(int A[][3], int B[][3]);
int diffleft(int A[][3], int B[][3]);
int diffright(int A[][3], int B[][3]);
int minimum(int a, int b, int c, int d);
void display(int A[][3]);
int main(int argc, char *argv[])
{
int A[3][3] = {{2,8,3}, {1,6,4}, {7,0,5}}; // the initial array
int B[3][3] = {{1,2,3}, {8,0,4}, {7,6,5}};
int i, j;
int d; // difference between the desired array and the test array
int steps = 0 ;
printf("Target: \n");
display(B);
printf("Original: \n");
display(A);
while(1)
{
d = difference(A, B);
if(d==0) { printf("files matched in steps: %d", steps); return 0; }
steps++;
printf("\nStep: %d \n", steps);
alter(A, B); // changes the array according to whichever suitable
display(A);
system("PAUSE");
}
system("PAUSE");
return 0;
}
void display(int A[][3])
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%d ", A[i][j]);
printf("\n");
}
}
int difference(int A[][3], int B[][3])
{
int counter =0 ,i,j;
for(i=0;i<3; i++)
for( j=0;j<3;j++)
if(A[i][j] != B[i][j]) counter++;
return counter;
}
int alter(int A[][3], int B[][3])
{
int dup, ddown, dleft, dright;
int temp, i , j, flag=0, serial=0;
char ran[4];
dup = diffup(A, B);
ddown = diffdown(A, B);
dleft = diffleft(A, B);
dright = diffright(A, B);
printf("%d %d %d %d\n", dright, dleft, dup, ddown);
int min = minimum(dup, ddown, dleft, dright);
printf("%d\n", min);
if (min == dright)
ran[serial++] = 'r';
if (min == dleft)
ran[serial++] = 'l';
if (min == dup)
ran[serial++] = 'u';
if (min == ddown)
ran[serial++] = 'd';
int sel = rand()%serial;
char change = ran[sel];
if(change == 'r')
{for(i=0;i<3;i++)
for (j=0;j<2;j++)
if(A[i][j]==0)
{
A[i][j] = A[i][j+1];
A[i][j+1] = 0;
printf("right\n");
return 0; }
}
else if(change == 'l')
{for(i=0;i<3;i++)
for (j=1;j<3;j++)
if(A[i][j]==0)
{
A[i][j] = A[i][j-1];
A[i][j-1] = 0;
printf("left\n");
return 0; }
}
else if(change == 'u')
{for(i=1;i<3;i++)
for (j=0;j<3;j++)
if(A[i][j]==0)
{
A[i][j] = A[i-1][j];
A[i-1][j] = 0;
printf("up\n");
return 0;}
}
else if(change == 'd')
{for(i=0;i<2;i++)
for (j=0;j<3;j++)
if(A[i][j]==0)
{
A[i][j] = A[i+1][j];
A[i+1][j] = 0;
printf("down\n");
return 0; }
}
return 0;
}
int diffup(int A[][3], int B[][3])
{
int temp[3][3], i, j;
for(i=0;i<3;i++) //copying the array A in array temp for testing
for (j=0;j<3;j++)
temp[i][j] = A[i][j];
for(i=1;i<3;i++) // swapping the space in upward direction
for (j=0;j<3;j++) // starting the array from i=1 insures that the condition i=0 avoided
if(A[i][j]==0)
{
temp[i-1][j] = 0;
temp[i][j] = A[i-1][j];
}
return difference(temp, B);
}
int diffdown(int A[][3], int B[][3])
{
int temp[3][3], i, j;
for(i=0;i<3;i++) //copying the array A in array temp for testing
for (j=0;j<3;j++)
temp[i][j] = A[i][j];
for(i=0;i<2;i++) // swapping the space in downward direction
for (j=0;j<3;j++) // ending at i=1 insures that the condition i=0 avoided
if(A[i][j]==0)
{
temp[i+1][j] = 0;
temp[i][j] = A[i+1][j];
}
return difference(temp, B);
}
int diffleft(int A[][3], int B[][3])
{
int temp[3][3], i, j;
for(i=0;i<3;i++) //copying the array A in array temp for testing
for(j=0;j<3;j++)
temp[i][j] = A[i][j];
for(i=0;i<3;i++) // swapping the space in the left direction
for(j=1;j<3;j++)
if(A[i][j]==0)
{
temp[i][j-1] = 0;
temp[i][j] = A[i][j-1];
}
return difference(temp, B);
}
int diffright(int A[][3], int B[][3])
{
int temp[3][3], i, j;
for(i=0;i<3;i++)
for (j=0;j<3;j++)
temp[i][j] = A[i][j];
for(i=0;i<3;i++)
for (j=0;j<2;j++)
if(A[i][j]==0)
{
temp[i][j+1] = 0;
temp[i][j] = A[i][j+1];
}
return difference(temp, B);
}
int minimum (int a, int b, int c , int d)
{
int min = a;
if(b<min)
min= b;
if(c<min)
min = c;
if(d<min)
min = d;
return min;
}
Result: The code is successfully implemented and the output is shown here.

Output:
FOR LEAST COST SEARCH:

Algorithm:

STEPS INVOLVED IN THIS PROCEDURE ARE AS FOLLOWS:

STEP 0: Generate cost matrix C [for the given graph g]

STEP 1: [ROW REDUCTION]

For all rows do step 2

STEP: 2 Find least cost in a row and negate it with rest of the

elements.

STEP 3: [COLUMN REDUCTION]


Use cost matrix- Row reduced one for all columns do STEP 4.

STEP 4: Find least cost in a column and negate it with rest of the elements.

STEP 5: Preserve cost matrix C [which row reduced first and then column reduced]
for the i th time.

STEP 6: Enlist all edges (i, j) having cost = 0.

STEP 7: Calculate effective cost of the edges.  (i, j)=least cost in the i th row excluding (i,
j) + least cost in the j th column excluding (i, j).

STEP 8: Compare all effective cost and pick up the largest l. If two or more have same cost
then arbitrarily choose any one among them.

STEP 9: Delete (i, j) means delete ith row and jth column change (j, i) value to infinity. (Used to
avoid infinite loop formation) If (i,j) not present, leave it.

STEP 10: Repeat step 1 to step 9 until the resultant cost matrix having order of 2*2 and
reduce it. (Both R.R and C.C)

STEP 11: Use preserved cost matrix Cn, Cn-1… C1

Choose an edge [i, j] having value =0, at the first time for a preserved matrix and
leave that matrix.

STEP 12: Use result obtained in Step 11 to generate a complete tour.


Code:

#include <bits/stdc++.h>
using namespace std;
#define N 5
struct Edge {
int source, dest, weight;
};
class Graph {
public:
vector<int>adjList[3*N];
Graph(vector<Edge> edges, int x)
{
for (unsigned i = 0; i <edges.size(); i++)
{
int v = edges[i].source;
int u = edges[i].dest;
int weight = edges[i].weight;
if (weight == 3*x)
{
adjList[v].push_back(v + N);
adjList[v + N].push_back(v + 2 * N);
adjList[v + 2 * N].push_back(u);
}
else if (weight == 2*x)
{
adjList[v].push_back(v + N);
adjList[v + N].push_back(u);
}
else
adjList[v].push_back(u);
}
}
};
void printPath(vector<int> predecessor, int v, int& cost)
{
if (v < 0)
return;
printPath(predecessor, predecessor[v], cost);
cost++;
if (v < N)
cout<< v << " ";
}
void BFS(Graph const& graph, int source, intdest)
{
vector<bool> discovered(3*N, false);
discovered[source] = true;
vector<int> predecessor(3*N, -1);
queue<int> q;
q.push(source);
while (!q.empty())
{
intcurr = q.front();
q.pop();
if (curr == dest)
{
int cost = -1;
cout<< "Least cost path between " << source << " and " <<
dest<< " is "; printPath(predecessor, dest, cost);
cout<< "having cost " << cost;
}
for (int v : graph.adjList[curr])
{
if (!discovered[v])
{
discovered[v] = true;
q.push(v);
predecessor[v] = curr;
}
}
}
}
int main()
{
int x = 1;
// vector of graph edges as per above diagram
vector<Edge> edges =
{
{0, 1, 3*x}, {0, 4, 1*x}, {1, 2, 1*x}, {1, 3, 3*x},
{1, 4, 1*x}, {4, 2, 2*x}, {4, 3, 1*x}
};
// create a graph from edges
Graph graph(edges, x);
// given source and destination vertex
int source = 0, dest = 2;
// Do BFS traversal from given source
BFS(graph, source, dest);
return 0;
}
OUTPUT :
Discussion:
The idea of Best First Search is to use an evaluation function to decide which adjacent is
most promising and then explore.

Best First Search falls under the category of Heuristic Search or Informed Search.

The Least Cost Method is another method used to obtain the initial feasible solution for the
transportation problem. The Least Cost Method is considered to produce more optimal
results

Findings&Learnings: Through the implementation of above algorithm we got to learn


about the artificial intelligence technique to implement best first search and least cost
search.

S-ar putea să vă placă și