# 使用贪心算法解决集合覆盖问题

`states_need = set(["mt", "wa", "or", "id", "nv", "ut", "ca", "az"]) # 传入一个数组， 它被转换为集合`

```#!/usr/bin/env
python# coding:utf-8states_need = set(["mt", "wa", "or", "id", "nv",
"ut", "ca", "az"]) # 传入一个数组， 它被转换为集合# 可供选择的广播台清单stations =
{}stations["kone"] = set(["id", "nv", "ut"])stations["ktwo"] =
set(["wa", "id", "mt"])stations["kthree"] = set(["or", "nv",
"ca"])stations["kfour"] = set(["nv", "ut"])stations["kfive"] =
set(["ca", "az"])print(stations)# 最终选择的广播台集合final_stations = set()while
states_need:best_station = Nonestates_covered = set()for station,
states_for_station in stations.items():covered = states_need &
states_for_station #

len(covered) > len(states_covered):best_station =
stationstates_covered = coveredstates_need -=

```{'kfive':
set(['ca', 'az']), 'ktwo': set(['mt', 'id', 'wa']), 'kthree':
set(['ca', 'or', 'nv']), 'kone': set(['ut', 'id', 'nv']), 'kfour':
set(['ut', 'nv'])}('states_need:', set(['wa', 'ut', 'ca', 'id', 'mt',
'az', 'or', 'nv']), 'states_for_station:', set(['ca', 'az']),
'covered:', set(['ca', 'az']))('states_needed:', set(['wa', 'ut', 'id',
'mt', 'or', 'nv']), 'best_station:', 'kfive', 'final_stations:',
set(['kfive']))---('states_need:', set(['wa', 'ut', 'id', 'mt', 'or',
'nv']), 'states_for_station:', set(['mt', 'id', 'wa']), 'covered:',
set(['mt', 'id', 'wa']))('states_needed:', set(['ut', 'or', 'nv']),
'best_station:', 'ktwo', 'final_stations:', set(['ktwo',
'kfive']))---('states_need:', set(['ut', 'or', 'nv']),
'states_for_station:', set(['ca', 'or', 'nv']), 'covered:', set(['or',
'nv']))('states_needed:', set(['ut', 'or', 'nv']), 'best_station:',
'ktwo', 'final_stations:', set(['ktwo', 'kfive']))---('states_need:',
set(['ut', 'or', 'nv']), 'states_for_station:', set(['ut', 'id', 'nv']),
'covered:', set(['ut', 'nv']))('states_needed:', set(['ut', 'or',
'nv']), 'best_station:', 'ktwo', 'final_stations:', set(['ktwo',
'kfive']))---('states_need:', set(['ut', 'or', 'nv']),
'states_for_station:', set(['ut', 'nv']), 'covered:', set(['ut',
'nv']))('states_needed:', set(['ut', 'or', 'nv']), 'best_station:',
'ktwo', 'final_stations:', set(['ktwo', 'kfive']))---('states_need:',
set(['ut', 'or', 'nv']), 'states_for_station:', set(['ca', 'az']),
'covered:', set([]))('states_needed:', set(['ut', 'or', 'nv']),
'best_station:', None, 'final_stations:', set(['ktwo', None,
'kfive']))---('states_need:', set(['ut', 'or', 'nv']),
'states_for_station:', set(['mt', 'id', 'wa']), 'covered:',
set([]))('states_needed:', set(['ut', 'or', 'nv']), 'best_station:',
None, 'final_stations:', set(['ktwo', None,
'kfive']))---('states_need:', set(['ut', 'or', 'nv']),
'states_for_station:', set(['ca', 'or', 'nv']), 'covered:', set(['or',
'nv']))('states_needed:', set(['ut']), 'best_station:', 'kthree',
'final_stations:', set(['ktwo', 'kthree', None,
'kfive']))---('states_need:', set(['ut']), 'states_for_station:',
set(['ut', 'id', 'nv']), 'covered:', set(['ut']))('states_needed:',
set(['ut']), 'best_station:', 'kthree', 'final_stations:', set(['ktwo',
'kthree', None, 'kfive']))---('states_need:', set(['ut']),
'states_for_station:', set(['ut', 'nv']), 'covered:',
set(['ut']))('states_needed:', set(['ut']), 'best_station:', 'kthree',
'final_stations:', set(['ktwo', 'kthree', None,
'kfive']))---('states_need:', set(['ut']), 'states_for_station:',
set(['ca', 'az']), 'covered:', set([]))('states_needed:', set(['ut']),
'best_station:', None, 'final_stations:', set(['ktwo', 'kthree', None,
'kfive']))---('states_need:', set(['ut']), 'states_for_station:',
set(['mt', 'id', 'wa']), 'covered:', set([]))('states_needed:',
set(['ut']), 'best_station:', None, 'final_stations:', set(['ktwo',
'kthree', None, 'kfive']))---('states_need:', set(['ut']),
'states_for_station:', set(['ca', 'or', 'nv']), 'covered:',
set([]))('states_needed:', set(['ut']), 'best_station:', None,
'final_stations:', set(['ktwo', 'kthree', None,
'kfive']))---('states_need:', set(['ut']), 'states_for_station:',
set(['ut', 'id', 'nv']), 'covered:', set(['ut']))('states_needed:',
set([]), 'best_station:', 'kone', 'final_stations:', set(['ktwo',
'kthree', None, 'kfive', 'kone']))---('states_need:', set([]),
'states_for_station:', set(['ut', 'nv']), 'covered:',
set([]))('states_needed:', set([]), 'best_station:', 'kone',
'final_stations:', set(['ktwo', 'kthree', None, 'kfive',
'kone']))---('Final_stations:', set(['ktwo', 'kthree', None, 'kfive',
'kone']))```

• 博文量
1498
• 访问量
14270350