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

In summary, the conversation discusses how 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))$ that does not contain MULT or DIV instructions. The experts suggest using a sub program to replace the MULT instruction and provide pseudocode and a RAM program to demonstrate this. They also discuss the time complexity of the sub program and how it affects the overall complexity of the program. Ultimately, it is concluded that the complexity of the while-loop is the most important factor in determining the overall time complexity.
  • #1
mathmari
Gold Member
MHB
5,049
7
Hey! :eek:

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
  • #2
mathmari said:
Hey! :eek:

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)
 
  • #3
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)
 
  • #4
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)
 
  • #5
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)
 
  • #6
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)
 
  • #7
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)
 
  • #8
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)
 
  • #9
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)
 

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

1. What is the Equivalent RAM Program?

The Equivalent RAM Program is an algorithm that models the behavior of a random access machine (RAM) and provides a way to analyze the complexity of other algorithms by comparing it to the complexity of the RAM program.

2. What does "hints to show complexity O(T^2(n))" mean?

This phrase refers to the fact that the Equivalent RAM Program can provide hints or insights into the complexity of a given algorithm, and can show that it has a complexity of O(T^2(n)), where T represents the number of steps and n represents the input size.

3. How does the Equivalent RAM Program help determine complexity?

The Equivalent RAM Program helps determine complexity by simulating the behavior of a RAM and counting the number of steps it takes to complete a given algorithm. By comparing this to the input size, the complexity of the algorithm can be determined.

4. Why is it important to analyze the complexity of algorithms?

Analyzing the complexity of algorithms is important because it allows for the evaluation of the efficiency and scalability of an algorithm. This can help in making decisions about which algorithm to use for a given problem and can also aid in improving the performance of existing algorithms.

5. Are there limitations to using the Equivalent RAM Program to analyze complexity?

While the Equivalent RAM Program can provide valuable insights into the complexity of an algorithm, it is not a perfect tool and has some limitations. For example, it does not take into account external factors such as hardware limitations or input data distribution, which can impact the actual performance of an algorithm in real-world scenarios.

Similar threads

  • Programming and Computer Science
Replies
15
Views
3K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
28
Views
5K
  • Programming and Computer Science
Replies
15
Views
3K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
2
Replies
36
Views
6K
Back
Top