49
Introduction To Algorithms. §16. Greedy Algorithms (2) 2010 / 06 1

Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

  • Upload
    others

  • View
    9

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

Introduction To Algorithms.§16. Greedy Algorithms (2)

2010 / 06

1

Page 2: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

Table of Contents

§16.4 Matroids and Greedy Choice

§16.5 Task scheduling and Matroid

前回(6/22)の続きになります

2

Page 3: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

前回の復習3

部分的に最適解を詰み上げる

それを全て真面目に計算しない Optimal Substructure -> 再帰計算式 Optimal Substructure & Greedy Choice

常に最適解が得られる保証はない 選択を省みない 小問題を解く前に選択を行なう & TopDown

最適解が得られる保証みたいなものが欲しい

3

Page 4: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

§16.4 Matroids and Greedy Choice

例外はあるが、多くの場合、貪欲法で最適解

多くの時に最適解になることを示したい

何か理論的に説明出来ないかなぁ…

Matroid というのものを使ってみよう

4

Page 5: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

Matroid (1) Definition

Matroid M は 順序対

Sは有限集合(台集合)

は空ではないSの部分集合族(独立集合族という)

Iは Hereditary と Exchange Property を満たす

Matroid : Hassler Whitney

5

Page 6: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

Matroid (2) Hereditary

Hereditary : 遺伝性

つまり空集合Φは必ず に入っている

6

Page 7: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

Matroid (3) Exchange Property

のとき

これをExchange Property (交換則) と言う

7

Page 8: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

Matroid と Whitney

Matrix Matroidのことを考えていた

行列の列をSの要素 は列の集合「独立」は「線形独立」を使った

ベクトルの一次独立・従属の組合せ論的な側面を抽象化して得られる公理系を見たすようなもの

8

Page 9: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

他のMatroid - Graphic Matroid

G = (V,E) に対する Graphic Matroid MG = (SG,IG)

SG = E

A ⊂ E として A が acyclic なら A ∈ IG

辺の集合Aが独立である → (V,A)がforest

閉路を持たない=独立

9

Page 10: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

グラフ理論の復習10

要は「つながっている」ということ連結

要は「輪っか」のこと閉路

経路 「道」

木 連結で閉路のない無向グラフ

森 要は「木がたくさん集ったもの」

10

Page 11: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

まずはじめに

11

Page 12: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

MGはマトロイドか? (1)

SG=E より有限集合である

forest の部分木も forest → 2. Independence.

Acyclic に何か加えても Acyclic

3. Exchange Property を確認すればいい

GA=(V,A), GB=(V,B) , |A| < |B| とする

12

Page 13: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

MGはマトロイドか? (2)

主張 : Forest (VF, EF) は |VF| - |EF| 個の木を含む

Fがt個の木から成るとする

i番目の木はViの頂点とei個の辺から成るとする

※定理B.2

13

Page 14: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

主張14

1

4

2

5

3

7

6

8 9

G=(V,E)

V=(1,2,3,4,5,6,7,8,9)

E=(12,14,36,56,69,78)

|V|-|E| = 3

木の数:3

14

Page 15: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

MGはマトロイドか? (3)

木の数はそれぞれ |V|-|A| , |V|-|B| 個 GBはGAより少ない木を持つ GBはいくつかの木Tを持つ

辺(u,v)が存在し、u,vがGAの異なる2木に含まれる

GA=(V,A)GB=(V,B) |A| < |B|

Tの頂点はGAの異なる2つの木に含まれている Tは連結グラフ

acyclicが保たれる → Exchange Propertyを満たす (u,v)はGAから、cycleを作らず加えることが出来る

15

Page 16: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

言葉の定義 (1)

Matroid

で のとき

x を extension という (外延?)

MG:辺の独立集合に加えてもcycleにならない辺

16

Page 17: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

言葉の定義 (2)

Matroid M の独立部分集合 A

定義 : A が maximal

A が extension を持たない

A より大きい独立部分集合が存在しない

17

Page 18: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

Theorem 16.6

Matroid の maximal な独立部分集合の大きさは同じ

A , Bがmaximal と仮定。|A|<|B|

x ∈ B - A を使ってAを拡張出来る (交換則)

A が拡張出来るのでmaximalではない

18

Page 19: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

MGの解析とGreedy Algorithm

