Validating Topological Sort Algorithm Implementation in javascript-algorithms
This test suite validates the implementation of topological sorting algorithm on a directed graph structure. It verifies the correct ordering of vertices based on their dependencies, ensuring that for each directed edge u→v, vertex u comes before v in the ordering.
Test Coverage Overview
Implementation Analysis
Technical Details
Best Practices Demonstrated
trekhleb/javascript-algorithms
src/algorithms/graph/topological-sorting/__test__/topologicalSort.test.js
import GraphVertex from '../../../../data-structures/graph/GraphVertex';
import GraphEdge from '../../../../data-structures/graph/GraphEdge';
import Graph from '../../../../data-structures/graph/Graph';
import topologicalSort from '../topologicalSort';
describe('topologicalSort', () => {
it('should do topological sorting on graph', () => {
const vertexA = new GraphVertex('A');
const vertexB = new GraphVertex('B');
const vertexC = new GraphVertex('C');
const vertexD = new GraphVertex('D');
const vertexE = new GraphVertex('E');
const vertexF = new GraphVertex('F');
const vertexG = new GraphVertex('G');
const vertexH = new GraphVertex('H');
const edgeAC = new GraphEdge(vertexA, vertexC);
const edgeBC = new GraphEdge(vertexB, vertexC);
const edgeBD = new GraphEdge(vertexB, vertexD);
const edgeCE = new GraphEdge(vertexC, vertexE);
const edgeDF = new GraphEdge(vertexD, vertexF);
const edgeEF = new GraphEdge(vertexE, vertexF);
const edgeEH = new GraphEdge(vertexE, vertexH);
const edgeFG = new GraphEdge(vertexF, vertexG);
const graph = new Graph(true);
graph
.addEdge(edgeAC)
.addEdge(edgeBC)
.addEdge(edgeBD)
.addEdge(edgeCE)
.addEdge(edgeDF)
.addEdge(edgeEF)
.addEdge(edgeEH)
.addEdge(edgeFG);
const sortedVertices = topologicalSort(graph);
expect(sortedVertices).toBeDefined();
expect(sortedVertices.length).toBe(graph.getAllVertices().length);
expect(sortedVertices).toEqual([
vertexB,
vertexD,
vertexA,
vertexC,
vertexE,
vertexH,
vertexF,
vertexG,
]);
});
});