Download presentation
Presentation is loading. Please wait.
Published byIreland Woolen Modified over 10 years ago
1
CSED101 INTRODUCTION TO COMPUTING GREEDY APPROACH, DIVIDE AND CONQUER Hwanjo Yu
2
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
3
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
4
Shortest path from Pohang to Seoul –4–4 PohangSeoul Found path = optimal path
5
Shortest path from Pohang to Seoul –5–5 PohangSeoul Optimal path Found path better path
6
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
7
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
8
Conclusion: Greedy approach Greedy approach Normally find a good solution which is sometimes optimal and sometimes not –8–8
9
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
10
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
11
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
12
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
13
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
14
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
15
Next week Sorting Greedy approach Selection sort Insertion sort Divide-and-conquer Merge sort Quick sort – 15
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.