1 of 14

Greedy algorithm

2 of 14

Definition

An algorithm that always takes the best immediate, or local, solution while finding an answer. Greedy algorithms find the overall, or globally, optimal solution for some optimization problems, but may find less-than-optimal solutions for some instances of other problems.

2

3 of 14

อัลกอริทึมเชิงละโมบ

  • เป็นวงวนที่ใช้เกณฑ์จากสภาพของปัญหา ณ ขณะนั้น เพื่อขยายผลเฉลย ในขณะเดียวกันปัญหาก็มีขนาดเล็กลง
  • ขยายผลเฉลยและลดขนาดของปัญหาไปเรื่อยๆ จนได้ผลเฉลยที่สมบูรณ์
  • โดยการขยายผลเฉลยจากสภาพปัจจุบัน (ไม่ได้มองอนาคต)
  • อันมีลักษระของความละโมบอยู่จึงเรียกว่า อัลกอริทึมเชิงละโมบ

3

4 of 14

ปัญหาต้นไม้ทวิภาค

4

ข้อมูล

ความถี่

พู

25%

ชินจัง

22%

ทิกเกอร์

20%

ดิบซี่

18%

ล้าลา

8%

ทิงกี้วิงกี้

5%

โพ

2%

ต้นไม้มีต้นทุนที่ = 2.43 ซึ่งไม่ใช่ต่ำสุด

5 of 14

Characteristic of Greedy algorithm

  • In many problems, we wish to not only find a solution, but to find the best or optimal solution.
  • A simple technique that works for some optimization problems is called the greedy technique.
  • As the name suggests, we solve a problem by being greedy—that is, choosing the best, most immediate solution (i.e. a local solution).
  • However, for some problems, this technique is not guaranteed to produce the best globally optimal solution.

5

6 of 14

ปัญหาถุงเป้ (Knapsack)

  • 0/1 Knapsack เลือกชิ้นใดก็ต้องเลือกทั้งอัน

  1. เลือกของตามมูลค่า ของแพงมาก่อน ละโมบมูลค่า
  2. เลือกของที่น้ำหนักน้อยที่สุด ต้องการจำนวนชิ้นมากที่สุด
  3. เลือกของที่มีมูลค่าต่อน้ำหนักมากที่สุด (เฉือนได้ และเฉือนไม่ได้)
  4. bounded knapsack problem (BKP) เลือกชิ้นเดียวกันหลายอันได้ภายใต้จำนวนที่กำหนด
  5. unbounded knapsack problem (UKP) เลือกชิ้นเดียวกันหลายอันได้

6

7 of 14

Knapsack Example: weight ไม่เกิน 10

1)

2)

3)

7

item

weight

value

1

3

25

2

2

20

3

1

15

4

4

40

5

5

50

8 of 14

Greedy changing-making algorithm

procedure change(c1, c2, … cr: ค่าของเหรียญ, c1 > c2 > … > cr : int){

for(i = 1 to r)

while (n >= ci) {

add a coin with value ci to the change

n = n - ci

}

}

8

Example! where c1 = 20, c2 = 15, c3 = 7, c4 = 1 and we want to give 22 “cents” in change.

  • What will this algorithm produce?
  • Is it optimal?

Example!, let c1 = 25, c2 = 10, c3 = 5, c4 = 1.

9 of 14

วิธีสั้นสุดแบบแหล่งต้นทางเดียว (shortest path)

9

10 of 14

ต้นไม้แบบทอดข้ามเล็กสุด (Minimum spanning tree)

  • A minimum spanning tree is a spanning tree of a connected, undirected graph. It connects all the vertices together with the minimal total weighting for its edges.
  • A single graph can have many different spanning trees. We can also assign a weight to each edge, which is a number representing how unfavorable it is, and use this to assign a weight to a spanning tree by computing the sum of the weights of the edges in that spanning tree. A minimum spanning tree (MST) or minimum weight spanning tree is then a spanning tree with weight less than or equal to the weight of every other spanning tree. More generally, any undirected graph (not necessarily connected) has a minimum spanning forest, which is a union of minimum spanning trees for its connected components.

10

11 of 14

Kruskal algorithm

11

12 of 14

Prim’s algorithm

12

13 of 14

Huffman code

13

14 of 14

Floyd Warshall Algorithm: All-pairs Shortest-paths

14