mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2026-03-13 08:51:02 +08:00
140 lines
4.2 KiB
JavaScript
140 lines
4.2 KiB
JavaScript
import Graph from '../Graph';
|
|
import GraphVertex from '../GraphVertex';
|
|
import GraphEdge from '../GraphEdge';
|
|
|
|
describe('Graph', () => {
|
|
it('should add vertices to graph', () => {
|
|
const graph = new Graph();
|
|
|
|
const vertexA = new GraphVertex('A');
|
|
const vertexB = new GraphVertex('B');
|
|
|
|
graph
|
|
.addVertex(vertexA)
|
|
.addVertex(vertexB);
|
|
|
|
expect(graph.toString()).toBe('A,B');
|
|
expect(graph.getVertexByKey(vertexA.getKey())).toEqual(vertexA);
|
|
expect(graph.getVertexByKey(vertexB.getKey())).toEqual(vertexB);
|
|
});
|
|
|
|
it('should add edges to undirected graph', () => {
|
|
const graph = new Graph();
|
|
|
|
const vertexA = new GraphVertex('A');
|
|
const vertexB = new GraphVertex('B');
|
|
|
|
const edgeAB = new GraphEdge(vertexA, vertexB);
|
|
|
|
graph.addEdge(edgeAB);
|
|
|
|
expect(graph.getAllVertices().length).toBe(2);
|
|
expect(graph.getAllVertices()[0]).toEqual(vertexA);
|
|
expect(graph.getAllVertices()[1]).toEqual(vertexB);
|
|
|
|
const graphVertexA = graph.findVertexByKey(vertexA.getKey());
|
|
const graphVertexB = graph.findVertexByKey(vertexB.getKey());
|
|
|
|
expect(graph.toString()).toBe('A,B');
|
|
expect(graphVertexA).toBeDefined();
|
|
expect(graphVertexB).toBeDefined();
|
|
|
|
expect(graph.findVertexByKey('not existing')).toBeNull();
|
|
|
|
expect(graphVertexA.getNeighbors().length).toBe(1);
|
|
expect(graphVertexA.getNeighbors()[0]).toEqual(vertexB);
|
|
expect(graphVertexA.getNeighbors()[0]).toEqual(graphVertexB);
|
|
|
|
expect(graphVertexB.getNeighbors().length).toBe(1);
|
|
expect(graphVertexB.getNeighbors()[0]).toEqual(vertexA);
|
|
expect(graphVertexB.getNeighbors()[0]).toEqual(graphVertexA);
|
|
});
|
|
|
|
it('should add edges to directed graph', () => {
|
|
const graph = new Graph(true);
|
|
|
|
const vertexA = new GraphVertex('A');
|
|
const vertexB = new GraphVertex('B');
|
|
|
|
const edgeAB = new GraphEdge(vertexA, vertexB);
|
|
|
|
graph.addEdge(edgeAB);
|
|
|
|
const graphVertexA = graph.findVertexByKey(vertexA.getKey());
|
|
const graphVertexB = graph.findVertexByKey(vertexB.getKey());
|
|
|
|
expect(graph.toString()).toBe('A,B');
|
|
expect(graphVertexA).toBeDefined();
|
|
expect(graphVertexB).toBeDefined();
|
|
|
|
expect(graphVertexA.getNeighbors().length).toBe(1);
|
|
expect(graphVertexA.getNeighbors()[0]).toEqual(vertexB);
|
|
expect(graphVertexA.getNeighbors()[0]).toEqual(graphVertexB);
|
|
|
|
expect(graphVertexB.getNeighbors().length).toBe(0);
|
|
});
|
|
|
|
it('should find edge by vertices in undirected graph', () => {
|
|
const graph = new Graph();
|
|
|
|
const vertexA = new GraphVertex('A');
|
|
const vertexB = new GraphVertex('B');
|
|
const vertexC = new GraphVertex('C');
|
|
|
|
const edgeAB = new GraphEdge(vertexA, vertexB, 10);
|
|
|
|
graph.addEdge(edgeAB);
|
|
|
|
const graphEdgeAB = graph.findEdge(vertexA, vertexB);
|
|
const graphEdgeBA = graph.findEdge(vertexB, vertexA);
|
|
const graphEdgeAC = graph.findEdge(vertexB, vertexC);
|
|
|
|
expect(graphEdgeAC).toBeNull();
|
|
expect(graphEdgeAB).toEqual(edgeAB);
|
|
expect(graphEdgeBA).toEqual(edgeAB);
|
|
expect(graphEdgeAB.weight).toBe(10);
|
|
});
|
|
|
|
it('should find edge by vertices in directed graph', () => {
|
|
const graph = new Graph(true);
|
|
|
|
const vertexA = new GraphVertex('A');
|
|
const vertexB = new GraphVertex('B');
|
|
const vertexC = new GraphVertex('C');
|
|
|
|
const edgeAB = new GraphEdge(vertexA, vertexB, 10);
|
|
|
|
graph.addEdge(edgeAB);
|
|
|
|
const graphEdgeAB = graph.findEdge(vertexA, vertexB);
|
|
const graphEdgeBA = graph.findEdge(vertexB, vertexA);
|
|
const graphEdgeAC = graph.findEdge(vertexB, vertexC);
|
|
|
|
expect(graphEdgeAC).toBeNull();
|
|
expect(graphEdgeBA).toBeNull();
|
|
expect(graphEdgeAB).toEqual(edgeAB);
|
|
expect(graphEdgeAB.weight).toBe(10);
|
|
});
|
|
|
|
it('should return vertex neighbors', () => {
|
|
const graph = new Graph(true);
|
|
|
|
const vertexA = new GraphVertex('A');
|
|
const vertexB = new GraphVertex('B');
|
|
const vertexC = new GraphVertex('C');
|
|
|
|
const edgeAB = new GraphEdge(vertexA, vertexB);
|
|
const edgeAC = new GraphEdge(vertexA, vertexC);
|
|
|
|
graph
|
|
.addEdge(edgeAB)
|
|
.addEdge(edgeAC);
|
|
|
|
const neighbors = graph.getNeighbors(vertexA);
|
|
|
|
expect(neighbors.length).toBe(2);
|
|
expect(neighbors[0]).toEqual(vertexB);
|
|
expect(neighbors[1]).toEqual(vertexC);
|
|
});
|
|
});
|