Skip to content

Commit 50df3bf

Browse files
committedMay 7, 2018
Update Graph.
1 parent ddf149b commit 50df3bf

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed
 

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

+6
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@ export default class Graph {
110110
return null;
111111
}
112112

113+
getWeight() {
114+
return this.getAllEdges().reduce((weight, graphEdge) => {
115+
return weight + graphEdge.weight;
116+
}, 0);
117+
}
118+
113119
toString() {
114120
return Object.keys(this.vertices).toString();
115121
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default class GraphEdge {
44
* @param {GraphVertex} endVertex
55
* @param {number} [weight=1]
66
*/
7-
constructor(startVertex, endVertex, weight = 1) {
7+
constructor(startVertex, endVertex, weight = 0) {
88
this.startVertex = startVertex;
99
this.endVertex = endVertex;
1010
this.weight = weight;

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

+44
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,48 @@ describe('Graph', () => {
174174
expect(edges[0]).toEqual(edgeAB);
175175
expect(edges[1]).toEqual(edgeBC);
176176
});
177+
178+
it('should calculate total graph weight for default graph', () => {
179+
const graph = new Graph();
180+
181+
const vertexA = new GraphVertex('A');
182+
const vertexB = new GraphVertex('B');
183+
const vertexC = new GraphVertex('C');
184+
const vertexD = new GraphVertex('D');
185+
186+
const edgeAB = new GraphEdge(vertexA, vertexB);
187+
const edgeBC = new GraphEdge(vertexB, vertexC);
188+
const edgeCD = new GraphEdge(vertexC, vertexD);
189+
const edgeAD = new GraphEdge(vertexA, vertexD);
190+
191+
graph
192+
.addEdge(edgeAB)
193+
.addEdge(edgeBC)
194+
.addEdge(edgeCD)
195+
.addEdge(edgeAD);
196+
197+
expect(graph.getWeight()).toBe(0);
198+
});
199+
200+
it('should calculate total graph weight for weighted graph', () => {
201+
const graph = new Graph();
202+
203+
const vertexA = new GraphVertex('A');
204+
const vertexB = new GraphVertex('B');
205+
const vertexC = new GraphVertex('C');
206+
const vertexD = new GraphVertex('D');
207+
208+
const edgeAB = new GraphEdge(vertexA, vertexB, 1);
209+
const edgeBC = new GraphEdge(vertexB, vertexC, 2);
210+
const edgeCD = new GraphEdge(vertexC, vertexD, 3);
211+
const edgeAD = new GraphEdge(vertexA, vertexD, 4);
212+
213+
graph
214+
.addEdge(edgeAB)
215+
.addEdge(edgeBC)
216+
.addEdge(edgeCD)
217+
.addEdge(edgeAD);
218+
219+
expect(graph.getWeight()).toBe(10);
220+
});
177221
});

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe('GraphEdge', () => {
1010
expect(edge.getKey()).toBe('A_B');
1111
expect(edge.startVertex).toEqual(startVertex);
1212
expect(edge.endVertex).toEqual(endVertex);
13-
expect(edge.weight).toEqual(1);
13+
expect(edge.weight).toEqual(0);
1414
});
1515

1616
it('should create graph edge with predefined weight', () => {

0 commit comments

Comments
 (0)
Please sign in to comment.