Greedy Method

Input : X = {x1, x2, . . ., xn}

Solution(s) (S) are contained in X (some combination)
S X ;
2|x| -possible subsets of X

Feasible solution (FS): FS 2|x| combinations. Feasible if satisfies constraints.

S FS <--> f(S) < 0

We need to find a feasible solution that either maximizes or minimizes a given objective function. A feasible solution that does this is called an optimal solution

Optimal solution: S FS such that g(S) is optimum

Method of solution:

  1. Order inputs according to some selection procedure
  2. Select next input
  3. If input can be included in solution subject to feasibility then do so
  4. Go to step 2

Knapsack Problem

n objects
wiweight of object
piprofit of object
Mcapacity of knapsack
xifraction of object i included in knapsack, 0<xi<1

Problem:
Find {x1, x2, . . ., xn} such that

i=1 to n  xiwi < M (weight satisfies feasibility)

i=1 to n   xipi is maximum (profit maximized)

Ex: n = 3       W = (18, 15, 10)       P = (25, 24, 15),       M = 20

X xiwixipi
1/2, 1/3, 1/4 16.5 24.25
0, 2/3, 1 20 31
0, 1, 1/2 20 31.5

We could order according to decreasing profit (pick the highest profit first):

P: 10 5 1 M = 10
W: 100 10 1  
X: 1/10 0 0 xipi = 1
but
X: 0 1 0 xipi = 5

But chosing the highest value first does not always give us optimal.

Order according to increasing weight:

W: 1 10 100 M = 1
P: 1 100 1000  
X: 1 0 0 xipi = 1
but
X: 0 1/10 0 xipi = 10

But chosing greedily here does not always give us optimal either.

Selection based on profit alone or weight alone will not lead to optimal solution.
Consider a ratio of profit to weight.

Solution:
If for i=1 to n wi < M       then xi = 1 for all 1< i <n is a "trivial" optimal solution.
This means we could put everything in the sack - boring.
Otherwise, assume any optimal solution must fill knapsack to capacity according to the following algorithm

Proof: by contradiction and transformation

X (vector of xi's): Solution obtained by algorithm

Suppose X not optimal

Let Y be optimal
Transform Y to X (transform Y to some Z, and show that that Z must be X)
without changing value of optimizing function, thus showing that X is optimal as well.

See also page 201 (read)

If xi = 1, 1 < i < n, then X is optimal.
Let j be least index such that xj < 1.
ie. xi = 1, 1 < i < j, xj < 1

From alg. it follows that xi = 0, j < i < n.

x1 x2      ...       xj-1 xj xj+1       ...       xn
1 1       ...       1 < 1 0      ...       0

If X not optimal, let Y be optimal

i=1 to n piyi       >       i=1 to n pixi

w.l.o.g. i=1 to n yiwi = M

Let k be the least index such that xk != yk (must be at least one)
Remember, j is the first index that is not a value of 1 for X.

Claim:      yk      <       xk
prove claim by looking at the only three cases possible:

This proves claim that yk      <       xk.

Transform Y to Z by increasing yk to xk and decreasing yk+1, . . ., yk to avoid overflowing knapsack
Note that Z looks more like X than Y.
Need to show that Z is optimal as well.

Y --> Z

Y y1      ...       yk-1 yk yk+1       ...       yn
 
||
      ...      
||
^
V/
      ...      
V/
  z1      ...       zk-1 zk zk+1       ...       zn
 
||
      ...      
||
||
        ...       
X x1      ...       xk-1 xk xk+1       ...       xn

increase in weight = wk (zk - yk)
decrease in weight = i=k+1 to n (yi - zi)wi

weight must stay the same so,

increase in profit = (zk - yk) pk
decrease in profit = i=k+1 to n (yi - zi)pi

If increase > decrease
then Z is optimal as well.

Repeat: Need to show increase > decrease which is:

(zk - yk) pk       >?       i=k+1 to n (yi - zi)pi

OR (take each side and divide and multipy by the same term - will need this form)

(zk - yk) pk/wk * wk       >?       i=k+1 to n (yi - zi)pi/wi * wi

We know the following (and it will be useful):
             pk/wk     >     pi/wi , k < i < n, (ordered that way)
          multiply both sides by the same terms
            i=k+1 to n (yi - zi)*pk/wk * wi     >    i=k+1 to n (yi - zi)*pi/wi * wi

OK, we now need to show

(zk - yk) pk/wk * wk       >?       i=k+1 to n (yi - zi)pk/wk * wi

(zk - yk)wk * pk/wk      >?       pk/wk i=k+1 to n (yi - zi)wi

divide both sides by pk/wk      note that what is left is the relation about weights that had to stay the same

Therefore profit increase      >       decrease.       hint on what just happened there

Transition Y --> Z does not decrease profit

If profit increases, Y not optimal
Therefore Z is optimal.

Repeat transformations until Y --> Z --> . . . --> X without change in profit.

Therefore X is optimal.


The graph stuff:
  1. General
  2. Spanning trees
  3. Single source shortest path