Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfd06589

Browse files
committed
Validate the parameters in dfs
1 parentb1f46ec commitfd06589

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

‎src/graphs/searching/dfs.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,47 @@ var depthFirstSearch = (function () {
6464
}
6565
}
6666

67+
/**
68+
* Validates the params
69+
*
70+
*@param {array} graph A matrix representation of the graph
71+
*@param {array} source The source node
72+
*@param {array} destination The destination node
73+
*/
74+
functionvalidateParams(graph,source,destination){
75+
if(!graph){
76+
thrownewError('The graph should be represented as a matrix');
77+
}
78+
if(graph[0]===undefined){
79+
thrownewError('The graph should be represented as '+
80+
'a matrix, with size at least 1x1');
81+
}
82+
varwidth=graph[0].length;
83+
for(vari=1;i<graph.length;i+=1){
84+
if(graph[i].length!==width){
85+
thrownewError('The graph should be represented as a matrix');
86+
}
87+
}
88+
source.concat(destination).filter(function(c,i){
89+
if(c<0){
90+
thrownewError('The source and destination coordinates '+
91+
'should be above zero');
92+
}
93+
if(i%2===0){
94+
if(c>=graph.length){
95+
thrownewError('The source and destination coordinates '+
96+
'should not be above graph\'s size');
97+
}
98+
}else{
99+
if(c>=graph[0].length){
100+
thrownewError('The source and destination coordinates '+
101+
'should not be above graph\'s size');
102+
}
103+
}
104+
});
105+
returntrue;
106+
}
107+
67108
/**
68109
* Finds whether there's a path between a given start node
69110
* to given destination
@@ -76,6 +117,7 @@ var depthFirstSearch = (function () {
76117
* whether there's a path between the nodes
77118
*/
78119
returnfunction(graph,source,destination){
120+
validateParams(graph,source,destination);
79121
init(graph,destination);
80122
returndfs(source);
81123
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp