Ao A
Ao A
Give
examples.
Ans:- The KMP algorithm is used to solve the pattern matching problem
which is a task of finding all the occurrences of a given pattern in a text. It
is very useful when it comes to finding multiple patterns. For instance, if
the text is "aabbaaccaabbaadde" and the pattern is "aabaa", then the
pattern occurs twice in the text, at indices 0 and 8.
The naive solution to this problem is to compare the pattern with every
possible substring of the text, starting from the leftmost position and
moving rightwards. This takes O(n*m) time, where 'n' is the length of the
text and 'm' is the length of the pattern.
When we work with long text documents, the brute force and naive
approaches may result in redundant comparisons. To avoid such
redundancy, Knuth, Morris, and Pratt developed a linear sequence-
matching algorithm named the KMP pattern matching algorithm. It is
also referred to as Knuth Morris Pratt pattern matching algorithm.
Example :-
Input:
main String: "AAAABCAAAABCBAAAABC"
pattern: "AAABC"
Output:
Pattern found at position: 1
Pattern found at position: 7
Pattern found at position: 14
Q ] Write note on : Travelling Salesperson Problem ?
Solutions :
➢ Start at A.
➢ Go to B: Distance = 5 miles.
➢ Go to C: Distance = 6 miles (from B).
➢ Go to D: Distance = 4 miles (from C).
➢ Return to A: Distance = 7 miles (from D).
Total distance = 5 + 6 + 4 + 7 = 22 miles.
Q ] Explain how branch and bound strategy can be used in the 15 – Puzzle
problem ?
The straightforward way for solving this puzzle is by searching the state
space for the final arrangement and using the way from the initial state to
the goal state.
We can easily see there are 16! (i.e.,. 20.9*1⁰¹²) a different way of
arrangements. But here the truth is only half of them are reachable from
the given random initial state. Here state space of the problem is big. So,
we must determine whether the goal state is reachable from the initial
state before attempting the search in the state space. We denote
positions for each tile in the problem from 1 to 16. If position i is the
initial state then position 16 is the goal state or the empty spot.
Q ] Compare backtracking Vs branch and bound ?
Q ] Write a note on : Graph coloring ?
Ans:- Graph Coloring The intuitive idea behind the colouring of a graph is
simply paint its vertices such that no two adjacent vertices have same color.
What is the minimum number of colors necessary? This constitutes a
coloring problem. After the vertices have been painted, they can be
categorized into the groups or its; for example, one set having all the blue
colors, another of red and so forth. This is a partitioning problem. The
colouring and partitioning can either be carried out on vertices or on edges
of the graph and for planer graphs; these can be performed in the regions.
Colouring and petitioning have several important applications such as;
coding theory, partitioning of logic in digital computer, scheduling and
assignments etc.
Definition 1:-
Definition 2:-
A graph G is said to be k-colourable if each vertex can be assigned one of k
colours so that adjacent vertices get different colours. Here, we are
interested with the proper colouring that requires the minimum number of
colours. The minimum colouring of a graph is generally difficult to compute,
but the following theorem provides an upper bound.
Q ] Write an algorithm of the sum of subsets with example?
Ans:- Algorithm:
• Let n be the number of elements and let m be the required sum.
• Let w[1…..n] be an array of weights in ascending order and Let
x[1….n] be the solution vector.
• Method
• S → existing sum
• w[k] → weight of current element
• w[k+1] → weight of next element
• s+w[k]+w[k+1]<=m → sum of existing element+weight of current
element+weight of next element is less tham m.
• Thus the next element can be selected for time being.
• s+r-w[k]>=m → Even if u discard the current element, you may still
get the required sum later on.
• s+w[k+1]<=m → The sum of selected element + weight of next
element is less than m . This is even if u take the next element the
sum will not exit the required sum(m)
• Thus next element can be selected.
Problem:
n = 5, W = {2, 7, 8, 9, 15} M = 17
3. Try all rows in the current column. For each row, if the queen can be
placed safely in this row, then mark this cell and try to solve the
problem recursively for the rest of the columns.
4. If placing the queen in the current row and proceeding to solve the
rest of the problem leads to a solution, then return true.
6. If all rows have been tried and none worked, return false to trigger
backtracking.
Example Solution
One of the possible solutions to the 8 Queens Problem is as follows:
Here, “Q” represents a queen, and “.” represents an empty square. In this
arrangement, no two queens threaten each other.
There are 92 distinct solutions to the 8 Queens Problem, but they can be
reduced to 12 unique solutions by considering rotations and reflections.
Q ] Explain the longest common subsequence with example ?
If S1 = {B, C, D, A, A, C, D}
If S1 = {B, C, D, A, A, C, D}
S2 = {A, C, D, B, A, C}
Here, common subsequences are {B, C}, {C, D, A, C}, {D, A, C}, {A, A, C}, {A,
C}, {C, D}, ...