@@ -3,7 +3,7 @@ import { AdjacencyList } from './adj-list';
33describe ( 'Adjacency List' , ( ) => {
44 let adjList ;
55 beforeEach ( ( ) => {
6- adjList = new AdjacencyList ( ) ;
6+ adjList = new AdjacencyList ( true ) ;
77 } ) ;
88
99 it ( 'should create adjacency list object' , ( ) => {
@@ -85,4 +85,108 @@ describe('Adjacency List', () => {
8585 expect ( lookupA . hasVal ) . toBeTruthy ( ) ;
8686 } ) ;
8787 } ) ;
88+
89+ describe ( 'Delete Edge Operation' , ( ) => {
90+ beforeEach ( ( ) => {
91+ adjList . addEdge ( 'A' , 'B' ) ;
92+ adjList . addEdge ( 'A' , 'C' ) ;
93+ } ) ;
94+
95+ it ( 'should delete edge A -> C' , ( ) => {
96+ expect ( adjList . edges . size ) . toBe ( 3 ) ;
97+ const vertexA = adjList . edges . get ( 'A' ) ;
98+ expect ( vertexA . len ) . toBe ( 2 ) ;
99+ expect ( vertexA . lookup ( 'C' ) . hasVal ) . toBeTruthy ( ) ;
100+ adjList . removeEdge ( 'A' , 'C' ) ;
101+ expect ( vertexA . len ) . toBe ( 1 ) ;
102+ expect ( vertexA . lookup ( 'C' ) . hasVal ) . toBeFalsy ( ) ;
103+ } ) ;
104+ it ( 'should return error while deleting edge D -> A' , ( ) => {
105+ expect ( adjList . removeEdge ( 'D' , 'A' ) . message ) . toBe ( 'No edge present between D and A' ) ;
106+ } ) ;
107+
108+ describe ( 'For undirectional Graph' , ( ) => {
109+ let undirectedList ;
110+ beforeEach ( ( ) => {
111+ undirectedList = new AdjacencyList ( false ) ;
112+ undirectedList . addEdge ( 'A' , 'B' ) ;
113+ undirectedList . addEdge ( 'A' , 'C' ) ;
114+ } ) ;
115+
116+ it ( 'should delete edge A -> C and edge C -> A' , ( ) => {
117+ expect ( undirectedList . edges . size ) . toBe ( 3 ) ;
118+ const msg = undirectedList . removeEdge ( 'A' , 'C' ) ;
119+ expect ( undirectedList . edges . get ( 'A' ) . lookup ( 'C' ) . hasVal ) . toBeFalsy ( ) ;
120+ expect ( undirectedList . edges . get ( 'C' ) . lookup ( 'A' ) . hasVal ) . toBeFalsy ( ) ;
121+ expect ( msg ) . toBe ( void 0 ) ;
122+ } ) ;
123+ } ) ;
124+ } ) ;
125+
126+ describe ( 'Delete Vertex Operation' , ( ) => {
127+ beforeEach ( ( ) => {
128+ adjList . addEdge ( 'A' , 'B' ) ;
129+ adjList . addEdge ( 'A' , 'C' ) ;
130+ } ) ;
131+
132+ it ( 'should delete vertex C and finally edge A -> C' , ( ) => {
133+ expect ( adjList . edges . size ) . toBe ( 3 ) ;
134+ expect ( adjList . edges . get ( 'A' ) . len ) . toBe ( 2 ) ;
135+ expect ( adjList . edges . get ( 'A' ) . lookup ( 'C' ) . hasVal ) . toBeTruthy ( ) ;
136+
137+ adjList . removeNode ( 'C' ) ;
138+ expect ( adjList . edges . size ) . toBe ( 2 ) ;
139+ expect ( adjList . edges . get ( 'A' ) . len ) . toBe ( 1 ) ;
140+ expect ( adjList . edges . get ( 'A' ) . lookup ( 'C' ) . hasVal ) . toBeFalsy ( ) ;
141+ } ) ;
142+
143+ describe ( 'For undirectional Graph' , ( ) => {
144+ let undirectedList ;
145+ beforeEach ( ( ) => {
146+ undirectedList = new AdjacencyList ( false ) ;
147+ undirectedList . addEdge ( 'A' , 'B' ) ;
148+ undirectedList . addEdge ( 'A' , 'C' ) ;
149+ } ) ;
150+
151+ it ( 'should delete node C and also edges A -> C and C -> A' , ( ) => {
152+ expect ( undirectedList . edges . size ) . toBe ( 3 ) ;
153+ expect ( undirectedList . edges . get ( 'A' ) . len ) . toBe ( 2 ) ;
154+ expect ( undirectedList . edges . get ( 'A' ) . lookup ( 'C' ) . hasVal ) . toBeTruthy ( ) ;
155+ expect ( undirectedList . edges . get ( 'C' ) . len ) . toBe ( 1 ) ;
156+ expect ( undirectedList . edges . get ( 'C' ) . lookup ( 'A' ) . hasVal ) . toBeTruthy ( ) ;
157+
158+ undirectedList . removeNode ( 'C' ) ;
159+
160+ expect ( undirectedList . edges . size ) . toBe ( 2 ) ;
161+ expect ( undirectedList . edges . get ( 'A' ) . len ) . toBe ( 1 ) ;
162+ expect ( undirectedList . edges . get ( 'A' ) . lookup ( 'C' ) . hasVal ) . toBeFalsy ( ) ;
163+ expect ( undirectedList . edges . has ( 'C' ) ) . toBeFalsy ( ) ;
164+ } ) ;
165+ } ) ;
166+ } ) ;
167+
168+ describe ( 'Get edge weight' , ( ) => {
169+ beforeEach ( ( ) => {
170+ adjList . addEdge ( 'A' , 'B' , 200 ) ;
171+ adjList . addEdge ( 'A' , 'C' , 150 ) ;
172+ adjList . addEdge ( 'B' , 'C' , 250 ) ;
173+ } ) ;
174+
175+ it ( 'should get 200 as weight between edge A and B' , ( ) => {
176+ const weight = adjList . getEdgeWeight ( 'A' , 'B' ) ;
177+ expect ( weight ) . toBe ( 200 ) ;
178+ } ) ;
179+ it ( 'should get 150 as weight between edge A and C' , ( ) => {
180+ const weight = adjList . getEdgeWeight ( 'A' , 'C' ) ;
181+ expect ( weight ) . toBe ( 150 ) ;
182+ } ) ;
183+ it ( 'should get Error for weight between edge A and D' , ( ) => {
184+ const weight = adjList . getEdgeWeight ( 'A' , 'D' ) ;
185+ expect ( weight . message ) . toBe ( 'Edge not found between A and D' ) ;
186+ } ) ;
187+ it ( 'should get Error for weight between edge D and A' , ( ) => {
188+ const weight = adjList . getEdgeWeight ( 'D' , 'A' ) ;
189+ expect ( weight . message ) . toBe ( 'Edge not found between D and A' ) ;
190+ } ) ;
191+ } ) ;
88192} ) ;
0 commit comments