Transcript Document

o “take what you can get now”
o make a decision that appears to be good
(close to optimal solution)
o proceed by searching a sequence of
choices iteratively to decide the (seem to be)
best solution
Greedy Algorithms หมายถึง เป็ นอัลกอริทมึ ทีจ่ ะหาคาตอบโดยการเลือกทางออกทีด่ ที ส่ี ุดทีพ่ บได้ในขณะนัน้ เพือ่ ให้ได้คาตอบ
ทีด่ ที ส่ี ุด แต่ในบางครัง้ Greedy Algorithms อาจจะไม่สามารถหาคาตอบของปญั หาทีด่ ที ส่ี ุดได้เสมอไป
1/93
ตัวอย่างวิธีคิดแบบ Greedy
o Coin Changing
o Fractional Knapsack
o Bin Packing
o Task Scheduling
2/93
ตัวอย่างวิธีคิดแบบ Greedy
o Prim’s
graph
o Kruskal’s
o Dijkstra’s
o Huffman Code
tree
3/93
Coin Changing
สมมุตวิ า่ เรามีเหรียญขนาดดังต่อไปนี้เหรียญ 10 บาท, เหรียญ 5
บาท, และเหรียญ 1 บาทและสมมุตวิ า่ เราต้องการแลกเงิน 89
บาท เราจะได้เงินเหรียญดังนี้ 10 บาท 8 เหรียญ, 5 บาท 1
เหรียญ, 1 บาท 4 เหรียญจะเห็นว่าอัลกอริทมึ ทีเ่ ราใช้ก็คอื เรา
เลือกเหรียญทีม่ คี า่ มากทีส่ ดุ แต่ไม่มากกว่า 89 บาท ออกมาก่อน
(เหรียญ 10 บาท 8 เหรียญ ) จากนัน้ ลบค่านี้ออกจาก 89 บาท ก็
จะเหลือ 9 บาท หลังจากนัน้ เราเลือกเหรียญทีม่ คี า่ มากที่สดุ แต่ไม่
เกิน 9 บาท นันก็
่ คอื ได้(เหรียญ 5 บาท 1 เหรียญ) แล้วลบค่านี้
ออกจาก 9 บาท จะเหลืออยูอ่ กี 4 บาท และในทีส่ ดุ เราก็จะได้
(เหรียญ 1 บาทอีก 4 เหรียญ)
4/93
Fractional Knapsack
o มีสงิ่ ของ n ประเภทซึง่ แต่ละประเภท(i) กาหนดให้ม ี
จานวน xi ชิน้ มีคา่ ความสาคัญ bi และมีน้ าหนัก wi
o ต้องการหาจานวนสิง่ ของแต่ละประเภทที่บรรจุล งใน
เป้ทร่ี บั น้ าหนักได้ไม่เกิน W กิโลกรัม
o ให้ เ ลื อ กหยิ บ สิ่ ง ของที ล ะชิ้ น ที่ ม ี ค่ า ดัช นี (vi=bi/wi)
สูงสุดและทาให้น้ าหนักรวมไม่เกิน W ก่อน
5/93
o ตัวอย่างเช่น มีของ 4 ประเภทคือ
oหนังสือ 4 เล่ม มี b1=10 และ w1=0.6 (v1=16.7)
oขนม 2 กล่อง มี b2=7 และ w2=0.4
(v2=17.5)
oน้ า 2 ขวด มี b3=5 และ w3=0.5
(v3=10)
oซีดเี พลง 8 แผ่น มี b4=3 และ w4=0.2 (v4=15)
o เป้รบั น้ าหนักได้ไม่เกิน 4 กิโลกรัม
o เลือก ขนม 2 กล่อง หนังสือ 4 เล่ม และซีดเี พลง 4 แผ่น
o ได้คา่ ความสาคัญสูงสุดและน้ าหนักไม่เกิน 4 กิโลกรัม
6/93
o Bin Packing
o given N items of sizes s1 , s2 , …, sN;
o while 0 < si < 1
o find a solution to pack these items in the fewest
number of bins
o 2 algor. versions :
o on-line : an item must be placed in a bin before the next
item is read
o off-line : all item list are read in a bin
7/93
Optimal Bin Packing solution
given an item list with sizes :
0.2 , 0.5 , 0.4 , 0.7 , 0.1 , 0.3 , 0.8
0.3
0.5
0.8
0.7
0.4
0.2
Bin 1
0.1
Bin 2
Bin 3
8/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
Bin Packing strategy
Next fit : fill items in a bin until the next item can’t fit , then
insert a new bin (never look back) [0.2 , 0.5 , 0.4 , 0.7 , 0.1 , 0.3 ,
0.8]
empty
empty
empty
0.1
empty
empty
0.5
0.8
0.7
0.2
Bin 1
0.4
Bin 2
0.3
Bin 3
Bin 4
Bin 5
9/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
Bin Packing strategy
First fit : fill items in a bin , but if any first previous bin can
fit the next item then we can fill in until no any bin can fit ,
then insert a new bin [0.2 , 0.5 , 0.4 , 0.7 , 0.1 , 0.3 , 0.8]
empty
empty
0.1
empty
empty
0.7
0.8
Bin 3
Bin 4
0.3
0.5
0.4
0.2
Bin 1
Bin 2
10/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
Bin Packing strategy
Best fit : fill items in a bin by trying to place the new item in
the bin that left the smallest space [0.2 , 0.5 , 0.4 , 0.7 , 0.1 , 0.3 , 0.8]
empty
0.1
Bin 1
empty
0.7
0.8
empty
0.5
0.2
0.3
0.4
Bin 2
Bin 3
Bin 4
11/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
Task Scheduling
o กาหนดงาน n งานด้วย (เวลาเริม่ ,เวลาเสร็จ) โดยให้
ทางานทีละงานบนเครื่อง และจะเอางานใดมาทาบน
เครื่องเดียวกันได้ถา้ เวลางานไม่ทบั ซ้อนกัน ทัง้ นี้ตอ้ ง
ใช้จานวนเครือ่ งในการทางานทัง้ หมดน้อยทีส่ ุด
o ตัวอย่างงาน (1,3) , (1,4) , (2,5) , (3,7) , (4,7) , (6,9) , (7,8)
จะได้ผลดังนี้ เครื่องที่ 1 : (1,3) , (3,7) , (7,8) เครื่องที่ 2 :
(1,4) , (4,7) และเครือ่ งที่ 3 : (2,5) , (6,9)
12/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
Job Scheduling (Uniprocessor)
Job
Time
j1
15
j2
8
j3
3
j4
10
j1
j2
0
15
j3
23
j4
26
36
First-come-First-serve : avg. completion time = 25
avg. waiting time = 16
j3
0
j2
3
j4
11
j1
21
36
Shortest Job First : avg. completion time = 17.75
avg. waiting time = 8.75
13/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
Job Scheduling (Multiprocessor)
Job
Time
j1
3
j2
5
j3
6
j4
10
j5
11
j6
14
j7
15
j8
18
j9
20
j1
j4
0
0
13
28
j5
j8
5
16
j3
0
j7
3
j2
FCFS
34
j6
6
j9
20
40
14/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
o divide : break a given problem into
subproblems
o recur : try to solve each in recursive way
o conquer : derive the final solution from all
solutions
15/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
Problem of size n
Subproblem 1
of size n/m
Solution to
Subproblem 1
Subproblem 2
of size n/m
Solution to
Subproblem 2
…
Subproblem m
of size n/m
Solution to
Subproblem m
Solution to the
original problem
344-211 Algorithmic Process and Programming , created by Dararat Saelee
16/93
o Merge sort
o Quick sort
o Binary Tree Traversal
o Closest-Pair and Convex-Hall
o Selection problem
17/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
Factorial
o Fibonacci
o Binary search
o Strassen’s Matrix Multiplication
o Big Integer Multiplication
o
18/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
Factorial
ตัวอย่าง
o n! = n * (n-1)!
o (n-1)! = (n-1) * (n-2)!
o…
o 1! = 1
o 0! = 1
4! = ?
4! = 4 * 3!
3! = 3 * 2!
2! = 2 * 1!
1! = 1 * 0!
0! = 1
Describe with Tree structure
344-211 Algorithmic Process and Programming , created by Dararat Saelee
19/93
Fibonacci num.: 1,1,2,3,5,8,13,…
o fibo (n) = fibo (n-1) + fibo (n-2)
o fibo (n-1) = fibo (n-2) + fibo (n-3)
o fibo (n-2) = fibo (n-3) + fibo (n-4)
o…
o fibo (3) = fibo (2) + fibo (1)
o fibo (2) = 1
o fibo (1) = 1
344-211 Algorithmic Process and Programming , created by Dararat Saelee
20/93
Binary search
12
15
18
Search = 37
23
26
37
39
41
43
48
37
39
41
43
48
43
48
mid
12
15
18
23
26
mid
12
15
18
23
26
37
39
41
mid
21/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
Strassen’s Matrix Multiplication
o Z = X * Y ; matrix n x n
o
I
K
J
L
=
A
C
B
D
x
E
F
G
H
o I = AxE + BxG , J = AxF + BxH
o K = CxE + DxG , L = CxF + DxH
22/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
Characterizing subproblems using a small
set of integer indices – to allow an optimal
solution to a subproblem to be defined by
the combination of solutions to even smaller
subproblems
23/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
o Binomial Coefficient
o Warshall’s & Floyd’s  Directed Graph
o Optimal Binary Search tree  Tree
o Ordering Matrix Multiplication or Matrix Chain-
Product (ABCD  A(BC)D , (AB)(CD))
o All-pair Shortest Path  Directed Graph
24/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
oSolving each of smaller subproblems only
once and recording the results in a table from
which can obtain a solution to the original
problem
o Using a table instead of recursion
o Factorial
o Fibonacci numbers
25/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
0-1 Knapsack problem
o 0-1 : reject or accept
o given n items of weights w1 , w2 ,…, wn
and values v1 , v2 ,…, vn and a knapsack of
capacity W
o find the best solution that gives maximum
weight and value
26/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
o use table to fill in by applying formulas
B[k-1,w]
B [k,w] =
, if wk > w
max{B[k-1,w],B[k-1,w-wk]+bk}, else
o e.g. let W = 5 and
data : (item,weight,value)
 (1,2,12) (2,1,10) (3,3,20) (4,2,15)
