Algorithms-Practical For Pattern Matching-Brute Force

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Algorithms-1 Practical

Pattern Matching Algorithm in Java:


Pattern searching is an important problem in computer science. When we do search
for a string in notepad/word file or browser or database, pattern searching algorithms
are used to show the search results.
Naive Pattern Searching:
Slide the pattern over text one by one and check for a match. If a match is found,
then slides by 1 again to check for subsequent matches.

Examples:

Input: txt[] = "THIS IS A TEST TEXT"


pat[] = "TEST"
Output: Pattern found at index 10

Input: txt[] = "AABAACAADAABAABA"


pat[] = "AABA"
Output: Pattern found at index 0
Pattern found at index 9
Pattern found at index 12
// Java program for Naive Pattern Searching
public class NaiveSearch {

public static void search(String txt, String pat)


{
int M = pat.length();
int N = txt.length();

/* A loop to slide pat one by one */


for (int i = 0; i <= N - M; i++) {

int j;

/* For current index i, check for pattern


match */
for (j = 0; j < M; j++)
if (txt.charAt(i + j) != pat.charAt(j))
break;

if (j == M) // if pat[0...M-1] = txt[i, i+1, ...i+M-1]


System.out.println("Pattern found at index " + i);
}
}

public static void main(String[] args)


{
String txt = "AABAACAADAABAAABAA";
String pat = "AABA";
search(txt, pat);
}
}

Output:

Pattern found at index 0


Pattern found at index 9
Pattern found at index 13

Travelling Sales Man Problem Java Implementation:

Travelling Salesman Problem implementation using BackTracking


Travelling Salesman Problem (TSP): Given a set of cities and distance between every pair of
cities, the problem is to find the shortest possible route that visits every city exactly once
and returns back to the starting point.
Note the difference between Hamiltonian Cycle and TSP. The Hamiltoninan cycle problem is
to find if there exist a tour that visits every city exactly once. Here we know that
Hamiltonian Tour exists (because the graph is complete) and in fact many such tours exist,
the problem is to find a minimum weight Hamiltonian Cycle.
For example, consider the graph shown in the figure. A TSP tour in the graph is 1 -> 2 -> 4 ->
3 -> 1. The cost of the tour is 10 + 25 + 30 + 15 which is 80.
The problem is a famous NP hard problem. There is no polynomial time know solution for
this problem.

// Java implementation of the approach


class GFG
{

// Function to find the minimum weight


// Hamiltonian Cycle
static int tsp(int[][] graph, boolean[] v,
int currPos, int n,
int count, int cost, int ans)
{

// If last node is reached and it has a link


// to the starting node i.e the source then
// keep the minimum value out of the total cost
// of traversal and "ans"
// Finally return to check for more possible values
if (count == n && graph[currPos][0] > 0)
{
ans = Math.min(ans, cost + graph[currPos][0]);
return ans;
}

// BACKTRACKING STEP
// Loop to traverse the adjacency list
// of currPos node and increasing the count
// by 1 and cost by graph[currPos,i] value
for (int i = 0; i < n; i++)
{
if (v[i] == false && graph[currPos][i] > 0)
{

// Mark as visited
v[i] = true;
ans = tsp(graph, v, i, n, count + 1,
cost + graph[currPos][i], ans);

// Mark ith node as unvisited


v[i] = false;
}
}
return ans;
}

// Driver code
public static void main(String[] args)
{

// n is the number of nodes i.e. V


int n = 4;

int[][] graph = {{0, 10, 15, 20},


{10, 0, 35, 25},
{15, 35, 0, 30},
{20, 25, 30, 0}};

// Boolean array to check if a node


// has been visited or not
boolean[] v = new boolean[n];

// Mark 0th node as visited


v[0] = true;
int ans = Integer.MAX_VALUE;

// Find the minimum weight Hamiltonian Cycle


ans = tsp(graph, v, 0, n, 1, 0, ans);

// ans is the minimum weight Hamiltonian Cycle


System.out.println(ans);
}
}

You might also like