Confusion between higher address and lower address - stack

I am very new to Assembly language. I was reading about MIPS architecture and I am stuck with a concept.
In the image above,shouldn't the highest address be the last item of the stack and the lowest address at the top? As the address are generated in ascending order.
Thanks in advance.

The top of the stack is the position of the last element that was pushed on. Looking at that picture, the 'top' of the stack is towards the bottom, which has a lower address. The addresses are generated in decending, not ascending order.
It is called the top as a stack is a LIFO (Last In First Out) structure - the last object added is the first one removed, and is therefore 'on top'.

Related

LinkedList Stack Structure Big O

The answer for the following question is O(n) for pop and O(1) for push. But i don't quite understand why pop cannot be O(1). We do have tail pointer pointing to the end of the linkedlist and we are supposed to access it in O(1) time right? Did i miss anything here?
What is the running time of the push and pop operations if the bottom of the Stack must be at the head of the linked memory structure? where n is the number of nodes in the structure.
Think about the invariants of your stack: tail points to the last element.
The pop operation removes the last element — this means we need to re-adjust tail. How do we do this? With a doubly-linked list we could just follow the pointer back to the previous node — but as your illustration clearly shows, there is no such arrow back to the previous node in a singly-linked list.
So instead we need to start at head (the only other node for which we hold a pointer), and iterate all the way until we arrive at the second-to-last node, and then set tail to point to that node.

What is the space complexity of moving one stack/queue to another stack/queue?

I'm trying to understand the space complexity of moving elements from one stack to another stack.
I found this article on leetcode but there are some discrepancies.
Let's say if we move stack1 (1-2-3) to another stack2 by doing pop() and push() three times, do we consider O(1) extra space since we delete one element from stack1 and create an element in stack2 thus no extra space is used? Or we consider it as O(n) space complexity due to we created a stack2 same size as stack1(but stack1 is gone..)?
Thanks in advance!
That depends on how your stack is implemented. That is, what is the backing store?
If the stack is implemented as an array, then the stack will be initialized with some default capacity, and a current count of 0. Something like:
s = int[10];
count = 0;
When you push something onto the stack, the item is added and the count is incremented to 1. If you try to push more items than the array will hold, then the array is extended. (Actually, probably a new array is allocated and the existing contents copied to it.)
When you pop something, count is reduced by 1, and s[count] is returned. But the array allocation is not changed.
So if the stack is implemented as an array, then copying from one stack to another will require O(n) extra space. At least temporarily.
If the stack is implemented as a linked list:
head > node > node > node ...
Then typically pop just returns head, and the new head is the node that head previously pointed to. In this case, the memory occupied by the stack is reduced with each pop. Of course, adding to the new stack increases the memory allocation by the same amount. So if the stack is implemented as a linked list, then copying from one stack to another is O(1) extra space.

Finding the shortest path between 2 points namely S and G?

Finding the shortest path between 2 points namely S and G? It SHOULD pass through points named #. The allowed pathway is denoted as . and the blocked pathway is denoted as #. The upper cap of the number of points inbetween is 19.
Example:
input
########
##....G#
##.#####
#..#..S#
##.....#
########
"It should pass through points named #" - If you can elaborate more on this. OR for the example above, what is the expected solution.
Also statement says "blocked pathway is denoted as X" and in example, we see "#". I believe you meant # as X.
I assume direction of movement would be left, right, up and down.
This can be solved by breadth first search (BFS) on the grid.
Start from S and explore all paths level by level based on given constraint where you can reach and where you can't reach.
We can take two arrays or lists say currentLevel and nextLevel. And a variable, say levelCount.
Store position S in currentLevel.
Loop through positions in currentLevel one by one. If it is G, levelCount is the shortest path.
Else, for all safe positions (left, right, up and down) where we can reach and store them in
nextLevel. "safe position" means that it should not be X, should not go beyond the given
grid extent (i.e. position index should not less than zero and not greater than length)
or other constraint given in problem.
Set currentLevel to nextLevel, increment levelCount and clear nextLevel. Go to step 1.

Questions from the second chapter of the dragon book

On page 29 it says "The leaves of a parse tree read from left to right form the yield of the tree, which is the string generated or derived from the nonterminal at the root of the parse tree. In Fig. 2.2 the generated string is 9-5*2. In that figure, all the leaves are shown at the bottom level. Henceforth, we shall not necessarily line up the leaves in this way." why not?
It also says "Any tree imparts a natural left-to-right order to its leaves, based on the idea that if a and b are two children with the same parent, and a is to the left of b, then all descendants of a are to the left of descendants of b." what does it mean?
P.S It's the second edition of the book
So first of all, for anyone else wanting to comment on this, the page numbers above refer to the first edition. In the second edition, the page number is 46, and the diagram referred to is figure 2.5.
EDIT: The author, when referring to extending the leaves down to the bottom, is talking about moving all leaves of the tree to be aligned vertically with each other, whether or not they are at the same level in the tree. Figure 2.2 has them extended to the bottom, such that every leaf is at the bottom of the diagram, aligned together vertically from left to right. If you look at some of the other diagrams later in the book, this is not done, and leaves are shown vertically aligned with other nodes at the same level, whether or not those other nodes are leaves. This latter way is the normal way of drawing trees, and is the most space efficient.
As for your first question, I believe the reason they do not do that is to save room. If you look at the right hand side of figure 2.4, if the author was to extend the leaves down to the bottom, then the subtree with letter as its root would have to be moved to the right, taking up more room than what is actually needed. While this is a minimal case and doesn't make a huge difference, one could imagine a larger tree (which I'm sure is in the book, although I didn't go looking) which would need more room.
For the second question, it is essentially saying that if you had a*b + c*d, and you considered the multiplications as siblings (as they would be to keep order of operations valid), then the leaves a and b would be to the left of c and d in the tree, just as they are to the left of c and d in the equation. Essentially its just saying what it already said in the first part, which is that the tree's leaves should be able to be read left to right in order to reproduce the original syntax exactly, not switching the ordering of any portions (i.e. if the tree read left to right c*d + a*b, that might still be valid, but would not be a tree we are considering).

Line spacing problem with \LaTeX{}

When I use \LaTeX{} to show the LaTeX logo, the following line of text is pushed down in order to accommodate the subscript 'E' in LaTeX.
How do I override this? Is there an option I can pass to \LaTeX{} ?
EDIT: Problem was due to horizontal space, not vertical space, and was caused by normal text as well as the \LaTeX{} command.
Short answer: Put \vspace{-0.1cm} after \LaTeX{} (adjust the size of the space to get the effect you want)
Long answer: I am not getting this extra spacing you are referring to, but in any case I can show you an example so you see how you can reduce the space between lines. The example of text I am using (I just got the first .tex document I had, so ignore what is written):
The way the picture is drawn, it
implies that the bank doesn't
originate loans anymore. Maybe bank
are still originators? \LaTeX{} The
difference now is that they sell the
loans to GSEs and other securitisers
and get securities back? Do they also
securitise loans?
This is what I get:
Now using \vspace{-0.1cm}:
The way the picture is drawn, it
implies that the bank doesn't
originate loans anymore. Maybe bank
are still originators? \LaTeX{} \vspace{-0.1cm} The
difference now is that they sell the
loans to GSEs and other securitisers
and get securities back? Do they also
securitise loans?
This is what I get:

Resources