Equivalent RAM Program: Hints to Show Complexity $O(T^2(n))$

Click For Summary

Discussion Overview

The discussion revolves around demonstrating that for each RAM program with time complexity $T(n)$ under a logarithmic cost function, there exists an equivalent RAM program with time complexity $O(T^2(n))$ that does not utilize MULT or DIV instructions. Participants are seeking hints and proposing pseudocode to illustrate their ideas.

Discussion Character

  • Exploratory, Technical explanation, Debate/contested, Mathematical reasoning

Main Points Raised

  • Some participants propose creating a subprogram to replace MULT instructions, questioning the complexity of such a subprogram.
  • One participant suggests a pseudocode for multiplication using repeated addition, but expresses uncertainty about its correctness.
  • Another participant questions the multiplication logic in the pseudocode, noting the absence of a second variable.
  • A later reply presents an alternative pseudocode that includes a check for zero and iterates based on the value of b, which some participants agree would work.
  • Participants discuss the time complexity of the proposed RAM programs, with one suggesting that the complexity of a MULT operation is $O(T(\text{whileloop}) \cdot n)$, indicating a potential bottleneck in the overall complexity.
  • There is a concern about the level of detail in the complexity analysis, with some suggesting it may be too detailed for the discussion.

Areas of Agreement / Disagreement

Participants express various viewpoints on the correctness and complexity of the proposed pseudocode, indicating that there is no clear consensus on the best approach or the accuracy of the implementations discussed.

Contextual Notes

Participants highlight the importance of the while-loop's complexity in determining the overall time complexity of the RAM programs, but the exact assumptions and definitions of complexity are not fully resolved.

mathmari
Gold Member
MHB
Messages
4,984
Reaction score
7
Hey! :o

I have to show that for each RAM program of time complexity $T(n)$ under the logarithmic cost function there is an equivalent RAM program of time complexity $O(T^2(n))$ which has no MULT or DIV instructions.

Could you give me some hints how to show that?? (Wondering)
 
Technology news on Phys.org
mathmari said:
Hey! :o

I have to show that for each RAM program of time complexity $T(n)$ under the logarithmic cost function there is an equivalent RAM program of time complexity $O(T^2(n))$ which has no MULT or DIV instructions.

Could you give me some hints how to show that?? (Wondering)

Hi! (Smile)

Let's assume a worst case scenario, where at an innermost loop on the critical path we have a MULT or DIV instruction.

Then we should replace that instruction by a sub program.
Can you create such a RAM sub program for the MULT instruction? (Wondering)

If so, what is its complexity? (Wondering)
 
I like Serena said:
Can you create such a RAM sub program for the MULT instruction? (Wondering)

Is it maybe as followed??

c(0) ← c(0)
c(0) ← c(0)+c(0)
...
c(0) ← c(0)+...c(0) (v(a) times)
Code:
LOAD =1
STORE 1

LOAD a
STORE 2

while: LOAD 2
      SUB 1
      JZERO endwhile

ADD 0
LOAD 1
ADD =1

endwhile: WRITE 0

(Wondering)
 
mathmari said:
Is it maybe as followed??

c(0) ← c(0)
c(0) ← c(0)+c(0)
...
c(0) ← c(0)+...c(0) (v(a) times)
Code:
LOAD =1
STORE 1

LOAD a
STORE 2

while: LOAD 2
      SUB 1
      JZERO endwhile

ADD 0
LOAD 1
ADD =1

endwhile: WRITE 0

(Wondering)

Something like that, although I don't think this will quite work. (Worried)

What are you multiplying exactly? (Wondering)
I'd expect an $a$ and a $b$, but I'm only seeing an $a$.

Which pseudo code do you have? (Wondering)
 
I like Serena said:
Something like that, although I don't think this will quite work. (Worried)

What are you multiplying exactly? (Wondering)
I'd expect an $a$ and a $b$, but I'm only seeing an $a$.

Which pseudo code do you have? (Wondering)

I thought again about it and I wrote the following pseudocode:

Code:
i=1
result=0
Read a
Read b
while b-i>0
     result=result+a
     i=i+1
Write result

And the RAM progam is the following:

Code:
LOAD =1
STORE 1

LOAD =0
STORE 2

READ 3
READ 4

while: LOAD 4
       SUB 1
       JZERO endwhile

LOAD 2
ADD 3

endwhile: WRITE 2

Is this correct? (Wondering)
 
mathmari said:
I thought again about it and I wrote the following pseudocode:

Code:
i=1
result=0
Read a
Read b
while b-i>0
     result=result+a
     i=i+1
Write result

Let's see... (Thinking)
Suppose I pick $a=b=1$, then I get... $a * b = 1 * 1 = 0$.
Does that sound right? (Worried)
 
