File tree Expand file tree Collapse file tree 2 files changed +39
-0
lines changed
src/data-structures/graph Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change @@ -119,6 +119,15 @@ export default class GraphVertex {
119119 return this . value ;
120120 }
121121
122+ /**
123+ * @return {GraphVertex }
124+ */
125+ deleteAllEdges ( ) {
126+ this . getEdges ( ) . forEach ( edge => this . deleteEdge ( edge ) ) ;
127+
128+ return this ;
129+ }
130+
122131 /**
123132 * @param {function } [callback]
124133 * @returns {string }
Original file line number Diff line number Diff line change @@ -70,6 +70,36 @@ describe('GraphVertex', () => {
7070 expect ( vertexA . getEdges ( ) . length ) . toBe ( 0 ) ;
7171 } ) ;
7272
73+ it ( 'should delete all edges from vertex' , ( ) => {
74+ const vertexA = new GraphVertex ( 'A' ) ;
75+ const vertexB = new GraphVertex ( 'B' ) ;
76+ const vertexC = new GraphVertex ( 'C' ) ;
77+
78+ const edgeAB = new GraphEdge ( vertexA , vertexB ) ;
79+ const edgeAC = new GraphEdge ( vertexA , vertexC ) ;
80+ vertexA
81+ . addEdge ( edgeAB )
82+ . addEdge ( edgeAC ) ;
83+
84+ expect ( vertexA . hasEdge ( edgeAB ) ) . toBeTruthy ( ) ;
85+ expect ( vertexB . hasEdge ( edgeAB ) ) . toBeFalsy ( ) ;
86+
87+ expect ( vertexA . hasEdge ( edgeAC ) ) . toBeTruthy ( ) ;
88+ expect ( vertexC . hasEdge ( edgeAC ) ) . toBeFalsy ( ) ;
89+
90+ expect ( vertexA . getEdges ( ) . length ) . toBe ( 2 ) ;
91+
92+ vertexA . deleteAllEdges ( ) ;
93+
94+ expect ( vertexA . hasEdge ( edgeAB ) ) . toBeFalsy ( ) ;
95+ expect ( vertexB . hasEdge ( edgeAB ) ) . toBeFalsy ( ) ;
96+
97+ expect ( vertexA . hasEdge ( edgeAC ) ) . toBeFalsy ( ) ;
98+ expect ( vertexC . hasEdge ( edgeAC ) ) . toBeFalsy ( ) ;
99+
100+ expect ( vertexA . getEdges ( ) . length ) . toBe ( 0 ) ;
101+ } ) ;
102+
73103 it ( 'should return vertex neighbors in case if current node is start one' , ( ) => {
74104 const vertexA = new GraphVertex ( 'A' ) ;
75105 const vertexB = new GraphVertex ( 'B' ) ;
You can’t perform that action at this time.
0 commit comments