File tree Expand file tree Collapse file tree 1 file changed +7
-8
lines changed
Expand file tree Collapse file tree 1 file changed +7
-8
lines changed Original file line number Diff line number Diff line change 11// https://en.wikipedia.org/wiki/Neighbourhood_(graph_theory)
22
3- class Graph {
3+ class Graph {
44 // Generic graph: the algorithm works regardless of direction or weight
5- constructor ( ) {
5+ constructor ( ) {
66 this . edges = [ ]
77 }
88
9- addEdge ( node1 , node2 ) {
9+ addEdge ( node1 , node2 ) {
1010 // Adding edges to the graph
1111 this . edges . push ( {
1212 node1,
1313 node2
1414 } )
1515 }
1616
17- nodeNeighbors ( node ) {
17+ nodeNeighbors ( node ) {
1818 // Returns an array with all of the node neighbors
19- let neighbors = [ ]
19+ const neighbors = [ ]
2020 for ( let i = 0 ; i < this . edges . length ; i ++ ) {
2121 // Checks if they have an edge between them and if the neighbor is not
2222 // already in the neighbors array
2323 if ( this . edges [ i ] . node1 === node && ! ( neighbors . includes ( this . edges [ i ] . node2 ) ) ) {
2424 neighbors . push ( this . edges [ i ] . node2 )
25- }
26- else if ( this . edges [ i ] . node2 === node && ! ( neighbors . includes ( this . edges [ i ] . node1 ) ) ) {
25+ } else if ( this . edges [ i ] . node2 === node && ! ( neighbors . includes ( this . edges [ i ] . node1 ) ) ) {
2726 neighbors . push ( this . edges [ i ] . node1 )
2827 }
2928 }
@@ -32,7 +31,7 @@ class Graph{
3231}
3332
3433( ( ) => {
35- let graph = new Graph ( )
34+ const graph = new Graph ( )
3635 graph . addEdge ( 1 , 2 )
3736 graph . addEdge ( 2 , 3 )
3837 graph . addEdge ( 3 , 5 )
You can’t perform that action at this time.
0 commit comments