Professional Documents
Culture Documents
LAB Manual
PART A
Experiment No.02
A.1 Aim: To Implement uninformed search methods using C or Java.
A.2 Prerequisite: Data Structure, Searching Techniques
A.3 Outcome:
After successful completion of this experiment students will be able to
● Solve the problems like water jug, tic-tac-toe using uninformed search
methods like DFS/BFS.
Tools Required: C /Java
A.4 Theory:
A problem determines the graph and the goal but not which path to select from the frontier.
This is the job of a search strategy. A search strategy specifies which paths are selected from
the frontier. Different strategies are obtained by modifying how the selection of paths in the
frontier is implemented.
The uninformed search strategies that do not take into account the location of the goal.
Intuitively, these algorithms ignore where they are going until they find a goal and report
success.
1. Depth-First Search
2. Breadth-First Search
3. Lowest-Cost-First Search
1. Depth-First Search: The first strategy is depth-first search. In depth-first search, the
frontier acts like a last-in first-out stack. The elements are added to the stack one at a
time. The one selected and taken off the frontier at any time is the last element that was
added.
Algorithm:
DEPTH(AdMax): Depth first search in state space
1. Init lists OPEN ← {Si}, CLOSED ← {}
2. if OPEN = {}
then return FAIL
3. Remove first node S from OPEN and insert it into CLOSED
3’. if Ad(S) = AdMaxthen repeat from 2
4. Expand node S
4.1. Generate all successors Sj of node S
4.2. for each succesorSj of S do
4.2.1. Set link Sj→ S
4.2.2. ifSj is final state
then
i. Solution is (Sj,.., Si)
ii. return SUCCESS
4.2.3. Insert Sj in OPEN, at the beginning
5. repeat from 2
end.
Example:
2. Breadth-First Search : In breadth-first search the frontier is implemented as a FIFO
(first-in, first-out) queue. Thus, the path that is selected from the frontier is the one that
was added earliest. This approach implies that the paths from the start node are generated
in order of the number of arcs in the path. One of the paths with the fewest arcs is selected
at each stage.
Algorithm BREADTH: Breadth first search in state space
1. Init lists OPEN ← {Si}, CLOSED ← {}
2. if OPEN = {}
then return FAIL
3. Remove first node S from OPEN and insert it in CLOSED
4. Expand node S
4.1. Generate all direct successors Sj of node S
4.2. for each successor Sj of S do
4.2.1. Make link Sj→ S
4.2.2. ifSj is final state
then
i. Solution is (Sj, S, .., Si)
ii. return SUCCESS
4.2.3. Insert Sj in OPEN, at the end
5. repeat from 2
end.
Example:
PART B
(Students must submit the soft copy as per following segments within two hours of the
practical. The soft copy must be uploaded on the Blackboard or emailed to the concerned lab
in charge faculties at the end of the practical in case the there is no Black board access
available)
#include<stdio.h>
#include<conio.h>
int a[20][20],q[20],visited[20],n,i,j,f=0,r=-1;
void bfs(int v) {
for (i=1;i<=n;i++)
q[++r]=i;
if(f<=r) {
visited[q[f]]=1;
bfs(q[f++]);
void main() {
int v;
scanf("%d",&n);
for (i=1;i<=n;i++) {
q[i]=0;
visited[i]=0;
}
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
scanf("%d",&a[i][j]);
scanf("%d",&v);
bfs(v);
for (i=1;i<=n;i++)
if(visited[i])
printf("%d\t",i); else
getch();
}
DEPTH FIRST SEARCH
#include<stdio.h>
#include<stdlib.h>
int vertex;
}node;
node *G[20];
int visited[20];
int n;
void read_graph();
void insert(int,int);
void DFS(int);
void main()
int i;
read_graph();
//initialised visited to 0
for(i=0;i<n;i++)
visited[i]=0;
DFS(0);
void DFS(int i)
node *p;
printf("\n%d",i);
p=G[i];
visited[i]=1;
while(p!=NULL)
i=p->vertex;
if(!visited[i])
DFS(i);
p=p->next;
void read_graph()
{
int i,vi,vj,no_of_edges;
scanf("%d",&n);
for(i=0;i<n;i++)
G[i]=NULL;
scanf("%d",&no_of_edges);
for(i=0;i<no_of_edges;i++)
printf("Enter an edge(u,v):");
scanf("%d%d",&vi,&vj);
insert(vi,vj);
node *p,*q;
q=(node*)malloc(sizeof(node));
q->vertex=vj;
q->next=NULL;
if(G[vi]==NULL)
G[vi]=q;
else
p=G[vi];
while(p->next!=NULL)
p=p->next;
p->next=q;
}
B.2 Observations and learning:
BFS stands for Breadth First Search is a vertex based technique for finding a shortest path in
graph. It uses a Queue data structure which follows first in first out. In BFS, one vertex is
selected at a time when it is visited and marked, then its adjacent vertices are visited and
stored in the queue. It is slower than DFS.
B.3 Conclusion:
The results in this thesis on methodology performance are analogous to the associated results
in search theory. All three search algorithms are complete for a static search space. The
differences appear when the search space is dynamic. A breadth-first search algorithm may
miss a solution. Both a depth-first search algorithm and a best-first search algorithm will find
a solution in a dynamic search space. In some cases, the best-first search algorithm will find a
solution in less than or equal time to the other two methodologies. The worst case
performance of all three search algorithms are the same. On average, the breadth-first search
algorithm will find a solution in O(N). On average, the depth-first search algorithm will find
a solution in O(N). On average, the best-first search algorithm will find a solution is an
order-of-magnitude less than N.
ans: A. 0 1 2 4 3 5
Explanation: A correct sequence of DFS traversal is 0 1 2 4 3 5 There is no edge between 2
and 3 and also 5 which is connected to 2 is unvisited.
Q2) Given a rooted tree, one desires to find the shortest path from the root to a given node v.
Which algorithm would one use to find this shortest path?
A.DFS
B. BFS
C. Either BFS or DFS
ans: C. Either BFS or DFS
Explanation: A tree has a unique path between any two pairs of nodes. Any traversal
strategy would give us the path (which is the shortest path)
Q3) Consider a graph G. Let T be a BFS tree with root r. Let d(u,v) denote the length of the
shortest path between the nodes u and v. If v is visited before u in the breadth first search
traversal, which of the following statements is true ?
A. d(r,v) > d(r,u)
B. d(r,v) = d(r,u)
C. d(r,v) < d(r,u)
D. insufficient information to comment on d(r,v) and d(r,u)