|
| 1 | +import GraphVertex from '../../../../data-structures/graph/GraphVertex'; |
| 2 | +import GraphEdge from '../../../../data-structures/graph/GraphEdge'; |
| 3 | +import Graph from '../../../../data-structures/graph/Graph'; |
| 4 | +import kruskal from '../kruskal'; |
| 5 | + |
| 6 | +describe('kruskal', () => { |
| 7 | + it('should fire an error for directed graph', () => { |
| 8 | + function applyPrimToDirectedGraph() { |
| 9 | + const graph = new Graph(true); |
| 10 | + |
| 11 | + kruskal(graph); |
| 12 | + } |
| 13 | + |
| 14 | + expect(applyPrimToDirectedGraph).toThrowError(); |
| 15 | + }); |
| 16 | + |
| 17 | + it('should find minimum spanning tree', () => { |
| 18 | + const vertexA = new GraphVertex('A'); |
| 19 | + const vertexB = new GraphVertex('B'); |
| 20 | + const vertexC = new GraphVertex('C'); |
| 21 | + const vertexD = new GraphVertex('D'); |
| 22 | + const vertexE = new GraphVertex('E'); |
| 23 | + const vertexF = new GraphVertex('F'); |
| 24 | + const vertexG = new GraphVertex('G'); |
| 25 | + |
| 26 | + const edgeAB = new GraphEdge(vertexA, vertexB, 2); |
| 27 | + const edgeAD = new GraphEdge(vertexA, vertexD, 3); |
| 28 | + const edgeAC = new GraphEdge(vertexA, vertexC, 3); |
| 29 | + const edgeBC = new GraphEdge(vertexB, vertexC, 4); |
| 30 | + const edgeBE = new GraphEdge(vertexB, vertexE, 3); |
| 31 | + const edgeDF = new GraphEdge(vertexD, vertexF, 7); |
| 32 | + const edgeEC = new GraphEdge(vertexE, vertexC, 1); |
| 33 | + const edgeEF = new GraphEdge(vertexE, vertexF, 8); |
| 34 | + const edgeFG = new GraphEdge(vertexF, vertexG, 9); |
| 35 | + const edgeFC = new GraphEdge(vertexF, vertexC, 6); |
| 36 | + |
| 37 | + const graph = new Graph(); |
| 38 | + |
| 39 | + graph |
| 40 | + .addEdge(edgeAB) |
| 41 | + .addEdge(edgeAD) |
| 42 | + .addEdge(edgeAC) |
| 43 | + .addEdge(edgeBC) |
| 44 | + .addEdge(edgeBE) |
| 45 | + .addEdge(edgeDF) |
| 46 | + .addEdge(edgeEC) |
| 47 | + .addEdge(edgeEF) |
| 48 | + .addEdge(edgeFC) |
| 49 | + .addEdge(edgeFG); |
| 50 | + |
| 51 | + expect(graph.getWeight()).toEqual(46); |
| 52 | + |
| 53 | + const minimumSpanningTree = kruskal(graph); |
| 54 | + |
| 55 | + expect(minimumSpanningTree.getWeight()).toBe(24); |
| 56 | + expect(minimumSpanningTree.getAllVertices().length).toBe(graph.getAllVertices().length); |
| 57 | + expect(minimumSpanningTree.getAllEdges().length).toBe(graph.getAllVertices().length - 1); |
| 58 | + expect(minimumSpanningTree.toString()).toBe('E,C,A,B,D,F,G'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should find minimum spanning tree for simple graph', () => { |
| 62 | + const vertexA = new GraphVertex('A'); |
| 63 | + const vertexB = new GraphVertex('B'); |
| 64 | + const vertexC = new GraphVertex('C'); |
| 65 | + const vertexD = new GraphVertex('D'); |
| 66 | + |
| 67 | + const edgeAB = new GraphEdge(vertexA, vertexB, 1); |
| 68 | + const edgeAD = new GraphEdge(vertexA, vertexD, 3); |
| 69 | + const edgeBC = new GraphEdge(vertexB, vertexC, 1); |
| 70 | + const edgeBD = new GraphEdge(vertexB, vertexD, 3); |
| 71 | + const edgeCD = new GraphEdge(vertexC, vertexD, 1); |
| 72 | + |
| 73 | + const graph = new Graph(); |
| 74 | + |
| 75 | + graph |
| 76 | + .addEdge(edgeAB) |
| 77 | + .addEdge(edgeAD) |
| 78 | + .addEdge(edgeBC) |
| 79 | + .addEdge(edgeBD) |
| 80 | + .addEdge(edgeCD); |
| 81 | + |
| 82 | + expect(graph.getWeight()).toEqual(9); |
| 83 | + |
| 84 | + const minimumSpanningTree = kruskal(graph); |
| 85 | + |
| 86 | + expect(minimumSpanningTree.getWeight()).toBe(3); |
| 87 | + expect(minimumSpanningTree.getAllVertices().length).toBe(graph.getAllVertices().length); |
| 88 | + expect(minimumSpanningTree.getAllEdges().length).toBe(graph.getAllVertices().length - 1); |
| 89 | + expect(minimumSpanningTree.toString()).toBe('A,B,C,D'); |
| 90 | + }); |
| 91 | +}); |
0 commit comments