Found 1900 Articles for Data Structure

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

373 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

883 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

Manacher’s Algorithm

karthikeya Boyini
Updated on 15-Jun-2020 18:40:57

718 Views

To find the longest palindromic substring from a string, we can use Manacher’s Algorithm. By selecting each character, we will try to find if there any palindrome using left and right pointer. There is another array to store information, from that information we can easily find how long the palindrome is. For each character, the array will store information. After traversing the whole string, we can find the longest palindromic subsequence from the created array.The time complexity of this algorithm is O(n).Input and OutputInput: String: “levelup” Output: Longest palindrome is: levelAlgorithmlongestPalindrome(text)Input − The text to find the longest palindromeOutput − ... Read More

Knuth-Morris-Pratt Algorithm

Sharon Christine
Updated on 15-Jun-2020 18:49:35

10K+ Views

Knuth Morris Pratt (KMP) is an algorithm, which checks the characters from left to right. When a pattern has a sub-pattern appears more than one in the sub-pattern, it uses that property to improve the time complexity, also for in the worst case.The time complexity of KMP is O(n).Input and OutputInput: Main String: “AAAABAAAAABBBAAAAB”, The pattern “AAAB” Output: Pattern found at location: 1 Pattern found at location: 7 Pattern found at location: 14AlgorithmfindPrefix(pattern, m, prefArray)Input − The pattern, the length of pattern and an array to store prefix locationOutput − The array to store where prefixes are locatedBegin    length ... Read More

Advertisements