Giter Site home page Giter Site logo

Comments (4)

MJinH avatar MJinH commented on August 31, 2024

Code for BFS

const bfs = (graph, node) => {
  let visited = [];
  let nodes= [];

  nodes.push(node); 

  while (nodes.length !== 0) { 
    const newNode = nodes.shift(); 
    if (!visited.includes(newNode )) {
      visited.push(newNode ); 
      nodes= [...nodes, ...graph[newNode ]];
    }
  }
  return visited;
};

Code for DFS

const dfs = (graph, node) => {
  let stack= [];
  let queue= [];

  stack.push(node);

  while (stack.length !== 0) {
    const newNode = stack.pop();
    if (!queue.includes(newNode )) {
      queue.push(newNode );
      stack= [...stack, ...graph[newNode]];
    }
  }
  return queue;
};

from age-viewer.

Nimra-1234 avatar Nimra-1234 commented on August 31, 2024

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

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
image

from age-viewer.

Nimra-1234 avatar Nimra-1234 commented on August 31, 2024

@aked21 I guess this will help you. If it is so then you can close this issue.

#44 (comment)

from age-viewer.

shinhanbyeol avatar shinhanbyeol commented on August 31, 2024

Thank you @Nimra-1234 @MJinH

from age-viewer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.