@@ -42,18 +42,18 @@ describe('Graph', () => {
4242
4343 it ( 'should add node a as adjacent of b' , ( ) => {
4444 const [ a , b ] = graph . addEdge ( 'a' , 'b' ) ;
45- expect ( a . adjacents . map ( n => n . value ) ) . toEqual ( [ 'b' ] ) ;
46- expect ( b . adjacents . map ( n => n . value ) ) . toEqual ( [ ] ) ;
45+ expect ( a . adjacents . map ( getValue ) ) . toEqual ( [ 'b' ] ) ;
46+ expect ( b . adjacents . map ( getValue ) ) . toEqual ( [ ] ) ;
4747
4848 graph . addEdge ( 'b' , 'a' ) ;
49- expect ( b . adjacents . map ( n => n . value ) ) . toEqual ( [ 'a' ] ) ;
49+ expect ( b . adjacents . map ( getValue ) ) . toEqual ( [ 'a' ] ) ;
5050 } ) ;
5151
5252 it ( 'should add both connection on undirected graph' , ( ) => {
5353 graph = new Graph ( Graph . UNDIRECTED ) ;
5454 const [ a , b ] = graph . addEdge ( 'a' , 'b' ) ;
55- expect ( a . adjacents . map ( n => n . value ) ) . toEqual ( [ 'b' ] ) ;
56- expect ( b . adjacents . map ( n => n . value ) ) . toEqual ( [ 'a' ] ) ;
55+ expect ( a . adjacents . map ( getValue ) ) . toEqual ( [ 'b' ] ) ;
56+ expect ( b . adjacents . map ( getValue ) ) . toEqual ( [ 'a' ] ) ;
5757 } ) ;
5858
5959 it ( 'should add falsy values' , ( ) => {
@@ -70,21 +70,21 @@ describe('Graph', () => {
7070
7171 it ( 'should remove edges if they exist' , ( ) => {
7272 const [ a , b ] = graph . removeEdge ( 'a' , 'b' ) ;
73- expect ( a . adjacents . map ( n => n . value ) ) . toEqual ( [ ] ) ;
74- expect ( b . adjacents . map ( n => n . value ) ) . toEqual ( [ ] ) ;
73+ expect ( a . adjacents . map ( getValue ) ) . toEqual ( [ ] ) ;
74+ expect ( b . adjacents . map ( getValue ) ) . toEqual ( [ ] ) ;
7575 } ) ;
7676
7777 it ( 'should remove edges with falsy values' , ( ) => {
7878 const [ a , b ] = graph . addEdge ( 0 , false ) ;
79- expect ( a . adjacents . map ( n => n . value ) ) . toEqual ( [ false ] ) ;
79+ expect ( a . adjacents . map ( getValue ) ) . toEqual ( [ false ] ) ;
8080 graph . removeEdge ( 0 , false ) ;
81- expect ( a . adjacents . map ( n => n . value ) ) . toEqual ( [ ] ) ;
81+ expect ( a . adjacents . map ( getValue ) ) . toEqual ( [ ] ) ;
8282 } ) ;
8383
8484 it ( 'should not create node when removing unexisting target' , ( ) => {
8585 const [ a , c ] = graph . removeEdge ( 'a' , 'c' ) ;
8686 expect ( graph . nodes . size ) . toBe ( 2 ) ;
87- expect ( a . adjacents . map ( n => n . value ) ) . toEqual ( [ 'b' ] ) ;
87+ expect ( a . adjacents . map ( getValue ) ) . toEqual ( [ 'b' ] ) ;
8888 expect ( c ) . toBe ( undefined ) ;
8989 } ) ;
9090
@@ -96,6 +96,26 @@ describe('Graph', () => {
9696 } ) ;
9797 } ) ;
9898
99+ describe ( 'remove vertex and update adjacency list' , ( ) => {
100+ let n1 , n2 , n4 ;
101+
102+ beforeEach ( ( ) => {
103+ // 0 -> 1 <- 2
104+ // ^\-> 4 -> 3
105+ graph . addEdge ( 0 , 1 ) ;
106+ [ n2 ] = graph . addEdge ( 2 , 1 ) ;
107+ [ n1 , n4 ] = graph . addEdge ( 1 , 4 ) ;
108+ graph . addEdge ( 4 , 1 ) ;
109+ graph . addEdge ( 4 , 3 ) ;
110+ } ) ;
111+
112+ it ( 'should remove nodes from adjacent list' , ( ) => {
113+ expect ( n4 . getAdjacents ( ) . map ( getValue ) ) . toEqual ( [ 1 , 3 ] ) ;
114+ graph . removeVertex ( 1 ) ;
115+ expect ( n4 . getAdjacents ( ) . map ( getValue ) ) . toEqual ( [ 3 ] ) ;
116+ } ) ;
117+ } ) ;
118+
99119 describe ( 'Graph Search' , ( ) => {
100120 let first ;
101121
@@ -116,19 +136,21 @@ describe('Graph', () => {
116136 describe ( '#dfs' , ( ) => {
117137
118138 it ( 'should visit nodes using depth-first search' , ( ) => {
119- const visitedOrder = Array . from ( graph . dfs ( first ) ) . map ( v => v . value ) ;
139+ const visitedOrder = Array . from ( graph . dfs ( first ) ) . map ( getValue ) ;
120140 expect ( visitedOrder ) . toEqual ( [ 0 , 1 , 3 , 2 , 4 , 5 ] ) ;
121141 } ) ;
122142 } ) ;
123143
124144 describe ( '#bfs' , ( ) => {
125145
126146 it ( 'should visit nodes using breadth-first search' , ( ) => {
127- const visitedOrder = Array . from ( graph . bfs ( first ) ) . map ( v => v . value ) ;
147+ const visitedOrder = Array . from ( graph . bfs ( first ) ) . map ( getValue ) ;
128148 expect ( visitedOrder ) . toEqual ( [ 0 , 5 , 4 , 1 , 3 , 2 ] ) ;
129149 } ) ;
130150 } ) ;
131151 } ) ;
132152
133-
153+ function getValue ( node ) {
154+ return node . value ;
155+ }
134156} ) ;
0 commit comments