Category Archives: AOAD

Program to Implement KMP Algorithm in Java

In this post, we’ll explore how to implement the Knuth-Morris-Pratt (KMP) string matching algorithm in Java. The KMP algorithm is an efficient way to search for a substring within a given string. It improves upon the brute-force approach by skipping over unnecessary comparisons after partial matches.

What is the Knuth-Morris-Pratt (KMP) Algorithm?

The KMP algorithm searches for a substring (pattern) in a larger string (text) efficiently by avoiding redundant comparisons. The key idea behind KMP is that when a mismatch happens, it uses previously gathered information about the pattern to shift the search window instead of starting from scratch.

The algorithm uses two main components:

  1. Failure Function (Prefix Table): It helps determine how much the pattern should shift when a mismatch occurs.
  2. Search Process: Once the failure function is computed, the algorithm searches through the text to find the pattern.
Continue reading Program to Implement KMP Algorithm in Java

Program to Implement K-Naive Algorithm in Java

In this post, we’ll implement the K-Naive (Brute-Force) string matching algorithm in Java. This is the simplest form of string search, where we check for the presence of a pattern in a text by checking one by one at every possible position.

What is the K-Naive (Brute-Force) Algorithm?

The Brute-Force algorithm searches for a substring (pattern) within a larger string (text) by checking for a match at every position in the text. It is simple to understand and implement but can be inefficient for large inputs.

Continue reading Program to Implement K-Naive Algorithm in Java

Implementing Sum of Subset by Backtracking in Java

The subset sum problem is a classic problem in computer science and is often solved using backtracking. The goal is to find subsets of elements selected from a given set whose sum adds up to a given number K.

Understanding the Problem

Given a set of n positive integers and a value m, the task is to determine all possible subsets whose sum is equal to m. This problem can be efficiently solved using backtracking.

Approach: Backtracking

Backtracking is a general algorithmic technique that incrementally builds candidates to a solution and abandons a candidate as soon as it is determined that the candidate cannot lead to a valid solution. In the subset sum problem, we:

  1. Start with an empty subset.
  2. Add elements one by one while keeping track of the sum.
  3. If the sum equals m, print the subset.
  4. If the sum exceeds m, backtrack and try another possibility.
Continue reading Implementing Sum of Subset by Backtracking in Java

Implementing 0-1 Knapsack in Java

The knapsack problem or rucksack problem is a problem in combinatorial optimization: Given a set of items, each with a mass and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible.

In 0-1 Knapsack, the restriction is that, one cannot select fraction of any item. He/She must select either whole item or try with alternate one.

Continue reading Implementing 0-1 Knapsack in Java