Greedy algorithm basis

how are you

basic introduction

  1. Greedy algorithm (a greedy algorithm) refers to when the problem is solved,At each step in the selection we have taken the best or optimal (ie most advantageous) choicesIn the hope that can lead to the best result or the best algorithm
  2. The results obtained by the greedy algorithm is not necessarily optimal results (sometimes is the optimal solution), but the results are relatively close to the optimal solution

Scenarios - set covering problem

[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-uvI9HbqL-1579084946341) (images / 12.png)]

analysis of idea

  1. How to find radio stations covering all areas of the collection it, use <Exhaustive method> Implementation, each set of lists of possible broadcast station, which is called power set. Suppose there are n is the total number of the broadcast station, the broadcast station is a total 2ⁿ -1, can be calculated assuming 10 per subset, as shown:

[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-HmwS6jNW-1579084946343) (images / 13.png)]

2.Greedy algorithms, high efficiency:

  1. Through all the radio stations, findA cover areas not covered by mostRadio (this radio station may include some areas already covered, but it does not matter)
  2. The radio station will be added to a collection (such as ArrayList),Find a way to cover parts of the radio station when comparing removed at the next (decreasing data in ArrayList)
  3. The first step is repeated until the entire area covered

[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-a8Pop9Rl-1579084946344) (images / 14.png)]

Code

package L十大算法.Greedy;

import java.util.*;

/**
 * @Author Zhou  jian
 * @Date 2020 ${month}  2020/1/15 0015  14:30
 */
public class GreedyAlgrothim {

    public static void main(String[] args) {

        //创建电台
        HashMap<String,HashSet<String>> brodacasts = new HashMap<>();
        //将各个电台放入
        HashSet<String> hashSet1 = new HashSet<>();
        hashSet1.add("北京");
        hashSet1.add("上海");
        hashSet1.add("天津");

        HashSet<String> hashSet2 = new HashSet<>();
        hashSet2.add("广州");
        hashSet2.add("北京");
        hashSet2.add("深圳");


        HashSet<String> hashSet3= new HashSet<>();
        hashSet3.add("成都");
        hashSet3.add("上海");
        hashSet3.add("杭州");

        HashSet<String> hashSet4 = new HashSet<>();
        hashSet4.add("上海");
        hashSet4.add("天津");

        HashSet<String> hashSet5 = new HashSet<>();
        hashSet5.add("杭州");
        hashSet5.add("大连");

        brodacasts.put("k1",hashSet1);
        brodacasts.put("k2",hashSet2);
        brodacasts.put("k3",hashSet3);
        brodacasts.put("k4",hashSet4);
        brodacasts.put("k5",hashSet5);

        //间allArratys添加所有城市
        HashSet<String> allAreas = new HashSet<>();
        for(Map.Entry<String,HashSet<String>> set : brodacasts.entrySet()){
            allAreas.addAll(set.getValue());
        }

        //创建一个ArrayList。存放选择的电台集合
        ArrayList<String>  selects = new ArrayList<>();

        //定义一个临时集合,在遍历过程中,存放遍历过程中电台覆盖地区和当前还没有覆盖地区的交集
        HashSet<String> tempSet = new HashSet<>();



        //定义maxKey:保存在一次遍历过程中,能够覆盖最大未覆盖地区对应的电台的key
        //如果maxKey!=null则会将maxKey加入到selects
        String  maxKey = null;

        while(allAreas.size()!=0){//如果allAreas不为0则表示还没有覆盖所有地区
            //每进行一次maxKey=null;
            maxKey=null;

            //遍历broadcats,取出对应的key
            for(String key:brodacasts.keySet()){

                //梅金星一次for将tempSet情况
                tempSet.clear();
                //当前广播能覆盖的地区
                HashSet<String>  areas = brodacasts.get(key);
                tempSet.addAll(areas);
                //取交集,取当前广播电台和需要广播地区的重合数量,交集会赋值给tempSet
                tempSet.retainAll(allAreas);
                //如果当前包含的集合包含未覆盖地区的数量比maxKey指向的集合未覆盖的地区还多
                //就需要重置maxKey
                //tempSet.size()>0&&(maxKey==null || tempSet.size()>brodacasts.get(maxKey).size())体现出贪心算法核新
                if(tempSet.size()>0&&(maxKey==null || tempSet.size()>brodacasts.get(maxKey).size())){
                    maxKey = key;
                }
            }

            //maxKey!=null,应该将maxKey加入到selects
            if(maxKey!=null){
                selects.add(maxKey);
                //将maxKey执行的广播电台从allAreas清掉
               allAreas.removeAll(brodacasts.get(maxKey));
            }
        }


        System.out.println("得到的选择结果"+selects);//k1 k2 k3 k5
        

    }
}

Notes and details

  1. The results obtained by the greedy algorithm is not necessarily optimal results (sometimes is the optimal solution), but are relatively similar solution (close) the results of the optimal solution



Published 101 original articles · won praise 17 · views 10000 +

Guess you like

Origin blog.csdn.net/ZHOUJIAN_TANK/article/details/103994414