Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions 08_greedy_algorithms/ES6/01_set_covering.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
// You pass an array in, and it gets converted to a set.
let statesNeeded = new Set(['mt', 'wa', 'or', 'id', 'nv', 'ut', 'ca', 'az']);
let statesNeeded = new Set(["mt", "wa", "or", "id", "nv", "ut", "ca", "az"]);

const stations = {};
stations.kone = new Set(['id', 'nv', 'ut']);
stations.ktwo = new Set(['wa', 'id', 'mt']);
stations.kthree = new Set(['or', 'nv', 'ca']);
stations.kfour = new Set(['nv', 'ut']);
stations.kfive = new Set(['ca', 'az']);
stations.kone = new Set(["id", "nv", "ut"]);
stations.ktwo = new Set(["wa", "id", "mt"]);
stations.kthree = new Set(["or", "nv", "ca"]);
stations.kfour = new Set(["nv", "ut"]);
stations.kfive = new Set(["ca", "az"]);

const finalStations = new Set();


while (statesNeeded.size) {
let bestStation = null;
let statesCovered = new Set();
Object.keys(stations).forEach((station) => {
Object.keys(stations).forEach(station => {
const states = stations[station];
const covered = new Set([...statesNeeded].filter(x => states.has(x)));
if (covered.size > statesCovered.size) {
Expand Down
29 changes: 14 additions & 15 deletions 08_greedy_algorithms/javascript/01_set_covering.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
"use strict";

// You pass an array in, and it gets converted to a set.
let states_needed = new Set(["mt", "wa", "or", "id", "nv", "ut", "ca", "az"]);
let statesNeeded = new Set(["mt", "wa", "or", "id", "nv", "ut", "ca", "az"]);

const stations = {};
stations["kone"] = new Set(["id", "nv", "ut"]);
Expand All @@ -10,22 +10,21 @@ stations["kthree"] = new Set(["or", "nv", "ca"]);
stations["kfour"] = new Set(["nv", "ut"]);
stations["kfive"] = new Set(["ca", "az"]);

const final_stations = new Set();
const finalStations = new Set();


while (states_needed.size) {
let best_station = null;
let states_covered = new Set();
while (statesNeeded.size) {
let bestStation = null;
let statesCovered = new Set();
for (let station in stations) {
let states = stations[station];
let covered = new Set([...states_needed].filter((x) => states.has(x)));
if (covered.size > states_covered.size) {
best_station = station;
states_covered = covered;
const states = stations[station];
const covered = new Set([...statesNeeded].filter(x => states.has(x)));
if (covered.size > statesCovered.size) {
bestStation = station;
statesCovered = covered;
}
}
states_needed = new Set([...states_needed].filter((x) => !states_covered.has(x)));
final_stations.add(best_station);
statesNeeded = new Set([...statesNeeded].filter(x => !statesCovered.has(x)));
finalStations.add(bestStation);
}

console.log(final_stations); // Set { 'kone', 'ktwo', 'kthree', 'kfive' }
console.log(finalStations); // Set { 'kone', 'ktwo', 'kthree', 'kfive' }