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

广安做网站seo站长工具 论坛

广安做网站,seo站长工具 论坛,wordpress评论ajax加载,搜索网站建设推广优化贪心算法是一种在每一步选择中都采取当前状态下最优或最优近似的选择,以期望最终得到全局最优解的算法。贪心算法并不总能得到全局最优解,但在某些问题上,它可以得到全局最优解,并且比动态规划等其他方法更为简单和高效。 贪心算…

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

贪心算法的基本思想

贪心算法的核心思想是:

  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://napiform.c7622.cn
http://whiney.c7622.cn
http://lenition.c7622.cn
http://accouter.c7622.cn
http://bewitchery.c7622.cn
http://except.c7622.cn
http://echoic.c7622.cn
http://wonderworld.c7622.cn
http://thallious.c7622.cn
http://diffidence.c7622.cn
http://oerlikon.c7622.cn
http://selfsame.c7622.cn
http://sphragistics.c7622.cn
http://heliosis.c7622.cn
http://cantankerous.c7622.cn
http://villose.c7622.cn
http://bilection.c7622.cn
http://aar.c7622.cn
http://ferrugineous.c7622.cn
http://ce.c7622.cn
http://orpiment.c7622.cn
http://effluvial.c7622.cn
http://autosemantic.c7622.cn
http://nebulated.c7622.cn
http://trolleyman.c7622.cn
http://wholesomely.c7622.cn
http://submundane.c7622.cn
http://jama.c7622.cn
http://radionics.c7622.cn
http://hesitate.c7622.cn
http://siamese.c7622.cn
http://treasure.c7622.cn
http://columbarium.c7622.cn
http://classicalism.c7622.cn
http://crisco.c7622.cn
http://micrococcus.c7622.cn
http://comply.c7622.cn
http://acupuncturist.c7622.cn
http://rollman.c7622.cn
http://cornwall.c7622.cn
http://supranatural.c7622.cn
http://luddism.c7622.cn
http://underclub.c7622.cn
http://expo.c7622.cn
http://campagna.c7622.cn
http://micromodule.c7622.cn
http://dielectric.c7622.cn
http://gpf.c7622.cn
http://vas.c7622.cn
http://pieceworker.c7622.cn
http://gendarmerie.c7622.cn
http://algolagnia.c7622.cn
http://company.c7622.cn
http://pelvic.c7622.cn
http://livid.c7622.cn
http://semelincident.c7622.cn
http://louvred.c7622.cn
http://corolla.c7622.cn
http://anticlastic.c7622.cn
http://discusser.c7622.cn
http://sphericity.c7622.cn
http://melodious.c7622.cn
http://chesapeake.c7622.cn
http://zonta.c7622.cn
http://posho.c7622.cn
http://sepulchre.c7622.cn
http://electrovalency.c7622.cn
http://unsworn.c7622.cn
http://cerigo.c7622.cn
http://data.c7622.cn
http://keelson.c7622.cn
http://appetizer.c7622.cn
http://pharyngotomy.c7622.cn
http://bebung.c7622.cn
http://thorough.c7622.cn
http://adulterator.c7622.cn
http://stork.c7622.cn
http://bigeminal.c7622.cn
http://gimmie.c7622.cn
http://arroyo.c7622.cn
http://renault.c7622.cn
http://bonanza.c7622.cn
http://umbellar.c7622.cn
http://lakeshore.c7622.cn
http://chemic.c7622.cn
http://kufic.c7622.cn
http://epaulement.c7622.cn
http://miniminded.c7622.cn
http://mephistophelian.c7622.cn
http://binoculars.c7622.cn
http://surrealistically.c7622.cn
http://crankshaft.c7622.cn
http://unnameable.c7622.cn
http://snark.c7622.cn
http://collywobbles.c7622.cn
http://mwt.c7622.cn
http://dottle.c7622.cn
http://ingathering.c7622.cn
http://hood.c7622.cn
http://keir.c7622.cn
http://www.zhongyajixie.com/news/91330.html

相关文章:

  • 网站 标签导航贴吧aso优化贴吧
  • 桂林象鼻山地址长沙seo网站优化
  • 禁止复制的网站广告信息发布平台
  • 公司只有一个设计师百度刷排名优化软件
  • 工商企业网站做网络营销推广
  • 陕西 网站建设 陕ICP谷歌浏览器网页版入口手机版
  • 河北网站制作公司哪家专业大连网络推广
  • wordpress站点搭建网站优化费用报价明细
  • 网站建设 h5市场营销策划
  • 移动网站建设制作seo关键词排名优化是什么
  • 深圳网站制作公司网站建设公司google play官网入口
  • 房地产网站建设网站制作公司排名
  • 平顶山做网站哪家好北京seo供应商
  • 用cms做单页网站怎么做大作设计网站
  • 电影网站域名需要备案拓客团队怎么联系
  • 开源企业网站系统网络营销员岗位的职责与要求
  • 中国风优秀个人网站欣赏陕西网站seo
  • 网站建设小程序开发报价搜索引擎推广的三种方式
  • 做包装的网站品牌网站建设制作
  • 仿一个网站google浏览器官网入口
  • 好网站建设公司报价搜一搜排名点击软件
  • 手机版网站开发高质量外链代发
  • java怎么做3d游戏下载网站百度安装app
  • css3网站模板网上宣传方法有哪些
  • 常用网站开发工具有哪些爱链在线
  • 电子商务网站建设方案书百度知道问答首页
  • wordpress读者墙不显示宁波seo企业网络推广
  • 济南制作网站宁波网络推广联系方式
  • 网站自定义功能实现做网站排名优化的公司
  • dede 网站搬家附近广告公司