Graphic Matroid MG

MGにおけるmaximalな独立部分集合とは何か

独立部分集合で最大 → 全域木

重み付きMatroidを使って最小全域木を求める

19

Page 20: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

Weighted Matroid

Matroid M と 重み関数 w の組合せ

∀x ∈ S に対して正の重み w(x) を対応付ける

集合Aの重みはw(x)の総和 (x ∈ A)

A が maximal かつ has maximum weight

A が optimal subset であるとは…

20

Page 21: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

Greedy Algorithms on a weighted matroid

出発点:貪欲法で最適解が求まる条件は何か。

この問題に還元出来れば最適解になる(と主張)

key: 最大の重みを持つ独立部分集合

例題で確かめる

21

Page 22: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

最小全域木 (1)

最小の重みを持つ全域木を求める

重み関数w : 辺の長さ

Weighted Matroid MG の重み関数 w‘を定義

w’(e) = w0 - w(e)

w0はグラフの中での最大な辺の長さ

w’ は常に正になる重み関数

22

Page 23: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

最小全域木 (2)

maximal A → 最小全域木 : |V|-1本の辺を持つ

maximize w’(A) ≡ minimize w(A)

23

Page 24: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

最小全域木 (3)

Weighted Matroidのmaximum問題に還元出来た

Greedy Algorithm で最適解が存在する(はず)

とりあえずGreedy Algorithmを考える

入力 : Weighted Matroid

出力 : optimal subset A

24

Page 25: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

Greedy (M,w) 1. A = φ 2. M.S を w の monotonically decreasing で sort

3. for x ∈ M.S 4. if A∪{x} ∈ M.I 5. A = A∪{x} 6. return A

monotonically decreasing : 単調減少

Activityの問題に似てますね

重みが大きい順でxを選んで追加していく

本当にoptimalか、Matroidの力で証明する

25

Page 26: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

Lemma 16.7 (1)

Sorted S, xは{x}が独立するSの最初の要素とする このようなxが存在すれば、xを含むoptimalなAが存在 xが存在しない → Φのみ。 xが存在する→ なるoptimalなBを仮定する

Sorted : w(x) <= w(y) なる y∈B は存在しない A={x}から始めてBの要素でextentしていく |A|=|B|になるとき、違いはxとyだけ

26

Page 27: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

Lemma 16.7 (2)

Bは仮定よりoptimalなのでAもoptimal

よってxを含むoptimalな独立部分集合が存在する

Greedyで作られるAがoptimalな可能性がある

27

Page 28: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

Lemma 16.8 & Corollary 16.9

x がAの外延なら x はΦの外延でもある (16.8)

Φの外延ではないxは、どのAの外延でもない(16.9)

対偶です。

ある時で使われないものは、二度と使われない

28

Page 29: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

Lemma 16.10 (1)

Matroid M , 最初に選ばれる要素 x

小問題を次のように作る(M’はMのxによる縮小)

これはoptimal substructureである

29

Page 30: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

Lemma 16.10 (2)

x を含む独立集合A → A’=A-{x}も独立である

M’での独立集合A’ → A’ ∪ {x} は独立集合を生む

全体としての最適解に小問題の最適解を含む。

w(A) = w(A’) + w(x)

Optimal Substructure

30

Page 31: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

Greedy (M,w) 1. A = φ 2. M.S を w の monotonically decreasing で sort

3. for x ∈ M.S 4. if A∪{x} ∈ M.I 5. A = A∪{x} 6. return A

monotonically decreasing : 単調減少

Activityの問題に似てますね

重みが大きい順でxを選んで追加していく

本当にoptimalか、Matroidの力で証明する

もういちど…

31

Page 32: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

Theorem 16.11 (1)

Greedy(M,w)が最適解を返す

(16.9) xはΦの外延なので使っていい

(16.7) xを含むoptimalなAが存在(xを含めていい)

(16.10) optimal substructureがある

32

Page 33: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

Theorem 16.11

BはM’で独立 ⇔ B∪{x}がMで独立

上手く小問題を作った

同じようにmaximum-weightな独立集合を求める

33

Page 34: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

結論:Greedy(M,w)は最適解を返す

つまり?

Greedy(M,w)に還元出来る問題は最適解になる。

Theorem 16.11

34

Page 35: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

