Skip to content

Commit 1797907

Browse files
tayllanfelipernb
authored andcommitted
Depth First Search
1 parent 531aaae commit 1797907

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* Copyright (C) 2014 Tayllan Búrigo
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"], to
6+
* deal in the Software without restriction, including without limitation the
7+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8+
* sell copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20+
* IN THE SOFTWARE.
21+
*/
22+
'use strict';
23+
24+
var time = 0,
25+
visitedNodes = {},
26+
finishingTimes = {};
27+
28+
/**
29+
* Depth First Search for all the vertices in the graph
30+
* Worst Case Complexity: O(V * E)
31+
*
32+
* @param {Object}
33+
* @return {Object} representing the order in which the
34+
* vertices are visited
35+
*/
36+
var dfsAdjacencyListStart = function (graph) {
37+
graph.vertices.forEach(function (v) {
38+
if (visitedNodes[v] !== true) {
39+
dfsAdjacencyList(graph, v);
40+
}
41+
});
42+
43+
return finishingTimes;
44+
};
45+
46+
/**
47+
* Depth First Search for the vertices reachable from 'startNode'
48+
* Worst Case Complexity: O(V * E)
49+
*
50+
* @param {Object}
51+
* @param {Object}
52+
* @return {Object}
53+
*/
54+
var dfsAdjacencyList = function (graph, startNode) {
55+
visitedNodes[startNode] = true;
56+
57+
graph.neighbors(startNode).forEach(function (v) {
58+
if (visitedNodes[v] !== true) {
59+
dfsAdjacencyList(graph, v);
60+
}
61+
});
62+
63+
finishingTimes[startNode] = time++;
64+
65+
return finishingTimes;
66+
};
67+
68+
var depthFirstSearch = {
69+
dfsAdjacencyList: dfsAdjacencyList,
70+
dfsAdjacencyListStart: dfsAdjacencyListStart
71+
};
72+
73+
module.exports = depthFirstSearch;

main.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ var lib = {
2828
SPFA: require('./algorithms/graph/SPFA'),
2929
bellmanFord: require('./algorithms/graph/bellman_ford'),
3030
eulerPath: require('./algorithms/graph/euler_path'),
31+
depthFirstSearch: require('./algorithms/graph/depth_first_search')
3132
},
3233
Math: {
3334
fibonacci: require('./algorithms/math/fibonacci'),
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* Copyright (C) 2014 Tayllan Búrigo
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"], to
6+
* deal in the Software without restriction, including without limitation the
7+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8+
* sell copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20+
* IN THE SOFTWARE.
21+
*/
22+
'use strict';
23+
24+
var depthFirstSearch = require('../../../algorithms/graph/depth_first_search'),
25+
Graph = require('../../../data_structures/graph'),
26+
assert = require('assert'),
27+
graph = new Graph(true);
28+
29+
graph.addEdge('one', 'three');
30+
graph.addEdge('one', 'four');
31+
graph.addEdge('four', 'two');
32+
graph.addEdge('two', 'one');
33+
graph.addEdge('three', 'one');
34+
graph.addEdge('five', 'six');
35+
36+
describe('Depth First Search Algorithm', function () {
37+
it('should visit only the nodes reachable from the node {one} (inclusive)',
38+
function () {
39+
var finishingTimes = depthFirstSearch.dfsAdjacencyList(graph, 'one');
40+
41+
assert.equal(finishingTimes.five, undefined);
42+
assert.equal(finishingTimes.six, undefined);
43+
44+
assert.equal(finishingTimes.one, 3);
45+
assert.equal(finishingTimes.two, 1);
46+
assert.equal(finishingTimes.three, 0);
47+
assert.equal(finishingTimes.four, 2);
48+
}
49+
);
50+
51+
it('should visit all the nodes in the graph',
52+
function () {
53+
var finishingTimes = depthFirstSearch.dfsAdjacencyListStart(graph);
54+
55+
assert.equal(finishingTimes.one, 3);
56+
assert.equal(finishingTimes.two, 1);
57+
assert.equal(finishingTimes.three, 0);
58+
assert.equal(finishingTimes.four, 2);
59+
assert.equal(finishingTimes.five, 5);
60+
assert.equal(finishingTimes.six, 4);
61+
}
62+
);
63+
});
64+

0 commit comments

Comments
 (0)