longest common subsequence matrix difference python












0














I'm working on a dynamic programming problem (longest common subsequence)



My issue: building the matrix.



I initially build my matrix with dp1. but it kept churning out the wrong answers. Then I referenced other answers and used dp2, which produces the right answer.



For example:



s1 = ELGGYJWKTDHLXJRBJLRYEJWVSUFZKYHOIKBGTVUTTOCGMLEXWDSXEBKRZTQUVCJNGKKRMUUBACVOEQKBFFYBUQEMYNENKYYGUZSP



s2 = FRVIFOVJYQLVZMFBNRUTIYFBMFFFRZVBYINXLDDSVMPWSQGJZYTKMZIPEGMVOUQBKYEWEYVOLSHCMHPAZYTENRNONTJWDANAMFRX



The right answer should be 27.




  • dp1 gives 30

  • dp2 gives 27


I'm puzzled. What's the difference? isn't "for _ in range(m+1)" essentially iterating whatever is before by m+1 times? please help me out.



def commonChild(s1, s2):
n, m = len(s1), len(s2)
dp1 = [[0] * (n+1)] * (m+1)
dp2 = [[0] * (n+1) for _ in range(m+1)]

for i in range(m):
for j in range(n):
if s2[i] == s1[j]:
dp[i+1][j+1] = dp[i][j] +1
else:
dp[i+1][j+1] = max(dp[i][j+1], dp[i+1][j])

return dp[-1][-1]









