You are on page 1of 6

1 Artificial Intelligence Lab 5

MT 486L Artificial Intelligence

Lab 5

Name Hammad Nadeem

Registration 191034
Number

Class BEMTS-F19- ‘A’ (7th Sem)

Instructor’s MTS-Engr. M. Naqash Ahmed


Name
2 Artificial Intelligence Lab 5

Lab 5: Getting familiar with depth-first algorithm.


Objectives
• Getting familiar with MATLAB and its workspace.
• Getting familiar with depth-first search algorithm.
• Using Search algorithm to solve search problem.
Introduction
Depth-First Search Algorithm:
Depth-first search algorithm traverses a graph in a way that it covers depth. Depth First Search is
a graph traversal algorithm that transverse across graph starting from root node and exploring
nodes by going in depth of any node linked to root node. Then it selects adjacent node linked to
root node and explore all unexplored nodes down to depth. It is a recursive algorithm to search
all the vertices of a tree or graph data structure. DFS puts every vertex of the graph into two
categories - visited and non-visited. The algorithm starts at the root (top) node of a tree and goes
as far as it can down a given branch (path), then backtracks until it finds an unexplored path, and
then explores it.

DFS uses a stack data structure for its implementation. For further explanation, we can see the
graphical traversing of depth-first search algorithm. It started at node S and explore node A
linked to S. Then instead of going to explore adjacent node H, it explored next node linked to our
node A and then down to D to reach depth of this path.
3 Artificial Intelligence Lab 5

Lab Tasks
Lab Task 1:
Write a code to traverse in 4X4 puzzle from any start point to goal point.
Solution:
Code: Output:

As you can see, I have traversed 0 from our start point in 4th row and 2nd column to our goal
point 1st row and 4th column using from loops to move 3 times up and then 2 times to the right.
Lab Task 2:
Depth-first Example from Internet.
Solution:
Code:
clear all,close all,clc;
%%
fig1=figure(1);
pause(2);
rng(111);
X = randi([0,1],10,9)

X(1,:)=0;
X(:,end)=0;
X(1,1)=5;
X(end,end)=3;
%
%% X2XID
numberOfNodes=length(X(:));
XID=reshape([1:1:numberOfNodes],size(X,2),size(X,1))';
nodeList=[];
for ii=1:1:length(XID(:))
node.ID = ii; %--------
node.depth = 0;
node.parent = [];
node.searched = [false];
[row,col] = find(XID==[ii]);
node.location =[row,col]; %--------
4 Artificial Intelligence Lab 5

node.type=[0];
nodeList=[nodeList,node];
end
%% update the "type" fields of each node in nodeList [start,end,wall,space]
[row,col]=find(X==1);
for ii=1:1:length(row)
nodeList(RowCol2ID(XID,nodeList,row(ii),col(ii))).type=1;
end
[row,col]=find(X==5);
for ii=1:1:length(row)
nodeList(RowCol2ID(XID,nodeList,row(ii),col(ii))).type=5;
end
[row,col]=find(X==3);
for ii=1:1:length(row)
nodeList(RowCol2ID(XID,nodeList,row(ii),col(ii))).type=3;
end
%% initialization
% init-step-1 "define 2 sets called “open� and “closed�"
open=[];
closed=[];
% init-step-2 "update the depth,parent and search fields of S-node"
nodeList(1).depth=0;
nodeList(1).parent=[];
nodeList(1).searched=true;
% init-step-3 "add S to the open set" = "open�Append(S,open)"
open=[nodeList(1),open];
while(true)
% "N=Head(open)"
N=open(1);
% "If GoalTest(N)==True return;"
if N.type==3
disp('SOLVED *****'); break;
else
%% STEP-1 "GenChildren"
NEW=GenChildren(XID,nodeList,N);
%% STEP-2 FilterOut the "searched nodes" and "wall nodes"
[NEW]=FilterOut(XID,nodeList,NEW);
%% STEP-3 "update the parent field of each node in NEW"--------------
for ii=1:1:numel(NEW)
NEW(ii).parent=N;
end
%% STEP-4 "delete N from open"
if numel(open)==1
open=[];
else
open=open(2:end);
end
%% STEP-5 "closed�Append(N,closed);"
closed=[N,closed];
%% STEP-6 "New(i).depth=(N.depth)+1" ---------------------------------
for ii=1:1:numel(NEW)
NEW(ii).depth=NEW(ii).parent.depth+1;
end
%% STEP-7 "New(i).searched=true"-----------------------------
for ii=1:1:numel(NEW)
NEW(ii).searched=true;
end
%% STEP-X "UPDATE nodeList"-----------------------------
for ii=1:1:numel(NEW)
nodeList(NEW(ii).ID)=NEW(ii);
end
%% STEP-8 "open� Append(open ,New);"
% open=[open,NEW]; % BFS
5 Artificial Intelligence Lab 5

open=[NEW,open]; % DFS
%% STEP-9 check if "open is empty"
if ~(numel(open)>=1)
disp('FAILURE to find a path');
break;
end
% PLOT CLOSED SET
noteIDs2Bplotted=[];
for ii=1:1:numel(closed)
noteIDs2Bplotted=[noteIDs2Bplotted,closed(ii).ID];
end
MapPlot(XID,nodeList,noteIDs2Bplotted);
drawnow
% pause(.1);
end
end
%% construct the path
tempNode=N
pathByIDs=[];
while(true)
pathByIDs=[pathByIDs,tempNode.ID]
tempNode=tempNode.parent;
if tempNode.ID==1
break;
end
end
pathByIDs=[1,fliplr(pathByIDs)]
MapPlot(XID,nodeList,pathByIDs);

Output:
Exploring first path down to depth.

Then exploring other path down to depth.


6 Artificial Intelligence Lab 5

Giving final path from start to goal which algorithm found as 2nd path.

Explanation:
In this code, we used depth first search algorithm to solve a 3X3 maze. In this, we have simply
first initialized an initial maze with 0 showing possible movement nodes, 1 showing wall where
we can’t go, 5 as start point and a goal 3. We explored linked 0 notes using depth-first search
algorithm.
Conclusion
In this lab, we have learned how depth-first search algorithm work. We have made code to traverse
0 tile from start point to goal point. We used for loop to traverse up and right. We have seen the
working of depth-first search algorithm through maze example and see how it explore path to the
depths and then move toward adjacent path and again explore down to the depth.

You might also like