- #1
CGandC
- 326
- 34
- TL;DR Summary
- How to prove the existence of optimal substructure for the dynamic programming problem "Sherlock and Cost"?
I was attempting to solve the "Sherlock and Cost" problem from HackerRank using DP:
But before I went to come up with a recursive relation, I wanted to find if the problem possesses an optimal substructure, and I was following these steps as written at CLRS book:
Mentor note: Inline images of mathematical expressions have been replaced by LaTeX.
I did step 1 as follows:
Define ## T = \{ \} ~~ . \forall i ## choose ## a \in B ## s.t. we define ## A = a ## s.t. ##1 \le A[ i] \le B[ i]## . After creating array ## A ## perform ## T.add(A) ##.
Continue adding arrays to set ## T ## until all possible added arrays ## A ## that satisfy ##1 \le A[ i] \le B[ i]## have been exhausted. Define the set ## SUMS = \{ \} ## . Afterwards, ## \forall j ## we look at the ## jth ## array ## A_j \in T ## , define ##S_j = \sum_{i = 1} (A_j[ i] - A_j[ i + 1])## and perform ## SUMS.add(S_j) ##.
Take ## max(SUMS) ## and we're finished. We showed the problem indeed consists of making a choice.Now, I want to prove steps 2+3+4 , this is done by taking an arbitrary object which you wish to optimize and assume that to optimize the object then ## X ## will occur, from this assumption subproblems should ensure and you show the solution to each one of them is an optimal solution.
Now, going back to proving steps 2+3+4 in the "Sherlock and Cost" problem, I have no idea what to do, I started with:
" Suppose that in order to find the array ## \hat{ A} ## s.t. ## S_{ \hat{ A} } ## is ##S_{ \hat{ A} } = \sum_{i = 1}({ \hat{ A} }_j[ i] - { \hat{ A} }_j[ i + 1])## is maximal ... [ stuff to be added ] "
but I have no idea how to continue, how would you proceed to formally proving steps 2+3+4 ( which is proving an optimal substructure exists for the problem )?
Note: Here's an example of how the existence of an optimal substructure is proved for the matrix parenthization problem ( from CLRS chapter 15.2 ) -
Thanks in advance for any help!
But before I went to come up with a recursive relation, I wanted to find if the problem possesses an optimal substructure, and I was following these steps as written at CLRS book:
Mentor note: Inline images of mathematical expressions have been replaced by LaTeX.
I did step 1 as follows:
Define ## T = \{ \} ~~ . \forall i ## choose ## a \in B ## s.t. we define ## A = a ## s.t. ##1 \le A[ i] \le B[ i]## . After creating array ## A ## perform ## T.add(A) ##.
Continue adding arrays to set ## T ## until all possible added arrays ## A ## that satisfy ##1 \le A[ i] \le B[ i]## have been exhausted. Define the set ## SUMS = \{ \} ## . Afterwards, ## \forall j ## we look at the ## jth ## array ## A_j \in T ## , define ##S_j = \sum_{i = 1} (A_j[ i] - A_j[ i + 1])## and perform ## SUMS.add(S_j) ##.
Take ## max(SUMS) ## and we're finished. We showed the problem indeed consists of making a choice.Now, I want to prove steps 2+3+4 , this is done by taking an arbitrary object which you wish to optimize and assume that to optimize the object then ## X ## will occur, from this assumption subproblems should ensure and you show the solution to each one of them is an optimal solution.
Now, going back to proving steps 2+3+4 in the "Sherlock and Cost" problem, I have no idea what to do, I started with:
" Suppose that in order to find the array ## \hat{ A} ## s.t. ## S_{ \hat{ A} } ## is ##S_{ \hat{ A} } = \sum_{i = 1}({ \hat{ A} }_j[ i] - { \hat{ A} }_j[ i + 1])## is maximal ... [ stuff to be added ] "
but I have no idea how to continue, how would you proceed to formally proving steps 2+3+4 ( which is proving an optimal substructure exists for the problem )?
Note: Here's an example of how the existence of an optimal substructure is proved for the matrix parenthization problem ( from CLRS chapter 15.2 ) -
Thanks in advance for any help!
Attachments
Last edited: