|
| 1 | +import GraphVertex from '../../../data-structures/graph/GraphVertex'; |
| 2 | + |
| 3 | +/** |
| 4 | + * @param {number[][]} adjacencyMatrix |
| 5 | + * @param {object} verticesIndices |
| 6 | + * @param {GraphVertex[]} cycle |
| 7 | + * @param {GraphVertex} vertexCandidate |
| 8 | + * @return {boolean} |
| 9 | + */ |
| 10 | +function isSafe(adjacencyMatrix, verticesIndices, cycle, vertexCandidate) { |
| 11 | + const endVertex = cycle[cycle.length - 1]; |
| 12 | + |
| 13 | + // Get end and candidate vertices indices in adjacency matrix. |
| 14 | + const candidateVertexAdjacencyIndex = verticesIndices[vertexCandidate.getKey()]; |
| 15 | + const endVertexAdjacencyIndex = verticesIndices[endVertex.getKey()]; |
| 16 | + |
| 17 | + // Check if last vertex in the path and candidate vertex are adjacent. |
| 18 | + if (!adjacencyMatrix[endVertexAdjacencyIndex][candidateVertexAdjacencyIndex]) { |
| 19 | + return false; |
| 20 | + } |
| 21 | + |
| 22 | + // Check if vertexCandidate is being added to the path for the first time. |
| 23 | + const candidateDuplicate = cycle.find(vertex => vertex.getKey() === vertexCandidate.getKey()); |
| 24 | + |
| 25 | + return !candidateDuplicate; |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * @param {number[][]} adjacencyMatrix |
| 30 | + * @param {object} verticesIndices |
| 31 | + * @param {GraphVertex[]} cycle |
| 32 | + * @return {boolean} |
| 33 | + */ |
| 34 | +function isCycle(adjacencyMatrix, verticesIndices, cycle) { |
| 35 | + // Check if first and last vertices in hamiltonian path are adjacent. |
| 36 | + |
| 37 | + // Get start and end vertices from the path. |
| 38 | + const startVertex = cycle[0]; |
| 39 | + const endVertex = cycle[cycle.length - 1]; |
| 40 | + |
| 41 | + // Get start/end vertices indices in adjacency matrix. |
| 42 | + const startVertexAdjacencyIndex = verticesIndices[startVertex.getKey()]; |
| 43 | + const endVertexAdjacencyIndex = verticesIndices[endVertex.getKey()]; |
| 44 | + |
| 45 | + // Check if we can go from end vertex to the start one. |
| 46 | + return !!adjacencyMatrix[endVertexAdjacencyIndex][startVertexAdjacencyIndex]; |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * @param {number[][]} adjacencyMatrix |
| 51 | + * @param {GraphVertex[]} vertices |
| 52 | + * @param {object} verticesIndices |
| 53 | + * @param {GraphVertex[][]} cycles |
| 54 | + * @param {GraphVertex[]} cycle |
| 55 | + */ |
| 56 | +function hamiltonianCycleRecursive({ |
| 57 | + adjacencyMatrix, |
| 58 | + vertices, |
| 59 | + verticesIndices, |
| 60 | + cycles, |
| 61 | + cycle, |
| 62 | +}) { |
| 63 | + // Clone cycle in order to prevent it from modification by other DFS branches. |
| 64 | + const currentCycle = [...cycle].map(vertex => new GraphVertex(vertex.value)); |
| 65 | + |
| 66 | + if (vertices.length === currentCycle.length) { |
| 67 | + // Hamiltonian path is found. |
| 68 | + // Now we need to check if it is cycle or not. |
| 69 | + if (isCycle(adjacencyMatrix, verticesIndices, currentCycle)) { |
| 70 | + // Another solution has been found. Save it. |
| 71 | + cycles.push(currentCycle); |
| 72 | + } |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + for (let vertexIndex = 0; vertexIndex < vertices.length; vertexIndex += 1) { |
| 77 | + // Get vertex candidate that we will try to put into next path step and see if it fits. |
| 78 | + const vertexCandidate = vertices[vertexIndex]; |
| 79 | + |
| 80 | + // Check if it is safe to put vertex candidate to cycle. |
| 81 | + if (isSafe(adjacencyMatrix, verticesIndices, currentCycle, vertexCandidate)) { |
| 82 | + // Add candidate vertex to cycle path. |
| 83 | + currentCycle.push(vertexCandidate); |
| 84 | + |
| 85 | + // Try to find other vertices in cycle. |
| 86 | + hamiltonianCycleRecursive({ |
| 87 | + adjacencyMatrix, |
| 88 | + vertices, |
| 89 | + verticesIndices, |
| 90 | + cycles, |
| 91 | + cycle: currentCycle, |
| 92 | + }); |
| 93 | + |
| 94 | + // Remove candidate vertex from cycle path in order to try another one. |
| 95 | + currentCycle.pop(); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +/** |
| 101 | + * @param {Graph} graph |
| 102 | + * @return {GraphVertex[][]} |
| 103 | + */ |
| 104 | +export default function hamiltonianCycle(graph) { |
| 105 | + // Gather some information about the graph that we will need to during |
| 106 | + // the problem solving. |
| 107 | + const verticesIndices = graph.getVerticesIndices(); |
| 108 | + const adjacencyMatrix = graph.getAdjacencyMatrix(); |
| 109 | + const vertices = graph.getAllVertices(); |
| 110 | + |
| 111 | + // Define start vertex. We will always pick the first one |
| 112 | + // this it doesn't matter which vertex to pick in a cycle. |
| 113 | + // Every vertex is in a cycle so we can start from any of them. |
| 114 | + const startVertex = vertices[0]; |
| 115 | + |
| 116 | + // Init cycles array that will hold all solutions. |
| 117 | + const cycles = []; |
| 118 | + |
| 119 | + // Init cycle array that will hold current cycle path. |
| 120 | + const cycle = [startVertex]; |
| 121 | + |
| 122 | + // Try to find cycles recursively in Depth First Search order. |
| 123 | + hamiltonianCycleRecursive({ |
| 124 | + adjacencyMatrix, |
| 125 | + vertices, |
| 126 | + verticesIndices, |
| 127 | + cycles, |
| 128 | + cycle, |
| 129 | + }); |
| 130 | + |
| 131 | + // Return found cycles. |
| 132 | + return cycles; |
| 133 | +} |
0 commit comments