SlideShare une entreprise Scribd logo
1  sur  29
Télécharger pour lire hors ligne
เทคนิคอัลกอริทึมแบบ
Dynamic Algorithm Programming
(DAG)
Advance Computer Programming
รหัสวิชา 32090207
อ. กิตตินันท์ น้1อยมณี 1
เนื้อหา
1. Introduction
2. Knapsack problem with DAG
อ. กิตตินันท์ น้1อยมณี 2
Introduction
อ. กิตตินันท์ น้3อยมณี 3
Introduction
• หลักการคือ เราไม่ทราบว่ากรณีไหนคือกรณีที่ดี
ที่สุด เราไม่รู้คําตอบว่าจริงๆ จะต้องตอบว่าอะไร
• ดังนั้น เมื่อเราไม่รู้เราจึงต้อง “เดา”
• โดยเราจะต้อง “เดา” หรือสุ่มไปในทุกๆ กรณีที่
สามารถเกิดขึ้นได้ แล้วเลือกเอาเฉพาะกรณีที่ได้
ผลลัพธ์ที่ดีที่สุด
อ. กิตตินันท์ น้1อยมณี 4
Introduction
• ปรกติแล้วจะใช้แก้ปัญหาเชิง Optimization
• ลักษณะการทํางานของ DAG จะคล้ายกับ Divide-
and-Conquer
อ. กิตตินันท์ น้1อยมณี 5
อ้างอิงข้อมูลดีๆ จาก อาจาย์กรุง สินอภิรมย์สราญ
(http://pioneer.netserv.chula.ac.th/~skrung/2301365/Lecture011.html)
หมายเหตุ: Divide-and-Conquer จะใช้ในกรณีที่การคํานวณของปัญหาย่อย
(Sub-problem) ไม่ขึ้นต่อกัน เรียกลักษณะการทํางานอย่างนี้ว่า Top-down
approach คือการซอยย่อยปัญหาต่างๆ ออกเป็นชิ้นเล็กๆ แล้วจัดการแก้ปัญหา
จากปัญหาเล็กๆ ก่อน แล้วค่อยรวมกันไปเพื่อไปหาคําตอบโดยรวม
Introduction
• แต่ DAG จะใช้ในกรณีที่การคํานวณของปัญหา
ย่อย (Sub-problem) ขึ้นต่อกัน ซึ่งมีลักษณะการ
ทํางานในเชิง (Bottom-up Approach) โดยจะเริ่ม
จากปัญหาขนาดเล็กก่อน แล้วค่อยรวมกันจนได้
ปัญหาหลัก
อ. กิตตินันท์ น้1อยมณี 6
อ้างอิงข้อมูลดีๆ จาก อาจาย์กรุง สินอภิรมย์สราญ
(http://pioneer.netserv.chula.ac.th/~skrung/2301365/Lecture011.html)
Knapsack problem with DAG
อ. กิตตินันท์ น้7อยมณี 7
Knapsack problem with DAG
• จะต้องทําการเก็บคําตอบของแต่ละรอบการ
ทํางานเอาไว้ เพื่อเปรียบเทียบหาคําตอบที่ดีที่สุด
• ดังนั้นเราจะดูทุกกรณีที่น่าจะเกิดขึ้นได้
อ. กิตตินันท์ น้1อยมณี 8
1 2 3 4 5 6 7 8 9 10 11
0
1
2
3
4
อ. กิตตินันท์ น้1อยมณี 9
0 1 2 3 4
V 1 6 18 22 28
0 1 2 3 4
W 1 2 5 6 7
w
n
1 2 3 4 5 6 7 8 9 10 11
0
1
2
3
4
อ. กิตตินันท์ น้1อยมณี 10
0 1 2 3 4
V 1 6 18 22 28
0 1 2 3 4
W 1 2 5 6 7
w
n
มูลค่า
น้ําหนัก
วิเคราะห์ตั้งแต่น้ําหนัก 1 จนถึงค่า Wx
วิเคราะห์ของทีละชิ้น
ตารางนี้จะเก็บมูลค่าของกรณีต่างๆ ทุกๆ
กรณี เพื่อหามูลค่าที่มากที่สุด
1 2 3 4 5 6 7 8 9 10 11
0 1
1
2
3
4
อ. กิตตินันท์ น้1อยมณี 11
0 1 2 3 4
V 1 6 18 22 28
0 1 2 3 4
W 1 2 5 6 7
w
n
Processing
n = 0, w = 1
ใส่ w0 ลงไปได้ เพราะ wj = w0 พอดี
1 2 3 4 5 6 7 8 9 10 11
0 1 1
1
2
3
4
อ. กิตตินันท์ น้1อยมณี 12
0 1 2 3 4
V 1 6 18 22 28
0 1 2 3 4
W 1 2 5 6 7
w
n
Processing
n = 0, w = 2
ใส่ w0 ลงไปได้อันเดียว เพราะตอนนี้
วัตถุมีแค่อันเดียวให้ใส่
1 2 3 4 5 6 7 8 9 10 11
0 1 1 1 1 1 1 1 1 1 1 1
1
2
3
4
อ. กิตตินันท์ น้1อยมณี 13
0 1 2 3 4
V 1 6 18 22 28
0 1 2 3 4
W 1 2 5 6 7
w
n
Processing
n = 0, w = 3 … 11
ใส่ w0 ลงไปได้อันเดียว เพราะตอนนี้
วัตถุมีแค่อันเดียวให้ใส่
1 2 3 4 5 6 7 8 9 10 11
0 1 1 1 1 1 1 1 1 1 1 1
1 1
2
3
4
อ. กิตตินันท์ น้1อยมณี 14
0 1 2 3 4
V 1 6 18 22 28
0 1 2 3 4
W 1 2 5 6 7
w
n
Processing
n = 1, w = 1
ใส่ w0 ลงไปได้ เพราะ wj = w0 พอดี
1 2 3 4 5 6 7 8 9 10 11
0 1 1 1 1 1 1 1 1 1 1 1
1 1 6
2
3
4
อ. กิตตินันท์ น้1อยมณี 15
0 1 2 3 4
V 1 6 18 22 28
0 1 2 3 4
W 1 2 5 6 7
w
n
Processing
n = 1, w = 2
ใส่ w1 ลงไปได้ เพราะน้ําหนักพอดี
และมีมูลค่ามากกว่า w0 ด้วย
1 2 3 4 5 6 7 8 9 10 11
0 1 1 1 1 1 1 1 1 1 1 1
1 1 6 7
2
3
4
อ. กิตตินันท์ น้1อยมณี 16
0 1 2 3 4
V 1 6 18 22 28
0 1 2 3 4
W 1 2 5 6 7
w
n
Processing
n = 1, w = 3
ใส่ w0 ลงไปได้ และใส่ w1 ลงไปได้
เพราะน้ําหนักพอดีเลย
1 2 3 4 5 6 7 8 9 10 11
0 1 1 1 1 1 1 1 1 1 1 1
1 1 6 7 7 7 7 7 7 7 7 7
2
3
4
อ. กิตตินันท์ น้1อยมณี 17
0 1 2 3 4
V 1 6 18 22 28
0 1 2 3 4
W 1 2 5 6 7
w
n
Processing
n = 1, w = 4 … 11
ใส่ได้แค่ 2 อย่าง เพราะวัตถุมีแต่ 2
อย่างเท่านั้น มูลค่าจึงได้ 7 เท่าเดิม
1 2 3 4 5 6 7 8 9 10 11
0 1 1 1 1 1 1 1 1 1 1 1
1 1 6 7 7 7 7 7 7 7 7 7
2 1
3
4
อ. กิตตินันท์ น้1อยมณี 18
0 1 2 3 4
V 1 6 18 22 28
0 1 2 3 4
W 1 2 5 6 7
w
n
Processing
n = 2, w = 1
ใส่ได้แค่ w0 เพราะน้ําหนักพอดี
1 2 3 4 5 6 7 8 9 10 11
0 1 1 1 1 1 1 1 1 1 1 1
1 1 6 7 7 7 7 7 7 7 7 7
2 1 6
3
4
อ. กิตตินันท์ น้1อยมณี 19
0 1 2 3 4
V 1 6 18 22 28
0 1 2 3 4
W 1 2 5 6 7
w
n
Processing
n = 2, w = 2
ใส่ได้แค่ w1 เพราะน้ําหนักพอดี
1 2 3 4 5 6 7 8 9 10 11
0 1 1 1 1 1 1 1 1 1 1 1
1 1 6 7 7 7 7 7 7 7 7 7
2 1 6 7 7
3
4
อ. กิตตินันท์ น้1อยมณี 20
0 1 2 3 4
V 1 6 18 22 28
0 1 2 3 4
W 1 2 5 6 7
w
n
Processing
n = 2, w = 3, 4
ใส่ได้แค่ w0 กับ w1 เพราะน้ําหนัก
พอดีกระเป๋าเลย
1 2 3 4 5 6 7 8 9 10 11
0 1 1 1 1 1 1 1 1 1 1 1
1 1 6 7 7 7 7 7 7 7 7 7
2 1 6 7 7 18
3
4
อ. กิตตินันท์ น้1อยมณี 21
0 1 2 3 4
V 1 6 18 22 28
0 1 2 3 4
W 1 2 5 6 7
w
n
Processing
n = 2, w = 5
ใส่ w2 ได้ เพราะน้ําหนัก 5 เท่าความ
จุกระเป๋าตอนนี้เลย
1 2 3 4 5 6 7 8 9 10 11
0 1 1 1 1 1 1 1 1 1 1 1
1 1 6 7 7 7 7 7 7 7 7 7
2 1 6 7 7 18 19
3
4
อ. กิตตินันท์ น้1อยมณี 22
0 1 2 3 4
V 1 6 18 22 28
0 1 2 3 4
W 1 2 5 6 7
w
n
Processing
n = 2, w = 6
ใส่ w2 ได้ แถมใส่ w0 ได้ เพราะความ
จุเต็มพอดี
1 2 3 4 5 6 7 8 9 10 11
0 1 1 1 1 1 1 1 1 1 1 1
1 1 6 7 7 7 7 7 7 7 7 7
2 1 6 7 7 18 19 24
3
4
อ. กิตตินันท์ น้1อยมณี 23
0 1 2 3 4
V 1 6 18 22 28
0 1 2 3 4
W 1 2 5 6 7
w
n
Processing
n = 2, w = 7
ใส่ w2 ได้ แถมใส่ w1 ได้ เพราะมี
มูลค่ามากกว่า w0 และความจุพอดี
1 2 3 4 5 6 7 8 9 10 11
0 1 1 1 1 1 1 1 1 1 1 1
1 1 6 7 7 7 7 7 7 7 7 7
2 1 6 7 7 18 19 24 25
3
4
อ. กิตตินันท์ น้1อยมณี 24
0 1 2 3 4
V 1 6 18 22 28
0 1 2 3 4
W 1 2 5 6 7
w
n
Processing
n = 2, w = 8
ใส่ w2 ได้ แถมใส่ w1 ได้ และ w0 ได้
ด้วย เพราะความจุพอดีเลย
1 2 3 4 5 6 7 8 9 10 11
0 1 1 1 1 1 1 1 1 1 1 1
1 1 6 7 7 7 7 7 7 7 7 7
2 1 6 7 7 18 19 24 25 25 25 25
3
4
อ. กิตตินันท์ น้1อยมณี 25
0 1 2 3 4
V 1 6 18 22 28
0 1 2 3 4
W 1 2 5 6 7
w
n
Processing
n = 2, w = 9 … 11
ใส่ w2 ได้ แถมใส่ w1 ได้ และ w0 ได้
ซึ่งได้แค่นั้น เพราะวัตถุมีแค่ 3 ชิ้น
1 2 3 4 5 6 7 8 9 10 11
0 1 1 1 1 1 1 1 1 1 1 1
1 1 6 7 7 7 7 7 7 7 7 7
2 1 6 7 7 18 19 24 25 25 25 25
3 1 6 7 7 18 22 24 28 29 29 40
4 1 6 7 7 18 22 28 29 34 35 40
อ. กิตตินันท์ น้1อยมณี 26
0 1 2 3 4
V 1 6 18 22 28
0 1 2 3 4
W 1 2 5 6 7
w
n
Processing
n = 3 … 4, w = 1 … 11
และทําอย่างนี้ไปเรื่อยๆ จนครบทุก
ช่อง ทําให้ได้ค่าดังตาราง
Knapsack problem with DAG
• จะเห็นได้ว่า DAG จะคิดทุกๆ กรณีที่น่าจะมีโอกาส
เกิดขึ้นได้
• ซึ่งข้อดีก็คือเราจะได้ค่าที่ถูกต้องที่สุดอย่างแน่นอน
• แต่ข้อเสียก็คือกระบวนการในการค้นหาอาจต้อง
ใช้เวลานานขึ้น หากมีข้อมูลที่มากขึ้น
อ. กิตตินันท์ น้1อยมณี 27
Algorithm KnapsackDP( w[n], v[n], wx ) : array[n]
for i := 0 to n-1
for j := 0 to wx
if i – 1 < 0 then a := 0, Sa :=
else a := u[i-1, j], Sa := y[i-1, j]
if j – w[i] < 0 then b := - , Sb :=
else if j – w[i] = 0 OR i – 1 < 0 then b := v[i], Sb := {i}
else
b := u[ i-1, j-w[i] ] + v[i]
Sb := {i} y[ i-1, j-w[i] ]
if a > b then u[i, j] := a, y[i, j] := Sa
else u[i, j] := b, y[i, j] := Sb
x := y[ n-1, wx ]
Return x
End Algorithm อ. กิตตินันท์ น้1อยมณี 28
Knapsack problem with DAG
อ. กิตตินันท์ น้1อยมณี 29
ลองทําด้วยตัวเองดูนะครับ???

Contenu connexe

En vedette

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

En vedette (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

(Big One) C Language - 12 เทคนิคอัลกอริทึมแบบ dynamic-algorithm-programming

  • 1. เทคนิคอัลกอริทึมแบบ Dynamic Algorithm Programming (DAG) Advance Computer Programming รหัสวิชา 32090207 อ. กิตตินันท์ น้1อยมณี 1
  • 2. เนื้อหา 1. Introduction 2. Knapsack problem with DAG อ. กิตตินันท์ น้1อยมณี 2
  • 4. Introduction • หลักการคือ เราไม่ทราบว่ากรณีไหนคือกรณีที่ดี ที่สุด เราไม่รู้คําตอบว่าจริงๆ จะต้องตอบว่าอะไร • ดังนั้น เมื่อเราไม่รู้เราจึงต้อง “เดา” • โดยเราจะต้อง “เดา” หรือสุ่มไปในทุกๆ กรณีที่ สามารถเกิดขึ้นได้ แล้วเลือกเอาเฉพาะกรณีที่ได้ ผลลัพธ์ที่ดีที่สุด อ. กิตตินันท์ น้1อยมณี 4
  • 5. Introduction • ปรกติแล้วจะใช้แก้ปัญหาเชิง Optimization • ลักษณะการทํางานของ DAG จะคล้ายกับ Divide- and-Conquer อ. กิตตินันท์ น้1อยมณี 5 อ้างอิงข้อมูลดีๆ จาก อาจาย์กรุง สินอภิรมย์สราญ (http://pioneer.netserv.chula.ac.th/~skrung/2301365/Lecture011.html) หมายเหตุ: Divide-and-Conquer จะใช้ในกรณีที่การคํานวณของปัญหาย่อย (Sub-problem) ไม่ขึ้นต่อกัน เรียกลักษณะการทํางานอย่างนี้ว่า Top-down approach คือการซอยย่อยปัญหาต่างๆ ออกเป็นชิ้นเล็กๆ แล้วจัดการแก้ปัญหา จากปัญหาเล็กๆ ก่อน แล้วค่อยรวมกันไปเพื่อไปหาคําตอบโดยรวม
  • 6. Introduction • แต่ DAG จะใช้ในกรณีที่การคํานวณของปัญหา ย่อย (Sub-problem) ขึ้นต่อกัน ซึ่งมีลักษณะการ ทํางานในเชิง (Bottom-up Approach) โดยจะเริ่ม จากปัญหาขนาดเล็กก่อน แล้วค่อยรวมกันจนได้ ปัญหาหลัก อ. กิตตินันท์ น้1อยมณี 6 อ้างอิงข้อมูลดีๆ จาก อาจาย์กรุง สินอภิรมย์สราญ (http://pioneer.netserv.chula.ac.th/~skrung/2301365/Lecture011.html)
  • 7. Knapsack problem with DAG อ. กิตตินันท์ น้7อยมณี 7
  • 8. Knapsack problem with DAG • จะต้องทําการเก็บคําตอบของแต่ละรอบการ ทํางานเอาไว้ เพื่อเปรียบเทียบหาคําตอบที่ดีที่สุด • ดังนั้นเราจะดูทุกกรณีที่น่าจะเกิดขึ้นได้ อ. กิตตินันท์ น้1อยมณี 8
  • 9. 1 2 3 4 5 6 7 8 9 10 11 0 1 2 3 4 อ. กิตตินันท์ น้1อยมณี 9 0 1 2 3 4 V 1 6 18 22 28 0 1 2 3 4 W 1 2 5 6 7 w n
  • 10. 1 2 3 4 5 6 7 8 9 10 11 0 1 2 3 4 อ. กิตตินันท์ น้1อยมณี 10 0 1 2 3 4 V 1 6 18 22 28 0 1 2 3 4 W 1 2 5 6 7 w n มูลค่า น้ําหนัก วิเคราะห์ตั้งแต่น้ําหนัก 1 จนถึงค่า Wx วิเคราะห์ของทีละชิ้น ตารางนี้จะเก็บมูลค่าของกรณีต่างๆ ทุกๆ กรณี เพื่อหามูลค่าที่มากที่สุด
  • 11. 1 2 3 4 5 6 7 8 9 10 11 0 1 1 2 3 4 อ. กิตตินันท์ น้1อยมณี 11 0 1 2 3 4 V 1 6 18 22 28 0 1 2 3 4 W 1 2 5 6 7 w n Processing n = 0, w = 1 ใส่ w0 ลงไปได้ เพราะ wj = w0 พอดี
  • 12. 1 2 3 4 5 6 7 8 9 10 11 0 1 1 1 2 3 4 อ. กิตตินันท์ น้1อยมณี 12 0 1 2 3 4 V 1 6 18 22 28 0 1 2 3 4 W 1 2 5 6 7 w n Processing n = 0, w = 2 ใส่ w0 ลงไปได้อันเดียว เพราะตอนนี้ วัตถุมีแค่อันเดียวให้ใส่
  • 13. 1 2 3 4 5 6 7 8 9 10 11 0 1 1 1 1 1 1 1 1 1 1 1 1 2 3 4 อ. กิตตินันท์ น้1อยมณี 13 0 1 2 3 4 V 1 6 18 22 28 0 1 2 3 4 W 1 2 5 6 7 w n Processing n = 0, w = 3 … 11 ใส่ w0 ลงไปได้อันเดียว เพราะตอนนี้ วัตถุมีแค่อันเดียวให้ใส่
  • 14. 1 2 3 4 5 6 7 8 9 10 11 0 1 1 1 1 1 1 1 1 1 1 1 1 1 2 3 4 อ. กิตตินันท์ น้1อยมณี 14 0 1 2 3 4 V 1 6 18 22 28 0 1 2 3 4 W 1 2 5 6 7 w n Processing n = 1, w = 1 ใส่ w0 ลงไปได้ เพราะ wj = w0 พอดี
  • 15. 1 2 3 4 5 6 7 8 9 10 11 0 1 1 1 1 1 1 1 1 1 1 1 1 1 6 2 3 4 อ. กิตตินันท์ น้1อยมณี 15 0 1 2 3 4 V 1 6 18 22 28 0 1 2 3 4 W 1 2 5 6 7 w n Processing n = 1, w = 2 ใส่ w1 ลงไปได้ เพราะน้ําหนักพอดี และมีมูลค่ามากกว่า w0 ด้วย
  • 16. 1 2 3 4 5 6 7 8 9 10 11 0 1 1 1 1 1 1 1 1 1 1 1 1 1 6 7 2 3 4 อ. กิตตินันท์ น้1อยมณี 16 0 1 2 3 4 V 1 6 18 22 28 0 1 2 3 4 W 1 2 5 6 7 w n Processing n = 1, w = 3 ใส่ w0 ลงไปได้ และใส่ w1 ลงไปได้ เพราะน้ําหนักพอดีเลย
  • 17. 1 2 3 4 5 6 7 8 9 10 11 0 1 1 1 1 1 1 1 1 1 1 1 1 1 6 7 7 7 7 7 7 7 7 7 2 3 4 อ. กิตตินันท์ น้1อยมณี 17 0 1 2 3 4 V 1 6 18 22 28 0 1 2 3 4 W 1 2 5 6 7 w n Processing n = 1, w = 4 … 11 ใส่ได้แค่ 2 อย่าง เพราะวัตถุมีแต่ 2 อย่างเท่านั้น มูลค่าจึงได้ 7 เท่าเดิม
  • 18. 1 2 3 4 5 6 7 8 9 10 11 0 1 1 1 1 1 1 1 1 1 1 1 1 1 6 7 7 7 7 7 7 7 7 7 2 1 3 4 อ. กิตตินันท์ น้1อยมณี 18 0 1 2 3 4 V 1 6 18 22 28 0 1 2 3 4 W 1 2 5 6 7 w n Processing n = 2, w = 1 ใส่ได้แค่ w0 เพราะน้ําหนักพอดี
  • 19. 1 2 3 4 5 6 7 8 9 10 11 0 1 1 1 1 1 1 1 1 1 1 1 1 1 6 7 7 7 7 7 7 7 7 7 2 1 6 3 4 อ. กิตตินันท์ น้1อยมณี 19 0 1 2 3 4 V 1 6 18 22 28 0 1 2 3 4 W 1 2 5 6 7 w n Processing n = 2, w = 2 ใส่ได้แค่ w1 เพราะน้ําหนักพอดี
  • 20. 1 2 3 4 5 6 7 8 9 10 11 0 1 1 1 1 1 1 1 1 1 1 1 1 1 6 7 7 7 7 7 7 7 7 7 2 1 6 7 7 3 4 อ. กิตตินันท์ น้1อยมณี 20 0 1 2 3 4 V 1 6 18 22 28 0 1 2 3 4 W 1 2 5 6 7 w n Processing n = 2, w = 3, 4 ใส่ได้แค่ w0 กับ w1 เพราะน้ําหนัก พอดีกระเป๋าเลย
  • 21. 1 2 3 4 5 6 7 8 9 10 11 0 1 1 1 1 1 1 1 1 1 1 1 1 1 6 7 7 7 7 7 7 7 7 7 2 1 6 7 7 18 3 4 อ. กิตตินันท์ น้1อยมณี 21 0 1 2 3 4 V 1 6 18 22 28 0 1 2 3 4 W 1 2 5 6 7 w n Processing n = 2, w = 5 ใส่ w2 ได้ เพราะน้ําหนัก 5 เท่าความ จุกระเป๋าตอนนี้เลย
  • 22. 1 2 3 4 5 6 7 8 9 10 11 0 1 1 1 1 1 1 1 1 1 1 1 1 1 6 7 7 7 7 7 7 7 7 7 2 1 6 7 7 18 19 3 4 อ. กิตตินันท์ น้1อยมณี 22 0 1 2 3 4 V 1 6 18 22 28 0 1 2 3 4 W 1 2 5 6 7 w n Processing n = 2, w = 6 ใส่ w2 ได้ แถมใส่ w0 ได้ เพราะความ จุเต็มพอดี
  • 23. 1 2 3 4 5 6 7 8 9 10 11 0 1 1 1 1 1 1 1 1 1 1 1 1 1 6 7 7 7 7 7 7 7 7 7 2 1 6 7 7 18 19 24 3 4 อ. กิตตินันท์ น้1อยมณี 23 0 1 2 3 4 V 1 6 18 22 28 0 1 2 3 4 W 1 2 5 6 7 w n Processing n = 2, w = 7 ใส่ w2 ได้ แถมใส่ w1 ได้ เพราะมี มูลค่ามากกว่า w0 และความจุพอดี
  • 24. 1 2 3 4 5 6 7 8 9 10 11 0 1 1 1 1 1 1 1 1 1 1 1 1 1 6 7 7 7 7 7 7 7 7 7 2 1 6 7 7 18 19 24 25 3 4 อ. กิตตินันท์ น้1อยมณี 24 0 1 2 3 4 V 1 6 18 22 28 0 1 2 3 4 W 1 2 5 6 7 w n Processing n = 2, w = 8 ใส่ w2 ได้ แถมใส่ w1 ได้ และ w0 ได้ ด้วย เพราะความจุพอดีเลย
  • 25. 1 2 3 4 5 6 7 8 9 10 11 0 1 1 1 1 1 1 1 1 1 1 1 1 1 6 7 7 7 7 7 7 7 7 7 2 1 6 7 7 18 19 24 25 25 25 25 3 4 อ. กิตตินันท์ น้1อยมณี 25 0 1 2 3 4 V 1 6 18 22 28 0 1 2 3 4 W 1 2 5 6 7 w n Processing n = 2, w = 9 … 11 ใส่ w2 ได้ แถมใส่ w1 ได้ และ w0 ได้ ซึ่งได้แค่นั้น เพราะวัตถุมีแค่ 3 ชิ้น
  • 26. 1 2 3 4 5 6 7 8 9 10 11 0 1 1 1 1 1 1 1 1 1 1 1 1 1 6 7 7 7 7 7 7 7 7 7 2 1 6 7 7 18 19 24 25 25 25 25 3 1 6 7 7 18 22 24 28 29 29 40 4 1 6 7 7 18 22 28 29 34 35 40 อ. กิตตินันท์ น้1อยมณี 26 0 1 2 3 4 V 1 6 18 22 28 0 1 2 3 4 W 1 2 5 6 7 w n Processing n = 3 … 4, w = 1 … 11 และทําอย่างนี้ไปเรื่อยๆ จนครบทุก ช่อง ทําให้ได้ค่าดังตาราง
  • 27. Knapsack problem with DAG • จะเห็นได้ว่า DAG จะคิดทุกๆ กรณีที่น่าจะมีโอกาส เกิดขึ้นได้ • ซึ่งข้อดีก็คือเราจะได้ค่าที่ถูกต้องที่สุดอย่างแน่นอน • แต่ข้อเสียก็คือกระบวนการในการค้นหาอาจต้อง ใช้เวลานานขึ้น หากมีข้อมูลที่มากขึ้น อ. กิตตินันท์ น้1อยมณี 27
  • 28. Algorithm KnapsackDP( w[n], v[n], wx ) : array[n] for i := 0 to n-1 for j := 0 to wx if i – 1 < 0 then a := 0, Sa := else a := u[i-1, j], Sa := y[i-1, j] if j – w[i] < 0 then b := - , Sb := else if j – w[i] = 0 OR i – 1 < 0 then b := v[i], Sb := {i} else b := u[ i-1, j-w[i] ] + v[i] Sb := {i} y[ i-1, j-w[i] ] if a > b then u[i, j] := a, y[i, j] := Sa else u[i, j] := b, y[i, j] := Sb x := y[ n-1, wx ] Return x End Algorithm อ. กิตตินันท์ น้1อยมณี 28
  • 29. Knapsack problem with DAG อ. กิตตินันท์ น้1อยมณี 29 ลองทําด้วยตัวเองดูนะครับ???