标签:sed inter add 增加 state array station get 算法
内容:
8.1教室调度问题
贪婪算法:每步都采取绝不最优解,最终的到的就是全局最优解。
贪婪苏凡并非任何情况下都有效,但它易于实现。
8.2背包问题
贪婪算法并不能得到最优解,但是非常接近。
在有些情况下,完美是优秀的敌人。 有时,只需找到一个单只解决问题的算法啊,此时贪婪算法正好派上用场,其易于实现,得到的结果又与最优结果相当接近。
8.3近似算法
贪婪算法是一种近似算法,在获得精确解需要时间太长时,可使用近似算法。
判断近似算法优劣的标准如下:
使用贪婪算法解决广播台覆盖问题。
1 # You pass an array in, and it gets converted to a set. 2 states_needed = set(["mt", "wa", "or", "id", "nv", "ut", "ca", "az"]) 3 4 stations = {} 5 stations["kone"] = set(["id", "nv", "ut"]) 6 stations["ktwo"] = set(["wa", "id", "mt"]) 7 stations["kthree"] = set(["or", "nv", "ca"]) 8 stations["kfour"] = set(["nv", "ut"]) 9 stations["kfive"] = set(["ca", "az"]) 10 11 final_stations = set() 12 13 for station, states in stations.items(): 14 # print "states: ", states 15 print "station: ", station 16 print "\n" 17 18 while states_needed: 19 best_station = None 20 states_covered = set() 21 for station, states in stations.items():#station is index, states is element 22 covered = states_needed & states # get intersection 23 print "covered: ", covered 24 print "states_covered: ", states_covered 25 print "states: ", states 26 print "station: ", station 27 print "***********\n" 28 29 if len(covered) > len(states_covered): 30 best_station = station 31 states_covered = covered 32 33 states_needed -= states_covered 34 final_stations.add(best_station) 35 36 print final_stations
说明:
8.4 NP完全问题
8.5小结
标签:sed inter add 增加 state array station get 算法
原文地址:https://www.cnblogs.com/mofei004/p/8909139.html