|
| 1 | +import { GraphVertex } from './GraphVertex'; |
| 2 | +import { GraphEdge } from './GraphEdge'; |
| 3 | + |
| 4 | +export class Graph<TVertex, TEdge> { |
| 5 | + vertices: Record<string | number, GraphVertex<TVertex, TEdge>>; |
| 6 | + edges: Record<string, GraphEdge<TVertex, TEdge>>; |
| 7 | + constructor(public isDirected = false) { |
| 8 | + this.vertices = {}; |
| 9 | + this.edges = {}; |
| 10 | + } |
| 11 | + |
| 12 | + addVertex(newVertex: GraphVertex<TVertex, TEdge>) { |
| 13 | + this.vertices[newVertex.getKey()] = newVertex; |
| 14 | + |
| 15 | + return this; |
| 16 | + } |
| 17 | + |
| 18 | + getVertexByKey(vertexKey: string | number) { |
| 19 | + return this.vertices[vertexKey]; |
| 20 | + } |
| 21 | + |
| 22 | + getNeighbors(vertex: GraphVertex<TVertex, TEdge>) { |
| 23 | + return vertex.getNeighbors(); |
| 24 | + } |
| 25 | + |
| 26 | + getAllVertices() { |
| 27 | + return Object.values(this.vertices); |
| 28 | + } |
| 29 | + |
| 30 | + getAllEdges() { |
| 31 | + return Object.values(this.edges); |
| 32 | + } |
| 33 | + |
| 34 | + addEdge(edge: GraphEdge<TVertex, TEdge>) { |
| 35 | + // Try to find and end start vertices. |
| 36 | + let startVertex = this.getVertexByKey(edge.startVertex.getKey()); |
| 37 | + let endVertex = this.getVertexByKey(edge.endVertex.getKey()); |
| 38 | + |
| 39 | + // Insert start vertex if it wasn't inserted. |
| 40 | + if (!startVertex) { |
| 41 | + this.addVertex(edge.startVertex); |
| 42 | + startVertex = this.getVertexByKey(edge.startVertex.getKey()); |
| 43 | + } |
| 44 | + |
| 45 | + // Insert end vertex if it wasn't inserted. |
| 46 | + if (!endVertex) { |
| 47 | + this.addVertex(edge.endVertex); |
| 48 | + endVertex = this.getVertexByKey(edge.endVertex.getKey()); |
| 49 | + } |
| 50 | + |
| 51 | + // Check if edge has been already added. |
| 52 | + if (this.edges[edge.getKey()]) { |
| 53 | + throw new Error('Edge has already been added before'); |
| 54 | + } else { |
| 55 | + this.edges[edge.getKey()] = edge; |
| 56 | + } |
| 57 | + |
| 58 | + // Add edge to the vertices. |
| 59 | + if (this.isDirected) { |
| 60 | + // If graph IS directed then add the edge only to start vertex. |
| 61 | + startVertex.addEdge(edge); |
| 62 | + } else { |
| 63 | + // If graph ISN'T directed then add the edge to both vertices. |
| 64 | + startVertex.addEdge(edge); |
| 65 | + endVertex.addEdge(edge); |
| 66 | + } |
| 67 | + |
| 68 | + return this; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @param {GraphEdge} edge |
| 73 | + */ |
| 74 | + deleteEdge(edge: GraphEdge<TVertex, TEdge>) { |
| 75 | + // Delete edge from the list of edges. |
| 76 | + if (this.edges[edge.getKey()]) { |
| 77 | + delete this.edges[edge.getKey()]; |
| 78 | + } else { |
| 79 | + throw new Error('Edge not found in graph'); |
| 80 | + } |
| 81 | + |
| 82 | + // Try to find and end start vertices and delete edge from them. |
| 83 | + const startVertex = this.getVertexByKey(edge.startVertex.getKey()); |
| 84 | + const endVertex = this.getVertexByKey(edge.endVertex.getKey()); |
| 85 | + |
| 86 | + startVertex.deleteEdge(edge); |
| 87 | + endVertex.deleteEdge(edge); |
| 88 | + } |
| 89 | + |
| 90 | + findEdge(startVertex: GraphVertex<TVertex, TEdge>, endVertex: GraphVertex<TVertex, TEdge>) { |
| 91 | + const vertex = this.getVertexByKey(startVertex.getKey()); |
| 92 | + |
| 93 | + if (!vertex) { |
| 94 | + return null; |
| 95 | + } |
| 96 | + |
| 97 | + return vertex.findEdge(endVertex); |
| 98 | + } |
| 99 | + |
| 100 | + // getEdgeValue() { |
| 101 | + // return this.getAllEdges().reduce((weight, graphEdge) => { |
| 102 | + // return weight + graphEdge.weight; |
| 103 | + // }, 0); |
| 104 | + // } |
| 105 | + |
| 106 | + reverse() { |
| 107 | + this.getAllEdges().forEach(edge => { |
| 108 | + // Delete straight edge from graph and from vertices. |
| 109 | + this.deleteEdge(edge); |
| 110 | + |
| 111 | + // Reverse the edge. |
| 112 | + edge.reverse(); |
| 113 | + |
| 114 | + // Add reversed edge back to the graph and its vertices. |
| 115 | + this.addEdge(edge); |
| 116 | + }); |
| 117 | + |
| 118 | + return this; |
| 119 | + } |
| 120 | + |
| 121 | + getVerticesIndices() { |
| 122 | + const verticesIndices: Record<string | number, number> = {}; |
| 123 | + this.getAllVertices().forEach((vertex, index) => { |
| 124 | + verticesIndices[vertex.getKey()] = index; |
| 125 | + }); |
| 126 | + |
| 127 | + return verticesIndices; |
| 128 | + } |
| 129 | + |
| 130 | + getAdjacencyMatrix() { |
| 131 | + const vertices = this.getAllVertices(); |
| 132 | + const verticesIndices = this.getVerticesIndices(); |
| 133 | + |
| 134 | + // Init matrix with infinities meaning that there is no ways of |
| 135 | + // getting from one vertex to another yet. |
| 136 | + const adjacencyMatrix = Array(vertices.length) |
| 137 | + .fill(null) |
| 138 | + .map(() => { |
| 139 | + return Array(vertices.length).fill(Infinity); |
| 140 | + }); |
| 141 | + |
| 142 | + // Fill the columns. |
| 143 | + vertices.forEach((vertex, vertexIndex) => { |
| 144 | + vertex.getNeighbors().forEach(neighbor => { |
| 145 | + const neighborIndex = verticesIndices[neighbor.getKey()]; |
| 146 | + adjacencyMatrix[vertexIndex][neighborIndex] = this.findEdge(vertex, neighbor)?.value; |
| 147 | + }); |
| 148 | + }); |
| 149 | + |
| 150 | + return adjacencyMatrix; |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * @return {string} |
| 155 | + */ |
| 156 | + toString() { |
| 157 | + return Object.keys(this.vertices).toString(); |
| 158 | + } |
| 159 | +} |
0 commit comments