Algorithm Design & AnalysisChapter

Embed Size (px)

Citation preview

  • 8/13/2019 Algorithm Design & AnalysisChapter

    1/58

    Algorithm Design & Analysis

    Chapter -04(Dynamic Programming & Greedy

    Algorithms )T.E(Computer)

    ByI.S.Borse

    SSVPS BSD COE ,DHULE3/8/2009 1ADA Unit -4 I.S Borse

  • 8/13/2019 Algorithm Design & AnalysisChapter

    2/58

    Outline Chapter 4

    1. Dynamic Programming ( Introduction)i) Multistage Graphii) Optimal Binary Search Tree(OBST)

    iii) 0/1 Knapsack Problemiv) Travelling Salesman Problem2. Greedy Algorithms ( Introduction)i) Job Sequencingii) Optimal Merge Patterns

    3/8/2009 2

    ADA Unit -4 I.S Borse

  • 8/13/2019 Algorithm Design & AnalysisChapter

    3/58

    Dynamic Programming

    Dynamic Programming is an algorithmdesign method that can be used when

    the solution to a problem may beviewed as the result of a sequence ofdecisions

    3/8/2009 3ADA Unit -4 I.S Borse

  • 8/13/2019 Algorithm Design & AnalysisChapter

    4/58

    The General MethodDynamic programming

    An algorithm design method that can be used when the solutioncan be viewed as the result of a sequence of decisions

    Some solvable by Greedy method under the conditionCondition : an optimal sequence of decisions can be found bymaking the decisions one at a time and never making anerroneous decision

    For many other problems

    Not possible to make stepwise decisions (based only on localinformation) in a manner like Greedy method

    3/8/2009 ADA Unit -4 I.S Borse 4

  • 8/13/2019 Algorithm Design & AnalysisChapter

    5/58

    The General Method Enumeration vs. dynamic programming

    Enumeration Enumerating all possible decision sequences and picking

    out the best prohibitive time and storage requirements Dynamic programming

    Drastically reducing the time and storage by avoiding somedecision sequences that cannot possibly be optimal

    Making explicit appeal to the principle of optimality Definition [Principle of optimality] The principle of optimality

    states that an optimal sequence of decisions has the property thatwhatever the initial state and decision are, the remainingdecisions must constitute an optimal decision sequence withregard to the state resulting from the first decision.

    3/8/2009 ADA Unit -4 I.S Borse 5

  • 8/13/2019 Algorithm Design & AnalysisChapter

    6/58

    Principle of optimality Principle of optimality: Suppose that in solving a

    problem, we have to make a sequence ofdecisions D 1, D2, , Dn. If this sequence isoptimal, then the last k decisions, 1 k n mustbe optimal.

    e.g. the shortest path problemIf i, i1, i2, , j is a shortest path from i to j, then i 1,i2, , j must be a shortest path from i 1 to j

    In summary, if a problem can be described by amultistage graph, then it can be solved bydynamic programming.

    3/8/2009 6ADA Unit -4 I.S Borse

  • 8/13/2019 Algorithm Design & AnalysisChapter

    7/58

    The General Method

    Greedy method vs. dynamic programming Greedy method

    Only one decision sequence is ever generated

    Dynamic programming Many decision sequences may be generated But sequences containing suboptimal subsequences

    discarded because they cannot be optimal due to theprinciple of optimality

    3/8/2009 ADA Unit -4 I.S Borse 7

  • 8/13/2019 Algorithm Design & AnalysisChapter

    8/58

    The General Method Notation and formulation for the principle

    Notation and formulation S0: initial problem state n decisions d i, 1in have to be made to solve the

    problem and D 1={r1,r2,,r j} is the set of possibledecision values for d1

    Si is the problem state when r i chosen, and i is anoptimal sequence wrt S

    i Then, when the principle of optimality holds, an

    optimal sequence wrt S 0 is the best of the decisionsequences r i,i, 1in

    3/8/2009 ADA Unit -4 I.S Borse 8

  • 8/13/2019 Algorithm Design & AnalysisChapter

    9/58

    Forward approach and backward approach:

    Note that if the recurrence relations are formulatedusing the forward approach then the relations aresolved backwards . i.e., beginning with the lastdecision

    On the other hand if the relations are formulatedusing the backward approach , they are solvedforwards.

    To solve a problem by using dynamic programming: Find out the recurrence relations. Represent the problem by a multistage graph.

    Dynamic Programming

    3/8/2009 9ADA Unit -4 I.S Borse

  • 8/13/2019 Algorithm Design & AnalysisChapter

    10/58

    Steps for Dynamic Programming1) The problem can be divided into stages with decision

    required at each stage2) Each state has a number of states associated with it

    3) The decision at one stage transform one stage to next

    4) Given current state the optimal decision for each of theremaining state does not depend on the previous stateor decision

    5) There exists a recursive relationship that identifies theoptimal solution for stage j , given the stage j+1 hasalready been solved

    6) One final stage must be solvable by itself.

    3/8/2009 ADA Unit -4 I.S Borse 10

  • 8/13/2019 Algorithm Design & AnalysisChapter

    11/58

    The shortest path

    To find a shortest path in a multi-stage graph

    Apply the greedy method :

    the shortest path from S to T :

    1 + 2 + 5 = 8

    S A B T

    3

    4

    5

    2 7

    1

    5 6

    3/8/2009 11ADA Unit -4 I.S Borse

  • 8/13/2019 Algorithm Design & AnalysisChapter

    12/58

    The shortest path in multistage graphs

    e.g.

    The greedy method can not be applied to this case:

    (S, A, D, T) 1+4+18 = 23. The real shortest path is:(S, C, F, T) 5+2+2 = 9.

    S T132

    B E

    9

    A D4

    C F2

    1

    5

    11

    5

    16

    18

    2

    3/8/2009 12ADA Unit -4 I.S Borse

  • 8/13/2019 Algorithm Design & AnalysisChapter

    13/58

    Multistage Graphs Definition: multistage graph G(V,E)

    A directed graph in which the vertices arepartitioned into k2 disjoint sets V i, 1ik

    If E, then u Vi and v Vi+1 for some I,1i

  • 8/13/2019 Algorithm Design & AnalysisChapter

    14/58

    Multistage Graphs

    3/8/2009 ADA Unit -4 I.S Borse 14

  • 8/13/2019 Algorithm Design & AnalysisChapter

    15/58

    Multistage Graphs

    3/8/2009 ADA Unit -4 I.S Borse 15

    DP formulation

    Every s to t path is the result of a sequence of k-2 decisionsThe principle of optimality holds (Why?)p(i, j) = a minimum-cost path from vertex j in V i to vertex tcost(i, j) = cost of path p(i , j)

    cost(k-1,j) = c(j,t) if E, otherwise

    Then computing cost(k-2,j) for all j Vk-2Then computing cost(k-3,j) for all j Vk-3Finally computing cost(1,s)

    )},1(cos),({min),(cos,

    1

    l it l jc jit E l j

    V l i

  • 8/13/2019 Algorithm Design & AnalysisChapter

    16/58

    Multistage Graphs

    3/8/2009 ADA Unit -4 I.S Borse 16

    (k=5)Stage 5

    cost(5,12) = 0.0Stage 4

    cost(4,9) = min {4+cost(5,12)} = 4

    cost(4,10) = min {2+cost(5,12)} = 2cost(4,11) = min {5+cost(5,12)} = 5Stage 3

    cost(3,6) = min {6+cost(4,9), 5+cost(4,10)} = 7cost(3,7) = min {4+cost(4,9), 3+cost(4,10)} = 5cost(3,8) = min {5+cost(4,10), 6+cost(4,11)} = 7

  • 8/13/2019 Algorithm Design & AnalysisChapter

    17/58

    Multistage Graphs

    Stage 2 cost(2,2) = min {4+cost(3,6), 2+cost(3,7), 1+cost(3,8)} = 7 cost(2,3) = min {2+cost(3,6), 7+cost(3,7)} = 9 cost(2,4) = min {11+cost(3,8)} = 18

    cost(2,5) = min {11+cost(3,7), 8+cost(3,8)} = 15 Stage 1

    cost(1,1) = min {9+cost(2,2), 7+cost(2,3), 3+cost(2,4), 2+cost(2,5)} = 16 Important notes: avoiding the recomputation of cost(3,6), cost(3,7), and

    cost(3,8) in computing cost(2,2)

    3/8/2009 ADA Unit -4 I.S Borse 17

  • 8/13/2019 Algorithm Design & AnalysisChapter

    18/58

    Multistage Graphs void Fgraph (graph G, int k, int n, int p[] ) // The input is a k-stage graph G = (V,E) with n vertices indexed in order

    // of stages. E is a set of edges and c[i][j] is the cost of . // p[1 : k] is a minimum-cost path. { float cost[MAXSIZE]; int d[MAXSIZE], r; cost[n] = 0.0; for (int j=n-1; j >= 1; j--) { // Compute cost[j]. let r be a vertex such that is an edge of G and c[j][r] + cost[r] is minimum; cost[j] = c[j][r] + cost[r]; d[j] = r; } // Find a minimum-cost path. p[1] = 1; p[k] =n ; for ( j=2; j

  • 8/13/2019 Algorithm Design & AnalysisChapter

    19/58

    Multistage Graphs

    3/8/2009 ADA Unit -4 I.S Borse 20

    )} j,l(c)l,1i(tcos b{min) j,i(tcos b

    El, j

    Vl 1i

    Backward approach

  • 8/13/2019 Algorithm Design & AnalysisChapter

    20/58

    The shortest path in multistage graphs

    e.g.

    The greedy method can not be applied to this case:

    (S, A, D, T) 1+4+18 = 23. The real shortest path is:(S, C, F, T) 5+2+2 = 9.

    S T132

    B E

    9

    A D4

    C F2

    1

    5

    11

    5

    16

    18

    2

    3/8/2009 21ADA Unit -4 I.S Borse

  • 8/13/2019 Algorithm Design & AnalysisChapter

    21/58

    Dynamic programming approach

    Dynamic programming approach

    d(S, T) = min{1+d(A, T), 2+d(B, T), 5+d(C, T)}

    S T2

    B

    A

    C

    1

    5 d(C, T)

    d(B, T)

    d(A, T)

    A

    T

    4

    E

    D

    11d(E, T)

    d(D, T)d(A,T) = min{4+d(D,T), 11+d(E,T)}= min{4+18, 11+13} = 22.

    3/8/2009 22ADA Unit -4 I.S Borse

  • 8/13/2019 Algorithm Design & AnalysisChapter

    22/58

    Dynamic programming

    d(B, T) = min{9+d(D, T), 5+d(E, T), 16+d(F, T)}= min{9+18, 5+13, 16+2} = 18.

    d(C, T) = min{ 2+d(F, T) } = 2+2 = 4

    d(S, T) = min{1+d(A, T), 2+d(B, T), 5+d(C, T)}= min{1+22, 2+18, 5+4} = 9.

    The above way of reasoning is called

    backward reasoning.

    B T5

    E

    D

    F

    9

    16 d(F, T)

    d(E, T)

    d ( D , T )

    3/8/2009 23ADA Unit -4 I.S Borse

  • 8/13/2019 Algorithm Design & AnalysisChapter

    23/58

    Backward approach (forward reasoning)

    d(S, A) = 1d(S, B) = 2

    d(S, C) = 5

    d(S,D)=min{d(S, A)+d(A, D),d(S, B)+d(B, D)}= min{ 1+4, 2+9 } = 5

    d(S,E)=min{d(S, A)+d(A, E),d(S, B)+d(B, E)}

    = min{ 1+11, 2+5 } = 7d(S,F)=min{d(S, A)+d(A, F),d(S, B)+d(B, F)}

    = min{ 2+16, 5+2 } = 7

    3/8/2009 24ADA Unit -4 I.S Borse

  • 8/13/2019 Algorithm Design & AnalysisChapter

    24/58

  • 8/13/2019 Algorithm Design & AnalysisChapter

    25/58

  • 8/13/2019 Algorithm Design & AnalysisChapter

    26/58

    Optimal binary search trees

    n identifiers : a 1

  • 8/13/2019 Algorithm Design & AnalysisChapter

    27/58

    Identifiers : 4, 5, 8, 10, 11,12, 14

    Internal node : successfulsearch, P i

    External node : unsuccessful

    search, Q i

    10

    14

    E 7

    5

    11

    12E 4

    4

    E 0 E 1

    8

    E 2 E 3

    E 5 E 6

    The expected cost of a binary tree:

    The level of the root : 1

    n

    0nii

    n

    1nii 1))(level(EQ)level(aP

    3/8/2009 28ADA Unit -4 I.S Borse

  • 8/13/2019 Algorithm Design & AnalysisChapter

    28/58

    The dynamic programming approach

    Let C(i, j) denote the cost of an optimal binary searchtree containing a i, ,a j . The cost of the optimal binary search tree with a k as its

    root :

    ak

    a1

    . . .ak -1

    ak + 1

    . . .an

    P 1 ...P k- 1Q 0 ...Q k- 1

    P k+1 ...P nQ k ...Q n

    C(1,k-1) C (k+1,n)

    n1,k CQPQ1k 1,CQPQPminn)C(1,n

    1k iiik

    1k

    1iii0k

    nk 1

    3/8/2009 29ADA Unit -4 I.S Borse

  • 8/13/2019 Algorithm Design & AnalysisChapter

    29/58

    j

    immm1-i

    jk i

    j

    1k mmmk

    1k

    immm1-ik jk i

    QPQ j1,k C1k i,Cmin

    j1,k CQPQ

    1k i,CQPQPmin j)C(i,

    General formula

    ak

    a1

    . . .ak -1

    ak + 1

    . . .an

    P1...P

    k- 1

    Q 0 ...Q k- 1

    Pk+1

    ...Pn

    Q k ...Q n

    C(1,k-1) C(k+1,n)3/8/2009 30ADA Unit -4 I.S Borse

  • 8/13/2019 Algorithm Design & AnalysisChapter

    30/58

  • 8/13/2019 Algorithm Design & AnalysisChapter

    31/58

    0/1 knapsack problem

    n objects , weight W 1, W2, ,Wnprofit P 1, P2, ,Pn capacity M

    maximize subject to Mxi = 0 or 1, 1 i n e. g.

    ni

    ii x P

    1 niii xW

    1

    i W i P i

    1 10 402 3 203 5 30

    M=10

    3/8/2009 32ADA Unit -4 I.S Borse

  • 8/13/2019 Algorithm Design & AnalysisChapter

    32/58

    The multistage graph solution The 0/1 knapsack problem can be described

    by a multistage graph.

    S T

    0

    10

    10

    00

    01

    100

    010

    011

    000

    001

    0

    0

    0

    0

    00

    40

    020

    0

    30

    0

    0

    30

    x1=1

    x1=0

    x2=0

    x2=1

    x2=0

    x3=0

    x3=1

    x3=0

    x3=1

    x3=0

    3/8/2009 33ADA Unit -4 I.S Borse

  • 8/13/2019 Algorithm Design & AnalysisChapter

    33/58

    The Dynamic Programming Approach

    The longest path represents the optimalsolution:x1=0, x2=1, x3=1

    = 20+30 = 50 Let f i(Q) be the value of an optimal solution to

    objects 1,2,3, ,i with capacity Q.

    f i(Q) = max{ f i-1(Q), f i-1(Q-W i)+Pi } The optimal solution is f n(M).

    ii x P

    3/8/2009 34ADA Unit -4 I.S Borse

  • 8/13/2019 Algorithm Design & AnalysisChapter

    34/58

    All Pairs Shortest Paths Problem definition

    Determine a matrix A such that A( i, j) is the length of a shortestpath from i to j

    One method using the algorithm ShortestPaths Each vertex requiring O(n 2) time, so total time is O(n 3)

    Restriction: no negative edge allowed DP algorithm

    The principle of optimality (Does it hold? Why?) (weaker) restriction: no cycle with negative length

    3/8/2009 ADA Unit -4 I.S Borse 35

  • 8/13/2019 Algorithm Design & AnalysisChapter

    35/58

    All Pairs Shortest Paths

    3/8/2009 ADA Unit -4 I.S Borse 36

    1k )}, j,k (A)k ,i(A), j,i(Amin{) j,i(A 1k 1k 1k k

    DP algorithm (Continued)

    Ak(i , j) = the length of a shortest path from i to j goingthrough no vertex of index greater than k

  • 8/13/2019 Algorithm Design & AnalysisChapter

    36/58

    All Pairs Shortest Paths Algorithm

    1. void AllPaths(float cost[][SIZE], float A[][SIZE], int n)2. // cost[1:n][1:n] is the cost adjacency matrix of 3. // a graph with n vertices; A[i][j] is the cost4. // of a shortest path from vertex i to vertex j.5. // cost[i][i] = 0.0, for 1

  • 8/13/2019 Algorithm Design & AnalysisChapter

    37/58

    The Traveling Salesperson Problem Problem

    TSP is a permutation problem (not subset problem) Usually the permutation problem is harder than the subset one Because n! > 2n

    Given a directed graph G(V,E)

    cij= edge cost A tour is a directed simple cycle that includes every vertex in V The TSP is to find a tour of minimum cost

    Many applications

    1. Routing a postal van to pick up mail from mail boxes located at n differensites 2. Planning robot arm movements to tighten the nuts on n different positions 3. Planning production in which n different commodities are manufactured

    on the same sets of machines3/8/2009 ADA Unit -4 I.S Borse 38

  • 8/13/2019 Algorithm Design & AnalysisChapter

    38/58

    The Traveling Salesperson Problem

    3/8/2009 ADA Unit -4 I.S Borse 39

    }k}){1,Vg(k,c{min 1k nk 2

    }{j})Sg(j,c{min ijS j

    DP formulation Assumption: A tour starts and ends at vertex 1The principle of optimality holds (Why?)g(i, S) = length of a shortest path starting at vertex i, going through allvertices in S, and terminating at vertex 1

    g(1,V-{1}) = -

    g(i, S) =

    Solving the recurrence relationg(i, ) = c i1, 1inThen obtain g(i, S) for all S of size 1

    Then obtain g(i, S) for all S of size 2Then obtain g(i, S) for all S of size 3Finally obtain g(1,V-{1})

  • 8/13/2019 Algorithm Design & AnalysisChapter

    39/58

    The Traveling Salesperson Problem

    For |S|=0

    g(2, )=c21=5 g(3, )=c31=6 g(4, )=c41=8

    For |S|=1 g(2,{3})=c 23+g(3, )=15

    g(2,{4})=c 24+g(4, )=18 g(3,{2})=c 32+g(2, )=18 g(3,{4})=c 34+g(4, )=20 g(4,{2})=c 42+g(2, )=13 g(4,{3})=c 43+g(3, )=15

    3/8/2009 ADA Unit -4 I.S Borse 40

    0988

    12013610905

    2012100110

    3

    2

    412

    5

    208

    9

    12

    6

    8

    139

    10

    The Traveling Salesperson Problem

  • 8/13/2019 Algorithm Design & AnalysisChapter

    40/58

    The Traveling Salesperson Problem

    3/8/2009 ADA Unit -4 I.S Borse 41

    22

    0

    2)1(2

    )1( n

    n

    k

    nk

    nn

    For |S|=2g(2,{3,4}) = min {c 23 +g(3,{4}), c 24 +g(4,{3})} = 25g(3,{2,4}) = min {c 32 +g(2,{4}), c 34 +g(4,{2})} = 25g(4,{2,3}) = min {c 42 +g(2,{3}), c 43 +g(3,{2})} = 23

    For |S|=3g(1,{2,3,4} = min {c 12 +g(2,{3,4}), c 13 +g(3,{2,4}),

    c14 +g(4,{2,3})} =

    = min {35, 40, 43} = 35Time complexity

    Let N be the number of g(i, S) that have to be computed before canbe used to compute g(1,V-{1})

    N =

    Total time = O(n 22n)This is better than enumerating all n! different tours

  • 8/13/2019 Algorithm Design & AnalysisChapter

    41/58

    The traveling salesperson (TSP) problem

    e.g. a directed graph :

    Cost matrix:

    12

    3

    2

    44

    2

    56

    7

    104

    8

    39

    1 2 3 4

    1 2 10 5

    2 2 9

    3 4 3 4

    4 6 8 7

    3/8/2009 42ADA Unit -4 I.S Borse

  • 8/13/2019 Algorithm Design & AnalysisChapter

    42/58

    A multistage graph can describe all possible tours of a directedgraph.

    Find the shortest path:(1, 4, 3, 2, 1) 5+7+3+2=17

    (1) (1,3)

    (1,2)

    (1,4)

    2

    5

    10

    (1,2,3)

    (1,2,4)

    (1,3,2)

    (1,3,4)

    (1,4,2)

    (1,4,3)

    9

    3

    4

    8

    7

    (1,2,3,4)

    (1,2,4,3)

    (1,3,2,4)

    (1,3,4,2)

    (1,4,2,3)

    (1,4,3,2)

    4

    7

    8

    9

    3

    1

    4

    6

    6

    2

    4

    2

    The multistage graph solution

    3/8/2009 43ADA Unit -4 I.S Borse

  • 8/13/2019 Algorithm Design & AnalysisChapter

    43/58

    The representation of a node

    Suppose that we have 6 vertices in the graph. We can combine {1, 2, 3, 4} and {1, 3, 2, 4} into one node.

    (3),(4,5,6) means that the last vertex visited is 3 and theremaining vertices to be visited are (4, 5, 6).

    combine

    (2), (4,5,6)

    (b)

    (3), (4,5,6)

    (4), (5,6)

    (1,3,2)

    (1,2,3) (1,2,3,4)

    (1,3,2,4)

    (a)

    3/8/2009 44ADA Unit -4 I.S Borse

  • 8/13/2019 Algorithm Design & AnalysisChapter

    44/58

  • 8/13/2019 Algorithm Design & AnalysisChapter

    45/58

    Review: Dynamic Programming

    Dynamic programming is another strategy fordesigning algorithms

    Use when problem breaks down into recurringsmall subproblems

    3/8/2009 46ADA Unit -4 I.S Borse

  • 8/13/2019 Algorithm Design & AnalysisChapter

    46/58

    Review: Dynamic Programming Summary of the basic idea:

    Optimal substructure: optimal solution to problemconsists of optimal solutions to subproblems

    Overlapping subproblems: few subproblems in total,

    many recurring instances of each Solve bottom-up, building a table of solved

    subproblems that are used to solve larger ones

    Variations: Table could be 3 -dimensional, triangular, a tree, etc.

    3/8/2009 47ADA Unit -4 I.S Borse

  • 8/13/2019 Algorithm Design & AnalysisChapter

    47/58

    Greedy Algorithms

    3/8/2009 48ADA Unit -4 I.S Borse

  • 8/13/2019 Algorithm Design & AnalysisChapter

    48/58

    Overview Like dynamic programming, used to solve

    optimization problems. Problems exhibit optimal substructure (like DP). Problems also exhibit the greedy-choice

    property. When we have a choice to make, make the one that

    looks best right now . Make a locally optimal choice in hope of getting a

    globally optimal solution .

    3/8/2009 49ADA Unit -4 I.S Borse

  • 8/13/2019 Algorithm Design & AnalysisChapter

    49/58

    Greedy Strategy The choice that seems best at the moment is the

    one we go with. Prove that when there is a choice to make, one

    of the optimal choices is the greedy choice.

    Therefore, its always safe to make the greedychoice.

    Show that all but one of the subproblems

    resulting from the greedy choice are emp ty.

    3/8/2009 50ADA Unit -4 I.S Borse

  • 8/13/2019 Algorithm Design & AnalysisChapter

    50/58

    Elements of Greedy Algorithms

    Greedy-choice Property. A globally optimal solution can be arrived at by

    making a locally optimal (greedy) choice.

    Optimal Substructure.

    3/8/2009 51ADA Unit -4 I.S Borse

    G d Al i h

  • 8/13/2019 Algorithm Design & AnalysisChapter

    51/58

    Greedy Algorithms

    A greedy algorithm always makes the choicethat looks best at the moment My everyday examples:

    Walking to the Corner Playing a bridge hand

    The hope: a locally optimal choice will lead to aglobally optimal solution

    For some problems, it works Dynamic programming can be overkill; greedy

    algorithms tend to be easier to code3/8/2009 52ADA Unit -4 I.S Borse

  • 8/13/2019 Algorithm Design & AnalysisChapter

    52/58

  • 8/13/2019 Algorithm Design & AnalysisChapter

    53/58

    Review: The Knapsack ProblemAnd Optimal Substructure

    Both variations exhibit optimal substructure To show this for the 0-1 problem, consider the

    most valuable load weighing at most W pounds If we remove item j from the load, what do we know

    about the remaining load? A: remainder must be the most valuable load

    weighing at most W - w j that thief could take frommuseum, excluding item j

    3/8/2009 54ADA Unit -4 I.S Borse

  • 8/13/2019 Algorithm Design & AnalysisChapter

    54/58

    Solving The Knapsack Problem The optimal solution to the fractional knapsack

    problem can be found with a greedy algorithm How?

    The optimal solution to the 0-1 problem cannot

    be found with the same greedy strategy Greedy strategy: take in order of dollars/pound Example: 3 items weighing 10, 20, and 30 pounds,

    knapsack can hold 50 pounds Suppose item 2 is worth $100. Assign values to the other

    items so that the greedy strategy will fail

    3/8/2009 55ADA Unit -4 I.S Borse

  • 8/13/2019 Algorithm Design & AnalysisChapter

    55/58

    Greedy Choice Property Dynamic programming? Memorize? Yes, but Activity selection problem also exhibits the

    greedy choice property: Locally optimal choice globally optimal soln Them 17.1: if S is an activity selection problem

    sorted by finish time, then optimal solution A S such that {1} A

    Sketch of proof: if optimal solution B that does notcontain {1}, can always replace first activity in B with {1}(Why? ). Same number of activities, thus optimal.

    3/8/2009 56ADA Unit -4 I.S Borse

  • 8/13/2019 Algorithm Design & AnalysisChapter

    56/58

    Review: The Knapsack Problem

    The famous knapsack problem : A thief breaks into a museum. Fabulous paintings,sculptures, and jewels are everywhere. The thief hasa good eye for the value of these objects, and knows

    that each will fetch hundreds or thousands of dollarson the clandestine art collectors market. But, thethief has only brought a single knapsack to the sceneof the robbery, and can take away only what he cancarry. What items should the thief take to maximizethe haul?

    3/8/2009 57ADA Unit -4 I.S Borse

  • 8/13/2019 Algorithm Design & AnalysisChapter

    57/58

  • 8/13/2019 Algorithm Design & AnalysisChapter

    58/58