# CSED101 INTRODUCTION TO COMPUTING GREEDY APPROACH, DIVIDE AND CONQUER Hwanjo Yu.

## Presentation on theme: "CSED101 INTRODUCTION TO COMPUTING GREEDY APPROACH, DIVIDE AND CONQUER Hwanjo Yu."— Presentation transcript:

CSED101 INTRODUCTION TO COMPUTING GREEDY APPROACH, DIVIDE AND CONQUER Hwanjo Yu

Design of Algorithm Computing problem Given inputs, how to generate the desirable outputs? Algorithm Sequence of instructions or process to solve a computing problem Design of algorithm There could be many ways to solve a computing problem. Each way may take a different amount of time and generate different results –2–2

Greedy approach When designing an algorithm, greedy approach does NOT look at the problem in a holistic way. Instead, it just follows the LOCALLY optimal way at each step. Thus, it may NOT find GLOBALLY OPTIMAL solution. E.g., finding the shortest path from Pohang to Seoul To find the optimal (or the shortest) path, one needs to compute the distance of every possible path from Pohang to Seoul. Greedy approach selects the shortest way to Seoul at each step. –3–3

Shortest path from Pohang to Seoul –4–4 PohangSeoul Found path = optimal path

Shortest path from Pohang to Seoul –5–5 PohangSeoul Optimal path Found path better path

Coin problem We want to minimize the number of coins to make a certain amount of money We have three types of coins - 100, 50, and 10 won. We want to make 160 wons Greedy approach: Use the largest coin first that does not exceed the target money Select one 100 Select one 50 Select one 10 Total three coins => Optimal –6–6

Coin problem What if the three types of coins we have are 120, 50, and 10 wons? Greedy approach to make 160 wons Select one 120 Select four 10 Total five coins we consumed. The optimal solution in this case is three 50 and one 10 wons => total four coins consumed –7–7

Conclusion: Greedy approach Greedy approach Normally find a good solution which is sometimes optimal and sometimes not –8–8

Divide and conquer Divide a big problem into several small problems Induce the solution of the big problem from solutions of small problems –9–9

The objective of the puzzle is to move the entire stack to anoth er rod, obeying the following rules: Only one disk may be moved at a time. Each move consists of taking the upper disk from one of the pegs and sliding it o nto another rod, on top of the other disks that may already be present on that r od. No disk may be placed on top of a smaller disk. Tower of Hanoi – 10 acb

Problem: Move n disks Move n-1 disks from a to b Move the biggest disk from a to c Move n-1 disks from b to c Problem: Move n-1 disks Move n-2 disks … Tower of Hanoi – 11 acb

Divide and conquer Moving n disks is divided into moving n-1 disks twice. Moving n-1 disks is divided into moving n-2 disks twice. … Moving 1 disk is simple Tower of Hanoi – 12 acb

Simple solution Alternate moves between the smallest piece and a non- smallest piec e. When moving the smallest piece, always move it in the same directio n (either to the left or to the right, but be consistent). If there is no to wer in the chosen direction, move it to the opposite end. When the tu rn is to move the non-smallest piece, there is only one legal move. Tower of Hanoi – 13 acb

Divide and conquer Divide-and-conquer is a widely used principle in algorithm design Dynamic programming is an implementation method using recursive function to implement the divide-and-conquer. Recursive function is based on the same principle of the divide-and-conquer. Tail-recursive function is an efficient way to implement the divide-and-conquer. – 14

Next week Sorting Greedy approach Selection sort Insertion sort Divide-and-conquer Merge sort Quick sort – 15

Download ppt "CSED101 INTRODUCTION TO COMPUTING GREEDY APPROACH, DIVIDE AND CONQUER Hwanjo Yu."

Similar presentations