Design and Analysis of Algorithms
Greedy Strategy
Dr. Lê Nguyên Khôi
The VNU University of Engineering and Technology
Contents
Problems
Coin changing
0-1 knapsack
Greedy strategy
Minimum spanning tree
Prim algorithm
Kruskal algorithm
Design and Analysis of Algorithms 1
Coin Changing
Coin type: , , , ,
Making change for cents using the
fewest number of coins
Example: change of
189 coins of
189 coins
18 coins of , 1 coin of , 4 coins of
23 coins
Design and Analysis of Algorithms 2
Coin Changing
Using the fewest number of coins?
Idea:
Use the coin with the highest denomination
(the face value)
Expectation: the fewest number of coins
Example: change of
1 coin of , 3 coins of , 1 coin of ,4
coin of
9 coins → optimal solution?
Design and Analysis of Algorithms 3
Dynamic Programming – Revisited
Applicable for optimization problems
Determine the optimal solution based on
The optimal solution of sub-problems
Overcomplicate the problem
Not applicable for real-world problems
Huge search space
Long search time
Design and Analysis of Algorithms 4
Greedy Strategy
Determine the best choice at the moment
Make a locally optimal choice
Could lead to a globally optimal solution
Could find global optimum in some cases
Effective in practice
Design and Analysis of Algorithms 5
Greedy Strategy – Dynamic Programming
Applicable for optimization problems
Dynamic programming
Bottom-up design
1. Solve smaller sub-problems
2. Determine choices
Greedy strategy
Top-down design
1. Determine choices
2. Solve smaller sub-problems
Design and Analysis of Algorithms 6
Coin Changing – Dynamic Programming
Design and Analysis of Algorithms 7
Coin Changing – Greedy Strategy
Coin type: , , ,
Making change for cents using the
fewest number of coins
Example: change of
1 coin of , 1 coins of , 8 coin of
10 coins (greedy strategy)
1 xu , 3 xu , 3 xu => 7 coins
Greedy strategy may not find the optimum
Solution of 10 coins acceptable?
Design and Analysis of Algorithms 8
0-1 Knapsack
Add items to the knapsack (for optimum)
Total weight of items does not exceed the
weight of the knapsack
Maximize profit
Problem:
items, weight with profit
Knapsack weight limit
Determine the set of items to maximize profit
Design and Analysis of Algorithms 9
0-1 Knapsack
,
A B C D E
10 20 30 40 50
20 30 66 40 50
Design and Analysis of Algorithms 10
0-1 Knapsack – Greedy Strategy
,
A B C D E
10 20 30 40 50
20 30 66 40 50
Greedy strategy based on item’s profit
Xi 0 1 1 0 1
Design and Analysis of Algorithms 11
0-1 Knapsack – Greedy Strategy
,
A B C D E
10 20 30 40 50
20 30 66 40 50
Greedy strategy based on profit-weight
Xi 1 1 1 1 0
Design and Analysis of Algorithms 12
0-1 Knapsack – Greedy Strategy
,
A B C D E
10 20 30 40 50
20 30 66 40 50
Greedy strategy based on item’s weights
Xi 1 1 1 1 0
Design and Analysis of Algorithms 13
Graph – Definition
Graph consists of
Set of vertices
Set of edges
Undirected graph
Unordered pair of vertices
Directed graph
Ordered pair of vertices
Design and Analysis of Algorithms 14
Graph
undirected directed
Cầu Giấy Cầu Giấy
ĐHQG BX Kim Mã ĐHQG BX Kim Mã
Ngã tư Sở Ngã tư Sở
Design and Analysis of Algorithms 15
Graph
weight non-weight
Cầu Giấy Cầu Giấy
5 7
ĐHQG BX Kim Mã ĐHQG BX Kim Mã
11 15
Ngã tư Sở Ngã tư Sở
Design and Analysis of Algorithms 16
Graph
connected unconnected
Design and Analysis of Algorithms 17
Minimum Spanning Tree
Input: connected and undirected graph
, and edge weights
Output: Spanning tree – a tree that
connects all vertices with minimum weight
Design and Analysis of Algorithms 18
Minimum Spanning Tree – Example
Input:
Design and Analysis of Algorithms 19
Minimum Spanning Tree – Example
Output
Design and Analysis of Algorithms 20
Optimal Structure
Remove any edge , then, tree is
divided into 2 sub-tree and
Theorem. Sub-tree is the minimum
spanning tree of , is a sub-
graph of including vertices of
đỉnh của
Similar for
Design and Analysis of Algorithms 21
Prim’s Algorithm
: tập các đỉnh kề các cạnh trong tập cạnh
Ban đầu tập chứa một đỉnh tuỳ chọn của
đồ thị , còn rỗng.
Tại mỗi bước lặp:
1. Chọn ngắn nhất, ,
2. Thêm đỉnh vào tập đỉnh
3. Thêm cạnh vào tập cạnh .
Tiếp tục phát triển cây cho tới khi , ta
nhận được là cây bao trùm
Design and Analysis of Algorithms 22
Kruskal’s Algorithm
Tập các cạnh được xây dựng dần từng
bước xuất phát từ rỗng.
Tại mỗi bước cạnh được chọn thêm
vào là cạnh ngắn nhất trong các cạnh
còn lại và không tạo thành chu trình với các
cạnh đã có trong .
Design and Analysis of Algorithms 23