Skip to content

Commit ff8f9c4

Browse files
committedMay 12, 2018
Make it possible to delete all vertex edges at once.
1 parent 4a6bc1e commit ff8f9c4

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed
 

‎src/data-structures/graph/GraphVertex.js

+9
Original file line numberDiff line numberDiff 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}

‎src/data-structures/graph/__test__/GraphVertex.test.js

+30
Original file line numberDiff line numberDiff 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');

0 commit comments

Comments
 (0)
Please sign in to comment.