Use Infinity instead of zero in Graph adjacency matrix to show that vertices are not connected.

This commit is contained in:
Oleksii Trekhleb
2018-05-22 07:10:46 +03:00
parent f966ef5d5d
commit 3e0ac7486c
3 changed files with 14 additions and 15 deletions

View File

@@ -15,7 +15,7 @@ function isSafe(adjacencyMatrix, verticesIndices, cycle, vertexCandidate) {
const endVertexAdjacencyIndex = verticesIndices[endVertex.getKey()];
// Check if last vertex in the path and candidate vertex are adjacent.
if (!adjacencyMatrix[endVertexAdjacencyIndex][candidateVertexAdjacencyIndex]) {
if (adjacencyMatrix[endVertexAdjacencyIndex][candidateVertexAdjacencyIndex] === Infinity) {
return false;
}
@@ -43,7 +43,7 @@ function isCycle(adjacencyMatrix, verticesIndices, cycle) {
const endVertexAdjacencyIndex = verticesIndices[endVertex.getKey()];
// Check if we can go from end vertex to the start one.
return !!adjacencyMatrix[endVertexAdjacencyIndex][startVertexAdjacencyIndex];
return adjacencyMatrix[endVertexAdjacencyIndex][startVertexAdjacencyIndex] !== Infinity;
}
/**