Trouble understanding what to do with output of shunting-yard algorithm - algorithm

I've been looking at the wiki page: http://en.wikipedia.org/wiki/Shunting-yard_algorithm
I've used the code example to build the first part, basically I can currently turn :
3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3 into 3 4 2 * 1 5 − 2 3 ^ ^ / +
But I don't know how to then use 3 4 2 * 1 5 − 2 3 ^ ^ / + to obtain 3.00012207
And the example code and explanation on wiki aren't making any sense to me.
Could someone please explain how to evaluate 3 4 2 * 1 5 − 2 3 ^ ^ / + and produce the answer. Thanks in advance. I don't need a code example just a good explanation or a breakdown of an example.
Not that it matters but I am working .net C#.

The purpose of the shunting yard algorithm is that its output is in Reverse Polish Notation, which is straightforward to evaluate:
create a stack to hold values
while there is reverse polish notation input left:
read an item of input
if it is a value, push it onto the stack
otherwise, it is an operation; pop values from the stack, perform the operation on those values, push the result back
when there's no input left, if the expression was well formed, there should be exactly one value on the stack; this is the evaluated result.

The post-fix notation is how you do the math in, say, a HP calculator.
Keep a stack, whenever you get a number add it to the top. Whenever you get an operator consume inputs from the top and then add the result to the top
token stack
*empty*
3 3 //push numbers...
4 3 4
2 3 4 2
* 3 8 //remove 4 and 2, add 4*2=8
1 3 8 1
5 3 8 1 5
- 3 8 -4
2 3 8 -4 2
3 3 8 -4 2 3
^ 3 8 -4 8
... ...

Process the elements 3 4 2 * 1 5 − 2 3 ^ ^ / + left-to-right as follows:
Initialize a stack to hold numbers.
If the element is a number, push it onto the stack.
if the element is an operator, remove the top two elements from the stack, apply the operator to those two elements, and push the result onto the stack.
When you get to the end, the stack should have a single element which will be the result.

I see I am a bit late to the party.
I saw the question and went off on a tangent writing a couple of tasks for Rosetta Code. It just so happens that this task might be what you are after. It gives an annottated table of what happens when calculating the value of an RPN expression, token by token.
Here is a sample of its output:
For RPN expression: '3 4 2 * 1 5 - 2 3 ^ ^ / +'
TOKEN ACTION STACK
3 Push num onto top of stack 3
4 Push num onto top of stack 3 4
2 Push num onto top of stack 3 4 2
* Apply op to top of stack 3 8
1 Push num onto top of stack 3 8 1
5 Push num onto top of stack 3 8 1 5
- Apply op to top of stack 3 8 -4
2 Push num onto top of stack 3 8 -4 2
3 Push num onto top of stack 3 8 -4 2 3
^ Apply op to top of stack 3 8 -4 8
^ Apply op to top of stack 3 8 65536
/ Apply op to top of stack 3 0.0001220703125
+ Apply op to top of stack 3.0001220703125
The final output value is: '3.0001220703125'

Related

Modified algorithm for building a Heap