実際の例題

35

Page 36: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

§16.5 Task Scheduling as matroid

Single-core processorのscheduling

タスクはdeadlineが決まってる

deadlineを超すとペナルティ

とても複雑な問題

Matroidだととても簡単!

36

Page 37: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

Problem

Schedule = Permutation : 順列

Unit-taskだけを扱う

Scheduling unit-time tasks with deadline and penalties for a single processor.

全体のペナルティを最小にするScheduleを見つける

37

Page 38: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

入力

集合 S = { a1 , ... , an }

deadline { d1 , ... , dn } , 1 <= di <= n

penalties { w1 , ... , wn }

task ai は di に終わる必要がある

終わらなければ、penalty wiがある

38

Page 39: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

定義 (1)

タスクがdeadlineより先に終わるか後に終わるか

early / late

scheduleはearly-first formに変換出来る

early-first formはearlyなタスクが先になっている

39

Page 40: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

定義 (2)

Schedule は canonical form に変換出来る

early-first form の early を 単調増加の順に

Task ai, aj (終了時間が k, k+1)

dj < di なら入れかえ

40

Page 41: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

問題→探索

探索を集合Aの探索に帰着する

Aはoptimal scheduleの中のearlyなやつ

この探索の中で、実際にscheduleを探してる

lateなやつはS-A

標準形を生成することで行なう

41

Page 42: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

独立 (1)

集合Aが独立とは?

lateなタスクが存在しないこととする

集合Aが独立かどうかの判定

Nt(A) , t=0,1,...,n

Aの中でdeadlineがtより早いタスクの個数を返す

42

Page 43: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

独立 (2) Lemma 16.12

タスクの集合Aに関して以下は等価

1. Aは独立

2. t=0,...,n に対して Nt(A) <=t

3. デッドラインで単調増加に整列したときに lateなタスクが存在しない

(3)->(1)は明らか

43

Page 44: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

Lemma 16.12

(1)ー>(2) Nt(A) > t と仮定する Ntはdeadlineがtより早いもの

(2)->(3)

どうしようもない! : 背理法で(1)->(2)

(2)が成りたつとき、(3)は簡単。 Nt(A)<=t は その時々でt以内に出来るということ

44

Page 45: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

Theorem 16.13 (1)

S:deadlineがあるunit-time tasksの集合

: the set of all independent sets of tasks

System ( S , ) は Matroid

(1) : S は有限集合

(2) : 独立な集合の部分集合も独立

(3) : Exchange property だけ示せばいい

45

Page 46: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

Theorem 16.13 (2)

A,B を考えて |B| > |A| とする

k を Nt(B) <= Nt(A) なる最大のtとする

Nn(B) = |B| , Nn(A) = |A| , |B| > |A| → k < n

k+1 <= ∀j <= n, Nj(B) > Nj(A)

N0(A)=N0(B)=0なので、存在する

Bはdeadline k+1 のタスクをAより多く含む。

それを aj ∈ B - A とする。

46

Page 47: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

Theorem 16.13 #3

A’ = A ∪ {aj} とする ( k+1 <= j < n )

A’が独立であることを示す

よってA’も独立。 (Lemma 16.12 (2))

k < t <= n , Nt(A’) <= Nt(B) <= t (Bが独立)

0 <= t <= k , Nt(A’) = Nt(A) <= t (Aが独立)

47

Page 48: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

方針

Theorem16.11

貪欲法で最大な重みの独立部分集合を求められる

つまりoptimalなschedulingが出来る

O(n^2)

O(n) 個の独立性の判定に O(n)のアルゴリズム

48

Page 49: Introduction To Algorithms. §16. Greedy Algorithms (2) · 出発点:貪欲法で最適解が求まる条件は何か。 この問題に還元出来れば最適解になる(と主張)

例題

1, 2, 3, 4, Reject 5, Reject 6, 7, 5, 6

N0({1}) = 0 N1({1,2}) = 0N2({1,2,3}) = 1N3({1,2,3,4}) = 2

ai

di

wi

1 2 3 4 5 6 7

4 2 4 3 1 4 6

70 60 50 40 30 20 10

N4({1~5}) = 5N4({1~4,6}) = 5N4({1~4,7}) = 4

<1,2,3,4,7,5,6> , penalty w5+w6 = 50

49