|
| 1 | +/** |
| 2 | + * Using the JavaScript language, have the function cityTraffic(strArr) read |
| 3 | + * strArr which will be a representation of an undirected graph in a form |
| 4 | + * similar to an adjacency list. Each element in the input will contain an |
| 5 | + * integer which will represent the population for that city, and then that will |
| 6 | + * be followed by a comma separated list of its neighboring cities and their |
| 7 | + * populations (these will be separated by a colon). For example: strArr may be |
| 8 | + * |
| 9 | + * ["1:[5]", "4:[5]", "3:[5]", "5:[1,4,3,2]", "2:[5,15,7]", "7:[2,8]", |
| 10 | + * "8:[7,38]", "15:[2]", "38:[8]"]. This graph then looks like the following |
| 11 | + * picture: |
| 12 | + * |
| 13 | + * [Image of cities graph](https://i.imgur.com/5xbQDUY.jpg) |
| 14 | + * |
| 15 | + * Each node represents the population of that city and each edge represents a |
| 16 | + * road to that city. Your goal is to determine the maximum traffic that would |
| 17 | + * occur via a single road if everyone decided to go to that city. For example: |
| 18 | + * if every single person in all the cities decided to go to city 7, then via |
| 19 | + * the upper road the number of people coming in would be (8 + 38) = 46. If all |
| 20 | + * the cities beneath city 7 decided to go to it via the lower road, the number |
| 21 | + * of people coming in would be (2 + 15 + 1 + 3 + 4 + 5) = 30. So the maximum |
| 22 | + * traffic coming into the city 7 would be 46 because the maximum value of (30, |
| 23 | + * 46) = 46. |
| 24 | + * |
| 25 | + * Your program should determine the maximum traffic for every single city and |
| 26 | + * return the answers in a comma separated string in the format: |
| 27 | + * city:max_traffic,city:max_traffic,... The cities should be outputted in |
| 28 | + * sorted order by the city number. For the above example, the output would |
| 29 | + * therefore be: 1:82,2:53,3:80,4:79,5:70,7:46,8:38,15:68,38:45. The cities will |
| 30 | + * all be unique positive integers and there will not be any cycles in the |
| 31 | + * graph. There will always be at least 2 cities in the graph. |
| 32 | + * |
| 33 | + * https://www.coderbyte.com/results/bhanson:City%20Traffic:JavaScript |
| 34 | + * |
| 35 | + * @param {array} strArr |
| 36 | + * @return {string} |
| 37 | + */ |
| 38 | +function cityTraffic(strArr) { |
| 39 | + 'use strict'; |
| 40 | + |
| 41 | + const graph = new Cities(); |
| 42 | + |
| 43 | + // Populate graph with nodes and connections |
| 44 | + const reverseConnections = []; |
| 45 | + strArr.forEach(city => { |
| 46 | + let [namePopulation, edges] = city.split(':'); |
| 47 | + namePopulation = Number(namePopulation); |
| 48 | + edges = JSON.parse(edges); |
| 49 | + |
| 50 | + const cityNode = graph.addNode(namePopulation); |
| 51 | + |
| 52 | + edges.forEach(edge => { |
| 53 | + cityNode.addEdge(edge); |
| 54 | + // Cannot add bidirectional connections yet because not all nodes |
| 55 | + // are created at this time |
| 56 | + reverseConnections.push([edge, namePopulation]); |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + // Add bidirectional connections |
| 61 | + reverseConnections.forEach(connection => { |
| 62 | + const [nodeA, nodeB] = connection; |
| 63 | + graph.node(nodeA).addEdge(nodeB); |
| 64 | + }); |
| 65 | + |
| 66 | + const trafficData = []; |
| 67 | + |
| 68 | + // Get max traffic of each city |
| 69 | + for (const city of graph) { |
| 70 | + const [namePopulation, cityNode] = city; |
| 71 | + const maxTraffic = graph.maxTrafficFromOneRoad(namePopulation); |
| 72 | + trafficData.push({ namePopulation, maxTraffic }); |
| 73 | + } |
| 74 | + |
| 75 | + trafficData.sort((a, b) => { |
| 76 | + return a.namePopulation - b.namePopulation; |
| 77 | + }); |
| 78 | + |
| 79 | + const trafficDataString = trafficData |
| 80 | + .map(city => `${city.namePopulation}:${city.maxTraffic}`) |
| 81 | + .join(','); |
| 82 | + |
| 83 | + return trafficDataString; |
| 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 | +class Graph { |
| 102 | + constructor() { |
| 103 | + this.nodes = new Map(); |
| 104 | + } |
| 105 | + |
| 106 | + *[Symbol.iterator]() { |
| 107 | + const nodeItr = this.nodes[Symbol.iterator](); |
| 108 | + for (const node of nodeItr) { |
| 109 | + yield node; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + addNode(key) { |
| 114 | + this.nodes.set(key, new Node(key)); |
| 115 | + return this.node(key); |
| 116 | + } |
| 117 | + |
| 118 | + node(key) { |
| 119 | + return this.nodes.get(key); |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +class Cities extends Graph { |
| 124 | + constructor() { |
| 125 | + super(); |
| 126 | + } |
| 127 | + |
| 128 | + maxTrafficFromOneRoad(key) { |
| 129 | + const city = this.node(key); |
| 130 | + |
| 131 | + let edgeTraffic = []; |
| 132 | + city.edges.forEach((_, edgeKey) => { |
| 133 | + edgeTraffic.push(this.sumTrafficFromAllRoads(edgeKey, [key])); |
| 134 | + }); |
| 135 | + |
| 136 | + if (edgeTraffic.length === 0) { |
| 137 | + return 0; |
| 138 | + } |
| 139 | + |
| 140 | + return Math.max(...edgeTraffic); |
| 141 | + } |
| 142 | + |
| 143 | + sumTrafficFromAllRoads(key, visited = []) { |
| 144 | + const city = this.node(key); |
| 145 | + |
| 146 | + visited = visited.slice(); |
| 147 | + visited.push(key); |
| 148 | + |
| 149 | + let edgeTraffic = []; |
| 150 | + city.edges.forEach((weight, edgeKey) => { |
| 151 | + if (!visited.includes(edgeKey)) { |
| 152 | + edgeTraffic.push(this.sumTrafficFromAllRoads(edgeKey, visited)); |
| 153 | + } |
| 154 | + }); |
| 155 | + |
| 156 | + if (edgeTraffic.length === 0) { |
| 157 | + return key; |
| 158 | + } |
| 159 | + |
| 160 | + const edgeSum = edgeTraffic.reduce((sum, val) => (sum += val), 0); |
| 161 | + |
| 162 | + return edgeSum + key; |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +module.exports = cityTraffic; |
0 commit comments