From 3e0ac7486c9eaa000f9900c84f929ccd0d91bab4 Mon Sep 17 00:00:00 2001 From: Oleksii Trekhleb Date: Tue, 22 May 2018 07:10:46 +0300 Subject: [PATCH] Use Infinity instead of zero in Graph adjacency matrix to show that vertices are not connected. --- .../graph/hamiltonian-cycle/hamiltonianCycle.js | 4 ++-- src/data-structures/graph/Graph.js | 9 ++++----- src/data-structures/graph/__test__/Graph.test.js | 16 ++++++++-------- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/algorithms/graph/hamiltonian-cycle/hamiltonianCycle.js b/src/algorithms/graph/hamiltonian-cycle/hamiltonianCycle.js index 84562615..329821fe 100644 --- a/src/algorithms/graph/hamiltonian-cycle/hamiltonianCycle.js +++ b/src/algorithms/graph/hamiltonian-cycle/hamiltonianCycle.js @@ -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; } /** diff --git a/src/data-structures/graph/Graph.js b/src/data-structures/graph/Graph.js index b3ee8730..d9ac9089 100644 --- a/src/data-structures/graph/Graph.js +++ b/src/data-structures/graph/Graph.js @@ -177,18 +177,17 @@ export default class Graph { const vertices = this.getAllVertices(); const verticesIndices = this.getVerticesIndices(); - // Init matrix with zeros. + // Init matrix with infinities meaning that there is no ways of + // getting from one vertex to another yet. const adjacencyMatrix = Array(vertices.length).fill(null).map(() => { - return Array(vertices.length).fill(0); + return Array(vertices.length).fill(Infinity); }); // Fill the columns. vertices.forEach((vertex, vertexIndex) => { vertex.getNeighbors().forEach((neighbor) => { const neighborIndex = verticesIndices[neighbor.getKey()]; - adjacencyMatrix[vertexIndex][neighborIndex] = this.isDirected ? - this.findEdge(vertex, neighbor).weight : - 1; + adjacencyMatrix[vertexIndex][neighborIndex] = this.findEdge(vertex, neighbor).weight; }); }); diff --git a/src/data-structures/graph/__test__/Graph.test.js b/src/data-structures/graph/__test__/Graph.test.js index e89093b8..1c6592ba 100644 --- a/src/data-structures/graph/__test__/Graph.test.js +++ b/src/data-structures/graph/__test__/Graph.test.js @@ -348,10 +348,10 @@ describe('Graph', () => { const adjacencyMatrix = graph.getAdjacencyMatrix(); expect(adjacencyMatrix).toEqual([ - [0, 1, 0, 0], - [1, 0, 1, 1], - [0, 1, 0, 1], - [0, 1, 1, 0], + [Infinity, 0, Infinity, Infinity], + [0, Infinity, 0, 0], + [Infinity, 0, Infinity, 0], + [Infinity, 0, 0, Infinity], ]); }); @@ -375,10 +375,10 @@ describe('Graph', () => { const adjacencyMatrix = graph.getAdjacencyMatrix(); expect(adjacencyMatrix).toEqual([ - [0, 2, 0, 0], - [0, 0, 1, 7], - [0, 0, 0, 5], - [0, 0, 0, 0], + [Infinity, 2, Infinity, Infinity], + [Infinity, Infinity, 1, 7], + [Infinity, Infinity, Infinity, 5], + [Infinity, Infinity, Infinity, Infinity], ]); }); });