Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand java greedy algorithm

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about how to understand the java greedy algorithm, many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

Brief introduction of algorithm

1) greedy algorithm means that when solving the problem, the best or the best (that is, the most favorable) choice is taken in each step, and it is hoped that it can lead to the best or the best algorithm.

2) the result obtained by greedy algorithm is not necessarily the optimal result (sometimes it is the optimal solution), but it is relatively close to the optimal solution.

Application scenarios-- > Collection overrides

Public class GreedyAlgorithm {public static void main (String [] args) {/ / create a radio station and put it into Map HashMap broadcasts = new HashMap (); / / put each station into broadcasts HashSet hashSet1 = new HashSet (); hashSet1.add ("Beijing"); hashSet1.add ("Shanghai") HashSet1.add (Tianjin); HashSet hashSet2 = new HashSet (); hashSet2.add (Guangzhou); hashSet2.add (Shanghai); hashSet2.add (Tianjin); HashSet hashSet3 = new HashSet (); hashSet3.add (Chengdu) HashSet3.add (Shanghai); hashSet3.add (Hangzhou); HashSet hashSet4 = new HashSet (); hashSet4.add (Shanghai); hashSet4.add (Tianjin); HashSet hashSet5 = new HashSet (); hashSet5.add (Hangzhou) HashSet5.add ("Dalian"); / / add to map broadcasts.put ("K1", hashSet1); broadcasts.put ("K2", hashSet2); broadcasts.put ("K3", hashSet3); broadcasts.put ("K4", hashSet4); broadcasts.put ("K5", hashSet5) / / allAreas, store all regions HashSet allAreas = new HashSet (); allAreas.add ("Beijing"); allAreas.add ("Shanghai"); allAreas.add ("Tianjin"); allAreas.add ("Guangzhou"); allAreas.add ("Shenzhen") AllAreas.add ("Chengdu"); allAreas.add ("Hangzhou"); allAreas.add ("Dalian"); / / create an ArrayList to store the selected radio collection ArrayList selects = new ArrayList () / / defines a temporary collection that, during traversal, stores the intersection of areas covered by radio stations during traversal and areas not currently covered by HashSet tempSet = new HashSet () / / define a maxKey and save it in a traversal process, which can cover the key of the corresponding radio station in the most uncovered areas / / if the maxKey is not null, it will be added to selects String maxKey = null While (allAreas.size ()! = 0) {/ / if allAreas is not 0, it means that all regions have not been covered / / every time while is performed, maxKey needs to be left empty maxKey = null / / iterate through the broadcasts, and take out the corresponding key for (String key: broadcasts.keySet ()) {/ / each for tempSet.clear () / / current areas covered by this key HashSet areas = broadcasts.get (key); tempSet.addAll (areas) / / find out the intersection of tempSet and allAreas sets, and assign the set to tempSet tempSet.retainAll (allAreas) / / the purpose of the retainAll method is to find the intersection / / if the current set contains the number of uncovered areas More / / than the collection area pointed to by maxKey, you need to reset maxKey / / tempSet.size () > broadcasts.get (maxKey). Size () reflects the characteristics of greedy algorithm. Choose the best if (tempSet.size () > 0 & & (maxKey = = null | | tempSet.size () > broadcasts.get (maxKey) .size ()) {maxKey = key) each time. }} / / maxKey! = null, you should add maxKey to selects if (maxKey! = null) {selects.add (maxKey) / / remove allAreas.removeAll (broadcasts.get (maxKey)) from allAreas in the area covered by the radio station pointed to by maxKey;}} System.out.println ("the selection result is" + selects ") }} after reading the above, do you have any further understanding of how to understand the java greedy algorithm? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report