|
| 1 | +/** |
| 2 | + * Using the JavaScript language, have the function hamiltonianPath(strArr) take |
| 3 | + * strArr which will be an array of length three. The first part of the array |
| 4 | + * will be a list of vertices in a graph in the form (A,B,C,...), the second |
| 5 | + * part of the array will be the edges connecting the vertices in the form |
| 6 | + * (A-B,C-D,...) where each edge is bidirectional. The last part of the array |
| 7 | + * will be a set of vertices in the form (X,Y,Z,...) and your program will have |
| 8 | + * to determine whether or not the set of vertices given forms a Hamiltonian |
| 9 | + * path on the graph which means that every vertex in the graph is visited only |
| 10 | + * once in the order given. |
| 11 | + * |
| 12 | + * For example: if strArr is ["(A,B,C,D)","(A-B,A-D,B-D,A-C)","(C,A,D,B)"] then |
| 13 | + * the vertices (C,A,D,B) in this order do in fact form a Hamiltonian path on |
| 14 | + * the graph so your program should return the string yes. If for example the |
| 15 | + * last part of the array was (D,A,B,C) then this doesn't form a Hamiltonian |
| 16 | + * path because once you get to B you can't get to C in the graph without |
| 17 | + * passing through the visited vertices again. Here your program should return |
| 18 | + * the vertex where the path had to terminate, in this case your program should |
| 19 | + * return B. |
| 20 | + * |
| 21 | + * The graph will have at least 2 vertices and all the vertices in the graph |
| 22 | + * will be connected. |
| 23 | + * |
| 24 | + * https://www.coderbyte.com/information/Hamiltonian%20Path |
| 25 | + * |
| 26 | + * @param {array} strArr |
| 27 | + * @return {string} |
| 28 | + */ |
| 29 | +function hamiltonianPath(strArr) { |
| 30 | + // Parse input |
| 31 | + let [nodes, edges, path] = strArr.map(str => |
| 32 | + str.substr(1, str.length - 2).split(',') |
| 33 | + ); |
| 34 | + |
| 35 | + const graph = new Graph(); |
| 36 | + |
| 37 | + // Add empty nodes |
| 38 | + nodes.forEach(key => { |
| 39 | + graph.addNode(key); |
| 40 | + }); |
| 41 | + |
| 42 | + // Add edges |
| 43 | + edges.forEach(edge => { |
| 44 | + const [nodeA, nodeB] = edge.split('-'); |
| 45 | + graph.node(nodeA).addEdge(nodeB); |
| 46 | + graph.node(nodeB).addEdge(nodeA); |
| 47 | + }); |
| 48 | + |
| 49 | + // https://en.wikipedia.org/wiki/Hamiltonian_path |
| 50 | + |
| 51 | + const missingConnections = graph.getMissingPathConnections(path); |
| 52 | + |
| 53 | + if (missingConnections.length === 0) { |
| 54 | + return 'yes'; |
| 55 | + } |
| 56 | + return missingConnections[0]; |
| 57 | +} |
| 58 | + |
| 59 | +class Graph { |
| 60 | + constructor() { |
| 61 | + this.nodes = new Map(); |
| 62 | + } |
| 63 | + |
| 64 | + addNode(key) { |
| 65 | + this.nodes.set(key, new Node(key)); |
| 66 | + return this.node(key); |
| 67 | + } |
| 68 | + |
| 69 | + node(key) { |
| 70 | + return this.nodes.get(key); |
| 71 | + } |
| 72 | + |
| 73 | + getMissingPathConnections(nodeKeyArr) { |
| 74 | + const missingConnections = []; |
| 75 | + for (let i = 1; i < nodeKeyArr.length; i++) { |
| 76 | + const nodeA = this.node(nodeKeyArr[i - 1]); |
| 77 | + const keyB = nodeKeyArr[i]; |
| 78 | + if (!nodeA.hasEdge(keyB)) { |
| 79 | + missingConnections.push(nodeA.key); |
| 80 | + } |
| 81 | + } |
| 82 | + return missingConnections; |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +class Node { |
| 87 | + constructor(key) { |
| 88 | + this.key = key; |
| 89 | + this.edges = new Map(); |
| 90 | + } |
| 91 | + |
| 92 | + addEdge(key, weight = 1) { |
| 93 | + this.edges.set(key, weight); |
| 94 | + } |
| 95 | + |
| 96 | + hasEdge(key) { |
| 97 | + return this.edges.has(key); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +module.exports = hamiltonianPath; |
0 commit comments