0% found this document useful (0 votes)
11 views10 pages

Ao A

The document discusses various algorithms and problems in computer science, including the Knuth-Morris-Pratt (KMP) pattern matching algorithm, the Traveling Salesman Problem (TSP), the 15-puzzle problem, graph coloring, and the longest common subsequence (LCS). It provides definitions, examples, and explanations of how these algorithms work, their applications, and comparisons between different strategies like backtracking and branch and bound. Additionally, it outlines the complexities and challenges associated with solving these problems.

Uploaded by

Karuna Sawant
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views10 pages

Ao A

The document discusses various algorithms and problems in computer science, including the Knuth-Morris-Pratt (KMP) pattern matching algorithm, the Traveling Salesman Problem (TSP), the 15-puzzle problem, graph coloring, and the longest common subsequence (LCS). It provides definitions, examples, and explanations of how these algorithms work, their applications, and comparisons between different strategies like backtracking and branch and bound. Additionally, it outlines the complexities and challenges associated with solving these problems.

Uploaded by

Karuna Sawant
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Q ] What is Knuth-Morris-Pratt method of pattern matching ?

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 ?

Ans:- The Traveling Salesman Problem (TSP) is a well known optimization


problem. Due to its ease of use and applicability, this problem has
captivated scholars and practitioners for many years. This well-known
puzzle aims to determine the fastest route between two cities by going
there exactly once and then coming back to the starting point. Reducing
the distance between the beginning and destination cities will help you
lower the overall distance (or expense) traveled. This problem can be
represented mathematically as the permutation of the cities less the total
of their distances. The time required to find the answer grows
exponentially as additional cities are added to the list since TSP is NP-
hard. There are real-world applications for the TSP problem in
manufacturing, logistics, and transportation. Because of this, it's one of
the most researched subjects globally, and numerous algorithms have
been created to address its intricate issues.
Types of Travelling Salesman Problem :-
➢ The Asymmetric Traveling Salesman Problem [5]: The distances
between cities in the Asymmetric Traveling Salesman Problem (ATSP)
might vary depending on your direction of travel. For instance, the
distance between city A and city B may differ from that of city B to city A.
A distance matrix that is asymmetric is used to illustrate this.
➢ The Symmetric Traveling Salesman Problem [5]: The distances between
cities in the Symmetric Traveling Salesman Problem (STSP) remain
constant regardless of the sequence in which you visit them. Therefore,
the distance is the same whether traveling from city A to city B or from B
to city A. This is a symmetric distance matrix since the distances are
displayed in the same manner in both directions.
Example :-
Imagine a delivery driver needs to visit four customers, A, B, C, and D,
located in different parts of a city. The distances between these locations
are:
➢ A to B: 5 miles
➢ A to C: 8 miles
➢ A to D: 7 miles
➢ B to C: 6 miles
➢ B to D: 3 miles
➢ C to D: 4 miles
This information can be represented as a distance matrix:

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 ?

Ans:- In 1878 Sam Loyad has invented a 15-puzzle problem. It consists of


15 numbered tiles on a square frame where the capacity of the frame is
16 tiles. We have given an arrangement of tiles initially which has to be
arranged accordingly to our goals through legal moves. Generally, legal
moves in which a tile adjacent to an empty spot(ES) is moved to ES. (
From the example below, tiles having number 2,3,5,6 i.e., anyone can be
moved to an empty spot(ES). After having this first move other tiles can
arrange in the same way.) Here, each move creates a new arrangement of
the tiles which are called states of the puzzle. The initial arrangement is
considered as the initial state and the final arrangement is considered as
the goal state.

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:-

A colouring of a simple graph is the assignment of a colour to each vertex of


the graph so that no two adjacent vertices are assigned the same colour. A
graph can be coloured by assigning a different colour to each of its vertices.
However, for most graphs, a colouring can be found that uses fewer colours
than the number of vertices in the graph. A colour with colours such that no
two adjacent vertices have the same colour is called properly coloured
graph.

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

Hence, there are three subset with given sum m=17


A(111)
B(10001)
C(0010)
Q ] Analyze the 8-Queen problem using backtracking strategy of problem
solving ?

Ans:- Backtracking Approach

1. Start in the leftmost column.

2. If all queens are placed, then return true.

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.

5. If placing a queen in any row in the current column and proceeding


with this placement does not lead to a solution, then backtrack and
return false.

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 ?

Ans:- The longest common subsequence (LCS) is defined as the longest


subsequence that is common to all the given sequences, provided that
the elements of the subsequence are not required to occupy consecutive
positions within the original sequences.

If S1 and S2 are the two given sequences then, Z is the common


subsequence of S1 and S2 if Z is a subsequence of both S1 and S2.
Furthermore, Z must be a strictly increasing sequence of the indices of
both S1 and S2.

In a strictly increasing sequence, the indices of the elements chosen from


the original sequences must be in ascending order in Z.

If S1 = {B, C, D, A, A, C, D}

Then, {A, D, B} cannot be a subsequence of S1 as the order of the


elements is not the same (ie. not strictly increasing sequence).

Let us understand LCS with an example.

If S1 = {B, C, D, A, A, C, D}

S2 = {A, C, D, B, A, C}

A subsequence is a sequence derived by deleting some or no elements


from the original sequence without changing the order of the remaining
elements. A common subsequence means a subsequence that appears in
both sequences in the same relative order.

Here, common subsequences are {B, C}, {C, D, A, C}, {D, A, C}, {A, A, C}, {A,
C}, {C, D}, ...

Among these subsequences, {C, D, A, C} is the longest common


subsequence. We are going to find this longest common subsequence
using dynamic programming.

Before proceeding further, if you do not already know about dynamic


programming, please go through dynamic programming.

You might also like