diff --git a/src/data-structures/graph/Graph.js b/src/data-structures/graph/Graph.js index 7df7afac..ad6b886b 100644 --- a/src/data-structures/graph/Graph.js +++ b/src/data-structures/graph/Graph.js @@ -110,6 +110,12 @@ export default class Graph { return null; } + getWeight() { + return this.getAllEdges().reduce((weight, graphEdge) => { + return weight + graphEdge.weight; + }, 0); + } + toString() { return Object.keys(this.vertices).toString(); } diff --git a/src/data-structures/graph/GraphEdge.js b/src/data-structures/graph/GraphEdge.js index 0650240d..9da70171 100644 --- a/src/data-structures/graph/GraphEdge.js +++ b/src/data-structures/graph/GraphEdge.js @@ -4,7 +4,7 @@ export default class GraphEdge { * @param {GraphVertex} endVertex * @param {number} [weight=1] */ - constructor(startVertex, endVertex, weight = 1) { + constructor(startVertex, endVertex, weight = 0) { this.startVertex = startVertex; this.endVertex = endVertex; this.weight = weight; diff --git a/src/data-structures/graph/__test__/Graph.test.js b/src/data-structures/graph/__test__/Graph.test.js index c4e1392b..c97cc56c 100644 --- a/src/data-structures/graph/__test__/Graph.test.js +++ b/src/data-structures/graph/__test__/Graph.test.js @@ -174,4 +174,48 @@ describe('Graph', () => { expect(edges[0]).toEqual(edgeAB); expect(edges[1]).toEqual(edgeBC); }); + + it('should calculate total graph weight for default graph', () => { + const graph = new Graph(); + + const vertexA = new GraphVertex('A'); + const vertexB = new GraphVertex('B'); + const vertexC = new GraphVertex('C'); + const vertexD = new GraphVertex('D'); + + const edgeAB = new GraphEdge(vertexA, vertexB); + const edgeBC = new GraphEdge(vertexB, vertexC); + const edgeCD = new GraphEdge(vertexC, vertexD); + const edgeAD = new GraphEdge(vertexA, vertexD); + + graph + .addEdge(edgeAB) + .addEdge(edgeBC) + .addEdge(edgeCD) + .addEdge(edgeAD); + + expect(graph.getWeight()).toBe(0); + }); + + it('should calculate total graph weight for weighted graph', () => { + const graph = new Graph(); + + const vertexA = new GraphVertex('A'); + const vertexB = new GraphVertex('B'); + const vertexC = new GraphVertex('C'); + const vertexD = new GraphVertex('D'); + + const edgeAB = new GraphEdge(vertexA, vertexB, 1); + const edgeBC = new GraphEdge(vertexB, vertexC, 2); + const edgeCD = new GraphEdge(vertexC, vertexD, 3); + const edgeAD = new GraphEdge(vertexA, vertexD, 4); + + graph + .addEdge(edgeAB) + .addEdge(edgeBC) + .addEdge(edgeCD) + .addEdge(edgeAD); + + expect(graph.getWeight()).toBe(10); + }); }); diff --git a/src/data-structures/graph/__test__/GraphEdge.test.js b/src/data-structures/graph/__test__/GraphEdge.test.js index a677a81d..347dbca0 100644 --- a/src/data-structures/graph/__test__/GraphEdge.test.js +++ b/src/data-structures/graph/__test__/GraphEdge.test.js @@ -10,7 +10,7 @@ describe('GraphEdge', () => { expect(edge.getKey()).toBe('A_B'); expect(edge.startVertex).toEqual(startVertex); expect(edge.endVertex).toEqual(endVertex); - expect(edge.weight).toEqual(1); + expect(edge.weight).toEqual(0); }); it('should create graph edge with predefined weight', () => {