Greedy

本文介绍了贪婪算法的基本概念及其在解决优化问题中的应用。通过几个典型例子,如克鲁斯卡尔最小生成树算法、普里姆最小生成树算法、迪杰斯特拉最短路径算法和霍夫曼编码,阐述了如何在每一步选择当前看起来最佳的解决方案,从而最终得到全局最优解。

Greedy is an algorithmic paradigm that builds up a solution piece by piece, always choosing the next piece that offers the most obvious and immediate benefit. Greedy algorithms are used for optimization problems. An optimization problem can be solved using Greedy if the problem has the following property: At every step, we can make a choice that looks best at the moment, and by doing this we get the optimal solution of the complete problem at the end.


If a Greedy Algorithm can solve a problem, then it generally becomes the best method to solve that problem as the Greedy algorithms are in general more efficient than other techniques like Dynamic Programming. But Greedy algorithms cannot always be applied. For example, Fractional Knapsack problem can be solved using Greedy, but 0-1 Knapsack  cannot be solved using Greedy.


Following are some standard algorithms that are Greedy algorithms.


1) Kruskal’s Minimum Spanning Tree (MST): In Kruskal’s algorithm, we create a MST by picking edges one by one. The Greedy Choice is to pick the smallest weight edge that doesn’t cause a cycle in the MST constructed so far.


2) Prim’s Minimum Spanning Tree: In Prim’s algorithm also, we create a MST by picking edges one by one. We maintain two sets: set of the vertices already included in MST and the set of the vertices not yet included. The Greedy Choice is to pick the smallest weight edge that connects the two sets.


3) Dijkstra’s Shortest PathThe Dijkstra’s algorithm is very similar to Prim’s algorithm. The shortest path tree is built up, edge by edge. We maintain two sets: set of the vertices already included in the tree and the set of the vertices not yet included. The Greedy Choice is to pick the edge that connects the two sets and is on the smallest weight path from source to the set that contains not yet included vertices.


4) Huffman Coding: Huffman Coding is a loss-less compression technique. It assigns variable length bit codes to different characters. The Greedy Choice is to assign least bit length code to the most frequent character.


References

http://www.geeksforgeeks.org/fundamentals-of-algorithms/

在提供的引用中,涉及到与 -greedy 相关的主要是 Greedy 和 Epsilon - GreedyGreedy 技术常用于资源受限场景,如在智能手机和平板电脑等移动设备中,该技术通过逐个选择具有最高加权效用的应用变体,在一定程度上减少了计算复杂度和资源消耗。例如在资源有限的智能手机上同时运行多个社交、娱乐和办公应用时,Greedy 技术可发挥作用,不过 Serene Greedy 技术在其基础上更进一步,通过考虑自适应的非功能方面,进行重要性判断和应用分类,避免不必要的自适应,在保证一定效用的同时,减少对用户体验的负面影响 [^1]。 Epsilon - Greedy 是强化学习中的一种策略。强化学习关注智能体在与环境的交互中学习最优策略以最大化累积奖励,在很多领域如游戏 AI、机器人控制、自然语言处理等有广泛应用。而 Epsilon - Greedy 变体相对基本的形式稍微复杂一些,与基本的 Epsilon - Greedy 变体相比,后续讨论的变体需要优化更多的参数,有时特定的算法变体甚至可能需要在估计过程中存储值才能正常工作,这些因素会进一步增加行为策略的复杂性 [^2][^4]。 ### 代码示例(简单的 Epsilon - Greedy 策略伪代码) ```python import random # 定义 Epsilon - Greedy 策略函数 def epsilon_greedy(epsilon, actions, q_values): if random.uniform(0, 1) < epsilon: # 探索:随机选择一个动作 return random.choice(actions) else: # 利用:选择具有最高 Q 值的动作 max_q_index = q_values.index(max(q_values)) return actions[max_q_index] # 示例使用 actions = [0, 1, 2] # 动作列表 q_values = [0.1, 0.5, 0.3] # Q 值列表 epsilon = 0.2 # Epsilon 值 selected_action = epsilon_greedy(epsilon, actions, q_values) print(f"选择的动作是: {selected_action}") ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值