Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

dfs javascript

const dfs = (node) => { 
     stack.push(node);
     while (!stack.isEmpty()) {
        node = stack.pop();
        if (visited[node] == false) {
           visited[node] = true;
           console.log(`we visited ${node}`)
           for (let j = 0; j < graphAdj[node].length; j++) {
              if (graphAdj[node][j] === 1){
                  stack.push(j);
               }
            }
       }
     }
}
Comment

JS DFS

class Graph {
    constructor() {
        this.vertices = [];
        this.adjacent = {};
        this.edges = 0;
    }

    addVertex(v) {
        this.vertices.push(v);
        this.adjacent[v] = [];
    }

    addEdge(v, w) {
        this.adjacent[v].push(w);
        this.adjacent[w].push(v);
        this.edges++;
    }


    bfs(goal, root = this.vertices[0]) {
        let adj = this.adjacent;

        const queue = [];
        queue.push(root);

        const discovered = [];
        discovered[root] = true;

        const edges = [];
        edges[root] = 0;

        const predecessors = [];
        predecessors[root] = null;

        const buildPath = (goal, root, predecessors) => {
            const stack = [];
            stack.push(goal);

            let u = predecessors[goal];

            while(u != root) {
                stack.push(u);
                u = predecessors[u];
            }

            stack.push(root);

            let path = stack.reverse().join('-');

            return path;
        }
    

        while(queue.length) {
            let v = queue.shift();

            if (v === goal) {
                return { 
                    distance: edges[goal],
                    path: buildPath(goal, root, predecessors)
                };
            }

            for (let i = 0; i < adj[v].length; i++) {
                if (!discovered[adj[v][i]]) {
                    discovered[adj[v][i]] = true;
                    queue.push(adj[v][i]);
                    edges[adj[v][i]] = edges[v] + 1;
                    predecessors[adj[v][i]] = v;
                }
            }
        }

        return false;
    }

    dfs(goal, v = this.vertices[0], discovered = []) {
        let adj = this.adjacent;

        discovered[v] = true;

        for (let i = 0; i < adj[v].length; i++){
            let w = adj[v][i];

            if (!discovered[w]) {
                this.dfs(goal, w, discovered);
            }
        }

        return discovered[goal] || false;
    }
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: Exporting And Importing From A Module 
Javascript :: Modules: Remember All Strings Will Now Have a Property After You Use Require 
Javascript :: Remove # id From URL When Clicked On Href Link 
Javascript :: "Uncaught (in promise) TypeError: dispatch is not a function" 
Javascript :: javascript password kodachi 
Javascript :: phaser max size of group or pool 
Javascript :: phaser wrap sprite 
Javascript :: inspect vuex store 
Javascript :: Creating Genesis Block for blockchain 
Javascript :: This Refers To The Window Object Here 
Javascript :: Inside Vs Static Methods 
Javascript :: change button text dynamically angular 6 
Javascript :: repate element every 2 seconds 
Javascript :: parseint javascript online 
Javascript :: success res node.js 
Javascript :: what does the symbol function do in javascript 
Javascript :: jquery properties 
Javascript :: withrouter in react-router v6 
Javascript :: Backbone View Template 
Javascript :: var maxNum = function(arr) {}; 
Javascript :: Update react final form field 
Javascript :: country select dropdown javascript 
Javascript :: How to Check if an Item is in an Array in JavaScript Using Array.includes() Starting From a Specified Index 
Javascript :: how to decrypt md5 hash 
Javascript :: js function to print word starts with vowels of given string 
Javascript :: javascript oop 
Javascript :: regex in javascript 
Javascript :: moment js remove seconds 
Javascript :: javascript timer countdown with seconds 59 
Javascript :: get latest input by .each jquery 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =