mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-11-10 11:09:43 +08:00
Compare commits
5 Commits
c5eb21f83e
...
8e75388d57
Author | SHA1 | Date | |
---|---|---|---|
|
8e75388d57 | ||
|
4b0dfd4dcd | ||
|
6045f67230 | ||
|
9465d0e4c4 | ||
|
37f3d54aff |
@ -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;
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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,29 @@ 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');
|
||||
});
|
||||
|
||||
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');
|
||||
});
|
||||
});
|
||||
|
@ -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');
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user