Found 510 Articles for Algorithms

Solving Cryptarithmetic Puzzles

Sharon Christine
Updated on 16-Jun-2020 07:16:15

14K+ Views

In the crypt-arithmetic problem, some letters are used to assign digits to it. Like ten different letters are holding digit values from 0 to 9 to perform arithmetic operations correctly. There are two words are given and another word is given an answer of addition for those two words.As an example, we can say that two words ‘BASE’ and ‘BALL’, and the result is ‘GAMES’. Now if we try to add BASE and BALL by their symbolic digits, we will get the answer GAMES.NOTE &minuns; There must be ten letters maximum, otherwise it cannot be solved.Input and OutputInput: This algorithm will ... Read More

Rat in a Maze Problem

karthikeya Boyini
Updated on 16-Jun-2020 07:23:16

5K+ Views

In this problem, there is a given maze of size N x N. The source and the destination location is top-left cell and bottom right cell respectively. Some cells are valid to move and some cells are blocked. If one rat starts moving from start vertex to destination vertex, we have to find that is there any way to complete the path, if it is possible then mark the correct path for the rat.The maze is given using a binary matrix, where it is marked with 1, it is a valid path, otherwise 0 for a blocked cell.NOTE: The rat can ... Read More

N Queen Problem

Sharon Christine
Updated on 16-Jun-2020 07:51:36

13K+ Views

This problem is to find an arrangement of N queens on a chess board, such that no queen can attack any other queens on the board.The chess queens can attack in any direction as horizontal, vertical, horizontal and diagonal way.A binary matrix is used to display the positions of N Queens, where no queens can attack other queens.Input and OutputInput: The size of a chess board. Generally, it is 8. as (8 x 8 is the size of a normal chess board.) Output: The matrix that represents in which row and column the N Queens can be placed. If the ... Read More

M-Coloring Problem

karthikeya Boyini
Updated on 16-Jun-2020 07:58:12

8K+ Views

In this problem, an undirected graph is given. There is also provided m colors. The problem is to find if it is possible to assign nodes with m different colors, such that no two adjacent vertices of the graph are of the same colors. If the solution exists, then display which color is assigned on which vertex.Starting from vertex 0, we will try to assign colors one by one to different nodes. But before assigning, we have to check whether the color is safe or not. A color is not safe whether adjacent vertices are containing the same color.Input and ... Read More

Hamiltonian Cycle

Sharon Christine
Updated on 07-Nov-2023 20:21:18

25K+ Views

In an undirected graph, the Hamiltonian path is a path, that visits each vertex exactly once, and the Hamiltonian cycle or circuit is a Hamiltonian path, that there is an edge from the last vertex to the first vertex. In this problem, we will try to determine whether a graph contains a Hamiltonian cycle or not. And when a Hamiltonian cycle is present, also print the cycle. Input and Output Input: The adjacency matrix of a graph G(V, E). Output: The algorithm finds the Hamiltonian path of the given graph. For this case it is (0, 1, 2, 4, ... Read More

Z Algorithm

Sharon Christine
Updated on 16-Jun-2020 08:13:31

405 Views

This algorithm is named Z Algorithm because, in this algorithm, we need to create a Z array. The size of the Z array is the same as the text size. This array is used to store the length of longest possible substring starting from the current character of the main string. At first, the pattern and the main text are concatenated with a special symbol which is not present in the text and pattern. If the P is pattern and T is the main text, then after concatenation, it would be P$T (Assuming $ is not present in the P ... Read More

Trie of all Suffixes

karthikeya Boyini
Updated on 15-Jun-2020 19:10:41

398 Views

From the text, we can generate all suffixes to make a tree structure. We know that every pattern that presents in the text, must be a prefix of one of the possible suffix in the text. By building Trie of all suffixes, we can find any substring in linear time. Every suffix is ending with string terminating symbol. From each node if there is any path, it moves forward, otherwise returns that pattern is not found.For this algorithm, the time complexity is O(m+k), where the m is the length of string and k is the frequency of the pattern in ... Read More

Suffix Array

Sharon Christine
Updated on 15-Jun-2020 19:15:53

884 Views

From a given string, we can get all possible suffixes. After sorting the suffixes in lexicographical order, we can get the suffix array. Suffix arrays can also be formed using suffix trees. By using the DFS traversal of suffix trees, we can get suffix arrays. Suffix arrays are helpful to find suffixes in linear time. We can also find substrings using suffix array by using binary search type procedure.The time complexity is O(m log n)Input and OutputInput: Main String: “BANANA”, Pattern: “NAN” Output: Pattern found at position: 2AlgorithmfillSuffixArray (text, suffArray)Input: The main stringOutput: The array of suffixesBegin    n := text Length ... Read More

Rabin-Karp Algorithm

karthikeya Boyini
Updated on 15-Jun-2020 19:24:52

2K+ Views

Rabin-Karp is another pattern searching algorithm to find the pattern in a more efficient way. It also checks the pattern by moving window one by one, but without checking all characters for all cases, it finds the hash value. When the hash value is matched, then only it tries to check each character. This procedure makes the algorithm more efficient.The time complexity is O(m+n), but for the worst case, it is O(mn).Input and OutputInput: Main String: “ABAAABCDBBABCDDEBCABC”, Pattern “ABC” Output: Pattern found at position: 4 Pattern found at position: 10 Pattern found at position: 18AlgorithmrabinKarpSearch(text, pattern, prime)Input − The main ... Read More

Naive Pattern Searching

Sharon Christine
Updated on 15-Jun-2020 18:34:58

6K+ Views

Naïve pattern searching is the simplest method among other pattern searching algorithms. It checks for all character of the main string to the pattern. This algorithm is helpful for smaller texts. It does not need any pre-processing phases. We can find substring by checking once for the string. It also does not occupy extra space to perform the operation.The time complexity of Naïve Pattern Search method is O(m*n). The m is the size of pattern and n is the size of the main string.Input and OutputInput: Main String: “ABAAABCDBBABCDDEBCABC”, pattern: “ABC” Output: Pattern found at position: 4 Pattern found at ... Read More

Advertisements