Transcript Slide 1

CSED101 INTRODUCTION TO COMPUTING
GREEDY APPROACH, DIVIDE AND CONQUER
유환조
Hwanjo Yu
Design of Algorithm
–2

Computing problem


Algorithm


Given inputs, how to generate the desirable outputs?
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

Greedy approach
–3


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.
Shortest path from Pohang to Seoul
–4
Pohang
Seoul
Found path = optimal
path
Shortest path from Pohang to Seoul
–5
Optimal path
Pohang
Seoul
better path
Found path
Coin problem
–6

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
Coin problem
–7


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
Conclusion: Greedy approach
–8

Greedy approach
 Normally
find a good solution which is sometimes
optimal and sometimes not
Divide and conquer
–9


Divide a big problem into several small problems
Induce the solution of the big problem from solutions
of small problems
Tower of Hanoi
–10
a

b
c
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
–11
a

Problem: Move n disks




b
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 …
c
Tower of Hanoi
–12
a

b
c
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
–13
a

b
c
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.
Divide and conquer
–14




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.
Next week
–15

Sorting
 Greedy
approach
 Selection
sort
 Insertion sort
 Divide-and-conquer
 Merge
sort
 Quick sort