MCPcopy Index your code
hub / github.com/trekhleb/javascript-algorithms / getAdjacencyMatrix

Method getAdjacencyMatrix

src/data-structures/graph/Graph.js:181–200  ·  view source on GitHub ↗

* @return {*[][]}

()

Source from the content-addressed store, hash-verified

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}

Callers 3

Graph.test.jsFile · 0.80
bfTravellingSalesmanFunction · 0.80
hamiltonianCycleFunction · 0.80

Calls 5

getAllVerticesMethod · 0.95
getVerticesIndicesMethod · 0.95
findEdgeMethod · 0.95
getNeighborsMethod · 0.45
getKeyMethod · 0.45

Tested by

no test coverage detected