I am quite new to programming and I am trying to understand a certain problem regarding heap sort. In a book I'm reading, there is a modified algorithm for building a max heap, which is:
BuildHeap(A)
A.heap-size = 1
for i = 2 to A.length
Heap-Insert(A, A[i])
So from my understanding, this algorithm takes in an array and defines the size of the heap to be 1 and then iterates from 2 to the total length of the array and then inserts the value into the heap.
But how would this build a max heap? If I had an array of [4, 7, 2, 3, 9, 1], then wouldn't the algorithm start at value 2 and then simply add all the values from the A[2] to A.length to the heap without actually building a max heap?
I do not understand how the heap-size = 1 does anything in the algorithm other than restrict the total size of the heap. I am confused as to how you would build a max heap.
From what it states in the book, the normal max heap works by first inserting every array value into a heap, and then starting at the A/2 place, then working backwards and swapping values that are larger than the current value being assessed by calling Max-Heapify.
So how would this max heap work since there is no Max-Heapify(A, largest) call, but instead there is simply a heap-insert(A, A[i])?
First of all, this question is not about heap sort, which is just one of the applications for a heap. You are asking about the heap construction.
The pseudo code you presented is indeed an alternative (and less efficient) way of building a heap, and this would actually be the algorithm that many would come up with when they wouldn't have known about the standard algorithm of Floyd.
So taking a look at the code:
BuildHeap(A)
A.heap-size = 1
for i = 2 to A.length
Heap-Insert(A, A[i])
Most of the logic of this algorithm is berried inside the Heap-Insert function, which is not just a simple "append" to an array: it does much more than that. Wikipedia describes that hidden algorithm as follows:
Add the element to the bottom level of the heap at the leftmost open space.
Compare the added element with its parent; if they are in the correct order, stop.
If not, swap the element with its parent and return to the previous step.
You write in your question:
there is no Max-Heapify(A, largest)
Indeed, it would be too simple if you already knew what the largest value was before using the heap. You need to first insert a value (any value) in a heap, and let the heap do its magic (inside Heap-Insert) to make sure that the largest value ends up in the first (top) position in the array A, i.e. in A[1].
The first step of the quoted algorithm is thus important: Heap-Insert expects the new value to be inserted at the end.
Let's work through the example [4, 7, 2, 3, 9, 1], and let's put a pipe symbol to indicate the end of the heap. At the start, the heap size is 1, so we have:
4 | 7 2 3 9 1
Let's also represent a more visually appealing binary tree at the right side -- it just has a root element:
4 | 7 2 3 9 1 4
Then we call Heap-Insert(A, A[2]), which is Heap-Insert(A, 7). The implementation of Heap-Insert will increase the size of the heap, and put that value in the last slot, so we get:
4 7 | 2 3 9 1 4
/
7
Heap-Insert has not finished yet -- this was just the first step it performs. Now it "bubbles up" that 7 following steps 2 and 3 of that quoted algorithm, and so we get:
7 4 | 2 3 9 1 7
/
4
At the second iteration of the pseudo code loop, we call Heap-Insert(A, 2), so Heap-Insert performs its first step:
7 4 2 | 3 9 1 7
/ \
4 2
...and finds out that nothing needs to change when performing step 2 and 3.
We continue inserting 3:
7 4 2 3 | 9 1 7
/ \
4 2
/
3
...and again nothing needs to change as 3 is less than 4 (remember that A[2] is the parent of A[4].
We continue inserting 9:
7 4 2 3 9 | 1 7
/ \
4 2
/ \
3 9
And here 9 > 4, and also 9 > 7, so Heap-Insert will further modify A to this:
9 7 2 3 4 | 1 9
/ \
7 2
/ \
3 4
One more to go:
9 7 2 3 4 1 9
/ \
7 2
/ \ /
3 4 1
And Heap-Insert has nothing more to do as 1 < 2.

Deleting element and getting it's neighbours

I have got a sequence 1 2 3 4 5 6 ... n. Now, I am given a sequence of n deletions - each deletion is a number which I want to delete. I need to respond to each deletion with two numbers - of a left and right neighbour of deleted number (-1 if any doesn't exists).
E.g. I delete 2 - I respond 1 3, then I delete 3 I respond 1 4 , I delete 6 I respond 5 -1 etc.
I want to do it fast - linear of linear-logarithmic time complexity.
What data structure should I use? I guess the key to the solution is the fact that the sequence is sorted.
A doubly-linked list will do fine.
We will store the links in two arrays, prev and next, to allow O(1) access for deletions.
First, for every element and two sentinels at the ends, link it to the previous and next integers:
init ():
for cur := 0, 1, 2, ..., n, n+1:
prev[cur] := cur-1
next[cur] := cur+1
When you delete an element cur, update the links in O(1) like this:
remove (cur):
print (num (prev[cur]), " ", num (next[cur]), newline)
prev[next[cur]] := prev[cur]
next[prev[cur]] := next[cur]
Here, the num wrapper is inserted to print -1 for the sentinels:
num (cur):
if (cur == 0) or (cur == n+1):
return -1
else:
return cur
Here's how it works:
prev next
n = 6 prev/ print 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
/next ------------------- -------------------
init () -1 0 1 2 3 4 5 6 1 2 3 4 5 6 7 8
remove (2) 1 3 1 3 -1 0 1 3 4 5 6 1 3 4 5 6 7 8
remove (3) 1 4 1 4 -1 0 1 4 5 6 1 4 5 6 7 8
remove (6) 5 7 5 -1 -1 0 1 4 5 1 4 5 7 8
remove (1) 0 4 -1 4 -1 0 4 5 4 5 7 8
remove (5) 4 7 4 -1 -1 0 4 4 7 8
remove (4) 0 7 -1 -1 -1 0 7 8
Above, the portions not used anymore are blanked out for clarity.
The respective elements of the arrays still store the values printed above them, but we no longer access them.
As Jim Mischel rightly noted (thanks!), storing the list in two arrays instead of dynamically allocating the storage is crucial to make this O(1) per deletion.
You can use a binary search tree. Deleting from it is logarithmic. If you want to remove n elements and the number of total elements is m, then the complexity of removing n elements from it will be
nlogm

I know how Merge Sort works, but How Merge Sort Code Works?

You can read this on Wikipedia:
function merge_sort(list m)
// Base case. A list of zero or one elements is sorted, by definition.
if length(m) <= 1
return m
// Recursive case. First, *divide* the list into equal-sized sublists.
var list left, right
var integer middle = length(m) / 2
for each x in m before middle
add x to left
for each x in m after or equal middle
add x to right
// Recursively sort both sublists
left = merge_sort(left)
right = merge_sort(right)
// Then merge the now-sorted sublists.
return merge(left, right)
On line 1 there's a list of numbers, let's say 9 6 3 7 5 1 8 2
They say that merge_sort divides the list on 2 and 2 again and again until each list has only 1 integer left, like this one:
9 6 3 7 5 1 8 2 -->
9 6 3 7 - 5 1 8 2 -->
9 6 - 3 7 - 5 1 - 8 2 -->
9 - 6 - 3 - 7 - 5 - 1 - 8 - 2
And then the numbers are put together like this:
6 9 - 3 7 - 1 5 - 2 8 -->
3 6 7 9 - 1 2 5 8 -->
1 2 3 5 6 7 8 9 -->
But I don't see where in the code the list of integers are divided on 2 again and again until each has only 1 integer left?
var list left, right
var integer middle = length(m) / 2
for each x in m before middle
add x to left
for each x in m after or equal middle
add x to right
As I understand, on the code above, the list of numbers is divided to two different lists:
9 6 3 7 and 5 1 8 2
What then happens on the code below?
left = merge_sort(left)
right = merge_sort(right)
Can someone explain me how the merge_sort code above exactly works step by step?
But I don't see where in the code the list of integers are divided on 2 again and again until each has only 1 integer left?
var list left, right
var integer middle = length(m) / 2 --------statement-1
for each x in m before middle --------statement-2
add x to left
for each x in m after or equal middle --------statement-3
add x to right
At the statement-1 you divide the array into two parts and add them to the left and right sub-array. In the statement-2, you are adding all the element before middle, which is your middle element of the array. Similarly statement-3, you are adding rest of the element in right sub-array. So essentially, you keep on dividing the array in two parts until their size is 1 or 0.
if length(m) <= 1
return m
In the start you have above conditional check, which return the method call if the size of the array is less then or equal to one.
What then happens on the code below?
left = merge_sort(left)
right = merge_sort(right)
This is a recursive call to sort (divide the array until size is one) the each sub array. Which is created in the above pseudo-code. You sort left and right sub-array separately and then join them into a single array.
return merge(left, right)
Here both left and right sub-array are passed to a merge function. These both array are sorted array. The task of the merge function is merge these sub-array into a single sorted array.
The pseudo code is missing some details. There was debate on the talk page about removing it or fixing it. Note it's supposed to be working with a list, not an array, which is why elements can only be appended one at a time. The list is not really split into 2 parts; instead two new initially empty lists left and right are created, then (middle = length/2) elements are moved from list to left, then (length - middle) elements are moved from list to right. This cleaned up example with C++ comments may make more sense, but it's still an inefficient way to sort a list. A bottom up merge sort using an array of pointers is much more efficient. I can add example code here if anyone is interested.
var list left, right
var integer middle = length(m) / 2
var integer count
for (count = 0; count < middle; count += 1)
get x from front of list // x = *list.front()
remove first element from list // list.pop_front()
add x to left // left.push_back(x)
for (count = middle; count < length; count += 1)
get x from front of list // x = *list.front()
remove first element from list // list.pop_front()
add x to right // right.push_back(x)
In that same wiki article, there are two C / C++ like code examples, which should be easier to understand. The examples are simplified and copy data back to the original array after each merge step, which could be avoided with more optimized code.
http://en.wikipedia.org/wiki/Merge_sort#Top-down_implementation
http://en.wikipedia.org/wiki/Merge_sort#Bottom-up_implementation
The sequence is different for top down merge sort, it's depth first, left first:
9 6 3 7 5 1 8 2
9 6 3 7|5 1 8 2
9 6|3 7
9|6
6 9
3|7
3 7
3 6 7 9
5 1|8 2
5|1
1 5
8|2
2 8
1 2 5 8
1 2 3 5 6 7 8 9
Bottom up merge sort skips the recursion and just starts off assuming a run size of 1, and merges width first, left to right:
9 6 3 7 5 1 8 2
9|6|3|7|5|1|8|2 run size = 1
6 9|3 7|1 5|2 8 run size = 2
3 6 7 9|1 2 5 8 run size = 4
1 2 3 5 6 7 8 9 done
Another example of bottom up merge sort algorithm:
http://www.mathcs.emory.edu/~cheung/Courses/171/Syllabus/7-Sort/merge-sort5.html

Sum stack without last element

is there a possibility to sum up the stack without the last element? So for example if the stack has the following elements:
1 2 3 4 5
After the operation the stack should look like this:
1 14
USE: generalizations
1 2 3 4 5 datastack length 1 - nsum

Post Order Traversal Formula

I am given 2 y 5 1 4 3 - * - * +, and am asked to evaluate it, and then draw the equivalent expression tree. I haven't done any work with this before, can someone show the steps you would take to solve this type of question?
I have looked at: Post order traversal of a formula
and am confused as to how to come to that answer.
What you are given is a postfix expression. It is well-known that these things are evaluated with stacks according to the following rule:
Working left to right, when you encounter a value, push it. When you encounter an operator, pop the top two values, apply the operation, and push the result back.
So your expression evaluation proceeds like this
2 (push 2)
2 y (push y)
2 y 5 (push 5)
2 y 5 1 (push 1)
2 y 5 1 4 (push 4)
2 y 5 1 4 3 (push 3)
2 y 5 1 1 (pop 3, pop 4, push 4-3)
2 y 5 1 (pop 1, pop 1, push 1*1)
2 y 4 (pop 1, pop 5, push 5-1)
2 4y (pop 4, pop y, push y*4)
2+4y (pop 4y, pop 2, push 2+4y)
Your answer is the value left on the stack.
Now, you asked about producing a tree also. To produce a tree, rather than evaluating the expression when you find an operator, you "apply" the operator by building a tree fragment with the operator as the root, and the popped tree fragments as children.
So after pushing
2 y 5 1 4 3
you see a -, so you pop the 4 and 3 and you push back this structure
-
/ \
4 3
Next you see the * so you pop the top tree fragment and the one below it, which is actually a tree fragment consisting of the single node
1
So it will look like
*
/ \
1 -
/ \
4 3
You should be able to take it from here.
The answer at Post order traversal of a formula says find the first operator. In your case it is '-'. The second step he describes is put it between the previous two operands.
In your case these two operands are 4 and 3 (they are directly before the '-'). So the formula after this step becomes:
2 y 5 1 (4-3) * - * +
Remember that the expression (4-3) is now one operand.
We apply the steps again to this formula. We see that the first operator now is ''.
The two operands before the '' are 1 and (4-3). The formula becomes:
2 y 5 (1*(4-3)) - * +
Now you can apply this untill all operators are gone.
I will not continue giving more steps because probably this is a homework question. However I think it is clear?
As stated by novalis from the question you linked, scan for the first operator and previous 2 operands and then replace that group with a more familiar expression in parentheses, ie.
if you have:
op1 op2 operator
4 3 -
this becomes:
(op1 operator op2)
(4 - 3 )
so, proceeding...
2 y 5 1 4 3 - * - * +
2 y 5 1 (4 - 3) * - * +
2 y 5 (1 * (4 - 3)) - * +
Proceed in a similar fashion to build the tree. Scan for the first operator and create a tiny tree:
-
/ \
4 3
Then, each new operand is the top node of your new tree:
*
/ \
1 -
/ \
4 3
and then:
-
/ \
5 *
/ \
1 -
/ \
4 3

Resources