Algorithm Design Strategies

(アルゴリズムの設計方法)

Data Structures and Algorithms

13th lecture, January 9, 2020

http://www.sw.it.aoyama.ac.jp/2019/DA/lecture13.html

Martin J. Dürst

AGU

© 2009-20 Martin J. Dürst 青山学院大学

Today's Schedule

 

Remaining Schedule

 

Summary of Last Lecture

 

Algorithm Design Strategies/Methods

  

The Knapsack Problem

Depending on the details of the problem, the best algorithm design strategy is different

 

Variations of the Knapsack Problem

  1. All items are the same; how many items fit in?
  2. Pack as many items as possible
  3. Use as much capacity as possible (integer version)
  4. Maximise value

 

All Items are the Same

Example: Capacity c = 20kg, weight per item: 3.5kg

'Algorithm': Divide the capacity by the weight per item, round down

Answer for example: 5 items

Design strategy: simplistic 'algorithm'

 

Simplistic Algorithm

 

Pack as Many Items as Possible

Example: Capacity c = 20kg, weight of items: 8kg, 2kg, 4kg, 7kg, 2kg, 1kg, 5kg, 12kg

Algorithm: Sort items by increasing weight, pack starting with lightest item

Answer for example: 5 items (e.g. 1kg, 2kg, 2kg, 4kg, 5kg)

Design strategy: Greedy algorithm

Time complexity: O(n log n)

 

Greedy Algorithm

 

Use as Much Capacity as Possible (Integer Version)

Example: Capacity c = 20kg, weight of items (ws1,...,wsn): 8kg, 2kg, 4kg, 7kg, 2kg, 1kg, 5kg, 12kg

Algorithm: Consider subproblems with capacity c'c using only the first k items s1,..., sk (kn)

Design strategy: Dynamic programming

Recursive problem description:

max_weight(0, ...) = 0 = max_weight(x, {})

max_weight(c1, {s1,..., sk}) = max(max_weight(c1-wsk, {s1,..., sk-1})+wsk,
                                                         max_weight(c1,      {s1,..., sk-1}))

 

Example Solution

kc 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 (8kg) 0 ×0 ×0 ×0 ×0 ×0 ×0 ×0 ○8 ○8 ○8 ○8 ○8 ○8 ○8 ○8 ○8 ○8 ○8 ○8 ○8
2 (2kg) 0 ×0 ○2 ○2 ○2 ○2 ○2 ○2 ×8 ×8 ○10 ○10 ○10 ○10 ○10 ○10 ○10 ○10 ○10 ○10 ○10
3 (4kg) 0 ×0 ×2 ×2 ○4 ○4 ○6 ○6 ×8 ×8 ×10 ×10 ○12 ○12 ○14 ○14 ○14 ○14 ○14 ○14 ○14
4 (7kg) 0 ×0 ×2 ×2 ×4 ×4 ×6 ○7 ×8 ○9 ×10 ○11 ×12 ○13 ×14 ○15 ○15 ○17 ○17 ○19 ○19
5 (2kg) 0 ×0 ×2 ×2 ×4 ×4 ×6 ×7 ×8 ×9 ×10 ×11 ×12 ×13 ×14 ×15 ○16 ×17 ×17 ×19 ×19
6 (1kg) 0 ○1 ×2 ○3 ×4 ○5 ×6 ×7 ×8 ×9 ×10 ×11 ×12 ×13 ×14 ×15 ×16 ×17 ○18 ×19 ○20
7 (5kg) 0 ×1 ×2 ×3 ×4 ×5 ×6 ×7 ×8 ×9 ×10 ×11 ×12 ×13 ×14 ×15 ×16 ×17 ×18 ×19 ×20
8 (12kg) 0 ×1 ×2 ×3 ×4 ×5 ×6 ×7 ×8 ×9 ×10 ×11 ×12 ×13 ×14 ×15 ×16 ×17 ×18 ×19 ×20

Table rows: Items to add (or not) to knapsack

Table columns: Total capacity

Table entries: ×: Item in this row is not used; ○: Item in this row is used; number: max weight possible

How to find the solution: See next slide

 

How to Find the Solution

Answer for example: 1kg, 7kg, 4kg, 8kg (total: 20kg; other solutions possible)

Time complexity: O(cn)

 

Value Maximization

Example: Capacity c = 20kg, weight and value of items: 8kg, 500¥; 2kg, 2000¥; 4kg, 235¥; 7kg, 700¥; 2kg, 400¥; 1kg, 100¥; 5kg, 450¥; 12kg, 650¥

Algorithm: Try all possible solutions

Design strategy: Brute force

Implementation: Dknapsack.rb (of various algorithms, only brute force finds optimal solution if weights and values can be non-integers)

 

Brute Force Algorithm

 

Variations of the Knapsack Problem (Summary)

  1. All items are the same; how many items fit in?
    Solution: Divide capacity by item weight
    Design strategy: Simplistic 'algorithm'
  2. Pack as many items as possible
    Solution: Start with lightest items
    Design strategy: Greedy algorithm
  3. Use as much capacity as possible (integer version)
    Solution: Consider subproblems with capacity c'c and items s1,..., sk (kn)
    Design Strategy: Dynamic programming
  4. Maximize value
    Design Strategy: Brute force

 

Algorithm Design Strategies

 

Goal for Remaining Time

Example Problem 1: 3-SAT

  

Example Problem 2: Independent Set

 

Example Problem 3: Traveling Salesman

 

Homework

(no need to submit)

 

Glossary

NP-completeness
NP-完全性
reducibility
帰着可能性
approximation algorithms
近似アルゴリズム
brute force
総当たり方、腕力法、虱潰し
greedy algorithm
貪欲アルゴリズム
knapsack problem
ナップサック問題
capacity
容量
closed formula
「閉じた式」
number sequence
数列
minimum spanning tree (problem)
最小全域気 (問題)
independent set
独立集合
conflict
競合
traveling salesman
巡回セールスマン