File tree 2 files changed +39
-0
lines changed
src/data-structures/graph
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 {
119
119
return this . value ;
120
120
}
121
121
122
+ /**
123
+ * @return {GraphVertex }
124
+ */
125
+ deleteAllEdges ( ) {
126
+ this . getEdges ( ) . forEach ( edge => this . deleteEdge ( edge ) ) ;
127
+
128
+ return this ;
129
+ }
130
+
122
131
/**
123
132
* @param {function } [callback]
124
133
* @returns {string }
Original file line number Diff line number Diff line change @@ -70,6 +70,36 @@ describe('GraphVertex', () => {
70
70
expect ( vertexA . getEdges ( ) . length ) . toBe ( 0 ) ;
71
71
} ) ;
72
72
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
+
73
103
it ( 'should return vertex neighbors in case if current node is start one' , ( ) => {
74
104
const vertexA = new GraphVertex ( 'A' ) ;
75
105
const vertexB = new GraphVertex ( 'B' ) ;
You can’t perform that action at this time.
0 commit comments