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 :: find an element 
Javascript :: append javascript variable to html 
Javascript :: react color picker 
Javascript :: sort list in javascript 
Javascript :: how to upload file in node js 
Javascript :: js array map concat 
Javascript :: fill array with array javascript 
Javascript :: getcontext in javascript 
Javascript :: upload image with react 
Javascript :: ajax get request javascript 
Javascript :: javascript timer countdown with seconds 59 
Javascript :: suitescript dialog box 
Javascript :: how to save data in javascript 
Javascript :: javascript handle updation of copy object 
Javascript :: JavaScript for loop Display Numbers from 1 to 5 
Javascript :: JavaScript, numbers are primitive data types 
Javascript :: javascript WeakMaps Are Not iterable 
Javascript :: javascript Assigning to a getter-only property is not allowed 
Javascript :: js console.log callstack 
Javascript :: json syntax 
Javascript :: test driven development javascript 
Javascript :: javascript equality operator 
Javascript :: phaser place on rectangle shift 
Javascript :: phaser animation random delay 
Javascript :: permissions in chrome extension javascript 
Javascript :: check notification permissopn allow or not 
Javascript :: HDEL in redis 
Javascript :: reduce function javascript 
Javascript :: sort function explained javascript 
Javascript :: react native toggle button with text 
ADD CONTENT
Topic
Content
Source link
Name
3+2 =