贪心算法是一种在每一步选择中都采取当前最佳选择的算法,以期在整体上达到最优解。它广泛应用于各种优化问题,如最短路径、最小生成树、活动选择等。本文将介绍贪心算法的基本概念、特点、应用场景及其局限性。
贪心算法的核心思想是局部最优策略,即在每一步选择中都选择当前看起来最优的选项,希望通过一系列的局部最优选择达到全局最优。
在活动选择问题中,给定一组活动及其开始和结束时间,要求选择尽可能多的互不重叠的活动。
def activity_selection(activities): activities.sort(key=lambda x: x[1]) # 按结束时间排序 selected_activities = [activities[0]] for i in range(1, len(activities)): if activities[i][0] >= selected_activities[-1][1]: selected_activities.append(activities[i]) return selected_activities activities = [(0, 6), (1, 4), (3, 5), (5, 7), (3, 9), (5, 9), (6, 10), (8, 11), (8, 12), (2, 14), (12, 16)] selected = activity_selection(activities) print("Selected activities:", selected)
在分数背包问题中,物品可以部分装入背包。目标是选择物品使得背包中的总价值最大。
def fractional_knapsack(items, capacity): items.sort(key=lambda x: x[1] / x[0], reverse=True) # 按价值密度排序 total_value = 0.0 for weight, value in items: if capacity >= weight: total_value += value capacity -= weight else: total_value += value * (capacity / weight) break return total_value items = [(10, 60), (20, 100), (30, 120)] # (weight, value) capacity = 50 max_value = fractional_knapsack(items, capacity) print("Maximum value in knapsack:", max_value)
在图论中,最小生成树是连接所有顶点的权重最小的树。Kruskal 算法通过贪心策略选择最小边来构建最小生成树。
class DisjointSet: def __init__(self, n): self.parent = list(range(n)) self.rank = [0] * n def find(self, u): if self.parent[u] != u: self.parent[u] = self.find(self.parent[u]) return self.parent[u] def union(self, u, v): root_u = self.find(u) root_v = self.find(v) if root_u != root_v: if self.rank[root_u] > self.rank[root_v]: self.parent[root_v] = root_u elif self.rank[root_u] < self.rank[root_v]: self.parent[root_u] = root_v else: self.parent[root_v] = root_u self.rank[root_u] += 1 def kruskal(n, edges): ds = DisjointSet(n) edges.sort(key=lambda x: x[2]) mst = [] for u, v, weight in edges: if ds.find(u) != ds.find(v): ds.union(u, v) mst.append((u, v, weight)) return mst edges = [(0, 1, 10), (0, 2, 6), (0, 3, 5), (1, 3, 15), (2, 3, 4)] n = 4 # Number of vertices mst = kruskal(n, edges) print("Edges in MST:", mst)
虽然贪心算法在许多问题中表现出色,但它并不适用于所有问题。贪心算法不能保证所有情况下都能找到全局最优解。例如,在0-1背包问题中,贪心算法可能无法找到最优解。