Algorithms and Applications – P1

Tram Ho

Series purpose.

With this series, I want to be able to arouse the passion for learning algorithms in me and my friends through:

  • Problems associated with reality.
  • The way to solve problems is based on the application of basic algorithms.
  • From there, we can see the importance of algorithms in programming.

Problem.

Let’s take a look at a simple example that most of us developers have to work with opening and closing brackets like {},[],() and most text editors now support us to check if We check its validity. Did you ever try to re-implement them or simply find out how it works? Today we will look at them under the basic algorithms learned in college.

Handle.

  1. Simplify the problem: We remove the syntax and statements inside each block of code, treating them as strings consisting only of {[]}() . Here the problem is simpler.
  2. Idea:
    • Divide them into 2 types : the opening character includes {[( and the closing character includes ]})
    • The parenthesis opened last must be closed first . For example, we have a simple expression like this: if (a[i] % 2) == 0 .
    • Finally – first , sound familiar to me. Exactly, it is working on the principle of Last in – First Out aka Stack.
    • In the above example, we see the brackets ( which are opened first followed by the [ sign, so according to the LIFO principle, in the string we will have to check in the string to see if the sign [ appears before the ) or not? => So with the stack algorithm, the problem has become quite clear, let’s take a look at the possible cases and solve it.

Test cases:

  • In the above example, we have just tested the happy case (aka the ideal case). Now let’s go through the list of possible cases.
    1. stack (string) to check is an odd stack.
      • This is the most obvious first case. If there is an opening parenthesis, there must be a closing, so a stack of odd length is of course invalid without considering the elements inside.
      • For example { [ } . With 3 elements then surely this stack is not valid without seeing if they are in pairs or not.
    2. The beginning of the stack is a block closing element. of course it’s wrong
    3. Stack is an even number but opens and closes out of order.
    • Without an algorithm, this case would seem rather complicated, but since we’ve chosen the stack, the rest is pretty simple.
    • We temporarily call the last element of the stack s1 , the next element in the sequence to consider is s_next
      • We check if s_next is open ( {,[,( ) or closed ( },],) ).
        • If it’s open, add s_next to the bottom of the stack. Now s_next will act as s1
        • If s_next is the closing character then check if s1 is the open element corresponding to s_next ?
          • If true remove s1 from the stack and continue with the next character in the string and don’t care about it anymore.
          • If false then determine this is an invalid string and terminate.
    1. Valid string:
    • With the above example, we can clearly see that if the end of the chain and stack.empty == true , the block markers are closed. ⇒ delectable.

Conclude.

  • I just presented a fairly typical example related to the Stack algorithm and its application. Hopefully through the article can arouse your desire to learn algorithms. The idea of ​​solving the problem is already there, let’s work together to solve the problem and don’t forget to give the code to solve the problem in the comment section.
  • To be continued….
Share the news now

Source : Viblo