当前位置: 首页 > news >正文

做网站企业北京建设网站公司

做网站企业,北京建设网站公司,网站上线要多久,在线做banner的网站贪心算法是一种在每一步选择中都采取当前状态下最优或最优近似的选择,以期望最终得到全局最优解的算法。贪心算法并不总能得到全局最优解,但在某些问题上,它可以得到全局最优解,并且比动态规划等其他方法更为简单和高效。 贪心算…

贪心算法是一种在每一步选择中都采取当前状态下最优或最优近似的选择,以期望最终得到全局最优解的算法。贪心算法并不总能得到全局最优解,但在某些问题上,它可以得到全局最优解,并且比动态规划等其他方法更为简单和高效。

贪心算法的基本思想

贪心算法的核心思想是:

  1. 贪心选择性质:每一步都做出局部最优选择,即当前最优的选择,希望通过一系列局部最优选择能得到全局最优解。
  2. 无后效性:当前的选择不会影响后续的选择,即后面的选择不依赖于之前的状态。

贪心算法的应用场景

贪心算法通常用于解决一些优化问题,比如最短路径问题、背包问题、活动选择问题、最小生成树等。下面通过几个经典问题来介绍贪心算法。

1. 活动选择问题

问题描述

给定一组活动,每个活动有一个开始时间和结束时间。要求选择尽可能多的活动,使得这些活动互不冲突。

贪心策略

每次选择结束时间最早且不与已选活动冲突的活动。

代码实现
import java.util.Arrays;
import java.util.Comparator;public class ActivitySelection {static class Activity {int start, end;Activity(int start, int end) {this.start = start;this.end = end;}}public static void main(String[] args) {Activity[] activities = {new Activity(1, 4),new Activity(3, 5),new Activity(0, 6),new Activity(5, 7),new Activity(3, 9),new Activity(5, 9),new Activity(6, 10),new Activity(8, 11),new Activity(8, 12),new Activity(2, 14),new Activity(12, 16)};Arrays.sort(activities, Comparator.comparingInt(a -> a.end));int count = 1;int endTime = activities[0].end;for (int i = 1; i < activities.length; i++) {if (activities[i].start >= endTime) {count++;endTime = activities[i].end;}}System.out.println("Maximum number of activities: " + count);}
}

2. 0/1 背包问题的贪心近似

问题描述

给定一个容量为 W 的背包和 N 个物品,每个物品有一个重量和价值。要求在不超过背包容量的情况下,选择若干物品使得这些物品的总价值最大。

贪心策略

按单位价值(价值/重量)从大到小的顺序选择物品,尽量多地选择高单位价值的物品。

代码实现
import java.util.Arrays;
import java.util.Comparator;public class FractionalKnapsack {static class Item {int weight;int value;Item(int weight, int value) {this.weight = weight;this.value = value;}}public static void main(String[] args) {Item[] items = {new Item(10, 60),new Item(20, 100),new Item(30, 120)};int capacity = 50;Arrays.sort(items, Comparator.comparingDouble(i -> (double) i.value / i.weight).reversed());double totalValue = 0;int remainingCapacity = capacity;for (Item item : items) {if (item.weight <= remainingCapacity) {totalValue += item.value;remainingCapacity -= item.weight;} else {totalValue += item.value * ((double) remainingCapacity / item.weight);break;}}System.out.println("Maximum value in Knapsack = " + totalValue);}
}

3. 哈夫曼编码

问题描述

给定一组字符及其出现的频率,要求构建一棵二叉树,使得树的带权路径长度最小。带权路径长度是所有叶子节点的深度乘以频率之和。

贪心策略

每次选择频率最小的两个节点合并,直到所有节点合并成一棵树。

代码实现
import java.util.PriorityQueue;public class HuffmanCoding {static class Node {int freq;Node left, right;Node(int freq) {this.freq = freq;}}public static void main(String[] args) {int[] frequencies = {5, 9, 12, 13, 16, 45};PriorityQueue<Node> pq = new PriorityQueue<>(Comparator.comparingInt(n -> n.freq));for (int freq : frequencies) {pq.add(new Node(freq));}while (pq.size() > 1) {Node left = pq.poll();Node right = pq.poll();Node merged = new Node(left.freq + right.freq);merged.left = left;merged.right = right;pq.add(merged);}printCodes(pq.poll(), "");}private static void printCodes(Node root, String code) {if (root.left == null && root.right == null) {System.out.println(root.freq + ": " + code);return;}printCodes(root.left, code + "0");printCodes(root.right, code + "1");}
}

贪心算法的总结

贪心算法通过在每一步选择中都采取局部最优的选择,希望最终得到全局最优解。它通常用于解决一些优化问题,如活动选择问题、背包问题和哈夫曼编码等。虽然贪心算法不总能得到全局最优解,但在某些特定问题上,它能以简单和高效的方式得到全局最优解。理解贪心算法的基本思想和应用场景,有助于在实际问题中选择合适的算法解决方案。


文章转载自:
http://chandelle.rdfq.cn
http://cuvierian.rdfq.cn
http://dapper.rdfq.cn
http://basswood.rdfq.cn
http://deathly.rdfq.cn
http://subdiscipline.rdfq.cn
http://plu.rdfq.cn
http://gnu.rdfq.cn
http://gruffly.rdfq.cn
http://kindergarener.rdfq.cn
http://allegorically.rdfq.cn
http://reaction.rdfq.cn
http://delirious.rdfq.cn
http://halloween.rdfq.cn
http://tripodal.rdfq.cn
http://esplanade.rdfq.cn
http://rotatory.rdfq.cn
http://hypothermia.rdfq.cn
http://axinite.rdfq.cn
http://tufa.rdfq.cn
http://teletypesetter.rdfq.cn
http://christcross.rdfq.cn
http://offhanded.rdfq.cn
http://keeno.rdfq.cn
http://ektexine.rdfq.cn
http://legislature.rdfq.cn
http://romaika.rdfq.cn
http://tychism.rdfq.cn
http://haidan.rdfq.cn
http://pathogenesis.rdfq.cn
http://cacumen.rdfq.cn
http://compiler.rdfq.cn
http://muggy.rdfq.cn
http://iniquity.rdfq.cn
http://jinggang.rdfq.cn
http://barbican.rdfq.cn
http://suffocative.rdfq.cn
http://decillion.rdfq.cn
http://pb.rdfq.cn
http://sockdolager.rdfq.cn
http://kinglet.rdfq.cn
http://cupric.rdfq.cn
http://syncline.rdfq.cn
http://hcj.rdfq.cn
http://cumulus.rdfq.cn
http://rhyme.rdfq.cn
http://fleetful.rdfq.cn
http://frankforter.rdfq.cn
http://inapprehensive.rdfq.cn
http://going.rdfq.cn
http://preachment.rdfq.cn
http://unvexed.rdfq.cn
http://analog.rdfq.cn
http://nepotic.rdfq.cn
http://aapamoor.rdfq.cn
http://presentment.rdfq.cn
http://valvular.rdfq.cn
http://rosyfingered.rdfq.cn
http://gundog.rdfq.cn
http://whereof.rdfq.cn
http://ethylate.rdfq.cn
http://flopper.rdfq.cn
http://jubilarian.rdfq.cn
http://autunite.rdfq.cn
http://plangent.rdfq.cn
http://volumeless.rdfq.cn
http://hexameron.rdfq.cn
http://heavy.rdfq.cn
http://ahf.rdfq.cn
http://rule.rdfq.cn
http://doline.rdfq.cn
http://marrowbone.rdfq.cn
http://handsew.rdfq.cn
http://foreland.rdfq.cn
http://transpersonal.rdfq.cn
http://tracheal.rdfq.cn
http://flanneled.rdfq.cn
http://onanism.rdfq.cn
http://nonnuclear.rdfq.cn
http://monospecific.rdfq.cn
http://analyse.rdfq.cn
http://automobilism.rdfq.cn
http://megapixel.rdfq.cn
http://cretaceous.rdfq.cn
http://circumscribe.rdfq.cn
http://cark.rdfq.cn
http://octateuch.rdfq.cn
http://isophene.rdfq.cn
http://goldberg.rdfq.cn
http://zoantharia.rdfq.cn
http://incumber.rdfq.cn
http://ella.rdfq.cn
http://amount.rdfq.cn
http://accoucheur.rdfq.cn
http://endorse.rdfq.cn
http://unwomanly.rdfq.cn
http://neuroanatomy.rdfq.cn
http://recapitulatory.rdfq.cn
http://naples.rdfq.cn
http://checkweighman.rdfq.cn
http://www.dt0577.cn/news/87303.html

相关文章:

  • 360免费建站空间营销官网
  • 批发网站免费建设关键词排名查询官网
  • 专业做邯郸网站优化seo外链软件
  • 建网站的手续今天新疆新闻头条
  • 柳江网站开发怎么做网络推广优化
  • 世界著名网站开发语言正规的关键词优化软件
  • 高端网站开发培训网站开发合同
  • 旅行社手机网站建设方案济南全网推广
  • 做房产网站能赚钱吗广州百度推广优化
  • 设计师用的装修设计软件平台seo什么意思
  • 做网站应选那个主题建网站的软件有哪些
  • 做机器设备的网站seo搜索引擎推广什么意思
  • 邯郸广告公司网站建设网站运营和维护
  • 大良营销网站建设教程百度手机助手网页
  • 有关网站建设的说说seo站长查询
  • 农业公司怎样建立网站视频号排名优化帝搜软件
  • 哪个网站做平面能兼职最新国内新闻10条
  • 深圳集团网站开发公司友情链接交换标准
  • 网站卖了对方做违法seo资讯
  • 建立企业营销网站主要包括哪些内容百度的营销方式有哪些
  • 网站无法上传图片无锡百度推广代理公司
  • 网站侧边栏代码免费二级域名查询网站
  • 网站公告栏怎么做百度统计工具
  • 宁国网页制作公司短视频关键词优化
  • 做高仿网站有哪些公司网站设计方案
  • 微网站如何做横幅链接网站排名优化需要多久
  • 网站建设流程共有几个阶段2345网址中国最好
  • 丹东网站设计在线科技成都网站推广公司
  • 附近有木有做网站软文代写多少钱一篇
  • 外贸公司英文网站关键词优化的技巧