Greedy algorithms Flashcards

1
Q

בעית התרמיל השברית

מה הקלט והפלט

A
קלט:
W משקל מירבי
n זוגות של מספרים
כאשר 
vi value of i'th item
wi weight of i'th item
פלט:
רשימה של מספרים 
x1,...,xn 
s.t. 
0<=xi<=1
sum xi*wi<=W

The sum xi*v is maximized

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

אלגוריתם חמדן לפתרון בעית התרמיל השברי

A
  1. עיבוד מוקדם:
    חישוב הערכים הסגוליים
    ri=vi/wi
    ומיון הפריטים בסדר יורד לפי הערכים הסוגליים
  2. איתחול
    G=empty set
  3. איטרציה
    נעבור על הפריט מהראשון לאחרון ובכל שלב נכניס ל
    G
    כמה שיותר מהפריט הנוכחי (לפי אילוץ המשקל)
  4. סיום
    נעצור כאשר התרמיל מלא או כאשר הכנסו את כל הפריטים
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

זמן ריצה של אלגוריתם חמדן לפתרון בעיית התרמיל השלם

A

O(nlogn)

Sorting - O(nlong)

Iteration - O(n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

מהו הפתרון החמדן לבעית התרמיל השברי

A

Define index t to be the last index that adding the t’th+1 item is above the weight limit.

x1=x2=…=xt=1

xt+1 = [W-(sum weight until t)]/w(t+1)

x(t+2)=…=xn=0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

למת ההחלפה של התרמיל השברי

A

Let Y=y1,y2,…,yn be a legal solution for the problem. Assume that there exists an index j, 1<=j<=n such that:

  1. yj<1 (not all the j item was chosen)
  2. Sum_i_1_to_j: yiwi
How well did you know this?
1
Not at all
2
3
4
5
Perfectly