Convention of a += operator in pseudo-code

I have to write some algorithm in pseudocode for a paper. For this I have the "$ \leftarrow$ " to assign a value.

$ x \leftarrow y^2$

now I want to write a method which adds to the variable. In normal Python code I would write x += 1 for this. Is there a style convention how to layout this in pseudo-code in a paper? I dont really like the redundance of $ x \leftarrow x+1$ .

What is the runtime of this pseudocode?

I need help figuring out the runtime of the following pseudocode. I believe it is O(|E| + |V|), but I’m not totally sure…

graphFunction(graph):      q = Queue()      for vertex in graph:          q.enqueue(vertex)      while q is not empty():          v = q.dequeue()          for  every outgoing edge of v:              edge.weight++          for every incoming edge of v:              edge.weight++ 

How many times in this pseudocode is the function F called?

For this question, I thought function F called twice but it called three times. Are those three functions were called? F(N), F(K) and f(N-1)?

How many times in this pseudocode is the function F called?

Main     Declare K as Integer     K = 3     Set Result = F(K)     Write Result End Program  Function F(N) as Integer         If N == 1 Then Set F = 1     Else         Set F = N * F(N - 1)         Set N = N - 1     End If End Function 

Pseudocode for sum, product and difference given implicit functions A and B

I have no idea how to do this. All I know is that I have two implicit functions, and I need to write the sum, product and difference of them in pseudocode. I tried to write it for sum and difference but I don’t really know what I’m doing. Any help is appreciated.

click here for functions

DECLARE A: ARRAY[4,10] of INT DECLARE B: ARRAY[1,6] of INT READ A READ B SUM = A+B DIFF = A-B WRITE SUM, DIFF END“`

What is the run time of this algorithm, written in pseudocode?

count = 0 for i = 1 to n:     for j = 1 to i:         count += 1 

So from my understanding, we can break this up into 2 summations, by nesting the $ j$ loop as a summation into the $ i$ loop as a summation as follows:

$ \sum\limits_{i=1}^{n} \sum\limits_{j=1}^{i}$ 1

Since incrementing count is 1 operation O(1).

Then, we can manipulate the above summation to:

= $ \sum\limits_{i=1}^{n} (i – 1 + 1)$ using property of summations

= $ \sum\limits_{i=1}^{n} i$

= $ \frac{n(n+1)}{2}$ = $ O(n^2)$

Is this the correct approach?

Please check my answer to a pseudocode CASE statement question

This is a pseudocode question in my IGCSE CompSci textbook:

Use a CASE statement to display the day of the week if the variable DAY has the value 1 to 7 and an error otherwise.

This is my answer to it:

CASE Day OF     1 : OUTPUT "Monday"     2 : OUTPUT "Tuesday"     3 : OUTPUT "Wednesday"     4 : OUTPUT "Thursday"     5 : OUTPUT "Friday"     6 : OUTPUT "Saturday"     7 : OUTPUT "Sunday"     OTHERWISE OUTPUT "Day invalid" ENDCASE 

Is this answer correct?

(I realise this is a very rudimentary question for a Year 10/11 CompSci class but I’m homeschooled with no teaching guidance whatsoever. So a big cheers to anyone who would take the time to check my answer for me.)

How to find time complexity of this pseudocode

Recently, I came across a question about finding sum of all values in range $ [low, high]$ in BST $ T$ .

Then I formulated following algorithm to carry out that task:

  1. We do inorder traversal of BST ($ sum = 0$ in start)
  2. But whenever we encounter any node having value strictly less than $ low$ than we don’t visit it’s left subtree. We only traverse it’s right subtree recursively in same manner.
  3. In same manner if we find any node having value strictly greater than $ high$ than we don’t traverse it’s right subtree.
  4. If value of node falls in given range then we increment $ sum$ by 1.

I think this algorithm correctly solve above problem in time complexity $ O(m+lg(n))$ where $ m$ is number of nodes having value in given range.

My reasoning for that time complexity is that we traverse at most $ O(lg(n))$ nodes(This statement I’m not able to prove) which don’t get added and we traverse $ m$ nodes that gets added into $ sum$ . Which gives above time complexity.

At least any hint regarding how to prove this time complexity would be great.

Thanks.

Pseudocode supposedly has an error in it

Recently finished an exam where I had to spot multiple errors, I can’t seem to find 1 from the pseudocode:

Count = 1 Input Number High = Number Low = Number Repeat   Input Number    If Number > High     High = Number   End If   If Number < Low     Low = Number   End If   Count = Count + 1 Until Count > 99 Print High Print Low 

The goal of the program is to find the highest and lowest numbers of 100 inputs, the errors aren’t syntax or efficiency related.

Thanks in advance

Introduction To Algorithms 3rd Edition MIT Press: Red Black Tree insertion error in pseudo-code?

I’m implementing the algorithm on page 316 of the book Introduction to Algorithms. When I look at the pseudo-code, I feel that there is something wrong between line 10 to 14. I feel it’s missing a check. There is a YouTube video explaining this whole function (and it includes the pseudo-code plus line numbers): https://youtu.be/5IBxA-bZZH8?t=323

The thing is, I think that //case 2 needs its own check. The else if z == z.p.right is both meant for //case 2 and //case 3. However, the code from //case 2 shouldn’t always fire. It should only fire when there is a triangle formation according to the YouTube video. In my implementation it always fires, even when it’s a line.

So I feel the pseudo-code is wrong, it’s also weird that it has an indentation, but I see no extra check.

Am I missing something?

Maybe superfluous, but I also typed the pseudo code given from the book here:

RB-INSERT-FIXUP(T, z)   while  z.p.color == RED     if  z.p == z.p.p.left       y = z.p.p.right        if y.color == RED         z.p.color = BLACK   // case 1         y.color = BLACK     // case 1         z.p.p.color = RED   // case 1         z = z.p.p           // case 1        else if z == z.p.right           z = z.p           // case 2           LEFT-ROTATE(T, z) // case 2         z.p.color = BLACK   // case 3         z.p.p.color = RED   // case 3         RIGHT-ROTATE(T, z.p.p) // case 3      else (same as then clause with "right and "left" exchanged)    T.root.color = BLACK 

Given the following pseudo-code, please show what the outputs will be

Given the following pseudo-code, please show what the outputs will be at LINE A, B, C and D. please put down “inconclusive” if you think the CPU process scheduling could potentially result in multiple values for integer i.

int i = 0; int main(){

if  (fork() == 0) {     i++;     if (fork() == 0){         if (fork() == 0){             i++;             print value of i;  /**/         } else {             wait for this process’s child processes to stop;             i++;             print value of i;  /*Line B*/                    }     } else {         print value of i;  /* LINE C*/         wait for this process’s child processes to stop;        } } else {     wait for this process’s child processes to stop;     print value of i;   /* LINE D*/  } 

}