From b1f5c3f8d6671b4331cef1dcebdf63b7a43a3a2b Mon Sep 17 00:00:00 2001 From: priyanka Date: Wed, 24 Jun 2015 15:03:17 +0530 Subject: initial commit / add all books --- 37/CH8/EX8.7/s7.sci | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100755 37/CH8/EX8.7/s7.sci (limited to '37/CH8/EX8.7') diff --git a/37/CH8/EX8.7/s7.sci b/37/CH8/EX8.7/s7.sci new file mode 100755 index 000000000..f9a788d2f --- /dev/null +++ b/37/CH8/EX8.7/s7.sci @@ -0,0 +1,61 @@ +////BFS Traversal +funcprot(0) +function[q2]=push(ele,q1) + if(q1.rear==q1.front) + q1.a=ele; + q1.rear=q1.rear+1; + else + q1.a=[q1.a(:,:) ele]; + q1.rear=q1.rear+1; + end + q2=q1; +endfunction +function[ele,q2]=pop(q1) + ele=-1; + q2=0; + if(q1.rear==q1.front) + return; + else + ele=q1.a(q1.rear-q1.front); + q1.front=q1.front+1; + i=1; + a=q1.a(1); + for i=2:(q1.rear-q1.front) + a=[a(:,:) q1.a(i)]; + end + q1.a=a; + end + q2=q1; +endfunction + +function[]=Bfs(adj,n); + i=1,j=1; + colour=[]; + for i=1:n + for j=1:n + colour=[colour(:,:) 0]; + end + end + disp("The BFS Traversal is"); +bfs(adj,colour,1,n); +endfunction +function[]=bfs(adj,colour,s,n) + colour(s)=1; + q=struct('rear',0,'front',0,'a',0); + q=push(s,q); + while((q.rear)-(q.front)>0) + [u,q]=pop(q); + disp(u," "); + for i=1:n + if(adj((u-1)*n+i)&(colour(i)==0)) + colour(i)=1; + q=push(i,q); + end + end + colour(u)=2; + end +endfunction +//Calling Routine: +n=4; +adj=[0 1 1 0 0 0 0 1 0 0 0 1 0 0 0 0] +Bfs(adj,n) \ No newline at end of file -- cgit