การออกแบบขั้นตอนวิธี

Download Report

Transcript การออกแบบขั้นตอนวิธี

Algorithm Design
Algorithm Design Techniques
Practice Problems
344-211 Algorithmic Process and Programming , created by Dararat Saelee
1/93
oแก้ปัญหาได้อย่างมี
ประสิทธิภาพ
่ กต้อง
oให้คาตอบทีถู
oใช้เวลาในการปฏิบต
ั ก
ิ าร
น้อย
oช ัดเจนและกะทัดร ัด
2/93
o definition
o requirement
o strategy
o techniques
3/93
o Algorithm Definition : a method
to solve a problem described as a
sequence of steps to be
performed in a specific logical
order
o Several algorithms for solving the
ั ,เจนในการ
Algorithm หมายถึ
ง กรรมวิธท
ี ม
ี่ ข
ี น
ั ้ ตอนแบบที
ลexist
ะขัน
้ อย่างชด
same
problem
may
based
แก ้ปั ญหาใดปั ญหาหนึง่ เพือ
่ ให ้ได ้คาตอบทีถ
่ ก
ู ต ้องสาหรับทุกรูปแบบข ้อมูล
ภายในเวลาและทรั
พยากรณ์ทจ
ี่ ากัด
on
very
different
ideas ,
4/93
Satisfied
Requirements :
o
o
o
o
unambiguousness
generality
correctness
finiteness
5/93
unambiguousness
o easier to understand and to
program , so contain fewer
bugs.
o sometimes simpler algorithm
are more efficient than more
complicated algorithm.
6/93
generality
o easier to design an algorithm
in more general terms.
o handle a range of input that is
natural for the problem.
7/93
correctness
proved by
must
be
1) Mathematical Induction.
2) Testing with sample input
data that possibly prove the
algorithm failure or give wrong
answer.
8/93
finiteness–must concern
about
1) execution in finite steps.
2) termination in finite time.
9/93
oComputational
Device
oSolving Decision
oData Structure
oEfficiency
10/93
Computational Device
o Von Neumann
osequential algorithm
o Multiprocessor
o parallel algorithm
o network algorithm
o distributed algorithm
11/93
Solving Decision
ochoose between solving the
problem with approximation
or exact algorithm
oapproximation algorithm
- square roots, integrals
- shortest path
12/93
Solving Decision (cont.)
ochoose to solve the problem
with
non-recursive
recursive algorithm
or
orecursion is easy to program,
but uses a large number of
function calls that affect to
execution efficiency.
13/93
Data Structure
o to solve problems easier, we
need to use appropriate data
structure.
- student id : int or string ?
- matrix 10x5 : array 2D or 50 var.?
- graph, tree : array or linked list ?
14/93
Efficiency
otime : how fast the algor. runs
ospace : how much extra memory
the algor. needs
oworst / best / average case
- sequential search : n / 1 / n/2
15/93
Efficiency (cont.)
oOrder of Growth for Input size
- when input size is large, how is the
run time ?
- order of growth : O (big oh)
- input size : n
16/93
Efficiency (cont.)
- O(n2) : n = 10  running time = 100
n = 100  running time = 10,000
- O(2n) : n = 10  running time = 1,024
- O(log2n) : n = 10  running time = 3.3
n = 100  running time = 6.6
17/93
o To
provide guidance for
designing algorithms for new
problems
o To make it possible to
classify algorithms according
to design idea
18/91
oNo
any general technique
can solve all problems
oe.g. Unsorted data cannot
use with Binary search
algorithm
19/91
An
algorithm
technique
(or
paradigm)
is
design
strategy
a
or
general
approach to solving problems
algorithmically
applicable
to
a
that
is
variety
of
20/93
o Greedy Method
o Divide and Conquer
o Decrease
and
Conquer / Prune-andSearch
o Transform
and
Conquer
21/93
o Dynamic
Programming
o Randomized
Algorithms
o Backtracking
Algorithms
22/93
Motto Today
เพราะแสวงหา มิใช่เพราะรอคอ
่
เพราะเชียวชาญ
มิใช่เพราะโอก
เพราะสามารถ มิใช่เพราะโชคช
344-211 Algorithmic Process and Programming ,
created by Dararat Saelee
23/93
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
่
่ ทสุ
Greedy Algorithms หมายถึง เป็ นอัลกอริทม
ึ ทีจะหาคาตอบโดยการเลื
อกทางออกทีดี
ี่ ด
่
่ ้ได ้คาตอบทีดี
่ ทสุ
ทีiteratively
พบได
้ในขณะนั้นเพือให
ี่ to
ด แต่ในบางครdecide
ง้ั Greedy Algorithms อาจจะไม่
the
่ ทสุ
สามารถหาคาตอบของปัญหาทีดี
ี่ ดได ้เสมอไป
(seem to be) best solution
24/93
ต ัวอย่างวิธค
ี ด
ิ แบบ Greedy
o Coin Changing
o Fractional Knapsack
o Bin Packing
o Task Scheduling
25/93
ต ัวอย่างวิธค
ี ด
ิ แบบ Greedy
o Prim’s
o Kruskal’s graph
o Dijkstra’s
o Huffman Code tree
26/93
Coin Changing
้ ยญ 10
สมมุตวิ ่าเรามีเหรียญขนาดดังต่อไปนี เหรี
บาท, เหรียญ 5 บาท, และเหรียญ 1 บาทและ
สมมุตวิ ่าเราต ้องการแลกเงิน 89 บาท เราจะได ้
เงินเหรียญดังนี ้ 10 บาท 8 เหรียญ, 5 บาท 1
เหรียญ, 1 บาท 4 เหรียญจะเห็นว่าอัลกอริทม
ึ ที่
่ ค่ามากทีสุ
่ ด แต่ไม่
เราใช ้ก็คอ
ื เราเลือกเหรียญทีมี
มากกว่า 89 บาท ออกมาก่อน (เหรียญ 10 บาท
้
8 เหรียญ ) จากนั้นลบค่านี ออกจาก
89 บาท ก็
่
จะเหลือ 9 บาท หลังจากนั้นเราเลือกเหรียญทีมี
่ ดแต่ไม่เกิน 9 บาท นั่นก็คอ
ค่ามากทีสุ
ื ได ้(เหรียญ
้
5 บาท 1 เหรียญ) แล ้วลบค่านี ออกจาก
9 บาท27/93
Fractional Knapsack
o มีสงของ
ิ่
่ ละประเภท(i)
n ประเภทซึงแต่
ก าหนดให ม้ ีจ านวน
xi ชิน้ มีค่ า
้
ความสาคัญ bi และมีนาหนั
ก wi
่
o ต ้องการหาจานวนสิงของแต่
ละประเภทที่
บรรจุ ล งในเป้ ที่ร บ
ั น้ าหนั กได ไ้ ม่ เ กิน W
กิโลกร ัม
้ ่มีค่า ดัช นี
o ให เ้ ลือ กหยิบ สิ่งของทีล ะชินที
(v =b /w ) สูงสุดและทาใหน้ ้าหนักรวม
28/93
o ตัวอย่างเช่น มีของ 4 ประเภทคือ
oหนั ง สือ 4 เล่ม มี b1=10
(v1=16.7)
oขนม 2 กล่ อ ง มี b2=7
(v2=17.5)
oน้ า 2 ขวด มี b3=5
(v3=10)
oซีดเี พลง 8 แผ่น มี b4=3
(v4=15)
้
o เป้ ร ับนาหนั
กได ้ไม่เกิน 4 กิโลกร ัม
และ w1=0.6
และ w2=0.4
และ w3=0.5
และ w4=0.2
29/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
30/93
Optimal Bin Packing
solution
given an item list with sizes :
0.2 , 0.5 , 0.4
, 0.7 , 0.1 , 0.3 ,
0.3
0.5
0.8
0.8
0.7
0.4
0.2
Bin 1
0.1
Bin 2
Bin 3
31/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
32/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 , empty
0.8]
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
33/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
34/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
o ร ้านขนมเค ้กแห่งหนึง
่ รับทาขนมเค ้กตามคาสงั่ ของลูกค ้า
โดยการสงั่ ขนมเค ้กอาจเป็ น 1 ก ้อน ½ ก ้อน ¼ ก ้อน และ
ขนมเค ้กทีล
่ ก
ู ค ้าได ้รับต ้องเป็ นขนาดก ้อนทีไ่ ม่มรี อยแบ่ง
ภายในก ้อน ทัง้ นีใ้ นการทาขนมเค ้ก ทางร ้านไม่ต ้องการ
ให ้เหลือขนมเค ้กมากจึงต ้องคานึงถึงจานวนก ้อนขนมเค ้กที่
น ้อยทีส
่ ด
ุ ทีต
่ ้องทา
ั่ 1 ½ ¼ ½ ¼
o ตัวอย่าง ถ ้าหากลูกค ้า 5 คน มีคาสง
ดังนัน
้ ทางร ้านต ้องทาเค ้ก 3 ก ้อน
o จงเขียนโปรแกรมเพือ
่ อ่านข ้อมูลคาสงั่ ของลูกค ้าจากแฟ้ ม
และหาว่าต ้องทาเค ้กอย่างน ้อยทีส
่ ด
ุ กีก
่ ้อน
35/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
Task Scheduling
่
งานด ้วย (เวลาเริม,เวลา
่
เสร็จ ) โดยให ท
้ างานทีล ะงานบนเครือง
่
และจะเอางานใดมาทาบนเครืองเดี
ยวกัน
้ ต
้ ้องใช ้
ได ้ถ ้าเวลางานไม่ทบ
ั ซ ้อนกัน ทังนี
่
้
จ านวนเครืองในการท
างานทังหมดน้
อย
่ ด
ทีสุ
o กาหนดงาน n
o ตัวอย่างงาน (1,3) , (1,4) , (2,5) , (3,7) ,
36/93
้
่
่
(4,7) , (6,9) , (7,8) จะได ้ผลดังนี เครืองที 1 :
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
37/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
FCFS
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
3
13
j2
0
28
j5
j8
5
16
j3
0
j7
34
j6
6
j9
20
40
38/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
Optimal #1
Job
Scheduling
(Multiprocessor)
Job
Time
j1
3
j2
5
j3
6
j4
10
j5
11
j6
14
j7
15
j8
18
j9
20
j1
0
j6
3
17
j2
0
j5
5
32
j8
16
j3
0
j7
j4
6
34
j9
16
36
39/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
j2
0
j5
j8
5
16
34
j6
j9
0
14
j1
0
j3
3
34
j4
9
j7
19
34
Optimal #2 – minimize completion time
40/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
41/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
42/93
Merge sort
o Quick sort
o Binary Tree Traversal
o Closest-Pair and ConvexHall
o Selection problem
o
43/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
44/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
Factorial
o n! = n * (n-1)!
ตัวอย่าง
4! = ?
o (n-1)! = (n-1) * (n-2)!4! = 4 * 3!
o …
3! = 3 * 2!
o 1! = 1
2! = 2 * 1!
o 0! = 1
1! = 1 * 0!
0! = 1
Describe with Tree structure
344-211 Algorithmic Process and Programming , created by Dararat Saelee
45/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 (n3)
o fibo (n-2) = fibo (n-3) + fibo (n4)
o…
o fibo (3) = fibo (2) + fibo (1)
46/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
Binary search
12
15
18
23
Search = 37
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
47/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
48/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
Big Integer Multiplication
o multiply 2 N-digit numbers : X , Y
o XY = XLYL10N + (XLYR + XRYL)10N/2 + XRYR
o XLYR+XRYL = (XL-XR)(YR-YL) + XLYL + XRYR
o require : 2 subtraction , 3 multiplication
o D1 = XL-XR , D2 = YR-YL
o XLYL , XRYR , D1D2
o D3 = D1D2 + XLYL + XRYR
49/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
Big Integer Multiplication
o X = 61,438,521 & Y = 94,736,407
o XL = 6143 , XR = 8521
o YL = 9473 , YR = 6407
o D1 = XL-XR = -2378, D2 = YR-YL = -3066
o XLYL = 58192639 , XRYR = 54594047
o D1D2 = 7290948
o D3 = D1D2 + XLYL + XRYR = 120077634
o XY = XLYL108 + D3104 + XRYR
344-211 Algorithmic Process and Programming , created by Dararat Saelee
50/93
Motto Today
ในการอยู ่รว่ มกันกับผู อ
้ น
ื่
ควร
มองโลกในแง่ ด ี และ คิด
ในทางสร ้างสรร
มีน้ าใจ เอาใจเขามาใส่ใจ
เรา และรู ้จักให้อภัย
344-211 Algorithmic Process and Programming , created by
Dararat Saelee
5
o based
on
exploiting
the
relationship between a solution
to a given instance of a problem
and a solution to a smaller
instance of the same problem.
o it can be exploited either top
down (recursively) or bottom up
(without a recursion) .
52/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
3 major variations :
o decrease-by-a-constant
o decrease-by-a-constant-
factor
o variable-size-decrease
From Anany
53/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
o decrease-by-a-constant
o the size of instance is reduced by
the same constant on each iteration
o decrease-by-a-constant-factor
o the size of instance is reduced by
the same constant factor on each
iteration
54/93
o Insertion sort
o Depth-First search and Breadth-
First search (graph)
o Topological sorting (graph)
o Generating Combinatorial Objects
55/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
Insertion sort
o use the decrease-by-one
o sorted-side
unsorted-side
o the size of unsorted data is
reduced by 1 on each loop
56/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
43
22
22
17
17
16
16
22
43
43
22
22
17
17
80
80
80
43
36
22
22
17
17
17
80
43
36
29
36
36
36
36
80
43
36
16
16
16
16
16
80
43
29
29
29
29
29
29
80
57/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
o Jasephus Problem
o Fake-Coin problem
o Multiplication à la Russe
58/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
Josephus problem
o to determine the survivor by
eliminating
every
second
person (stand in circle) until
only one survivor is left
o e.g. J(6) = 5 , J(7) = 7 , J(9) = 3
o use the decrease-by-half (=2)
59/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
use the decrease-by-half (=2)
o can consider J(n)
o if n is even (n=2k) ,
J(2k) = 2 J(k) -1
o if n is odd (n=2k+1) ,
o
J(2k+1) = 2 J(k) +1
o
o J(6) = 2 J(3) -1 , J(3) = 2 J(1) + 1 ,
o
J(1) = 1  J(3) = 3  J(6) = 5
344-211 Algorithmic Process and Programming , created by Dararat Saelee
60/93
use the decrease-by-half (=2)
can be obtained by rotate left
1 bit
o
o
J(6) = J(1102)  1012 = 5
o
J(7) = J(1112)  1112 = 7
J(9) = J(10012)  00112 = 3
o
61/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
use the decrease-by-3 (=3)
o eliminate every 3 person
o J(6) = 1
o J(7) = 4
o J(9) = 1
62/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
3 major variations :
o decrease-by-a-constant
o decrease-by-a-constant-factor
o variable-size-decrease
oa
size reduction pattern
varies from one iteration to
another
63/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
o Euclid algor.
o Computing a median and
the selection problem
o Interpolation search
o Binary search tree
64/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
Euclidean algor.:
o
finding gcd(a,b) recursively
gcd(a,b) =
,otherwise
e.g. gcd(124,40)
a
,if b=0
b
,if a=0
gcd (b,a%b)
= gcd(40,4)
=4
344-211 Algorithmic Process and Programming , created by Dararat Saelee
65/91
Problem’s instance
Simpler instance
Change
representation
Change to Another
problem instance
solution
66/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
o Horner’s Rule
o Presorting
o Gaussian
Elimination
o Balanced
Search
tree
o Heap sort (tree)
344-211 Algorithmic Process and Programming , created by Dararat Saelee
67/93
Horner’s rules
o p(x) = anxn + an-1xn-1 + … +
a1x1 + a0
ouse the representation change
technique 
p(x) = (…(anx + an-1)x +…)x
+a
4
3
2
o e.g. p(x) = 2x – x + 3x +
68/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
Problem Reduction
o reduce
the problem to
another problem that solving
algor. is known
o Problem1  Problem2 
Solution
o Problem : counting paths in
a
graph
,
linear
69/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
o
the Least Common Multiple
(lcm)
lcm(24,60) = 2 2 3 2 5 =
120
o
24 = 2 2 2 3
o
60 = 2 2 3 5
o lcm(11,5) = 55
o
70/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
o the Least Common Multiple
(lcm)
mn
gcd( m, n)
o lcm (m,n) =
o lcm(24,60)
=
gcd(24,60) = 12
120
344-211 Algorithmic Process and Programming , created by Dararat Saelee

