Skip to content

Commit b1f46ec

Browse files
committed
Fix the indentation in dfs
1 parent c392297 commit b1f46ec

1 file changed

Lines changed: 63 additions & 53 deletions

File tree

‎src/graphs/searching/dfs.js‎

Lines changed: 63 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
'use strict';
2+
/**
3+
* BUGGY DON'T USE
4+
*/
5+
16
/* * * * * * * * * * * * * * * * * *
27
3-
Sample graph
4-
8+
Sample graph
9+
510
var graph = [[1,0,1,0,0,0],
611
[0,1,0,1,0,0],
712
[1,0,1,0,1,0],
@@ -14,60 +19,65 @@ var graph = [[1,0,1,0,0,0],
1419
* Depth-first search algorithm for matrix representation of graph.
1520
* The algorithm finds whether there's a path between two given nodes.
1621
*/
17-
var depthFirstSearch = function () {
22+
var depthFirstSearch = (function () {
1823

19-
var visted = [],
20-
target,
21-
graph;
24+
var visited = [],
25+
target,
26+
graph;
2227

23-
/**
24-
* Returns whether the destination could be reached
25-
* from given node
26-
*
27-
* @private
28-
* @param {number} current Current node
29-
* @returns {boolean} True/false depending whether
30-
* the destination can be reached from the current node
31-
*/
32-
function dfs(current) {
33-
if (current === target) return true;
34-
visited[current] = true;
35-
for (var i = 0; i < graph.length; i += 1) {
36-
if (graph[current][i] === 1 &&
37-
!visited[i]) {
38-
return depthFirstSearch(i);
39-
}
40-
}
41-
return false;
28+
/**
29+
* Returns whether the destination could be reached
30+
* from given node
31+
*
32+
* @private
33+
* @param {number} current Current node
34+
* @returns {boolean} True/false depending whether
35+
* the destination can be reached from the current node
36+
*/
37+
function dfs(current) {
38+
if (current === target) {
39+
return true;
40+
}
41+
visited[current] = true;
42+
for (var i = 0; i < graph.length; i += 1) {
43+
if (graph[current][i] === 1 &&
44+
!visited[i]) {
45+
return depthFirstSearch(i);
46+
}
4247
}
48+
return false;
49+
}
4350

44-
/**
45-
* Initializes the algorithm
46-
*
47-
* @private
48-
* @param {array} inputGraph The input matrix of the graph
49-
* @param {number} destination The destination
50-
*/
51-
function init(inputGraph, destination) {
52-
graph = inputGraph;
53-
target = destination;
54-
visited = [];
55-
for (var i = 0; i < graph.length; i += 1)
56-
visited[i] = false;
51+
/**
52+
* Initializes the algorithm
53+
*
54+
* @private
55+
* @param {array} inputGraph The input matrix of the graph
56+
* @param {number} destination The destination
57+
*/
58+
function init(inputGraph, destination) {
59+
graph = inputGraph;
60+
target = destination;
61+
visited = [];
62+
for (var i = 0; i < graph.length; i += 1) {
63+
visited[i] = false;
5764
}
65+
}
66+
67+
/**
68+
* Finds whether there's a path between a given start node
69+
* to given destination
70+
*
71+
* @public
72+
* @param {array} graph A matrix representation of the graph
73+
* @param {number} source The source node
74+
* @param {number} destination The destination node
75+
* @returns {boolean} true/false depending
76+
* whether there's a path between the nodes
77+
*/
78+
return function (graph, source, destination) {
79+
init(graph, destination);
80+
return dfs(source);
81+
};
5882

59-
/**
60-
* Finds whether there's a path between a given start node
61-
* to given destination
62-
*
63-
* @public
64-
* @param {array} graph A matrix representation of the graph
65-
* @param {number} source The source node
66-
* @param {number} destination The destination node
67-
* @returns {boolean} true/false depending whether there's a path between the nodes
68-
*/
69-
return function (graph, source, destination) {
70-
init(graph, destination);
71-
return dfs(source);
72-
};
73-
}();
83+
}());

0 commit comments

Comments
 (0)