From 37f3d54affcc101ea438dbc2841286b14cff8d33 Mon Sep 17 00:00:00 2001 From: Alex Rock Ancelet Date: Wed, 18 Dec 2019 08:37:24 +0100 Subject: [PATCH 1/4] Allow graph edges with custom keys --- src/data-structures/graph/GraphEdge.js | 19 ++++++++++++------- .../graph/__test__/GraphEdge.test.js | 16 ++++++++++++++-- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/data-structures/graph/GraphEdge.js b/src/data-structures/graph/GraphEdge.js index 9a5b5db4..c0002f0f 100644 --- a/src/data-structures/graph/GraphEdge.js +++ b/src/data-structures/graph/GraphEdge.js @@ -2,22 +2,27 @@ export default class GraphEdge { /** * @param {GraphVertex} startVertex * @param {GraphVertex} endVertex - * @param {number} [weight=1] + * @param {number} [weight=0] + * @param key */ - constructor(startVertex, endVertex, weight = 0) { + constructor(startVertex, endVertex, weight = 0, key = null) { this.startVertex = startVertex; this.endVertex = endVertex; this.weight = weight; + this.key = key; } - /** - * @return {string} - */ getKey() { + if (this.key) { + return this.key; + } + const startVertexKey = this.startVertex.getKey(); const endVertexKey = this.endVertex.getKey(); - return `${startVertexKey}_${endVertexKey}`; + this.key = `${startVertexKey}_${endVertexKey}`; + + return this.key; } /** @@ -35,6 +40,6 @@ export default class GraphEdge { * @return {string} */ toString() { - return this.getKey(); + return this.getKey().toString(); } } diff --git a/src/data-structures/graph/__test__/GraphEdge.test.js b/src/data-structures/graph/__test__/GraphEdge.test.js index ceb84ab2..c2729845 100644 --- a/src/data-structures/graph/__test__/GraphEdge.test.js +++ b/src/data-structures/graph/__test__/GraphEdge.test.js @@ -7,8 +7,6 @@ describe('GraphEdge', () => { const endVertex = new GraphVertex('B'); const edge = new GraphEdge(startVertex, endVertex); - expect(edge.getKey()).toBe('A_B'); - expect(edge.toString()).toBe('A_B'); expect(edge.startVertex).toEqual(startVertex); expect(edge.endVertex).toEqual(endVertex); expect(edge.weight).toEqual(0); @@ -39,4 +37,18 @@ describe('GraphEdge', () => { expect(edge.endVertex).toEqual(vertexA); expect(edge.weight).toEqual(10); }); + + it('should return edges names as key', () => { + const edge = new GraphEdge(new GraphVertex('A'), new GraphVertex('B'), 0); + + expect(edge.getKey()).toBe('A_B'); + expect(edge.toString()).toBe('A_B'); + }); + + it('should return custom key if defined', () => { + const edge = new GraphEdge(new GraphVertex('A'), new GraphVertex('B'), 0, 'custom_key'); + + expect(edge.getKey()).toEqual('custom_key'); + expect(edge.toString()).toEqual('custom_key'); + }); }); From 9465d0e4c45d140481ea4c6afcff7f309164cedb Mon Sep 17 00:00:00 2001 From: Alex Rock Ancelet Date: Wed, 18 Dec 2019 08:45:00 +0100 Subject: [PATCH 2/4] Make sure graph vertex value is converted to string --- src/data-structures/graph/GraphVertex.js | 2 +- .../graph/__test__/GraphVertex.test.js | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/data-structures/graph/GraphVertex.js b/src/data-structures/graph/GraphVertex.js index b28ced32..7eabd9ef 100644 --- a/src/data-structures/graph/GraphVertex.js +++ b/src/data-structures/graph/GraphVertex.js @@ -133,6 +133,6 @@ export default class GraphVertex { * @returns {string} */ toString(callback) { - return callback ? callback(this.value) : `${this.value}`; + return callback ? callback(this.value).toString() : this.value.toString(); } } diff --git a/src/data-structures/graph/__test__/GraphVertex.test.js b/src/data-structures/graph/__test__/GraphVertex.test.js index 4810a1c2..3fc2110d 100644 --- a/src/data-structures/graph/__test__/GraphVertex.test.js +++ b/src/data-structures/graph/__test__/GraphVertex.test.js @@ -185,4 +185,20 @@ describe('GraphVertex', () => { expect(vertexA.getEdges().length).toEqual(3); }); + + it('should execute callback when passed to toString', () => { + const vertex = new GraphVertex('A'); + + expect(vertex.toString(() => 'B')).toEqual('B'); + }); + + it('should execute toString on value when calling toString on vertex', () => { + const value = { + toString() { return 'A'; }, + }; + + const vertex = new GraphVertex(value); + + expect(vertex.toString()).toEqual('A'); + }); }); From 6045f67230fcaa39505bc671165846921520b686 Mon Sep 17 00:00:00 2001 From: Alex Rock Ancelet Date: Wed, 18 Dec 2019 08:55:29 +0100 Subject: [PATCH 3/4] Make sure toString is called on edge key when calling edge.toString() --- src/data-structures/graph/__test__/GraphEdge.test.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/data-structures/graph/__test__/GraphEdge.test.js b/src/data-structures/graph/__test__/GraphEdge.test.js index c2729845..18dc7317 100644 --- a/src/data-structures/graph/__test__/GraphEdge.test.js +++ b/src/data-structures/graph/__test__/GraphEdge.test.js @@ -51,4 +51,15 @@ describe('GraphEdge', () => { expect(edge.getKey()).toEqual('custom_key'); expect(edge.toString()).toEqual('custom_key'); }); + + it('should execute toString on key when calling toString on edge', () => { + const customKey = { + toString() { return 'custom_key'; }, + }; + + const edge = new GraphEdge(new GraphVertex('A'), new GraphVertex('B'), 0, customKey); + + expect(edge.getKey()).toEqual(customKey); + expect(edge.toString()).toEqual('custom_key'); + }); }); From 4b0dfd4dcd08b2dbdb2963a386c412c66954e203 Mon Sep 17 00:00:00 2001 From: Alex Rock Ancelet Date: Wed, 18 Dec 2019 09:05:58 +0100 Subject: [PATCH 4/4] Make sure a vertex can't be added twice to a graph --- src/data-structures/graph/Graph.js | 8 +++++++- src/data-structures/graph/__test__/Graph.test.js | 13 +++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/data-structures/graph/Graph.js b/src/data-structures/graph/Graph.js index ba0b6d01..9a9194f2 100644 --- a/src/data-structures/graph/Graph.js +++ b/src/data-structures/graph/Graph.js @@ -13,7 +13,13 @@ export default class Graph { * @returns {Graph} */ addVertex(newVertex) { - this.vertices[newVertex.getKey()] = newVertex; + const key = newVertex.getKey(); + + if (this.vertices[key]) { + throw new Error('Vertex has already been added before'); + } + + this.vertices[key] = newVertex; return this; } diff --git a/src/data-structures/graph/__test__/Graph.test.js b/src/data-structures/graph/__test__/Graph.test.js index 936a69b8..dcae57d8 100644 --- a/src/data-structures/graph/__test__/Graph.test.js +++ b/src/data-structures/graph/__test__/Graph.test.js @@ -158,6 +158,19 @@ describe('Graph', () => { expect(addSameEdgeTwice).toThrow(); }); + it('should throw an error when trying to add vertex twice', () => { + function addSameEdgeTwice() { + const graph = new Graph(true); + const vertexA = new GraphVertex('A'); + + graph + .addVertex(vertexA) + .addVertex(vertexA); + } + + expect(addSameEdgeTwice).toThrow(); + }); + it('should return the list of all added edges', () => { const graph = new Graph(true);