Greedy策略(近似算法)
let states = new Set(['mt','wa','or','id','nv','ut','ca','az'])
let 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'])
let finalChooseStations = new Set()
while(states.size!==0){
let bestStation = null
let statesCovered = new Set()
for(let i in stations){
let covered = new Set([...states].filter(item=>stations[i].has(item)))
if(covered.size>statesCovered.size){
bestStation = i
statesCovered = covered
}
}
states = new Set([...states].filter(item=>!statesCovered.has(item)))
finalChooseStations.add(bestStation)
}
console.log(finalChooseStations)
//集合覆盖中的贪婪策略