Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Depth-first search

I am trying to implement DFS in JavaScript but I am having a little problem. Here is my Algorithm class:

"use strict";

define([], function () {

    return function () {

        var that = this;

        this.search = function (searchFor, node) {
            if (searchFor === node.getValue()) {
                return node;
            }
            var i, children = node.getChildren(), child, found;
            for (i = 0; i < children.length; i += 1) {
                child = children[i];
                found = that.search(searchFor, child);
                if (found) {
                    return found;
                }
            }
        };

    };

});

My Node class which represents a single node in the graph:

"use strict";

define([], function () {

    return function (theValue) {
        var value = theValue,
            children = [];

        this.addChild = function (theChild) {
            children.push(theChild);
        };

        this.hasChildren = function () {
            return children.length > 0;
        };

        this.getChildren = function () {
            return children;
        };

        this.getValue = function () {
            return value;
        };
    };

});

I create a tree like this:

enter image description here

"use strict";

define(["DFS/Node", "DFS/Algorithm"], function (Node, Algorithm) {

    return function () {

        this.run = function () {
            var node1 = new Node(1),
                node2 = new Node(2),
                node3 = new Node(3),
                node4 = new Node(4),
                node5 = new Node(5),
                node6 = new Node(6),
                node7 = new Node(7),
                node8 = new Node(8),
                node9 = new Node(9),
                node10 = new Node(10),
                node11 = new Node(11),
                node12 = new Node(12),
                dfs = new Algorithm();

            node1.addChild(node2, node7, node8);
            node2.addChild(node3, node6);
            node3.addChild(node4, node5);
            node8.addChild(node9, node12);
            node9.addChild(node10, node11);

            console.log(dfs.search(5, node1));
        };

    };

});

I see undefined in the logs. I am not sure why my code is stopping at 4 and not continuing.

enter image description here

like image 859
Richard Knop Avatar asked Oct 12 '13 20:10

Richard Knop


People also ask

What is depth first search with example?

Depth First Search (DFS) algorithm traverses a graph in a depthward motion and uses a stack to remember to get the next vertex to start a search, when a dead end occurs in any iteration. As in the example given above, DFS algorithm traverses from S to A to D to G to E to B first, then to F and lastly to C.

What is DFS depth first search?

Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures. The algorithm starts at the root node (selecting some arbitrary node as the root node in the case of a graph) and explores as far as possible along each branch before backtracking.

What is DFS vs BFS?

The full form of BFS is Breadth-First Search, while the full form of DFS is Depth-First Search. BFS uses a queue to keep track of the next location to visit. whereas DFS uses a stack to keep track of the next location to visit. BFS traverses according to tree level, while DFS traverses according to tree depth.


1 Answers

The problem is your addChild() method only expects one parameter, but you are passing in multiple nodes to it.

Change your calling code to:

node1.addChild(node2);
node1.addChild(node7);
node1.addChild(node8);

node2.addChild(node3);
node2.addChild(node6);

node3.addChild(node4);
node3.addChild(node5);

node8.addChild(node9);
node8.addChild(node12);

node9.addChild(node10);
node9.addChild(node11);

Or you can change addChild to accept multiple children (probably want to change the name too):

this.addChildren = function () {
    for (var i = 0; i < arguments.length; i++) {
        children.push(arguments[i]);
    }
};
like image 181
nkron Avatar answered Oct 17 '22 19:10

nkron