File tree Expand file tree Collapse file tree 4 files changed +158
-0
lines changed Top Open diff view settings Expand file tree Collapse file tree 4 files changed +158
-0
lines changed Top Open diff view settings Original file line number Diff line number Diff line change 1+ const Queue = require ( '../stacks-and-queues/queue' ) ;
2+ /**
3+ * Digraph
4+ */
5+ class Graph {
6+ constructor ( ) {
7+ this . nodes = [ ] ;
8+ }
9+
10+ /**
11+ * use adjacency list to store nodes and more
12+ *
13+ * O(1)
14+ *
15+ * @param a
16+ * @param b
17+ */
18+ add ( a , b ) {
19+ if ( ! this . nodes [ a ] ) {
20+ this . nodes [ a ] = [ ] ;
21+ }
22+
23+ this . nodes [ a ] . push ( b ) ;
24+ }
25+
26+ /**
27+ * Depth-first search
28+ * @param node
29+ */
30+ dfs ( node ) {
31+
32+ }
33+ }
34+
35+ /**
36+ * Breadth-first search
37+ * @param node
38+ */
39+ Graph . prototype . bfs = function * ( node ) {
40+ const queue = new Queue ( ) ;
41+
42+ queue . add ( node ) ;
43+
44+ while ( ! queue . isEmpty ( ) ) {
45+ const adjacents = this . nodes [ queue . remove ( ) ] ;
46+ yield adjacents ;
47+ adjacents . forEach ( function ( adjNode ) {
48+ queue . add ( adjNode ) ;
49+ } ) ;
50+ }
51+ } ;
52+
53+ module . exports = Graph ;
Original file line number Diff line number Diff line change 1+ const expect = require ( 'chai' ) . expect ;
2+ const Graph = require ( './graph' ) ;
3+
4+ describe ( 'Graph' , function ( ) {
5+ let graph ;
6+
7+ beforeEach ( function ( ) {
8+ graph = new Graph ( ) ;
9+ } ) ;
10+
11+ describe ( 'add' , function ( ) {
12+ it ( 'should add two elements' , function ( ) {
13+ graph . add ( 0 , 1 ) ;
14+ expect ( graph . nodes [ 0 ] ) . to . eql ( [ 1 ] ) ;
15+ } ) ;
16+
17+ it ( 'should allow one element to have two children' , function ( ) {
18+ graph . add ( 0 , 1 ) ;
19+ graph . add ( 0 , 2 ) ;
20+ expect ( graph . nodes [ 0 ] ) . to . eql ( [ 1 , 2 ] ) ;
21+ } ) ;
22+
23+ it ( 'should store complex data' , function ( ) {
24+ let a = { a :1 } ;
25+ let b = { b :2 } ;
26+
27+ graph . add ( a , b ) ;
28+ expect ( graph . nodes [ a ] ) . to . eql ( [ b ] ) ;
29+ } ) ;
30+ } ) ;
31+
32+ describe ( 'bfs' , function ( ) {
33+ beforeEach ( function ( ) {
34+ graph . add ( 0 , 1 ) ;
35+ graph . add ( 0 , 2 ) ;
36+ graph . add ( 1 , 3 ) ;
37+ } ) ;
38+
39+ it ( 'should get all adjancent in the first level' , function ( ) {
40+ const bfs = graph . bfs ( 0 ) ;
41+ expect ( bfs . next ( ) . value ) . to . eql ( [ 1 , 2 ] ) ;
42+ } ) ;
43+
44+ it ( 'should get 2nd level adjacents' , function ( ) {
45+ const bfs = graph . bfs ( 0 ) ;
46+ bfs . next ( ) ;
47+ expect ( bfs . next ( ) . value ) . to . eql ( [ 3 ] ) ;
48+ expect ( bfs . next ( ) . value ) . to . equal ( undefined ) ;
49+ } ) ;
50+ } ) ;
51+ } ) ;
Original file line number Diff line number Diff line change 1+ const Graph = require ( './graph' ) ;
2+ const Queue = require ( '../stacks-and-queues/queue' ) ;
3+
4+ /**
5+ * Test if two nodes are connected using BFS
6+ * @param node1
7+ * @param node2
8+ * @returns {boolean }
9+ */
10+ Graph . prototype . isConnected = function ( node1 , node2 ) {
11+ const queue = new Queue ( ) ;
12+
13+ queue . add ( node1 ) ;
14+
15+ while ( ! queue . isEmpty ( ) ) {
16+ const current = queue . remove ( ) ;
17+
18+ if ( current === node2 ) {
19+ return true ;
20+ }
21+
22+ ( this . nodes [ current ] || [ ] ) . forEach ( function ( node ) {
23+ queue . add ( node ) ;
24+ } ) ;
25+ }
26+
27+ return false ;
28+ } ;
29+
30+ module . exports = Graph ;
Original file line number Diff line number Diff line change 1+ const expect = require ( 'chai' ) . expect ;
2+ const Graph = require ( './routes-between-nodes' ) ;
3+
4+ describe ( 'Graph: is connected' , function ( ) {
5+ let graph ;
6+
7+ beforeEach ( function ( ) {
8+ graph = new Graph ( ) ;
9+ } ) ;
10+
11+ describe ( '.isConnected' , function ( ) {
12+ it ( 'should return true if connected' , function ( ) {
13+ graph . add ( 0 , 1 ) ;
14+ graph . add ( 1 , 2 ) ;
15+ expect ( graph . isConnected ( 0 , 2 ) ) . to . equal ( true ) ;
16+ } ) ;
17+
18+ it ( 'should return false if connected' , function ( ) {
19+ graph . add ( 0 , 1 ) ;
20+ graph . add ( 2 , 3 ) ;
21+ expect ( graph . isConnected ( 0 , 3 ) ) . to . equal ( false ) ;
22+ } ) ;
23+ } ) ;
24+ } ) ;
You can’t perform that action at this time.
0 commit comments