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

Java Program for Longest Common Subsequence (LCS) Problem

The Longest Common Subsequence (LCS) problem asks for the longest sequence of characters that appears in the same relative order in two strings, but not necessarily contiguously. For example, the LCS of "president" and "providence" is "priden" (length 6). LCS is solved efficiently using dynamic programming in O(m×n) time, where m and n are the lengths of the two sequences. It is widely used in diff tools, DNA sequence analysis, and file comparison utilities.

Continue reading Java Program for Longest Common Subsequence (LCS) Problem

Implementation of Graph Coloring Algorithm in Java

Graph Coloring is the problem of assigning colours to the vertices of an undirected graph such that no two adjacent vertices (vertices connected by an edge) share the same colour. The minimum number of colours needed is called the chromatic number of the graph. Graph colouring has real-world applications in scheduling, register allocation, and map colouring. This post implements the m-colouring backtracking algorithm, which finds all valid colourings of a graph using at most m colours.

Continue reading Implementation of Graph Coloring 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

Program for Implementation of N-Queen Problem in Java

The N-Queens problem is the challenge of placing N chess queens on an N×N chessboard such that no two queens threaten each other. A valid solution requires that no two queens share the same row, column, or diagonal. It is a classic backtracking problem: we place queens one row at a time, and whenever a placement leads to a conflict, we backtrack and try the next column. For N=4 there are 2 solutions; for N=8 there are 92.

Continue reading Program for Implementation of N-Queen Problem in Java

Implementing 0-1 Knapsack in Java

The 0-1 Knapsack problem is a fundamental problem in combinatorial optimization: given a set of items, each with a weight and a profit, find the subset of items that maximises total profit without exceeding a weight capacity. The “0-1” constraint means you must either take an item entirely or leave it — no fractions allowed. Unlike the Fractional Knapsack, this requires dynamic programming to find the optimal solution, since greedy selection does not guarantee optimality.

Continue reading Implementing 0-1 Knapsack in Java