浙江大学 算法与程序设计 贪心算法
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Time complexity: O(n log n + n) = O(n log n)
13
Planning of schools
A collection of towns. We want to plan schools in towns.
Each school should be in a town No one should have to travel more than 30 miles to reach one of them.
9
Analysis – exact solution cont.
Let A {A1,, Ak } and OPT {B1,, Bm} be sorted. By definition of OPT we have k ≤ m Fact: for every i ≤ k, Ai finishes not later than Bi. Pf. by induction.
nt 1 nt 1 nt nt (1 ) k k
17
Upper bound con.
Then , since 1 x e x for all x, with equality if and only if x=0.
1 t nt n0 (1 ) k
Thus
1 t nt n0 (1 ) n0 (e 1/ k )t ne t / k k
Analyzing optimal greedy algorithms by showing that:
in every step it is not worse than any other algorithm, or every algorithm can be gradually transformed to thegreedy one without hurting its quality
18
Matroids
When will the greedy algorithm yields optimal solutions? Matroids [Hassler Whitney]: A matroid is an ordered pair M=(S, ℓ) satisfying the following conditions. S is a finite nonempty set ℓ is a nonempty family of subsets of S, called the independent subsets of S, such that if
OPT #4
Algorithm #3
6
Rule 4
Select the interval which ends first (but still not overlapping the already chosen intervals) Quite a nature idea: we ensure that our resource become free as soon as possible while still satisfying one request Hurray! Exact solution!
Greedy algorithm
叶德仕 yedeshi@
1
Greedy algorithm’s paradigm
Algorithm is greedy if
it builds up a solution in small steps it chooses a decision at each step myopically to optimize some underlying criterion
Greedy algorithm:
Select intervals one after another using some rule
3
Rule 1
Select the interval which starts earliest (but not overlapping the already chosen intervals) Underestimated solution!
5
Rule 3
Select the interval with the fewest conflicts with other remaining intervals (but still not overlapping the already chosen intervals) Underestimated solution!
Edge: towns no far than 30 miles
14
Set cover
Input. A set of elements B, sets S ,S B Output. A selection of the Si whose union is B. Cost. Number of sets picked.
2
Interval scheduling
Input: set of intervals on the line, represented by pairs of points (ends of intervals). In another word, the ith interval, starts at time si and finish at fi. Output: finding the largest set of intervals such that none two of them overlap. Or the maximum number of intervals that without overlap.
1 m
15
Greedy
Greedy: first choose a set that covers the largest number of elements.
example: place a school at town a, since this covers the largest number of other towns.
i-1 i
Ai-1
Ai
11
Analysis con.
Theorem: A is the exact solution. Proof: we show that k = m. Suppose to the contrary that k < m. We have that Ak finishes not later than Bk Hence we could add Bk+1 to A and obtain bigger solution by the algorithm-contradiction
For i = 1 by definition of a step in the algorithm. Suppose that Ai-1 finishes not later than Bi-1.
10
Analysis con.
From the definition of a step in the algorithm we get that Ai is the first interval that finishes after Ai-1 and does not verlap it. If Bi finished before Ai then it would overlap some of the previous A1 ,…, Ai-1 and consequently - by the inductive assumption it would overlap Bi-1, which would be a B contradiction. B
B , and A B, then A .
We say that ℓ is hereditary if it satisfies this property. Note that empty set is necessarily a member of ℓ.
, then there is some element x B A such that A {x} . We say that M satisfies the exchage property. 19
The solution is exact:
Let A be the set of intervals obtained by the algorithm, and OPT be the largest set of pairwise nonoverlapping intervals. We show that A must be as large as OPT
7
f1 smallest
Algorithm #3
8
Analysis - exact solution
Algorithm gives non-overlapping intervals:
obvious, since we always choose an interval which does not overlap the previously chosen intervals
OPT #4 Algorithm #1
4
Rule 2
Select the interval which is shortest (but not overlapping the already chosen intervals) Underestimated solution!
OPT #2 Algorithm #1
At t=k ln n, therefore, nt is strictly less than ne-ln n =1, which means no elements remains to be covered. Consequently, the approximation ratio is at most ln n
If
A , B , and | A | | B |
பைடு நூலகம்
Max independent
Theorem. All maximal independent subsets in a matroid have the same size. Pf. Suppose to the contrary that A is a maximal independent subset of M and there exists another larger maximal independent subset B of M. Then, the exchange property implies that A is extendible to a larger independent set A ∪ {x} for some x ∈ B - A, contradicting the assumption that A is maximal.
Greedy #4 OPT #3
16
Upper bound
Theorem. Suppose B contains n elements that the optimal cover consist of k sets. Then the greedy algorithm will use at most k ln n sets. Pf. Let nt be the number of elements still not covered after t iterations of the greedy algorithm (n0=n). Since these remaining elements are covered by the optimal k sets, there must be some set with at least nt /k of them. Therefore, the greedy algorithm will ensure that
Bk-1 Bk Bk+1
Ak-1
Ak
12
Time complexity
Sorting intervals according to the right-most ends For every consecutive interval: If the left-most end is after the right-most end of the last selected interval then we select this interval Otherwise we skip it and go to the next interval
13
Planning of schools
A collection of towns. We want to plan schools in towns.
Each school should be in a town No one should have to travel more than 30 miles to reach one of them.
9
Analysis – exact solution cont.
Let A {A1,, Ak } and OPT {B1,, Bm} be sorted. By definition of OPT we have k ≤ m Fact: for every i ≤ k, Ai finishes not later than Bi. Pf. by induction.
nt 1 nt 1 nt nt (1 ) k k
17
Upper bound con.
Then , since 1 x e x for all x, with equality if and only if x=0.
1 t nt n0 (1 ) k
Thus
1 t nt n0 (1 ) n0 (e 1/ k )t ne t / k k
Analyzing optimal greedy algorithms by showing that:
in every step it is not worse than any other algorithm, or every algorithm can be gradually transformed to thegreedy one without hurting its quality
18
Matroids
When will the greedy algorithm yields optimal solutions? Matroids [Hassler Whitney]: A matroid is an ordered pair M=(S, ℓ) satisfying the following conditions. S is a finite nonempty set ℓ is a nonempty family of subsets of S, called the independent subsets of S, such that if
OPT #4
Algorithm #3
6
Rule 4
Select the interval which ends first (but still not overlapping the already chosen intervals) Quite a nature idea: we ensure that our resource become free as soon as possible while still satisfying one request Hurray! Exact solution!
Greedy algorithm
叶德仕 yedeshi@
1
Greedy algorithm’s paradigm
Algorithm is greedy if
it builds up a solution in small steps it chooses a decision at each step myopically to optimize some underlying criterion
Greedy algorithm:
Select intervals one after another using some rule
3
Rule 1
Select the interval which starts earliest (but not overlapping the already chosen intervals) Underestimated solution!
5
Rule 3
Select the interval with the fewest conflicts with other remaining intervals (but still not overlapping the already chosen intervals) Underestimated solution!
Edge: towns no far than 30 miles
14
Set cover
Input. A set of elements B, sets S ,S B Output. A selection of the Si whose union is B. Cost. Number of sets picked.
2
Interval scheduling
Input: set of intervals on the line, represented by pairs of points (ends of intervals). In another word, the ith interval, starts at time si and finish at fi. Output: finding the largest set of intervals such that none two of them overlap. Or the maximum number of intervals that without overlap.
1 m
15
Greedy
Greedy: first choose a set that covers the largest number of elements.
example: place a school at town a, since this covers the largest number of other towns.
i-1 i
Ai-1
Ai
11
Analysis con.
Theorem: A is the exact solution. Proof: we show that k = m. Suppose to the contrary that k < m. We have that Ak finishes not later than Bk Hence we could add Bk+1 to A and obtain bigger solution by the algorithm-contradiction
For i = 1 by definition of a step in the algorithm. Suppose that Ai-1 finishes not later than Bi-1.
10
Analysis con.
From the definition of a step in the algorithm we get that Ai is the first interval that finishes after Ai-1 and does not verlap it. If Bi finished before Ai then it would overlap some of the previous A1 ,…, Ai-1 and consequently - by the inductive assumption it would overlap Bi-1, which would be a B contradiction. B
B , and A B, then A .
We say that ℓ is hereditary if it satisfies this property. Note that empty set is necessarily a member of ℓ.
, then there is some element x B A such that A {x} . We say that M satisfies the exchage property. 19
The solution is exact:
Let A be the set of intervals obtained by the algorithm, and OPT be the largest set of pairwise nonoverlapping intervals. We show that A must be as large as OPT
7
f1 smallest
Algorithm #3
8
Analysis - exact solution
Algorithm gives non-overlapping intervals:
obvious, since we always choose an interval which does not overlap the previously chosen intervals
OPT #4 Algorithm #1
4
Rule 2
Select the interval which is shortest (but not overlapping the already chosen intervals) Underestimated solution!
OPT #2 Algorithm #1
At t=k ln n, therefore, nt is strictly less than ne-ln n =1, which means no elements remains to be covered. Consequently, the approximation ratio is at most ln n
If
A , B , and | A | | B |
பைடு நூலகம்
Max independent
Theorem. All maximal independent subsets in a matroid have the same size. Pf. Suppose to the contrary that A is a maximal independent subset of M and there exists another larger maximal independent subset B of M. Then, the exchange property implies that A is extendible to a larger independent set A ∪ {x} for some x ∈ B - A, contradicting the assumption that A is maximal.
Greedy #4 OPT #3
16
Upper bound
Theorem. Suppose B contains n elements that the optimal cover consist of k sets. Then the greedy algorithm will use at most k ln n sets. Pf. Let nt be the number of elements still not covered after t iterations of the greedy algorithm (n0=n). Since these remaining elements are covered by the optimal k sets, there must be some set with at least nt /k of them. Therefore, the greedy algorithm will ensure that
Bk-1 Bk Bk+1
Ak-1
Ak
12
Time complexity
Sorting intervals according to the right-most ends For every consecutive interval: If the left-most end is after the right-most end of the last selected interval then we select this interval Otherwise we skip it and go to the next interval