Nimra-1234 commented on issue #44:
URL: https://github.com/apache/age-viewer/issues/44#issuecomment-1345502362

   Code for DFS: 
   class Graph_Implementation
   {
       constructor(v)
       {
           this.V = v;
           this.adj = new Array(v);
           for(let i = 0; i < v; i++)
               this.adj[i] = [];
       }
       addedges(v, w)
       {
           this.adj[v].push(w);
       }
   
       DFSUtil(v, visited)
       {
           visited[v] = true;
           document.write(v + " ");
           for(let i of this.adj[v].values())
           {
               let n = i
               if (!visited[n])
                   this.DFSUtil(n, visited);
           }
       }
       DFS(v)
       {
           let visited = new Array(this.V);
           for(let i = 0; i < this.V; i++)
               visited[i] = false;
           this.DFSUtil(v, visited);
       }
   }
   
   g = new Graph_Implementation(4);
     
   g.addedges(0, 1);
   g.addedges(0, 2);
   g.addedges(1, 2);
   g.addedges(2, 0);
   g.addedges(2, 3);
   g.addedges(3, 3);
    
   document.write("Following is Depth First Traversal " +
                  " ");
   g.DFS(2);
   
   Output:
   
![image](https://user-images.githubusercontent.com/72203032/206895981-c23b7c70-b1fb-4897-af6c-be49f8523ffc.png)
   
   BFS Code:
   class Graph_Implmentation
   {
       constructor(v)
       {
           this.V = v;
           this.adj = new Array(v);
           for(let i = 0; i < v; i++)
               this.adj[i] = [];
       }
       addedges(v, w)
       {
           this.adj[v].push(w);
       }
       BFS(s)
       {
           let visited = new Array(this.V);
           for(let i = 0; i < this.V; i++)
               visited[i] = false;
           let queue=[];
           visited[s]=true;
           queue.push(s);
           while(queue.length>0)
           {
               s = queue[0];
               document.write(s+" ");
               queue.shift();
                           this.adj[s].forEach((adjacent,i) => {
                   if(!visited[adjacent])
                   {
                       visited[adjacent]=true;
                       queue.push(adjacent);
                   }
               });
           }
       }
   }
   g = new Graph_Implmentation(11);
   g.addedges(1,2);
      g.addedges(1,4);
      g.addedges(2,3);
      g.addedges(2,6);
      g.addedges(3,5);
      g.addedges(3,7);
      g.addedges(4,6);
      g.addedges(5,2);
      g.addedges(5,6);
      g.addedges(6,1);
      g.addedges(7,5);
   
   console.log("Following is Breadth First Traversal " + " ");
    
   g.BFS(1);
   
    Output:
   
![image](https://user-images.githubusercontent.com/72203032/206896053-8831d948-311c-45af-a8f3-2ed7fcf938d1.png)
   
![image](https://user-images.githubusercontent.com/72203032/206896058-a528d88c-fde5-4a71-a1f4-a6fcf265b147.png)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to