Lecture2.pdf

Embed Size (px)

Citation preview

  • Competitive Programming-Lecture 2

    Programming Club

    IIT Kanpur

    June 8, 2012

    Programming Club (IIT Kanpur) Competitive Programming-Lecture 2 June 8, 2012 1 / 11

  • Divide and Conquer

    Integer Multiplication

    Multiply two numbers of x and y of n bits

    Bitwise Multiplication: O(n2)

    x=x1*2n/2+x0

    y=y1*2n/2+y0

    xy=x1*y1*2n+(x1*y0+y1*x0)*2

    n/2+x0*y0

    Compute 4 products:

    T (n) = 4T (n/2) + O(n) = T (n) = O(n2)Compute three different products: x1*y1, x0*y0, (x1+x0).(y1+y0)

    T(n)=3T(n/2)+O(n)T(n)=O(n1.59)

    Programming Club (IIT Kanpur) Competitive Programming-Lecture 2 June 8, 2012 2 / 11

  • Divide and Conquer

    Integer Multiplication

    Multiply two numbers of x and y of n bits

    Bitwise Multiplication: O(n2)

    x=x1*2n/2+x0

    y=y1*2n/2+y0

    xy=x1*y1*2n+(x1*y0+y1*x0)*2

    n/2+x0*y0

    Compute 4 products:

    T (n) = 4T (n/2) + O(n) = T (n) = O(n2)Compute three different products: x1*y1, x0*y0, (x1+x0).(y1+y0)

    T(n)=3T(n/2)+O(n)T(n)=O(n1.59)

    Programming Club (IIT Kanpur) Competitive Programming-Lecture 2 June 8, 2012 2 / 11

  • Divide and Conquer

    Integer Multiplication

    Multiply two numbers of x and y of n bits

    Bitwise Multiplication: O(n2)

    x=x1*2n/2+x0

    y=y1*2n/2+y0

    xy=x1*y1*2n+(x1*y0+y1*x0)*2

    n/2+x0*y0

    Compute 4 products:

    T (n) = 4T (n/2) + O(n) = T (n) = O(n2)Compute three different products: x1*y1, x0*y0, (x1+x0).(y1+y0)

    T(n)=3T(n/2)+O(n)T(n)=O(n1.59)

    Programming Club (IIT Kanpur) Competitive Programming-Lecture 2 June 8, 2012 2 / 11

  • Divide and Conquer

    Integer Multiplication

    Multiply two numbers of x and y of n bits

    Bitwise Multiplication: O(n2)

    x=x1*2n/2+x0

    y=y1*2n/2+y0

    xy=x1*y1*2n+(x1*y0+y1*x0)*2

    n/2+x0*y0

    Compute 4 products:

    T (n) = 4T (n/2) + O(n) = T (n) = O(n2)Compute three different products: x1*y1, x0*y0, (x1+x0).(y1+y0)

    T(n)=3T(n/2)+O(n)T(n)=O(n1.59)

    Programming Club (IIT Kanpur) Competitive Programming-Lecture 2 June 8, 2012 2 / 11

  • Divide and Conquer

    Integer Multiplication

    Multiply two numbers of x and y of n bits

    Bitwise Multiplication: O(n2)

    x=x1*2n/2+x0

    y=y1*2n/2+y0

    xy=x1*y1*2n+(x1*y0+y1*x0)*2

    n/2+x0*y0

    Compute 4 products:

    T (n) = 4T (n/2) + O(n) = T (n) = O(n2)Compute three different products: x1*y1, x0*y0, (x1+x0).(y1+y0)

    T(n)=3T(n/2)+O(n)T(n)=O(n1.59)

    Programming Club (IIT Kanpur) Competitive Programming-Lecture 2 June 8, 2012 2 / 11

  • Dynamic Programming

    Dynamic Programming

    DP solves problems by combining the solutions to overlappingsubproblems

    Solves each subproblem just once and stores its result, therebyavoiding recomputing

    Programming Club (IIT Kanpur) Competitive Programming-Lecture 2 June 8, 2012 3 / 11

  • Dynamic Programming

    Traversing a matrix

    Problem: Given a matrix with the cost of visiting a node and theconstraint that from (i,j), you can go to (i+1,j), (i+1,j-1) and(i+1,j+1)

    Programming Club (IIT Kanpur) Competitive Programming-Lecture 2 June 8, 2012 4 / 11

  • Dynamic Programming

    Coin Change

    Problem: Given coins of certain denomination and infinite coins ofeach denominations, find the minimum number of coins such thatthey sum to N rupees

    Dynamic Programming

    Let the coins denomination be c1, c2,. . . , ck . Let C[i] be theminimum number of coins to exchange i rupees

    Optimal Substructure

    C[i]=1+minj{C[i-cj ]}

    Programming Club (IIT Kanpur) Competitive Programming-Lecture 2 June 8, 2012 5 / 11

  • Dynamic Programming

    Coin Change

    Problem: Given coins of certain denomination and infinite coins ofeach denominations, find the minimum number of coins such thatthey sum to N rupees

    Dynamic Programming

    Let the coins denomination be c1, c2,. . . , ck . Let C[i] be theminimum number of coins to exchange i rupees

    Optimal Substructure

    C[i]=1+minj{C[i-cj ]}

    Programming Club (IIT Kanpur) Competitive Programming-Lecture 2 June 8, 2012 5 / 11

  • Dynamic Programming

    Longest Common Subsequence

    Problem: Given two sequences, find the longest subsequence commonto both

    opt(i,j) = length of subsequence in X[0.1. . . i] and Y[0.1. . . j]

    opt(i,j) = 0 if i=0 or j=0

    opt(i,j) = opt(i-1,j-1)+1 if xi=yj i , j > 0

    opt(i,j) = Max[opt(i,j-1), opt(i-i,j)] where i , j > 0 xi 6= yj

    Programming Club (IIT Kanpur) Competitive Programming-Lecture 2 June 8, 2012 6 / 11

  • Dynamic Programming

    Longest Common Subsequence

    Problem: Given two sequences, find the longest subsequence commonto both

    opt(i,j) = length of subsequence in X[0.1. . . i] and Y[0.1. . . j]

    opt(i,j) = 0 if i=0 or j=0

    opt(i,j) = opt(i-1,j-1)+1 if xi=yj i , j > 0

    opt(i,j) = Max[opt(i,j-1), opt(i-i,j)] where i , j > 0 xi 6= yj

    Programming Club (IIT Kanpur) Competitive Programming-Lecture 2 June 8, 2012 6 / 11

  • Dynamic Programming

    Longest Common Subsequence

    Problem: Given two sequences, find the longest subsequence commonto both

    opt(i,j) = length of subsequence in X[0.1. . . i] and Y[0.1. . . j]

    opt(i,j) = 0 if i=0 or j=0

    opt(i,j) = opt(i-1,j-1)+1 if xi=yj i , j > 0

    opt(i,j) = Max[opt(i,j-1), opt(i-i,j)] where i , j > 0 xi 6= yj

    Programming Club (IIT Kanpur) Competitive Programming-Lecture 2 June 8, 2012 6 / 11

  • Dynamic Programming

    Matrix Chain Multiplication

    Problem: To find an optimal bracketing so as to minimize the totalnumber of multiplication steps

    opt(i,j): number of multiplications between matrices Ai and A jSubstructure

    opt(i,j) = Min{opt(i,k)+opt(k+1,j)+mi .mk+1.mj+1}where k=i+1 to j-1

    Base Case: opt(i,i)=0

    Programming Club (IIT Kanpur) Competitive Programming-Lecture 2 June 8, 2012 7 / 11

  • Dynamic Programming

    Matrix Chain Multiplication

    Problem: To find an optimal bracketing so as to minimize the totalnumber of multiplication steps

    opt(i,j): number of multiplications between matrices Ai and A jSubstructure

    opt(i,j) = Min{opt(i,k)+opt(k+1,j)+mi .mk+1.mj+1}where k=i+1 to j-1

    Base Case: opt(i,i)=0

    Programming Club (IIT Kanpur) Competitive Programming-Lecture 2 June 8, 2012 7 / 11

  • Dynamic Programming

    Longest Non-Decreasing Subsequence

    Problem: Given an array, find the subsequence which isnon-decreasign and of maximum length

    L[i]: Length of maximum subsequence ending at index i

    Substructure

    L[i]=1+Max{L[j] st. i > j and A[i ] A[j ]}

    Programming Club (IIT Kanpur) Competitive Programming-Lecture 2 June 8, 2012 8 / 11

    userSticky Note

  • Dynamic Programming

    Longest Non-Decreasing Subsequence

    Problem: Given an array, find the subsequence which isnon-decreasign and of maximum length

    L[i]: Length of maximum subsequence ending at index i

    Substructure

    L[i]=1+Max{L[j] st. i > j and A[i ] A[j ]}

    Programming Club (IIT Kanpur) Competitive Programming-Lecture 2 June 8, 2012 8 / 11

  • Dynamic Programming

    Subsets of an array

    Problem: Given 2 integers a,b and a set S, calculate #X such thata < X < b and X is not divisible by all elements of S

    Programming Club (IIT Kanpur) Competitive Programming-Lecture 2 June 8, 2012 9 / 11

  • Dynamic Programming

    Task Scheduling

    Problem: To find an optimum scheduling of overlapping tasks withdifferent starting and ending time : tasks have no weight

    Sort the events in the order of finishing time

    Greedy Approach

    Keep picking tasks compatible to previous one in successionThe greedy strategy stays ahead

    Dynamic Approach

    Substructure

    opt(j)=Max{opt(p(j)), opt(j-1)}// where p(j) is the next compatible event with j

    Programming Club (IIT Kanpur) Competitive Programming-Lecture 2 June 8, 2012 10 / 11

  • Dynamic Programming

    Task Scheduling

    Problem: To find an optimum scheduling of overlapping tasks withdifferent starting and ending time : tasks have no weight

    Sort the events in the order of finishing time

    Greedy Approach

    Keep picking tasks compatible to previous one in successionThe greedy strategy stays ahead

    Dynamic Approach

    Substructure

    opt(j)=Max{opt(p(j)), opt(j-1)}// where p(j) is the next compatible event with j

    Programming Club (IIT Kanpur) Competitive Programming-Lecture 2 June 8, 2012 10 / 11

  • Dynamic Programming

    Task Scheduling

    Problem: To find an optimum scheduling of overlapping tasks withdifferent starting and ending time : tasks have no weight

    Sort the events in the order of finishing time

    Greedy Approach

    Keep picking tasks compatible to previous one in successionThe greedy strategy stays ahead

    Dynamic Approach

    Substructure

    opt(j)=Max{opt(p(j)), opt(j-1)}// where p(j) is the next compatible event with j

    Programming Club (IIT Kanpur) Competitive Programming-Lecture 2 June 8, 2012 10 / 11

  • Dynamic Programming

    Integer (0/1) Knapsack Problem

    Problem: Given n items of sizes si and values vi and a knapsack ofcapacity C, find the maximum value of items that can be filled in theknapsack

    M(i,j) = optimal value for filling a capacity j knapsack with somesubset of items 1. . . i

    Substructure

    M(i,j)=Max{M(i-1,j),M(i-1,j-si )+vi}

    Programming Club (IIT Kanpur) Competitive Programming-Lecture 2 June 8, 2012 11 / 11

  • Dynamic Programming

    Integer (0/1) Knapsack Problem

    Problem: Given n items of sizes si and values vi and a knapsack ofcapacity C, find the maximum value of items that can be filled in theknapsack

    M(i,j) = optimal value for filling a capacity j knapsack with somesubset of items 1. . . i

    Substructure

    M(i,j)=Max{M(i-1,j),M(i-1,j-si )+vi}

    Programming Club (IIT Kanpur) Competitive Programming-Lecture 2 June 8, 2012 11 / 11

    Divide and ConquerDynamic Programming