Please explain how does postfix expression work on this equation? - expression

What postfix expression does the following infix expression translate to?
2 + 3 - 4 / 3 * 3 + 4
I'm really confused on how does it translate to 2 3 4 3 / 3 * - + 4 +?.
Can somebody explain it, please?
Thanks in advance

Put parens around the infix to show order of operations honoring precedence and associativity:
((2 + 3) - ((4 / 3) * 3)) + 4
Now you can use this to draw a syntax tree:
+
_________/ \_
| |
- 4
_/ \____
| |
+ *
/ \ _/ \_
2 3 | |
'/' 3
/ \
4 3
Now you can get postfix by traversing the tree in post order:
2 3 + 4 3 / 3 * - 4 +
There are other post orders that give the right answer. For example, you can get more by choosing to evaluate either the left or the right subtree first for each commutative operator. Equivalently, you can reverse the left and right hand subtrees for each commutative operator and always use a standard, left child first post order search.
You can check the order by executing it with a stack machine:
Stack
read 2, push 2 [2
read 3, push 3 [2 3
read +, pop 3, pop 2, push 5 (i.e. 2 + 3) [5
read 4, push 4 [5 4
read 3, push 3 [5 4 3
read /, pop 3, pop 4, push 1.33... (i.e. 4/3) [5 1.33...
read 3, push 3 [5 1.33... 3
read *, pop 3, pop 1.33..., push 4 (i.e. 1.33... * 3)[5 4
read -, pop 4, pop 5, push 1 (i.e. 5 - 4) [1
read 4, push 4 [1 4
read +, pop 4, pop 1, push 5 (i.e. 1 + 4) [5
So 5 is the answer, which agrees with the infix evaluation.

Related

Stack And Arithmetic Evaluation

A stack is said to be the ideal data structure for Arithmetic Evaluation. Why is it so?
Why do we even need a data structure for Arithmetic Evaluation? I've been studying about this for some time now and still confused. I don't understand what is the use of Prefix and Postfix expressions because the Infix expression is quite readable.
Answer for the part of why postfix/prefix over infix is quite well explained here.As a summary infix is readable but not easily parsed
As for why stack is used here is:
1: push,pop in O(1) time are quite useful for evaluation.
2: push: add the operand on stack.
3: pop: remove the operand and evaluate expression(binary)
4: final result is the only one left on stack after parsing
The infix expression is readable, yes. But if you want to write an algorithm that can evaluate an arithmetic expression, how would you do?
Take the following expression:
3 + 4 * 5 + 2 ^ 3 * 12 + 6
How does your algorithm proceed from there?
A simple and naive way is to look for the highest-precedence operation, evaluate it, then rewrite the string, and keep doing that until all operations have been performed. You'd get this result:
3 + 4 * 5 + 2 ^ 3 * 12 + 6
3 + 4 * 5 + 8 * 12 + 6
3 + 20 + 96 + 6
23 + 102
125
That is one way to do it. But not a particularly efficient way. Looking through the string for the highest-precedence operation takes time linear in the length of the string, and you have to do that once per operation, and rewrite the string every time. You end up with something like a quadratic complexity. There might be a few tricks to be slightly more efficient, but it's not going to be as efficient as other existing methods.
Another possible method is to put the expression into a tree, called a "syntax tree" or "abstract syntax tree". We get this:
+
/ / \ \
3 * * 6
/ \ / \
4 5 ^ 12
/ \
2 3
This tree is easier to evaluate for an algorithm, compared to the expression we had before: it is a linked structure, in which you can easily replace one branch by the value of that branch without having to rewrite everything else in the tree. So you replace 2^3 with 8 in the tree, then 8 * 12 with 96, etc.
Postfix (or prefix) notation is harder to read for humans, but much easier to manipulate for an algorithm. My previous example becomes this in postfix:
3 4 5 * + 2 3 ^ 12 * + 6 +
This can be evaluated easily reading it from left to right; every time you encounter a number, push it onto a stack; every time you encounter an operator, pop the two numbers on top of the stack, perform the operation, and push the result.
Assuming the postfix expression was correct, there should be a single number in the stack at the end of the evaluation.
EXPR | [3] 4 5 * + 2 3 ^ 12 * + 6 +
STACK | 3
EXPR | 3 [4] 5 * + 2 3 ^ 12 * + 6 +
STACK | 3 4
EXPR | 3 4 [5] * + 2 3 ^ 12 * + 6 +
STACK | 3 4 5
EXPR | 3 4 5 [*] + 2 3 ^ 12 * + 6 +
STACK | 3 20
EXPR | 3 4 5 * [+] 2 3 ^ 12 * + 6 +
STACK | 23
EXPR | 3 4 5 * + [2] 3 ^ 12 * + 6 +
STACK | 23 2
EXPR | 3 4 5 * + 2 [3] ^ 12 * + 6 +
STACK | 23 2 3
EXPR | 3 4 5 * + 2 3 [^] 12 * + 6 +
STACK | 23 8
EXPR | 3 4 5 * + 2 3 ^ [12] * + 6 +
STACK | 23 8 12
EXPR | 3 4 5 * + 2 3 ^ 12 [*] + 6 +
STACK | 23 96
EXPR | 3 4 5 * + 2 3 ^ 12 * [+] 6 +
STACK | 119
EXPR | 3 4 5 * + 2 3 ^ 12 * + [6] +
STACK | 119 6
EXPR | 3 4 5 * + 2 3 ^ 12 * + 6 [+]
STACK | 125
And there we have the result. We only had to read through the expression once. Thus the execution time is linear. This is much better than the quadratic execution time we had when trying to evaluate the infix expression directly and had to read through it several time, looking for the next operation to perform.
Note that converting from infix to postfix can also be done in linear time, using the so-called Shunting Yard algorithm, which uses two stacks. Stacks are awesome!

Getting the combination of facevalues that gives the highest score in a dicegame

Working on a dicegame for school and I have trouble figuring out how to do automatic calculation of the result. (we don't have to do it automatically, so I could just let the player choose which dice to use and then just check that the user choices are valid) but now that I have started to think about it I can't stop...
the problem is as follows:
I have six dice, the dice are normal dice with the value of 1-6.
In this example I have already roled the dice and they have the following values:
[2, 2, 2, 1, 1, 1]
But I don't know how to calulate all combinations so that as many dicecombinations as possible whose value combined(addition) are 3 (in this example) are used.
The values should be added together (for example a die with value 1 and another die with the value 2 are together 3) then there are different rounds in the game where the aim is to get different values (which can be a combination(addition) of die-values for example
dicevalues: [2, 2, 2, 2, 2, 2]
could give the user a total of 12 points if 4 is the goal for the current round)
2 + 2 = 4
2 + 2 = 4
2 + 2 = 4
if the goal of the round instead where 6 then the it would be
2 + 2 + 2 = 6
2 + 2 + 2 = 6
instead which would give the player 12 points (6 + 6)
[1, 3, 6, 6, 6, 6]
with the goal of 3 would only use the dice with value 3 and discard the rest since there is no way to add them up to get three.
2 + 1 = 3
2 + 1 = 3
2 + 1 = 3
would give the user 9 points.
but if it where calculated the wrong way and the ones where used up together instead of each 1 getting apierd with a two 1 + 1 + 1 which would only give the player 3 points och the twos couldn't be used.
Another example is:
[1, 2, 3, 4, 5, 6]
and all combinations that are equal to 6 gives the user points
[6], [5, 1], [4 ,2]
user gets 18 points (3 * 6)
[1 ,2 ,3], [6]
user gets 12 points (2 * 6) (Here the user gets six points less due to adding upp 1 + 2 + 3 instead of doing like in the example above)
A dice can have a value between 1 and 6.
I haven't really done much more than think about it and I'm pretty sure that I could do it right now, but it would be a solution that would scale really bad if I for example wanted to use 8 dices instead and every time I start programming on it I start to think that have to be a better/easier way of doing it... Anyone have any suggestion on where to start? I tried searching for an answer and I'm sure it's out there but I have problem forumulating a query that gives me relevant result...
With problems that look confusing like this, it is a really good idea to start with some working and examples. We have 6 die, with range [1 to 6]. The possible combinations we could make therefore are:
target = 2
1 combination: 2
2 combination: 1+1
target = 3
1 combination: 3
2 combination: 2+1
3 combination: 1+1+1
target = 4
1 combination: 4
2 combination: 3+1
2+2
3 combination: 2+1+1
4 combination: 1+1+1+1
target = 5
1 combination: 5
2 combination: 4+1
3+2
3 combination: 2+2+1
4 combination: 2+1+1+1
5 combination: 1+1+1+1+1
See the pattern? Hint, we go backwards from target to 1 for the first number we can add, and then given this first number, and the size of the combination, there is a limit to how big subsequent numbers can be!
There is a finite list of possible combinations. You can by looking for 1 combination scores, and remove these from the die available. Then move on to look for 2 combination scores, etc.
If you want to read more about this sub-field of mathematics, the term you need to look for is "Combinatorics". Have fun!

basic concept for Linear Algebra

Question is: Let A and B be 3x3 (square) matrices. Which of following must necessarily hold true?
One of the option is A + B = B + A, but I didn't choose. The answer display this equation is true. I don't understand. In the identity matrix, A * I(n*n) = I * A
Can this multiply principal also be used in addition? Could you give a specific and simple example? Thanks a lot.
Adding Matrices is a element-wise operator:
Note adding is commutative which means you can change the order around.
A = [ 1 2 3; 4 5 6; 7 8 9]
B = [4 9 2 ; 2 0 4 ; 3 6 7]
A + B = [1 + 4 ,2 + 9, 3 + 2; 4 + 2, 5 + 0, 6 + 4; 7 + 3 ,8 + 6, 7 + 9]
B + A = [4 + 1, 9 + 2, 2 + 3; 2 + 4, 0 + 5, 4 + 6; 3 + 7, 6 + 8, 9 + 7]
Notice both operations yield the same result since adding is commutative and bit wise adding means adding matrices is also commutative so the operands can be swapped! Not too sure about your second question though :(

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

Trouble understanding what to do with output of shunting-yard 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'

Resources