71/93
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
344-211 Algorithmic Process and Programming , created by Dararat Saelee
72/93
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
73/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
74/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
75/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
o use table to fill in by applying
formulas
>w
B [k,w] =
B[k-1,w]
, if wk
max{B[k-1,w],B[k1,w-wk]+bk}, else
o e.g. let W = 5 and
data : (item,weight,value)
344-211 Algorithmic Process and Programming , created by Dararat Saelee
76/93
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
77/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
Ordering
Multiplication or
Chain-Product
Matrix
Matrix
o find the best solution that
gives minimum times of
multiplication
o e.g. ABCD , A(BC)D ,
78/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
79/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
80/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
81/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
Skip Lists
insertion)
(in
searching
&
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
82/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
83/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
84/93
344-211 Algorithmic Process and Programming , created by Dararat Saelee
o
Games :
chess n-Queens
checker Tic-Tac-Toe
o
Minimax strategy
85/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.
86/91
Knapsack problem
Greedy
o Bin packing
o Task scheduling
o Strassen’s matrix
Divide &conquer
multiplication
o Big Integer multiplication
Decrease&conquer
o Josephus problem
o an
o
87/91
344-211 Algorithmic Process and Programming , created by Dararat Saelee
o
o
o
o
o
o
Non-recursive
:
Dynamic programming
an, fibonacci
Recursive exponentiation :
n
a , Horner’s Rule transform
LCM & GCD – Euclid Algor.
0-1 Knapsack Problem
88/91
344-211 Algorithmic Process and Programming , created by Dararat Saelee
Motto Today
้ คือหนทางสู ค
๋
่ วามสา
ทาเดียวนี
คิดแต่ไม่ทา เท่ากับไม่ได้คด
ิ
344-211 Algorithmic Process and Programming ,
created by Dararat Saelee
89
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 ,
90/93
Task Scheduling : act# start finish
a1 1 4 , a2 2 5 , a3 1 5 , a4 5 8
a5 4 6 , a6 6 10 , a7 7 9
Big Integer Multiplication
X = 4,123,450,732
Y = 8,159,324,570
an  1321 (decrease-by-2)
Josephus Problem : J(82) decreaseby-3
91/93
Euclidean :
gcd (2039,113) , gcd (1548,204)
Horner’s Rule :
p(x) = 7x9 + 4x6 - 3x5 - x4 + 5x2 + 9
Least Common Multiple :
lcm (2039,113) , lcm (1548,204)
0-1 knapsack Problem :
W = 6 , (1,2,12) (2,1,10) (3,4,20)
(4,3,15) (5,2,14)
92/93