Check if Graph is Bipartite – Adjacency List using Depth-First Search(DFS)

Objective: Given a graph represented by the adjacency List, write a Depth-First Search(DFS) algorithm to check whether the graph is bipartite or not. Earlier we have solved the same problem using Adjacency Matrix (Check if Graph is Bipartite – Adjacency Matrix) with Time complexity: O(V2) where V – No of vertices in the graph. In … Read more Check if Graph is Bipartite – Adjacency List using Depth-First Search(DFS)

Evaluation of Prefix Expressions (Polish Notation) | Set 2

Earlier we had discussed how to evaluate prefix expression where operands are of single-digit. In this article, we will discuss how to evaluate prefix expression for any number ( not necessarily single digit.) Prefix notation is a notation for writing arithmetic expressions in which the operands appear after their operators. Let’s assume the below Operands … Read more Evaluation of Prefix Expressions (Polish Notation) | Set 2

Evaluation of Prefix Expressions (Polish Notation) | Set 1

Prefix notation is a notation for writing arithmetic expressions in which the operands appear after their operators. There are no precedence rules to learn. Let’s assume the below Operands are real numbers in real digits. (Later we will Enhance the solution for any number) Permitted operators: +,-, *, /, ^(exponentiation) Blanks are NOT permitted in … Read more Evaluation of Prefix Expressions (Polish Notation) | Set 1

Convert Prefix to Postfix Expression

Objective: Given a Prefix expression, write an algorithm to convert it into Postfix expression. Example: Input: Prefix expression:  + A B  Output: Postfix expression: A B + Input: Prefix expression:  *-A/BC-/AKL Output: Postfix expression: ABC/-AK/L-* Approach: Use Stacks Algorithm: Iterate the given expression from right to left, one character at a time If the character … Read more Convert Prefix to Postfix Expression

Convert Postfix to Prefix Expression

Objective: Given a Postfix expression, write an algorithm to convert it into prefix expression. Example: Input: Postfix expression:  A B +  Output: Prefix expression- + A B Input: Postfix expression:  ABC/-AK/L-* Output: Infix expression: *-A/BC-/AKL Approach: Use Stack Algorithm: Iterate the given expression from left to right, one character at a time If the character … Read more Convert Postfix to Prefix Expression

Convert Postfix to Infix Expression

Objective: Given a Postfix expression, write an algorithm to convert it into Infix expression. Example: Input: Postfix expression:  A B +  Output: Infix expression- (A + B) Input: Postfix expression:  ABC/-AK/L-* Output: Infix expression: ((A-(B/C))*((A/K)-L)) Approach: Use Stack Algorithm: Iterate the given expression from left to right, one character at a time If a character … Read more Convert Postfix to Infix Expression

Convert Prefix to Infix Expression

Objective: Given a Prefix expression, write an algorithm to convert it into Infix expression. Example: Input: Prefix expression: + A B Output: Infix expression- (A + B) Input: Prefix expression: *-A/BC-/AKL Output: Infix expression: ((A-(B/C))*((A/K)-L)) Approach: Use Stacks Algorithm: Iterate the given expression from right to left (in reverse order), one character at a time … Read more Convert Prefix to Infix Expression

Convert Infix to Prefix Expression

Objective: Given an Infix expression, write an algorithm to convert it into Prefix expression. Example: Input: Infix expression – A + B Output: Prefix expression- +AB Input: Infix expression – A+B*(C^D-E) Output: Prefix expression- +A*B-^CDE Approach: Use Stack Operator stack: This stack will be used to keep operations (+, -, *, /, ^) Order of … Read more Convert Infix to Prefix Expression

Convert Infix to Postfix Expression

Objective: Given an Infix expression, write an algorithm to convert it into Postfix expression. Example: Input: Infix expression – A + B Output: Postfix expression- AB+ Input: Infix expression – A+B*(C^D-E) Output: Postfix expression- ABCD^E-*+ Approach: Use Stacks Operator stack: This stack will be used to keep operations (+, -, *, /, ^) Order of … Read more Convert Infix to Postfix Expression

Evaluation of Infix expressions

Infix notation is commonly used in arithmetic formula or statements, the operators are written in-between their operands. Let’s assume the below Operands are real numbers. Permitted operators: +,-, *, /, ^(exponentiation) Blanks are permitted in expression. Parenthesis are permitted Example: A * ( B + C ) / D 2 * (5 + 3) / … Read more Evaluation of Infix expressions

Valid Brackets – Part 2 | Stack Method

Objective: Given a string containing just the characters ( , ) determine if the input string is valid. Example: ()()(()(()))() valid: true )()()( valid: false ()()) valid: false Approach: Earlier we discussed the solution which keeps the count of open and closed parentheses. In this solution we will solve this problem using stack. Using Stack: Initialize a … Read more Valid Brackets – Part 2 | Stack Method

Dynamic Programming – Egg Dropping Problem

Objec­tive:  There are n number of eggs and building which has k floors. Write an algorithm to find the minimum number of drops is required to know the floor from which if the egg is dropped, it will break. Note: One trial is – dropping an egg once from the particular floor. If egg does not … Read more Dynamic Programming – Egg Dropping Problem

Number of 1’s in bit representation of a number

Bit Manipulation

Objec­tive:  Write an algorithm to count the number of 1’s in the bit representation in given number. Exam­ple: Number: 6 Output: 2 ( 1 1 0) Number: 11 Output: 3 ( 1 0 1 1)  Approach: Method 1: Run Code While number > 0 Keep doing the number & 1, increment count if result is 1 … Read more Number of 1’s in bit representation of a number

Dynamic Programming – Maximum size square sub-matrix with all 1s

Objective: Given a matrix of 0’s and 1’s (binary matrix). Find out Maximum size square sub-matrix with all 1’s. Example: Approach: Base Cases: If only one row is given then cells with 1’s will be the Maximum size square sub-matrix with size = 1. If only one column is given then cells with 1’s will … Read more Dynamic Programming – Maximum size square sub-matrix with all 1s

Backtracking – Search a Word In a Matrix

Objective : Given a 2D matrix of characters. Check whether the word exist in the matrix or not. If it exists then print its path. All movements are allowed (right, left, up, down and diagonally). Example: Approach: We will show the path as increment counter   Create a solution matrix of the same structure as … Read more Backtracking – Search a Word In a Matrix