<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Complexity of algorithm in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/complexity-of-algorithm/m-p/8288525#M1634530</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thks Pranavjeet and Volker Borowski alot , i got it.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 25 Oct 2011 04:41:47 GMT</pubDate>
    <dc:creator>Former Member</dc:creator>
    <dc:date>2011-10-25T04:41:47Z</dc:date>
    <item>
      <title>Complexity of algorithm</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/complexity-of-algorithm/m-p/8288522#M1634527</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi guys , i'm a newbie in ABAP..&lt;/P&gt;&lt;P&gt;Today when i read optimizer part in SAP , it give me some confuse.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI level="1" type="ul"&gt;&lt;P&gt;Part1&lt;/P&gt;&lt;/LI&gt;&lt;LI level="1" type="ul"&gt;&lt;P&gt;Entries: 1000 (ITAB1), 300 (ITAB2)&lt;/P&gt;&lt;/LI&gt;&lt;LI level="1" type="ul"&gt;&lt;P&gt;Line width: 100&lt;/P&gt;&lt;/LI&gt;&lt;LI level="1" type="ul"&gt;&lt;P&gt;Both tables sorted by unique key K ascending&lt;/P&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;LOOP AT ITAB1 INTO WA1.&lt;/P&gt;&lt;P&gt;  READ TABLE ITAB2 INTO WA2&lt;/P&gt;&lt;P&gt;             WITH KEY K = WA1-K BINARY SEARCH.&lt;/P&gt;&lt;P&gt;  IF SY-SUBRC = 0.&lt;/P&gt;&lt;P&gt;    " ...&lt;/P&gt;&lt;P&gt;  ENDIF.&lt;/P&gt;&lt;P&gt;ENDLOOP.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In documentation , it write the complexity of program Part1 : O ( n * log2n ) . I'm totally agree with that.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI level="1" type="ul"&gt;&lt;P&gt;Part 2&lt;/P&gt;&lt;/LI&gt;&lt;LI level="1" type="ul"&gt;&lt;P&gt;Entries: 1000 (ITAB1), 300 (ITAB2)&lt;/P&gt;&lt;/LI&gt;&lt;LI level="1" type="ul"&gt;&lt;P&gt;Line width: 100&lt;/P&gt;&lt;/LI&gt;&lt;LI level="1" type="ul"&gt;&lt;P&gt;Both tables sorted by unique key K ascending&lt;/P&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;DATA: I TYPE I.&lt;/P&gt;&lt;P&gt;I = 1.&lt;/P&gt;&lt;P&gt;LOOP AT ITAB1 INTO WA1.&lt;/P&gt;&lt;P&gt;  do.&lt;/P&gt;&lt;P&gt;    READ TABLE ITAB2 INTO WA2 INDEX I.&lt;/P&gt;&lt;P&gt;    IF SY-SUBRC &amp;lt;&amp;gt; 0. EXIT. ENDIF.&lt;/P&gt;&lt;P&gt;    IF WA2-K &amp;lt; WA1-K.&lt;/P&gt;&lt;P&gt;      ADD 1 TO I.&lt;/P&gt;&lt;P&gt;    ELSEIF WA2-K = WA1-K.&lt;/P&gt;&lt;P&gt;      " ...&lt;/P&gt;&lt;P&gt;      ADD 1 TO I.&lt;/P&gt;&lt;P&gt;      EXIT.&lt;/P&gt;&lt;P&gt;    ELSE.&lt;/P&gt;&lt;P&gt;      EXIT.&lt;/P&gt;&lt;P&gt;    endif.&lt;/P&gt;&lt;P&gt;  enddo.&lt;/P&gt;&lt;P&gt;  if sy-subrc &amp;lt;&amp;gt; 0. exit. endif.&lt;/P&gt;&lt;P&gt;ENDLOOP.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It said " the complexity of program Part2 is : O ( n1 + n2) . I don't understand with this ( how it can separate loop statment and read statment like that ? ) &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Anybody can explain it for me ? Thks alot.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 23 Oct 2011 17:04:58 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/complexity-of-algorithm/m-p/8288522#M1634527</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2011-10-23T17:04:58Z</dc:date>
    </item>
    <item>
      <title>Re: Complexity of algorithm</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/complexity-of-algorithm/m-p/8288523#M1634528</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Please find an example. I hope it helps ..&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;suppose ITAB1 = ( 1, 2, 3, 4, 5) and ITAB2 = (2, 4)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;What would be the total number of loop count in this case:&lt;/P&gt;&lt;P&gt;Following would be the flow: &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;ITAB1 = 1. I = 1.&lt;/P&gt;&lt;P&gt;ITAB2 = 2.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;1 &amp;lt; 2 . (WA1-K &amp;lt; WA2-K) Exit. Enddo. - Total Loop Count 1 + 1 = 2.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;ITAB1 = 2. I = 1.&lt;/P&gt;&lt;P&gt;ITAB2 = 2.&lt;/P&gt;&lt;P&gt;2 = 2. &lt;/P&gt;&lt;P&gt;I =  1+ 1 = 2.&lt;/P&gt;&lt;P&gt;EXIT ENDDO. Total Loop Count = 1 + 1 = 2.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;ITAB1 = 3.  I = 2. &lt;/P&gt;&lt;P&gt;ITAB2 = 4.&lt;/P&gt;&lt;P&gt;3 &amp;lt; 4.  (WA1-K &amp;lt; WA2-K) Exit. Enddo. Total Loop Count 1 + 1 = 2.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;ITAB1 = 4. I = 2.&lt;/P&gt;&lt;P&gt;ITAB2 = 4. &lt;/P&gt;&lt;P&gt;I = 3.&lt;/P&gt;&lt;P&gt;4 = 4. EXIT. ENDDO. Total Loop Cpunt 1 + 1 = 2.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;ITAB1 = 4. I = 3.&lt;/P&gt;&lt;P&gt;READ ITAB2 fails. Exit. Enddo. Exit Outer Loop. Total Loop Cont = 1 + 1 = 2.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So total Loop Count = 2 + 2 + 2 + 2 + 2 = 10 ( which is of the order 5 + 2 = 7).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Similarly if ITAB1 = (1,  2, 3, 4, 5, 6, 7, 8, 9, 10) and ITAB2 = (2, 4).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Total loop count in this case will also be 10 which is again of the order 10 + 2 = 12. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Similar will be the scenario in all cases.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 23 Oct 2011 23:43:01 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/complexity-of-algorithm/m-p/8288523#M1634528</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2011-10-23T23:43:01Z</dc:date>
    </item>
    <item>
      <title>Re: Complexity of algorithm</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/complexity-of-algorithm/m-p/8288524#M1634529</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;UL&gt;&lt;LI level="1" type="ul"&gt;&lt;P&gt;* Part1&lt;/P&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Part one looks to me like the classic Nested Loop Join.&lt;/P&gt;&lt;P&gt;It does not require the outer table to be sorted, and the inner Table needs only &lt;/P&gt;&lt;P&gt;to be sorted to support the binary search of the key.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;UL&gt;&lt;LI level="1" type="ul"&gt;&lt;P&gt;* Part 2&lt;/P&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt; &lt;/P&gt;&lt;UL&gt;&lt;LI level="1" type="ul"&gt;&lt;P&gt;Both tables sorted by unique key K ascending&lt;/P&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;And this is the try for a merge join, but I think the code is not optimized.&lt;/P&gt;&lt;P&gt;Total maximum required readcount being n1+n2 is correct, if the key is unique (might be even better).&lt;/P&gt;&lt;P&gt;But this sample code is doing at least one inner read for each outer key.&lt;/P&gt;&lt;P&gt;So even when itab2 starts with K=10, the first itab2 record will &lt;/P&gt;&lt;P&gt;be re-read for all itab1 values of 1-9&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I think the classical mergejoin with unique keys should look like the code below.&lt;/P&gt;&lt;P&gt;It might be worth to discuss putting the smaller table (itab2) to be read first,&lt;/P&gt;&lt;P&gt;depending on the probability that it gets the "not found" first, to save reads on itab1.&lt;/P&gt;&lt;P&gt;This approach has the benefit, that you might be even better than n1+n2, depending on real data,&lt;/P&gt;&lt;P&gt;as one of the tables might run out of big enough keys early.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
I_itab1 = 1.
I_itab2 = 1.
need_to_read_itab1 = 1.
need_to_read_itab2 = 1.
do
  IF need_to_read_itab1 = 1.
    READ TABLE ITAB1 INTO WA1 INDEX I_itab1.
    IF SY-SUBRC 0. EXIT. ENDIF.
    need_to_read_itab1 = 0.
  ENDIF.
  IF need_to_read_itab2 = 1.
    READ TABLE ITAB2 INTO WA2 INDEX I_itab2.
    IF SY-SUBRC 0. EXIT. ENDIF.
    need_to_read_itab2 = 0.
  ENDIF.
  
  IF WA2-K &amp;lt; WA1-K.
    ADD 1 TO I_itab2.
    need_to_read_itab2 = 1.
  ELSEIF WA2-K &amp;gt; WA1-K.
    ADD 1 TO I_itab1.
    need_to_read_itab1 = 1.
  ELSE  "got a match, process and re-read both
    " ...
    ADD 1 TO I_itab1.
    ADD 1 TO I_itab2.
    need_to_read_itab2 = 1.
    need_to_read_itab2 = 1.
  endif.
enddo.
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Volker&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 24 Oct 2011 07:56:43 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/complexity-of-algorithm/m-p/8288524#M1634529</guid>
      <dc:creator>volker_borowski2</dc:creator>
      <dc:date>2011-10-24T07:56:43Z</dc:date>
    </item>
    <item>
      <title>Re: Complexity of algorithm</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/complexity-of-algorithm/m-p/8288525#M1634530</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thks Pranavjeet and Volker Borowski alot , i got it.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 25 Oct 2011 04:41:47 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/complexity-of-algorithm/m-p/8288525#M1634530</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2011-10-25T04:41:47Z</dc:date>
    </item>
  </channel>
</rss>

