* @return {*[][]}
()
| 179 | * @return {*[][]} |
| 180 | */ |
| 181 | getAdjacencyMatrix() { |
| 182 | const vertices = this.getAllVertices(); |
| 183 | const verticesIndices = this.getVerticesIndices(); |
| 184 | |
| 185 | // Init matrix with infinities meaning that there is no ways of |
| 186 | // getting from one vertex to another yet. |
| 187 | const adjacencyMatrix = Array(vertices.length).fill(null).map(() => { |
| 188 | return Array(vertices.length).fill(Infinity); |
| 189 | }); |
| 190 | |
| 191 | // Fill the columns. |
| 192 | vertices.forEach((vertex, vertexIndex) => { |
| 193 | vertex.getNeighbors().forEach((neighbor) => { |
| 194 | const neighborIndex = verticesIndices[neighbor.getKey()]; |
| 195 | adjacencyMatrix[vertexIndex][neighborIndex] = this.findEdge(vertex, neighbor).weight; |
| 196 | }); |
| 197 | }); |
| 198 | |
| 199 | return adjacencyMatrix; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * @return {string} |
no test coverage detected