39
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 ) 1

Shunting yard algo

Embed Size (px)

Citation preview

Page 1: Shunting yard algo

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 )

Page 2: Shunting yard algo

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.

Page 3: Shunting yard algo

3

GRAPHICAL REPRESENTATION

Page 4: Shunting yard algo

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.

Page 5: Shunting yard algo

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.

Page 6: Shunting yard algo

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

Page 7: Shunting yard algo

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

Page 8: Shunting yard algo

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.

Page 9: Shunting yard algo

9

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

How to evaluate this (or similar) Algorithm?

Page 10: Shunting yard algo

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 ) )

Page 11: Shunting yard algo

11

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

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

2 + ( 3 * ( 8 - 4 ) )

Page 12: Shunting yard algo

12

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

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

2 + ( 3 * ( 8 - 4 ) )

Page 13: Shunting yard algo

13

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

Left parenthesis goes always down.

2

+

( 3 * ( 8 - 4 ) )

Page 14: Shunting yard algo

14

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

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

2

+

(

3 * ( 8 - 4 ) )

Page 15: Shunting yard algo

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 ) )

Page 16: Shunting yard algo

16

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

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

2

+

(

3

*

( 8 - 4 ) )

Page 17: Shunting yard algo

17

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

A number, straight to the left.

2

+

(

3

*

(

8 - 4 ) )

Page 18: Shunting yard algo

18

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

A number, straight to the left.

2

+

(

3

*

(

8 - 4 ) )

Page 19: Shunting yard algo

19

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

An operator, move it down.

2

+

(

3

*

(

8 - 4 ) )

Page 20: Shunting yard algo

20

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

A number, to the left, as always.

2

+

(

3

*

(

8

-

4 ) )

Page 21: Shunting yard algo

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 ) )

Page 22: Shunting yard algo

22

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

The pair of the parenthesis just disappear.

2

+

(

3

*

(

8 -4 ) )

Page 23: Shunting yard algo

23

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

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

2

+

(

3

*

8 -4 )

Page 24: Shunting yard algo

24

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

A pair of parenthesis disappear.

2

+

(

3 *8 -4 )

Page 25: Shunting yard algo

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

Page 26: Shunting yard algo

26

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

Now the transformation is done, how to evaluate it?

2 +3 *8 -4

Page 27: Shunting yard algo

27

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

Move the cars back to the right side.

2 +3 *8 -4

Page 28: Shunting yard algo

28

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

Move the cars back to the right side.

2 +3 *8 -4

Page 29: Shunting yard algo

29

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

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

2 +3 *8 -4

Page 30: Shunting yard algo

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

Page 31: Shunting yard algo

31

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

Do the calculation and put the result back to down.

2

+

3

*4-8

Page 32: Shunting yard algo

32

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

Do the calculation and put the result back to down.

2

+

3

*4

Page 33: Shunting yard algo

33

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

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

2

+

3

*

4

Page 34: Shunting yard algo

34

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

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

2

+3 * 4

Page 35: Shunting yard algo

35

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

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

2

+12

Page 36: Shunting yard algo

36

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

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

2

+

12

Page 37: Shunting yard algo

37

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

Calculate the result and that’s it!

2 + 12

Page 38: Shunting yard algo

38

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

Calculate the result and that’s it!

14

Page 39: Shunting yard algo

39

END

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