I like Serena said:
Let's see... (Thinking)
Suppose I pick $a=b=1$, then I get... $a * b = 1 * 1 = 0$.
Does that sound right? (Worried)

Is it maybe as followed??

Code:
i=1
Read a
Read b
if b=0
     Write 0
result=a
while b-i>0
     result=result+a
     i=i+1
Write result

(Wondering)
 
mathmari said:
Is it maybe as followed??

Code:
i=1
Read a
Read b
if b=0
     Write 0
result=a
while b-i>0
     result=result+a
     i=i+1
Write result

(Wondering)

That would work. (Smile)

How about:
Code:
i=b
result=0
while i>0
     result=result+a
     i=i-1
(Thinking)
 
mathmari said:
Code:
i=1
Read a
Read b
if b=0
     Write 0
result=a
while b-i>0
     result=result+a
     i=i+1
Write result

Is the RAM program the following??
Code:
LOAD =1
STORE 1

READ 2
READ 3

LOAD 3
JZERO z

LOAD 2
STORE 4

while: LOAD 3
       SUB 1
       JZERO endwhile

LOAD 4
ADD 2
STORE 4

LOAD 1
ADD =1
STORE 1

JUMP while

endwhile: WRITE 4
          HALT

So is the the time complexity under the logarithmic cost as followed??

LOAD =1 : l(1)=1
STORE 1 : l(c(0))+l(1)=l(1)+l(1)=2

READ 2 : l(a)+2
READ 3 : l(b)+2

LOAD 3 : l(3)+l(c(3))=log(3)+1+l(b)
JZERO z : l(c(0))=l(b)

LOAD 2 : l(2)+l(c(2))=2+l(a)
STORE 4 : l(c(0))+l(4)=l(a)+3

while: LOAD 3 : l(3)+l(c(3))=log(3)+l(b)
SUB 1 : l(c(0))+l(1)+l(c(1))=l(b)+1+l(1)=l(b)+1+1=l(b)+2
JZERO endwhile : l(c(0))=l(b-1)=log(b-1)+1

LOAD 4 : l(4)+l(c(4))=3+l(a)
ADD 2 : l(c(0))+l(2)+l(c(2))=l(a)+2+l(a)=2l(a)+2
STORE 4 : l(c(0))+l(4)=l(2a)+3

LOAD 1 : l(1)+l(c(1))=1=1=2
ADD =1 : l(c(0))+l(1)=l(1)+l(1)=2
STORE 1 : l(c(0)+l(1)=l(2)+1=3+1=4

JUMP while : 1

endwhile: WRITE 4 : l(4)+l(c(4))=3+l(2a)
HALT : 1$$1+2+l(a)+2+l(b)+2+log(3)+1+l(b)+l(b)+2+l(a)+l(a)+3+log(3)+l(b)+l(b)+2+log(b-1)+1+WHILE +3 l(2a)+1=20+3l(a)+l(2a)+5l(b)+2log(3)+log(b-1)+WHILE=20+3log(a)+3+log(2a)+1+5log(b)+5+2log(3)+log(b-1)+WHILE$$

Where $$WHILE=(b-1)(log(3)+l(b)+l(b)+2+log(b-1)+1+3+l(a)+2l(a)+2+l(2a)+3+2+2+4+1)=(b-1)(20+2l(b)+3l(a)+l(2a)+log(3)+log(b-1))=(b-1)(20+2log(b)+2+3log(a)+3+log(2a)+1+log(3)+log(b-1))$$

Is this correct?? (Wondering)
 
  • #10
mathmari said:
Is the RAM program the following??

It should be something like that, but the exact form is not all that important.
What is important, is that you have a while-loop that will iterate $n$ times in a worst case scenario.
When determining the order of complexity, the stuff that is outside the while-loop is not relevant. (Nerd)
So is the the time complexity under the logarithmic cost as followed??

That might be correct I guess, but I think it is too detailed. (Worried)

The important thing to note is that the while-loop is more "complex" than the rest of the RAM sub program.
The complexity of this MULT operation is $O(T(\text{whileloop}) \cdot n)$

Since we were assuming the real RAM program had complexity $T(n)$, that means that if we replace a MULT, that is a bottleneck, by the sub program, the resulting complexity is $O(T(n) \cdot T(\text{whileloop}) \cdot n)$. (Wasntme)
 

Similar threads

  • · Replies 15 ·
Replies
15
Views
3K
Replies
1
Views
2K
  • · Replies 28 ·
Replies
28
Views
6K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 36 ·
2
Replies
36
Views
6K
  • · Replies 4 ·
Replies
4
Views
2K