SlideShare a Scribd company logo
1 of 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 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.
                                                                        2
GRAPHICAL REPRESENTATION




                           3
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.




                                                                         4
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.
                                                                       5
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-associative
            and 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
                                                                                         6
RUNNING TIME COMPLEXITY
   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.


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


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




           How to evaluate this (or similar) Algorithm?




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

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

                                                          2   +   (   3   *   (   8   -   4   )    )




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

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

                                                       2   +   (   3   *   (   8   -   4   )    )




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

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

2                                                            +   (   3   *   (   8   -   4   )    )




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

Left parenthesis goes always down.

2                                        (   3   *   (   8   -   4   )    )




                                                                     13


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

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

2                                                                 3   *   (   8   -   4   )    )




                                                                                          14
                                             (

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

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

2   3                                                                *   (   8   -   4   )    )




                                                                                         15
                                           (

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

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

2   3                                                    (   8   -   4   )    )




                                             *
                                                                         16
                                             (

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

A number, straight to the left.

2   3                                 8   -   4   )    )




                                  (

                                  *
                                                  17
                                  (

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

A number, straight to the left.

2   3   8                             -   4   )    )




                                  (

                                  *
                                              18
                                  (

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

An operator, move it down.

2   3   8                        -   4   )    )




                             (

                             *
                                         19
                             (

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

A number, to the left, as always.

2   3   8                               4   )    )




                                    -

                                    (

                                    *
                                            20
                                    (

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

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

2   3   8   4                                                                        )   )




                                           -

                                           (

                                           *
                                                                                    21
                                           (

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

The pair of the parenthesis just disappear.

2   3   8   4   -                                 )    )




                                              (

                                              *
                                                  22
                                              (

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

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

2   3   8   4   -                                                     )




                                             *
                                                                 23
                                             (

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

A pair of parenthesis disappear.

2   3   8   4   -   *                       )




                                       24
                                   (

                                   +
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   -   *




                                                                                   25


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

Now the transformation is done, how to evaluate it?

2   3   8   4   -   *   +




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

Move the cars back to the right side.

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 numbers to the down until we find an operator.

                                                          2   3   8   4   -    *   +




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

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

                                                                                    -    *   +




                                           4

                                           8
                                                                                        30
                                           3

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

Do the calculation and put the result back to down.

                                     8   -   4         *   +




                                                      31
                                             3

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

Do the calculation and put the result back to down.

                                        4              *   +




                                                      32
                                            3

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

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

                                                                  *   +




                                         4
                                                                 33
                                         3

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

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

                                      3   *   4                      +




                                                                34


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

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

                                         12                          +




                                                                35


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

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

                                                             +




                                                        36
                                             12

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

Calculate the result and that’s it!

                                      2   +   12




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

Calculate the result and that’s it!

                                      14




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




                                                 39

More Related Content

What's hot (20)

Wp unit 1 (1)
Wp  unit 1 (1)Wp  unit 1 (1)
Wp unit 1 (1)
 
Parsing
ParsingParsing
Parsing
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Evaluation of postfix expression using stack
Evaluation of postfix expression using stackEvaluation of postfix expression using stack
Evaluation of postfix expression using stack
 
Circular linked list
Circular linked listCircular linked list
Circular linked list
 
Array dan Pointer
Array dan PointerArray dan Pointer
Array dan Pointer
 
Context free langauges
Context free langaugesContext free langauges
Context free langauges
 
Data Structure (Stack)
Data Structure (Stack)Data Structure (Stack)
Data Structure (Stack)
 
Python programming : Arrays
Python programming : ArraysPython programming : Arrays
Python programming : Arrays
 
Set data structure
Set data structure Set data structure
Set data structure
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in python
 
Return Oriented Programming - ROP
Return Oriented Programming - ROPReturn Oriented Programming - ROP
Return Oriented Programming - ROP
 
Lecture 02 lexical analysis
Lecture 02 lexical analysisLecture 02 lexical analysis
Lecture 02 lexical analysis
 
Selection sort
Selection sortSelection sort
Selection sort
 
Queue
QueueQueue
Queue
 
Materi 3 Finite State Automata
Materi 3   Finite State AutomataMateri 3   Finite State Automata
Materi 3 Finite State Automata
 
Trees and graphs
Trees and graphsTrees and graphs
Trees and graphs
 
Lecture11 syntax analysis_7
Lecture11 syntax analysis_7Lecture11 syntax analysis_7
Lecture11 syntax analysis_7
 
Topological Sorting
Topological SortingTopological Sorting
Topological Sorting
 
Address translation-mechanism-of-80386 by aniket bhute
Address translation-mechanism-of-80386 by aniket bhuteAddress translation-mechanism-of-80386 by aniket bhute
Address translation-mechanism-of-80386 by aniket bhute
 

Similar to CSE-305L Algorithm Analysis Shunting-Yard Algorithm

The concept of stack is extremely important in computer science and .pdf
The concept of stack is extremely important in computer science and .pdfThe concept of stack is extremely important in computer science and .pdf
The concept of stack is extremely important in computer science and .pdfarihantsherwani
 
Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examplesgreatqadirgee4u
 
Discrete Math IP4 - Automata Theory
Discrete Math IP4 - Automata TheoryDiscrete Math IP4 - Automata Theory
Discrete Math IP4 - Automata TheoryMark Simon
 
Floating point basicsThe core idea of floating-point representatio.pdf
Floating point basicsThe core idea of floating-point representatio.pdfFloating point basicsThe core idea of floating-point representatio.pdf
Floating point basicsThe core idea of floating-point representatio.pdfinfo235816
 
Ip 5 discrete mathematics
Ip 5 discrete mathematicsIp 5 discrete mathematics
Ip 5 discrete mathematicsMark Simon
 
01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seoJinTaek Seo
 
Introduction to MatLab programming
Introduction to MatLab programmingIntroduction to MatLab programming
Introduction to MatLab programmingDamian T. Gordon
 
Assignment statements
Assignment statementsAssignment statements
Assignment statementsDivya Devan
 
Robotic Manipulator with Revolute and Prismatic Joints
Robotic Manipulator with Revolute and Prismatic JointsRobotic Manipulator with Revolute and Prismatic Joints
Robotic Manipulator with Revolute and Prismatic JointsTravis Heidrich
 
CS17604_TOP Parser Compiler Design Techniques
CS17604_TOP Parser Compiler Design TechniquesCS17604_TOP Parser Compiler Design Techniques
CS17604_TOP Parser Compiler Design Techniquesd72994185
 
Python programming for Beginners - II
Python programming for Beginners - IIPython programming for Beginners - II
Python programming for Beginners - IINEEVEE Technologies
 
C programming assignment help
C programming assignment helpC programming assignment help
C programming assignment helpHebrew Johnson
 
Assignment statements
Assignment statementsAssignment statements
Assignment statementsDivya Devan
 

Similar to CSE-305L Algorithm Analysis Shunting-Yard Algorithm (20)

Shunting yard
Shunting yardShunting yard
Shunting yard
 
The concept of stack is extremely important in computer science and .pdf
The concept of stack is extremely important in computer science and .pdfThe concept of stack is extremely important in computer science and .pdf
The concept of stack is extremely important in computer science and .pdf
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
Ch8a
Ch8aCh8a
Ch8a
 
Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examples
 
Discrete Math IP4 - Automata Theory
Discrete Math IP4 - Automata TheoryDiscrete Math IP4 - Automata Theory
Discrete Math IP4 - Automata Theory
 
Floating point basicsThe core idea of floating-point representatio.pdf
Floating point basicsThe core idea of floating-point representatio.pdfFloating point basicsThe core idea of floating-point representatio.pdf
Floating point basicsThe core idea of floating-point representatio.pdf
 
Ip 5 discrete mathematics
Ip 5 discrete mathematicsIp 5 discrete mathematics
Ip 5 discrete mathematics
 
01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo
 
Introduction to MatLab programming
Introduction to MatLab programmingIntroduction to MatLab programming
Introduction to MatLab programming
 
Left factor put
Left factor putLeft factor put
Left factor put
 
Array
ArrayArray
Array
 
Assignment statements
Assignment statementsAssignment statements
Assignment statements
 
Robotic Manipulator with Revolute and Prismatic Joints
Robotic Manipulator with Revolute and Prismatic JointsRobotic Manipulator with Revolute and Prismatic Joints
Robotic Manipulator with Revolute and Prismatic Joints
 
CS17604_TOP Parser Compiler Design Techniques
CS17604_TOP Parser Compiler Design TechniquesCS17604_TOP Parser Compiler Design Techniques
CS17604_TOP Parser Compiler Design Techniques
 
Python programming for Beginners - II
Python programming for Beginners - IIPython programming for Beginners - II
Python programming for Beginners - II
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
C programming assignment help
C programming assignment helpC programming assignment help
C programming assignment help
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
 
Assignment statements
Assignment statementsAssignment statements
Assignment statements
 

Recently uploaded

"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 

Recently uploaded (20)

"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 

CSE-305L Algorithm Analysis Shunting-Yard Algorithm

  • 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 ) 1
  • 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. 2
  • 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. 4
  • 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. 5
  • 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-associative and 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 6
  • 7. RUNNING TIME COMPLEXITY  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. 7
  • 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. 8
  • 9. 2 + (3 * (8 - 4)) = ? How to evaluate this (or similar) Algorithm? 9
  • 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 ) ) 10
  • 11. 2 + (3 * (8 - 4)) = ? The first car is a number, it goes straight through. 2 + ( 3 * ( 8 - 4 ) ) 11
  • 12. 2 + (3 * (8 - 4)) = ? Next, the third track (the stack) is empty, we move the operator there. 2 + ( 3 * ( 8 - 4 ) ) 12
  • 13. 2 + (3 * (8 - 4)) = ? Left parenthesis goes always down. 2 ( 3 * ( 8 - 4 ) ) 13 +
  • 14. 2 + (3 * (8 - 4)) = ? Again, there is a number. It moves always straight to the left. 2 3 * ( 8 - 4 ) ) 14 ( +
  • 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 ) ) 15 ( +
  • 16. 2 + (3 * (8 - 4)) = ? Again a left parenthesis, they go always to the stack. 2 3 ( 8 - 4 ) ) * 16 ( +
  • 17. 2 + (3 * (8 - 4)) = ? A number, straight to the left. 2 3 8 - 4 ) ) ( * 17 ( +
  • 18. 2 + (3 * (8 - 4)) = ? A number, straight to the left. 2 3 8 - 4 ) ) ( * 18 ( +
  • 19. 2 + (3 * (8 - 4)) = ? An operator, move it down. 2 3 8 - 4 ) ) ( * 19 ( +
  • 20. 2 + (3 * (8 - 4)) = ? A number, to the left, as always. 2 3 8 4 ) ) - ( * 20 ( +
  • 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 ) ) - ( * 21 ( +
  • 22. 2 + (3 * (8 - 4)) = ? The pair of the parenthesis just disappear. 2 3 8 4 - ) ) ( * 22 ( +
  • 23. 2 + (3 * (8 - 4)) = ? Again, we pop out the items until there is a left parenthesis. 2 3 8 4 - ) * 23 ( +
  • 24. 2 + (3 * (8 - 4)) = ? A pair of parenthesis disappear. 2 3 8 4 - * ) 24 ( +
  • 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 - * 25 +
  • 26. 2 + (3 * (8 - 4)) = ? Now the transformation is done, how to evaluate it? 2 3 8 4 - * + 26
  • 27. 2 + (3 * (8 - 4)) = ? Move the cars back to the right side. 2 3 8 4 - * + 27
  • 28. 2 + (3 * (8 - 4)) = ? Move the cars back to the right side. 2 3 8 4 - * + 28
  • 29. 2 + (3 * (8 - 4)) = ? Move the numbers to the down until we find an operator. 2 3 8 4 - * + 29
  • 30. 2 + (3 * (8 - 4)) = ? When operator is found, place it to the middle so that it is between two numbers. - * + 4 8 30 3 2
  • 31. 2 + (3 * (8 - 4)) = ? Do the calculation and put the result back to down. 8 - 4 * + 31 3 2
  • 32. 2 + (3 * (8 - 4)) = ? Do the calculation and put the result back to down. 4 * + 32 3 2
  • 33. 2 + (3 * (8 - 4)) = ? Again, operator to the middle, between the two upmost numbers. * + 4 33 3 2
  • 34. 2 + (3 * (8 - 4)) = ? Calculate the expression and put the result back to the down. 3 * 4 + 34 2
  • 35. 2 + (3 * (8 - 4)) = ? Calculate the expression and put the result back to the down. 12 + 35 2
  • 36. 2 + (3 * (8 - 4)) = ? And the last operator, it is handled in the same way. + 36 12 2
  • 37. 2 + (3 * (8 - 4)) = ? Calculate the result and that’s it! 2 + 12 37
  • 38. 2 + (3 * (8 - 4)) = 14 Calculate the result and that’s it! 14 38
  • 39. END  Thank You For listening to our presentation.  For any queries ask now or Forever regret! 39