Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

js pgrah

// Original answer by Tepinly

function Graph() {
  this.nodes = [];
}

function Node(val) {
  this.val = val;
  this.edges = [];
}

// Add ass many nodes as you want at once
Graph.prototype.addNodes = function (...val) {
  for (i of val) {
    this.nodes.push(new Node(i));
  }
};

// Add edges to each node
Graph.prototype.addEdges = function (val, ...edges) {
  const node = this.nodes.find((i) => i.val === val);
  for (edgeVal of edges) {
    node.edges.push(this.nodes.find((i) => i.val === edgeVal));
  }
};

// BFS algorithm
Graph.prototype.bfs = function (root) {
  let node = this.nodes.find((i) => i.val === root);
  const queue = [root];
  const visited = [];

  while (queue.length > 0) {
    current = queue.pop();

    if (visited.includes(current)) continue;
    visited.push(current);

    node = this.nodes.find((i) => i.val === current);
    console.log(node.val);

    for (i of node.edges) {
      if (!visited.includes(i.val)) queue.unshift(i.val);
    }
  }
};

// DFS algorithm
Graph.prototype.dfs = function (root, visited = []) {
  if (!visited.includes(root)) {
    visited.push(root);
    console.log(root);

    const node = this.nodes.find((i) => i.val === root);
    for (i of node.edges) this.dfs(i.val, visited);
  }
};

const graph = new Graph();

graph.addNodes(0, 1, 2, 3, 4);
graph.addEdges(0, 1, 2, 3);
graph.addEdges(1, 4);
graph.addEdges(4, 3, 1);

// 0 -> 1, 2, 3
// 1 -> 4
// 4 -> 3, 1

graph.dfs(0);

// DFS: 0, 1, 4, 3, 2
// BFS: 0, 1, 2, 3, 4
Comment

PREVIOUS NEXT
Code Example
Javascript :: get top n objects from list node js 
Javascript :: @react-navigation/native unmount inactive 
Javascript :: jquery create html element 
Javascript :: js how to know if element touch border 
Javascript :: external button to fire dropzone.js 
Javascript :: moment between exclusivity 
Javascript :: sendgrid bulk hide each other on the email 
Javascript :: Iterate with JavaScript While Loops 
Javascript :: jest regex jsx tsx js ts 
Javascript :: tailwind css not working with react 
Javascript :: remove tr having delete icon inside jquery 
Javascript :: react material modal custom backdrop 
Javascript :: electron app to exe 
Javascript :: fill all field of object in js 
Javascript :: - Root composer.json requires tymon/jwt-auth ^0.5.12 - satisfiable by tymon/jwt-auth[0.5.12]. 
Javascript :: send form data with file upload using ajax 
Javascript :: react slick slider duplicate items when infinite true #1623 
Javascript :: Codewars Century From Year 
Javascript :: array notation in javascript 
Javascript :: javascript get object from array where property equals 
Javascript :: curl accept json 
Javascript :: add download buttons in datatable 
Javascript :: discord js sending a message to a specific channel 
Javascript :: router link active in vue.js 
Javascript :: how to check if element is in viewport 
Javascript :: js loop over object 
Javascript :: js cookie 
Javascript :: appTsConfig.compilerOptions[option] = value; 
Javascript :: drupal 8 node has field 
Javascript :: js for object 
ADD CONTENT
Topic
Content
Source link
Name
1+9 =