27/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
capacity j (W=5)
weight, value
w1=2, v1=12
w2=1, v2=10
w3=3, v3=20
w4=2, v4=15
i
1
2
3
4
1
0
10
10
10
2
12
12
12
15
3
12
22
22
25
4
12
22
30
30
5
12
22
32
37
Select 1, 2, 4
28/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
Ordering Matrix Multiplication or Matrix
Chain-Product
o find the best solution that gives minimum
times of multiplication
o e.g. ABCD , A(BC)D , (AB)(CD)
29/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
30/91
o a random number is used to make a decision
in some situation
o e.g. giving a quiz by using a coin
o a good randomized algor. has no bad inputs
and no relative to the particular input , e.g.
data testing
31/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
Random Number Generator
a method to generate true randomness is
impossible to do on computer since numbers ,
called pseudorandom numbers, will depend on
the algorithm
e.g. linear congruential generator
xi+1 = Axi % M , given seed (x0) value
32/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
Skip Lists (in searching & insertion)
o e.g. use random number generator to determine
which node to be traversed within the expected
time
Primality Testing
o
e.g. to determine a large N-digit number is prime
or not by factoring into N/2-digit primes, then a
random generator is needed
33/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
othere are many possibilities to try, if not succeed
then step back to try another way
othe elimination of a large group of possibilities in
one step is known as pruning
oe.g. arranging furniture in a new house , never
place sofa, bed & closet in the kitchen
34/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
o n-Queens
o Hamiltonian Circuit
o Subset-Sum
o Goal Seeking
o Turnpike Reconstruction
35/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
o Games :
chess n-Queens
checker Tic-Tac-Toe
o Minimax strategy
36/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
When confronted with a problem, it is
worthwhile to see if any method of algor.
design can apply properly together with
data structure that will lead to efficient
solution.
37/91
Fractional Knapsack : W= 5
หนังสือ 4 เล่ม มี b1=10 และ w1=0.5
ขนม 3 กล่อง มี b2=5 และ w2=0.4
น้า 2 ขวด มี b3=7 และ w3=0.5
โน้ ตบุค้ 1 เครื่อง มี b4=15 และ w4=1.8
Bin Packing :
0.25 , 0.5 , 1.0 , 0.75 , 0.125 , 0.25 , 0.5
38/93