share|improve this question



























    0














    I'm working on a dynamic programming problem (longest common subsequence)



    My issue: building the matrix.



    I initially build my matrix with dp1. but it kept churning out the wrong answers. Then I referenced other answers and used dp2, which produces the right answer.



    For example:



    s1 = ELGGYJWKTDHLXJRBJLRYEJWVSUFZKYHOIKBGTVUTTOCGMLEXWDSXEBKRZTQUVCJNGKKRMUUBACVOEQKBFFYBUQEMYNENKYYGUZSP



    s2 = FRVIFOVJYQLVZMFBNRUTIYFBMFFFRZVBYINXLDDSVMPWSQGJZYTKMZIPEGMVOUQBKYEWEYVOLSHCMHPAZYTENRNONTJWDANAMFRX



    The right answer should be 27.




    • dp1 gives 30

    • dp2 gives 27


    I'm puzzled. What's the difference? isn't "for _ in range(m+1)" essentially iterating whatever is before by m+1 times? please help me out.



    def commonChild(s1, s2):
    n, m = len(s1), len(s2)
    dp1 = [[0] * (n+1)] * (m+1)
    dp2 = [[0] * (n+1) for _ in range(m+1)]

    for i in range(m):
    for j in range(n):
    if s2[i] == s1[j]:
    dp[i+1][j+1] = dp[i][j] +1
    else:
    dp[i+1][j+1] = max(dp[i][j+1], dp[i+1][j])

    return dp[-1][-1]









    share|improve this question

























      0












      0








      0







      I'm working on a dynamic programming problem (longest common subsequence)



      My issue: building the matrix.



      I initially build my matrix with dp1. but it kept churning out the wrong answers. Then I referenced other answers and used dp2, which produces the right answer.



      For example:



      s1 = ELGGYJWKTDHLXJRBJLRYEJWVSUFZKYHOIKBGTVUTTOCGMLEXWDSXEBKRZTQUVCJNGKKRMUUBACVOEQKBFFYBUQEMYNENKYYGUZSP



      s2 = FRVIFOVJYQLVZMFBNRUTIYFBMFFFRZVBYINXLDDSVMPWSQGJZYTKMZIPEGMVOUQBKYEWEYVOLSHCMHPAZYTENRNONTJWDANAMFRX



      The right answer should be 27.




      • dp1 gives 30

      • dp2 gives 27


      I'm puzzled. What's the difference? isn't "for _ in range(m+1)" essentially iterating whatever is before by m+1 times? please help me out.



      def commonChild(s1, s2):
      n, m = len(s1), len(s2)
      dp1 = [[0] * (n+1)] * (m+1)
      dp2 = [[0] * (n+1) for _ in range(m+1)]

      for i in range(m):
      for j in range(n):
      if s2[i] == s1[j]:
      dp[i+1][j+1] = dp[i][j] +1
      else:
      dp[i+1][j+1] = max(dp[i][j+1], dp[i+1][j])

      return dp[-1][-1]









      share|improve this question













      I'm working on a dynamic programming problem (longest common subsequence)



      My issue: building the matrix.



      I initially build my matrix with dp1. but it kept churning out the wrong answers. Then I referenced other answers and used dp2, which produces the right answer.



      For example:



      s1 = ELGGYJWKTDHLXJRBJLRYEJWVSUFZKYHOIKBGTVUTTOCGMLEXWDSXEBKRZTQUVCJNGKKRMUUBACVOEQKBFFYBUQEMYNENKYYGUZSP



      s2 = FRVIFOVJYQLVZMFBNRUTIYFBMFFFRZVBYINXLDDSVMPWSQGJZYTKMZIPEGMVOUQBKYEWEYVOLSHCMHPAZYTENRNONTJWDANAMFRX



      The right answer should be 27.




      • dp1 gives 30

      • dp2 gives 27


      I'm puzzled. What's the difference? isn't "for _ in range(m+1)" essentially iterating whatever is before by m+1 times? please help me out.



      def commonChild(s1, s2):
      n, m = len(s1), len(s2)
      dp1 = [[0] * (n+1)] * (m+1)
      dp2 = [[0] * (n+1) for _ in range(m+1)]

      for i in range(m):
      for j in range(n):
      if s2[i] == s1[j]:
      dp[i+1][j+1] = dp[i][j] +1
      else:
      dp[i+1][j+1] = max(dp[i][j+1], dp[i+1][j])

      return dp[-1][-1]






      python dynamic-programming






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 at 6:01









      Kwok Wen Jian

      12410




      12410
























          1 Answer
          1






          active

          oldest

          votes


















          2














          >>> a=[[0] * (5) for i in range(4)]
          >>> a
          [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
          >>> a[0][0]=1
          >>> a
          [[1, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
          >>> a=[[0] * (5) ]*4
          >>> a[0][0]=1
          >>> a
          [[1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0]]


          You can see the difference yourself,



          in [[0]*(n+1)]*(m+1) it is referring to the same array [0] * (n+1) so changing one array value changes the same value in all






          share|improve this answer





















          • oh god i need a new brain
            – Kwok Wen Jian
            Nov 20 at 6:28










          • then is there a difference between a=[["0" for x in range(4)]for x in range(4)] and a=[[0] * (5) for i in range(4)]?
            – Kwok Wen Jian
            Nov 20 at 6:31












          • no there is no difference
            – Albin Paul
            Nov 20 at 6:33










          • why is that so?
            – Kwok Wen Jian
            Nov 20 at 6:52










          • @KwokWenJian thislink has the answer you are looking for
            – Albin Paul
            Nov 20 at 6:56











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53387097%2flongest-common-subsequence-matrix-difference-python%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          >>> a=[[0] * (5) for i in range(4)]
          >>> a
          [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
          >>> a[0][0]=1
          >>> a
          [[1, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
          >>> a=[[0] * (5) ]*4
          >>> a[0][0]=1
          >>> a
          [[1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0]]


          You can see the difference yourself,



          in [[0]*(n+1)]*(m+1) it is referring to the same array [0] * (n+1) so changing one array value changes the same value in all






          share|improve this answer





















          • oh god i need a new brain
            – Kwok Wen Jian
            Nov 20 at 6:28










          • then is there a difference between a=[["0" for x in range(4)]for x in range(4)] and a=[[0] * (5) for i in range(4)]?
            – Kwok Wen Jian
            Nov 20 at 6:31












          • no there is no difference
            – Albin Paul
            Nov 20 at 6:33










          • why is that so?
            – Kwok Wen Jian
            Nov 20 at 6:52










          • @KwokWenJian thislink has the answer you are looking for
            – Albin Paul
            Nov 20 at 6:56
















          2














          >>> a=[[0] * (5) for i in range(4)]
          >>> a
          [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
          >>> a[0][0]=1
          >>> a
          [[1, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
          >>> a=[[0] * (5) ]*4
          >>> a[0][0]=1
          >>> a
          [[1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0]]


          You can see the difference yourself,



          in [[0]*(n+1)]*(m+1) it is referring to the same array [0] * (n+1) so changing one array value changes the same value in all






          share|improve this answer





















          • oh god i need a new brain
            – Kwok Wen Jian
            Nov 20 at 6:28










          • then is there a difference between a=[["0" for x in range(4)]for x in range(4)] and a=[[0] * (5) for i in range(4)]?
            – Kwok Wen Jian
            Nov 20 at 6:31












          • no there is no difference
            – Albin Paul
            Nov 20 at 6:33










          • why is that so?
            – Kwok Wen Jian
            Nov 20 at 6:52










          • @KwokWenJian thislink has the answer you are looking for
            – Albin Paul
            Nov 20 at 6:56














          2












          2








          2






          >>> a=[[0] * (5) for i in range(4)]
          >>> a
          [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
          >>> a[0][0]=1
          >>> a
          [[1, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
          >>> a=[[0] * (5) ]*4
          >>> a[0][0]=1
          >>> a
          [[1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0]]


          You can see the difference yourself,



          in [[0]*(n+1)]*(m+1) it is referring to the same array [0] * (n+1) so changing one array value changes the same value in all






          share|improve this answer












          >>> a=[[0] * (5) for i in range(4)]
          >>> a
          [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
          >>> a[0][0]=1
          >>> a
          [[1, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
          >>> a=[[0] * (5) ]*4
          >>> a[0][0]=1
          >>> a
          [[1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0]]


          You can see the difference yourself,



          in [[0]*(n+1)]*(m+1) it is referring to the same array [0] * (n+1) so changing one array value changes the same value in all







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 at 6:18









          Albin Paul

          1,345717




          1,345717












          • oh god i need a new brain
            – Kwok Wen Jian
            Nov 20 at 6:28










          • then is there a difference between a=[["0" for x in range(4)]for x in range(4)] and a=[[0] * (5) for i in range(4)]?
            – Kwok Wen Jian
            Nov 20 at 6:31












          • no there is no difference
            – Albin Paul
            Nov 20 at 6:33










          • why is that so?
            – Kwok Wen Jian
            Nov 20 at 6:52










          • @KwokWenJian thislink has the answer you are looking for
            – Albin Paul
            Nov 20 at 6:56


















          • oh god i need a new brain
            – Kwok Wen Jian
            Nov 20 at 6:28










          • then is there a difference between a=[["0" for x in range(4)]for x in range(4)] and a=[[0] * (5) for i in range(4)]?
            – Kwok Wen Jian
            Nov 20 at 6:31












          • no there is no difference
            – Albin Paul
            Nov 20 at 6:33










          • why is that so?
            – Kwok Wen Jian
            Nov 20 at 6:52










          • @KwokWenJian thislink has the answer you are looking for
            – Albin Paul
            Nov 20 at 6:56
















          oh god i need a new brain
          – Kwok Wen Jian
          Nov 20 at 6:28




          oh god i need a new brain
          – Kwok Wen Jian
          Nov 20 at 6:28












          then is there a difference between a=[["0" for x in range(4)]for x in range(4)] and a=[[0] * (5) for i in range(4)]?
          – Kwok Wen Jian
          Nov 20 at 6:31






          then is there a difference between a=[["0" for x in range(4)]for x in range(4)] and a=[[0] * (5) for i in range(4)]?
          – Kwok Wen Jian
          Nov 20 at 6:31














          no there is no difference
          – Albin Paul
          Nov 20 at 6:33




          no there is no difference
          – Albin Paul
          Nov 20 at 6:33












          why is that so?
          – Kwok Wen Jian
          Nov 20 at 6:52




          why is that so?
          – Kwok Wen Jian
          Nov 20 at 6:52












          @KwokWenJian thislink has the answer you are looking for
          – Albin Paul
          Nov 20 at 6:56




          @KwokWenJian thislink has the answer you are looking for
          – Albin Paul
          Nov 20 at 6:56


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53387097%2flongest-common-subsequence-matrix-difference-python%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Paul Cézanne

          UIScrollView CustomStickyHeader Resize height generates problems when scroll is too fast

          Angular material date-picker (MatDatepicker) auto completes the date on focus out