@@ -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+ function validateParams ( graph , source , destination ) {
75+ if ( ! graph ) {
76+ throw new Error ( 'The graph should be represented as a matrix' ) ;
77+ }
78+ if ( graph [ 0 ] === undefined ) {
79+ throw new Error ( 'The graph should be represented as ' +
80+ 'a matrix, with size at least 1x1' ) ;
81+ }
82+ var width = graph [ 0 ] . length ;
83+ for ( var i = 1 ; i < graph . length ; i += 1 ) {
84+ if ( graph [ i ] . length !== width ) {
85+ throw new Error ( 'The graph should be represented as a matrix' ) ;
86+ }
87+ }
88+ source . concat ( destination ) . filter ( function ( c , i ) {
89+ if ( c < 0 ) {
90+ throw new Error ( 'The source and destination coordinates ' +
91+ 'should be above zero' ) ;
92+ }
93+ if ( i % 2 === 0 ) {
94+ if ( c >= graph . length ) {
95+ throw new Error ( 'The source and destination coordinates ' +
96+ 'should not be above graph\'s size' ) ;
97+ }
98+ } else {
99+ if ( c >= graph [ 0 ] . length ) {
100+ throw new Error ( 'The source and destination coordinates ' +
101+ 'should not be above graph\'s size' ) ;
102+ }
103+ }
104+ } ) ;
105+ return true ;
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 return function ( graph , source , destination ) {
120+ validateParams ( graph , source , destination ) ;
79121 init ( graph , destination ) ;
80122 return dfs ( source ) ;
81123 } ;
0 commit comments