Question

In: Computer Science

Algorithm design:Suppose that three types of parentheses are allowed in an expression: parentheses, square brackets, and...

Algorithm design:Suppose that three types of parentheses are allowed in an expression: parentheses, square brackets, and curly braces. Please design an efficient algorithm to determine whether the parentheses in the expression are correctly paired. Please write down the algorithm idea and steps.

Please explain what kind of data structure is used to solve the problem, and then give the specific algorithm steps

Solutions

Expert Solution

  • Below is the detailed algorithm for finding whether the given expression has balanced parentheses or not.
  • Algorithm:

Function checkParentheses(expression):

1. Declare a stack of characters

2. Now start traversing the expression character by character

3 if current character is a opening one i.e, ( or { or [ then,

4. push it into stack

5. else if current character is a closing one i.e, ) or } or ] then,

6. pop the top character from stack

7. and check if the popped opening bracket matches current closing one

8. if matches then continue

9. else return false i.e, unbalanced parentheses

10. After the matching if we did not get to the result then

11. check if stack is empty then return true

12. otherwise return false

  • The above algorithm computes if the expression has balanced parantheses or not by using a stack data structure which has the property of LIFO i.e, Last In First Out. So we traverse the expression from start and if we encounter an opening bracket of any of the three i.e, ( or { or [ then we add it to stack and when we encounter a closing bracket i.e, ) or } or ] then we check if the recent opening bracket was of the same type or not(as we have used stack we will get the recently added opening bracket in the top of it), if they are of the same type then we pop the opening one from stack and move forward traversing the expression otherwise we return false from the function as here the parentheses are unbalanced.
  • In the end after traversing the expression if we did not find any unbalanced pair we check for stack to be empty or not, so if stack is empty then we return true otherwise if it contains some opening bracket then we return false as those brackets do not have their closing ones.
  • The above algorithm runs in linear time as we inly use a loop(for/while) to traverse the expression and inside it to pop an element from stack i.e, an O(1) operation and all other comparisons made inside the loop are also constant time operation. So resultingly we have our algorithm to run in linear time i.e, in worst case , O(n) where n is the length of the expression.
  • So what we used is a stack data structure to solve the problem.

So if you still have any doubt regarding this solution please feel free to ask it in the comment section below and if it is helpful then please upvote this solution, THANK YOU.


Related Solutions

Describe an O(n)-time algorithm that checks if brackets in a given arithmetic expression is properly placed,...
Describe an O(n)-time algorithm that checks if brackets in a given arithmetic expression is properly placed, i.e., all left brackets are matched with their right counterparts and there are no unmatched brackets between any pair of matched brackets. Note that there are generally more than one types of brackets and only those of the same type can match against each other. For simplicity, you can assume that all operands are represented as ‘a’ and addition, +, is the only available...
Write a pseudocode method to determine if a set of parentheses and brackets is balanced. For...
Write a pseudocode method to determine if a set of parentheses and brackets is balanced. For example, such a method should return true for the string, "[()]{}{[()()]()}" and false for the string "[(])". Discuss how the algorithm will perform and how much space in memory it will take if somebody passes a massive string as input.
Devise a Turing machine to check the correct nesting of brackets (parentheses). So, your machine should...
Devise a Turing machine to check the correct nesting of brackets (parentheses). So, your machine should start with a tape with something like (()()(())) on it and halt with T printed on the tape, and it should start with something like (()()(() on the tape and halt with F. That is, correctly nested strings of brackets should give T, and wrongly nested strings of brackets should give the answer F. This example is important since it shows that Turing machines...
Answer ALL of the questions below! Circle the right expression in the parentheses. Q1. Import tariff...
Answer ALL of the questions below! Circle the right expression in the parentheses. Q1. Import tariff (a. raises, b. lowers, c, keeps constant) the domestic price and (a. always, b. sometimes, c. never) (a. raises, b lowers) the world price. 2. (Multiple choices.) A tariff in a large country tends to benefit (a. domestic producers/ b. foreign producers/ c. domestic consumers/ d. foreign consumers/ e. domestic government revenue) and hurt (a. domestic producers/ b. foreign producers/ c. domestic consumers/ d....
(Convert infix to postfix) Note: Postfix notation is a way of writing expression without using parentheses....
(Convert infix to postfix) Note: Postfix notation is a way of writing expression without using parentheses. For example, the expression ( 11 + 12 ) * 13 would be written as 11 12 + 13 * Assume that ALWAYS there is a space between operands and operators in the input expression. Use two stacks, one to store the operands and one to store the operators. Your program only accpets following operators : ( ) + - / * Write a...
Please answer ALL the questions below! Circle the right expression in the parentheses. Q1. Free trade...
Please answer ALL the questions below! Circle the right expression in the parentheses. Q1. Free trade as against trade policy can be justified with several reasons. Various forms of trade policy (a. always / b. sometimes) generate welfare loss to the economy, so that free trade is (a. always/ b. usually/ c. sometimes) more efficient than under trade policy. Optimal tariff argument goes (a. along with/ b. against) free trade where the terms of trade (a. can/ b. cannot) dominate...
Babylonian Algorithm. The Babylonian algorithm to compute the square root of a positive number n is as follows:
Chapter 3 Exercise 1Babylonian Algorithm. The Babylonian algorithm to compute the square root of a positive number n is as follows:      1. Make a guess at the answer (you can pick n/2 as your initial guess).      2. Computer = n / guess.      3. Set guess = (guess +r) / 2.      4. Go back to step 2 until the last two guess values are within 1% of each other.Write a program that inputs an integer for n, iterates through the Babylonian algorithm until the guess...
These questions refer to the expression for the magnetic field at the center of the square...
These questions refer to the expression for the magnetic field at the center of the square solenoid. Assume: for all coils, each side of the square a = 4.87 cm and the length of the solenoid L = 47.8 cm. NOTE: Everything must be in MKS units! NOTE: μo = 1.26x10-6 in MKS units (determined in question 1). a) In procedure 1, you will plot Bmax vs. N, the number of turns of the coil. Suppose you set the current...
Use the algorithm below to evaluate the following infix expression: a) a + 3 * 4...
Use the algorithm below to evaluate the following infix expression: a) a + 3 * 4 – 9 b) ( 2 + 6 ) / ( 3 – 5 ) . Algorithm WRITE STEP BY STEP Scan the characters in the infix expression. While there are characters left in the infix expression: Check the next character in the expression. 1. If the next character is operand, remove it from the expression and push it onto the operand stack. 2. If...
The mathematical expression of the encryption and decryption process of a Caesar cipher algorithm is expressed...
The mathematical expression of the encryption and decryption process of a Caesar cipher algorithm is expressed respectively as: c=Ep, k=p+k%26                                                                                                                         (1) p=Dc,k=c-k%26                                                                                                                         (2) Please do the following: Write at least two paragraphs to explain the principle of operation of the algorithm. For a full credit, your explanation must show the architectural diagram of the encryption and decryption process. Write a program to implement the Caesar algorithm Code must have two functions; encryption and decryption Test your codes with p as...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT