shunting yard algo

Post on 22-May-2015

1.205 Views

Category:

Technology

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

1

ALGORITHM ANALYSIS

Course Code : CSE-305L Course Teacher: Safaet Hossain Presentation on Algorithm Analysis

Topic: Shunting-yard algorithm

Presented By: MD. Toufiq Akbar Shawon ( ID: 011103004 ) Al-Fariha Arpa (ID: 011103011 )

2

SHUNTING-YARD ALGORITHM

In computer science, the shunting-yard algorithm is a method for parsing mathematical expressions specified in infix notation. It can be used to produce output in Reverse Polish notation (RPN) or as an abstract syntax tree (AST). The algorithm was invented by Edger Dijkstra and named the "shunting yard" algorithm because its operation resembles that of a railroad shunting yard. Dijkstra first described the Shunting Yard Algorithm in Mathematics Centrum report.

Like the evaluation of RPN, the shunting yard algorithm is stack-based. Infix expressions are the form of mathematical notation most people are used to, for instance 3+4 or 3+4*(2−1). For the conversion there are two text variables (strings), the input and the output. There is also a stack that holds operators not yet added to the output queue. To convert, the program reads each symbol in order and does something based on that symbol.

The shunting-yard algorithm has been later generalized into operator-precedence parsing.

3

GRAPHICAL REPRESENTATION

4

EXPLANATION OF THE GRAPHICAL REPRESENTATION Graphical illustration of algorithm, using a three way

railroad junction. The input is processed one symbol at a time, if a variable or number is found it is copied direct to the output b), d), f), h). If the symbol is an operator it is pushed onto the operator stack c), e), however, if its precedence is less than that of the operator at the top of the stack or the precedence's are equal and the operator is left associative then that operator is popped off the stack and added to the output g). Finally remaining operators are popped off the stack and added to the output.

5

A SIMPLE EXAMPLE

Input: 3+4 Add 3 to the output queue (whenever a number is

read it is added to the output) Push + (or its ID) onto the operator stack Add 4 to the output queue After reading the expression pop the operators off

the stack and add them to the output. In this case there is only one, "+". Output 3 4 +

This already shows a couple of rules: All numbers are added to the output when they

are read. At the end of reading the expression, pop all

operators off the stack and onto the output.

6

THE ALGORITHM For all the input tokens :

Read the next token ; If token is an operator (x) :

While there is an operator (y) at the top of the operators stack and either (x) is left-associative and its precedence is less or equal to that of (y), or (x) is right-associativeand its precedence is less than (y) :

Pop (y) from the stack ; Add (y) output buffer ;

Push (x) on the stack ; Else If token is left parenthesis, then push it on the stack ; Else If token is a right parenthesis :

Until the top token (from the stack) is left parenthesis, pop from the stack to the output buffer ;

Also pop the left parenthesis but don’t include it in the output buffer ;

Else add token to output buffer . While there are still operator tokens in the stack, pop them to output

Exit

7

To analyze the running time complexity of this algorithm, one has only to note that each token will be read once, each number, function, or operator will be printed once, and each function, operator, or parenthesis will be pushed onto the stack and popped off the stack once – therefore, there are at most a constant number of operations executed per token, and the running time is thus O(n) – linear in the size of the input.

RUNNING TIME COMPLEXITY

8

APPLICATIONS

The Shunting-yard algorithm is Used to parsing mathematical expressions specified in infix notation. It can be used to produce output in Reverse Polish notation (RPN) or as an abstract syntax tree (AST).

It’s also Used in Game-Programming (mainly in the Graphics Section).

Used In calculating huge amount of numbers with efficiency.

9

2 + (3 * (8 - 4)) = ?

How to evaluate this (or similar) Algorithm?

10

2 + (3 * (8 - 4)) = ?

Let’s play that the tokens are train cars and we are shunting the shunting yard.

2 + ( 3 * ( 8 - 4 ) )

11

2 + (3 * (8 - 4)) = ?

The first car is a number, it goes straight through.

2 + ( 3 * ( 8 - 4 ) )

12

2 + (3 * (8 - 4)) = ?

Next, the third track (the stack) is empty, we move the operator there.

2 + ( 3 * ( 8 - 4 ) )

13

2 + (3 * (8 - 4)) = ?

Left parenthesis goes always down.

2

+

( 3 * ( 8 - 4 ) )

14

2 + (3 * (8 - 4)) = ?

Again, there is a number. It moves always straight to the left.

2

+

(

3 * ( 8 - 4 ) )

15

2 + (3 * (8 - 4)) = ?

Next there is an operator, it goes down because the topmost car there is an parenthesis.

2

+

(

3 * ( 8 - 4 ) )

16

2 + (3 * (8 - 4)) = ?

Again a left parenthesis, they go always to the stack.

2

+

(

3

*

( 8 - 4 ) )

17

2 + (3 * (8 - 4)) = ?

A number, straight to the left.

2

+

(

3

*

(

8 - 4 ) )

18

2 + (3 * (8 - 4)) = ?

A number, straight to the left.

2

+

(

3

*

(

8 - 4 ) )

19

2 + (3 * (8 - 4)) = ?

An operator, move it down.

2

+

(

3

*

(

8 - 4 ) )

20

2 + (3 * (8 - 4)) = ?

A number, to the left, as always.

2

+

(

3

*

(

8

-

4 ) )

21

2 + (3 * (8 - 4)) = ?

A right parenthesis. Now we move the cars from the bottom until there is left parenthesis.

2

+

(

3

*

(

8

-

4 ) )

22

2 + (3 * (8 - 4)) = ?

The pair of the parenthesis just disappear.

2

+

(

3

*

(

8 -4 ) )

23

2 + (3 * (8 - 4)) = ?

Again, we pop out the items until there is a left parenthesis.

2

+

(

3

*

8 -4 )

24

2 + (3 * (8 - 4)) = ?

A pair of parenthesis disappear.

2

+

(

3 *8 -4 )

25

2 + (3 * (8 - 4)) = ?

No more cars on the right side, so we move the cars from the bottom to the left.

2

+

3 *8 -4

26

2 + (3 * (8 - 4)) = ?

Now the transformation is done, how to evaluate it?

2 +3 *8 -4

27

2 + (3 * (8 - 4)) = ?

Move the cars back to the right side.

2 +3 *8 -4

28

2 + (3 * (8 - 4)) = ?

Move the cars back to the right side.

2 +3 *8 -4

29

2 + (3 * (8 - 4)) = ?

Move the numbers to the down until we find an operator.

2 +3 *8 -4

30

2 + (3 * (8 - 4)) = ?

When operator is found, place it to the middle so that it is between two numbers.

2

+

3

*

8

-

4

31

2 + (3 * (8 - 4)) = ?

Do the calculation and put the result back to down.

2

+

3

*4-8

32

2 + (3 * (8 - 4)) = ?

Do the calculation and put the result back to down.

2

+

3

*4

33

2 + (3 * (8 - 4)) = ?

Again, operator to the middle, between the two upmost numbers.

2

+

3

*

4

34

2 + (3 * (8 - 4)) = ?

Calculate the expression and put the result back to the down.

2

+3 * 4

35

2 + (3 * (8 - 4)) = ?

Calculate the expression and put the result back to the down.

2

+12

36

2 + (3 * (8 - 4)) = ?

And the last operator, it is handled in the same way.

2

+

12

37

2 + (3 * (8 - 4)) = ?

Calculate the result and that’s it!

2 + 12

38

2 + (3 * (8 - 4)) = 14

Calculate the result and that’s it!

14

39

END

Thank You For listening to our presentation. For any queries ask now or Forever regret!

top related