@@ -37,13 +37,12 @@ class Graph {
3737 * @param {any } value node's value
3838 */
3939 addVertex ( value ) {
40- if ( this . nodes . has ( value ) ) {
40+ if ( this . nodes . has ( value ) ) {
4141 return this . nodes . get ( value ) ;
42- } else {
43- const vertex = new Node ( value ) ;
44- this . nodes . set ( value , vertex ) ;
45- return vertex ;
4642 }
43+ const vertex = new Node ( value ) ;
44+ this . nodes . set ( value , vertex ) ;
45+ return vertex ;
4746 }
4847
4948 /**
@@ -55,10 +54,8 @@ class Graph {
5554 */
5655 removeVertex ( value ) {
5756 const current = this . nodes . get ( value ) ;
58- if ( current ) {
59- for ( const node of this . nodes . values ( ) ) {
60- node . removeAdjacent ( current ) ;
61- }
57+ if ( current ) {
58+ Array . from ( this . nodes . values ( ) ) . forEach ( node => node . removeAdjacent ( current ) ) ;
6259 }
6360 return this . nodes . delete ( value ) ;
6461 }
@@ -79,7 +76,7 @@ class Graph {
7976
8077 sourceNode . addAdjacent ( destinationNode ) ;
8178
82- if ( this . edgeDirection === Graph . UNDIRECTED ) {
79+ if ( this . edgeDirection === Graph . UNDIRECTED ) {
8380 destinationNode . addAdjacent ( sourceNode ) ;
8481 }
8582
@@ -99,10 +96,10 @@ class Graph {
9996 const sourceNode = this . nodes . get ( source ) ;
10097 const destinationNode = this . nodes . get ( destination ) ;
10198
102- if ( sourceNode && destinationNode ) {
99+ if ( sourceNode && destinationNode ) {
103100 sourceNode . removeAdjacent ( destinationNode ) ;
104101
105- if ( this . edgeDirection === Graph . UNDIRECTED ) {
102+ if ( this . edgeDirection === Graph . UNDIRECTED ) {
106103 destinationNode . removeAdjacent ( sourceNode ) ;
107104 }
108105 }
@@ -119,7 +116,7 @@ class Graph {
119116 const sourceNode = this . nodes . get ( source ) ;
120117 const destinationNode = this . nodes . get ( destination ) ;
121118
122- if ( sourceNode && destinationNode ) {
119+ if ( sourceNode && destinationNode ) {
123120 return sourceNode . isAdjacent ( destinationNode ) ;
124121 }
125122
@@ -131,33 +128,33 @@ class Graph {
131128 * Use a stack to visit nodes (LIFO)
132129 * @param {Node } first node to start the dfs
133130 */
134- * dfs ( first ) {
135- yield * this . _graphSearch ( first , Stack ) ;
131+ * dfs ( first ) {
132+ yield * this . graphSearch ( first , Stack ) ;
136133 }
137134
138135 /**
139136 * Depth-first search
140137 * Use a queue to visit nodes (FIFO)
141138 * @param {Node } first node to start the dfs
142139 */
143- * bfs ( first ) {
144- yield * this . _graphSearch ( first , Queue ) ;
140+ * bfs ( first ) {
141+ yield * this . graphSearch ( first , Queue ) ;
145142 }
146143
147144 /**
148145 * Generic graph search where we can pass a Stack or Queue
149146 * @param {Node } first node to start the search
150147 * @param {Stack|Queue } dsType Stack for DFS or Queue for BFS
151148 */
152- * _graphSearch ( first , dsType = Stack ) {
149+ * graphSearch ( first , dsType = Stack ) {
153150 const visited = new Map ( ) ;
154151 const visitList = new dsType ( ) ;
155152
156153 visitList . add ( first ) ;
157154
158- while ( ! visitList . isEmpty ( ) ) {
155+ while ( ! visitList . isEmpty ( ) ) {
159156 const node = visitList . remove ( ) ;
160- if ( node && ! visited . has ( node ) ) {
157+ if ( node && ! visited . has ( node ) ) {
161158 yield node ;
162159 visited . set ( node ) ;
163160 node . getAdjacents ( ) . forEach ( adj => visitList . add ( adj ) ) ;
@@ -174,10 +171,10 @@ class Graph {
174171 const sourceNode = this . nodes . get ( source ) ;
175172 const destinationNode = this . nodes . get ( destination ) ;
176173
177- if ( sourceNode && destinationNode ) {
174+ if ( sourceNode && destinationNode ) {
178175 const bfsFromFirst = this . bfs ( sourceNode ) ;
179176 for ( const node of bfsFromFirst ) {
180- if ( node === destinationNode ) {
177+ if ( node === destinationNode ) {
181178 return true ;
182179 }
183180 }
@@ -201,15 +198,16 @@ class Graph {
201198
202199 path . set ( sourceNode ) ;
203200
201+ // eslint-disable-next-line no-iterator
204202 for ( const node of sourceNode . getAdjacents ( ) ) {
205- if ( node === destinationNode ) {
203+ if ( node === destinationNode ) {
206204 path . set ( destinationNode ) ;
207205 return Array . from ( path . keys ( ) ) ;
208206 }
209207
210- if ( ! path . has ( node ) ) {
208+ if ( ! path . has ( node ) ) {
211209 const newPath = this . findPath ( node . value , destination , path ) ;
212- if ( newPath . length ) {
210+ if ( newPath . length ) {
213211 return newPath ;
214212 }
215213 }
@@ -222,4 +220,4 @@ class Graph {
222220Graph . UNDIRECTED = Symbol ( 'directed graph' ) ; // one-way edges
223221Graph . DIRECTED = Symbol ( 'undirected graph' ) ; // two-ways edges
224222
225- module . exports = Graph ;
223+ module . exports = Graph ;
0 commit comments