0% found this document useful (0 votes)
66 views42 pages

Ada Ans

An algorithm is a step-by-step procedure to solve a problem within a finite amount of time and space. All algorithms must have input, output, definiteness, and finiteness. An algorithm is considered better if it is more correct and efficient, taking the least time and memory to solve the problem correctly. Asymptotic notation like Big-O is used to describe an algorithm's running time as the input size increases and helps analyze best, average, and worst-case scenarios.

Uploaded by

Nirav Parikh
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)
66 views42 pages

Ada Ans

An algorithm is a step-by-step procedure to solve a problem within a finite amount of time and space. All algorithms must have input, output, definiteness, and finiteness. An algorithm is considered better if it is more correct and efficient, taking the least time and memory to solve the problem correctly. Asymptotic notation like Big-O is used to describe an algorithm's running time as the input size increases and helps analyze best, average, and worst-case scenarios.

Uploaded by

Nirav Parikh
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/ 42

1. What is an algorithm? What do you mean by correct algorithm?

What do you mean by instance of a problem? List out the criteria that
all algorithms must satisfy. On what bases will you consider algorithm
A is better than algorithm B?

What is an algorithm?
An algorithm is an effective, efficient and best method which can be used to
express solution of any problem within a finite amount of space and timeand in a
well-defined formal language. Starting from an initial state the instructions describe a
process or computational process that, when executed, proceeds through a
finitenumber of well-defined successive states, eventually producing "output"and
terminating at a final ending state.

In other words, we can say that,

1. Step by step procedure for solving any problem is known as algorithm.


2. An algorithm is a finite set of instructions that, if followed, accomplishes a
particular task.
3. An algorithm is a sequence of computational steps that transform the input into
a valuable or required output.
4. Any special method of solving a certain kind of problem is known as algorithm.

All Algorithms must satisfy the following criteria -

5. 1) Input
There are more quantities that are extremely supplied.
6. 2) Output
At least one quantity is produced.
7. 3) Definiteness
Each instruction of the algorithm should be clear and unambiguous.
8. 4) Finiteness
The process should be terminated after a finite number of steps.
9. 5) Effectiveness
Every instruction must be basic enough to be carried out theoretically or by
using paper and pencil.

Properties of Algorithm
Simply writing the sequence of instructions as an algorithm is not sufficient to
accomplish certain task. It is necessary to have following properties associated with an
algorithm.

10. Non Ambiguity


Each step in an algorithm should be non-ambiguous. That means each
instruction should be clear and precise. The instruction in any algorithm
should not denote any conflicting meaning. This property also indicates the
effectiveness of algorithm.
11. Range of Input
The range of input should be specified. This is because normally the
algorithm is input driven and if the range of input is not being specified
then algorithm can go in an infinite state.
12. Multiplicity
The same algorithm can be represented into several different ways. That
means we can write in simple English the sequence of instruction or we can
write it in form of pseudo code. Similarly, for solving the same problem we
can write several different algorithms.
13. Speed
The algorithmis written using some specified ideas. Bus such algorithm
should be efficient and should produce the output with fast speed.
14. Finiteness
The algorithm should be finite. That means after performing required
operations it should be terminate.
Correctness. • An algorithm is correct only if it produces correct result for all input
instances. – If the algorithm gives an incorrect answer for one or more input instances, it
is an incorrect algorithm.
A specific selection of values for the parameters is called an instance of the
problem. For example, the input parameter to a sorting function might be an array of
integers. A particular array of integers, with a given size and specific values for each
position in the array, would be an instance of the sorting problem.
What makes an algorithm better than another?

Created by Pamela Fox. A good algorithm is correct, but a great algorithm is both
correct and efficient. The most efficient algorithm is one that takes the least amount of
execution time and memory usage possible while still yielding a correct answer.

2.What is an Algorithm? What do you mean by linear inequalities and


linear equations? Explain asymptotic notation with the help of
example.
Linear equations
The various types of linear equations and the various strategies to solve them are dealt with at
length in the module Linear equations (Years 7–8) , and so we will only quickly revise some
of these ideas here via two examples. The basic rule throughout is that whatever you do to one
side of the equation you must also do to the other.

Linear inequalities
A linear inequality resembles in form an equation, but with the equal sign replaced by an
inequality symbol. The solution of a linear inequality is generally a range of values, rather than
one specific value. Such inequalities arise naturally in problems involving words such as 'at least'
or 'at most'.
To solve an inequality we use the same procedures as we used when solving linear equations,
with the modification that when an inequality is multiplied or divided by a negative number, the
inequality is reversed.
A linear inequality resembles in form an equation, but with the equal sign
replaced by an inequality symbol. The solution of a linear inequality is generally a
range of values, rather than one specific value. Such inequalities arise naturally in
problems involving words such as 'at least' or 'at most'.

Asymptotic Notations
Asymptotic Notation is used to describe the running time of an algorithm - how
much time an algorithm takes with a given input, n. There are three different notations:
big O, big Theta (Θ), and big Omega (Ω).

Asymptotic notations are the mathematical notations used to describe the running
time of an algorithm when the input tends towards a particular value or a limiting
value.

For example: In bubble sort, when the input array is already sorted, the time taken
by the algorithm is linear i.e. the best case.

But, when the input array is in reverse condition, the algorithm takes the maximum
time (quadratic) to sort the elements i.e. the worst case.

When the input array is neither sorted nor in reverse order, then it takes average
time. These durations are denoted using asymptotic notations.

There are mainly three asymptotic notations:

• Big-O notation
• Omega notation
• Theta notation

3.Why do we use asymptotic notations in the study of Algorithms?


Briefly describe the commonly used asymptotic Notations
Asymptotic Notations, also known as 'algorithm’s growth rate' are languages which
allow analyzing the running time of an algorithm. This is calculated by identifying its
behavior as the input size for the algorithm changes.

Asymptotic analysis of an algorithm refers to defining the mathematical boundation


or the framing of its run-time performance.

By using asymptotic analysis, the best case, average case, and worst case scenario of
an algorithm can be concluded.

Big O
Big O, commonly written as OO(n), is an asymptotic notation for the worstworst
casecase. It provides an upper bound for the runtime of an algorithm. OO(n) is
useful when you only have an upper bound on the time complexity of an algorithm.
Since you can easily find an upper bound just by looking at an algorithm, OO(n) is the
most commonly used amongst the three notations.

Big Theta
Big Theta, commonly written as \ThetaΘ(n), is an asymptotic notation for the
averageaverage casecase. It provides a tight bound for the runtime of an algorithm.
\ThetaΘ(n), bounds a function from above and below, so it defines the exact
runtime behavior.
Big Omega
Big Omega, commonly written as \OmegaΩ(n), is an asymptotic notation for the
bestbest casecase. It provides you with a lower bound for the runtime of an
algorithm. \OmegaΩ(n) notation can be useful when you have a lower bound on the
time complexity of an algorithm. Omega notation is the least used notation among
all three since best case performance of an algorithm is generally not useful.

4.What is an amortized analysis? Explain accounting method and


aggregate analysis with suitable example.
Amortize Analysis
This analysis is used when the occasional operation is very slow, but most of the
operations which are executing very frequently are faster. Data structures we need
amortized analysis for Hash Tables, Disjoint Sets etc.

In the Hash-table, the most of the time the searching time complexity is O(1), but
sometimes it executes O(n) operations. When we want to search or insert an element in
a hash table for most of the cases it is constant time taking the task, but when a collision
occurs, it needs O(n) times operations for collision resolution.

Aggregate Method
The aggregate method is used to find the total cost. If we want to add a bunch of data,
then we need to find the amortized cost by this formula.

For a sequence of n operations, the cost is −

Example on Amortized Analysis


For a dynamic array, items can be inserted at a given index in O(1) time. But if that index
is not present in the array, it fails to perform the task in constant time. For that case, it
initially doubles the size of the array then inserts the element if the index is present.
5.Explain following terms with example.

1. Set

2. Relation
3. Function
6.Define following terms
Quantifier:
Quantifier is used to quantify the variable of predicates. It
contains a formula, which is a type of statement whose truth
value may depend on values of some variables. When we
assign a fixed value to a predicate, then it becomes a
proposition.
In logic, a quantifier is a language element that helps in generation of a
quantification, which is a construct that mentions the number of specimens in the
given domain of discourse satisfying a given open formula. Quantifiers are largely used
in logic, natural languages and discrete mathematics.

What is Time complexity:


Time complexity is defined in terms of how many times it takes
to run a given algorithm, based on the length of the input. Time
complexity is not a measurement of how much time it takes to
execute a particular algorithm because such factors as
programming language, operating system, and processing power
are also considered.
Time complexity is a type of computational complexity that describes the time required
to execute an algorithm. The time complexity of an algorithm is the amount of time it
takes for each statement to complete. As a result, it is highly dependent on the size of
the processed data. It also aids in defining an algorithm's effectiveness and evaluating
its performance.

Space Complexity:
Space complexity refers to the total amount of memory space
used by an algorithm/program, including the space of input
values for execution. Calculate the space occupied by variables
in an algorithm/program to determine space complexity.
However, people frequently confuse Space-complexity with
auxiliary space. Auxiliary space is simply extra or temporary
space, and it is not the same as space complexity. To put it
another way,
Auxiliary space + space use by input values = Space Complexity
The best algorithm/program should have a low level of space
complexity. The less space required, the faster it executes .
When an algorithm is run on a computer, it necessitates a certain amount of memory
space. The amount of memory used by a program to execute it is represented by its
space complexity. Because a program requires memory to store input data and
temporal values while running, the space complexity is auxiliary and input space.

What is an algorithm:
An algorithm is a procedure used for solving a problem or performing
computation. Algorithms act as an exact list of instructions that conduct
specified actions step by step in either hardware- or software-based routines.
Algorithms are widely used throughout all areas of IT. In mathematics and
computer science, an algorithm usually refers to a small procedure that solves
a recurrent problem. Algorithms are also used as specifications for performing
data processing and play a major role in automated systems.
An algorithm could be used for sorting sets of numbers or for more
complicated tasks, like recommending user content on social media.
Algorithms typically start with initial input and instructions that describe a
specific computation. When the computation is executed, the process
produces an output.

7. Explain why analysis of algorithm is important? Explain:


Worst case, Best case, Average case complexity.
Algorithm Analysis:
Algorithm analysis is an important part of computational complexity theory,
which provides theoretical estimation for the required resources of an
algorithm to solve a specific computational problem. Analysis of algorithms is
the determination of the amount of time and space resources required to
execute it.
Why Analysis of Algorithms is important:
To predict the behavior of an algorithm without implementing it on a specific
computer. It is much more convenient to have simple measures for the
efficiency of an algorithm than to implement the algorithm and test the
efficiency every time a certain parameter in the underlying computer system
changes. It is impossible to predict the exact behavior of an algorithm. There
are too many influencing factors. The analysis is thus only an approximation; it
is not perfect. More importantly, by analyzing different algorithms, we can
compare them to determine the best one for our purpose.
Worst Case Analysis:
In the worst-case analysis, we calculate the upper limit of the
execution time of an algorithm. It is necessary to know the case
which causes the execution of the maximum number of
operations.
For linear search, the worst case occurs when the element to
search for is not present in the array. When x is not present, the
search () function compares it with all the elements of arr [] one
by one. Therefore, the temporal complexity of the worst case of
linear search would be Θ (n).
Average Case Analysis:
In the average case analysis, we take all possible inputs and
calculate the computation time for all inputs. Add up all the
calculated values and divide the sum by the total number of
entries.
We need to predict the distribution of cases. For the linear
search problem, assume that all cases are uniformly distributed.
So we add all the cases and divide the sum by (n + 1).
Best Case Analysis:
In the best case analysis, we calculate the lower bound of the
execution time of an algorithm. It is necessary to know the case
which causes the execution of the minimum number of
operations. In the linear search problem, the best case occurs
when x is present at the first location.
The number of operations in the best case is constant. The
best-case time complexity would therefore be Θ (1) Most of the
time, we perform worst-case analysis to analyze algorithms. In
the worst analysis, we guarantee an upper bound on the
execution time of an algorithm which is good information.
8. Write a program/algorithm of selection sort methods. What
is complexity of the method?
The selection sort algorithm sorts an array by repeatedly finding the minimum
element (considering ascending order) from the unsorted part and putting it at
the beginning.

The algorithm maintains two subarrays in a given array.

The subarray which already sorted.


The remaining subarray was unsorted.

// Java program for implementation of Selection Sort


class SelectionSort
{
void sort(int arr[])
{
int n = arr.length;

// One by one move boundary of unsorted subarray


for (int i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
int min_idx = i;
for (int j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
// Swap the found minimum element with the first
// element
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}

// Prints the array


void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i]+" ");
System.out.println();
}

// Driver code to test above


public static void main(String args[])
{
SelectionSort ob = new SelectionSort();
int arr[] = {64,25,12,22,11};
ob.sort(arr);
System.out.println("Sorted array");
ob.printArray(arr);
}
}
/* This code is contributed by Rajat Mishra*/
Output
Sorted array:
11 12 22 25 64
Complexity Analysis of Selection Sort:
Time Complexity: The time complexity of Selection Sort is O(N2) as there are
two nested loops:

One loop to select an element of Array one by one = O(N)


Another loop to compare that element with every other Array element = O(N)
Therefore overall complexity = O(N) * O(N) = O(N*N) = O(N2)

9. What is Recursion? Give the implementation of Tower of


Hanoi Problem using recursion.
What is a recursion ?

Recursion is the technique of making a function call itself. This technique provides a
way to break complicated problems down into simple problems which are easier to
solve. Recursion may be a bit difficult to understand.

Tower of Hanoi is a mathematical puzzle where we have three rods (A, B, and C) and N
disks. Initially, all the disks are stacked in decreasing value of diameter i.e., the smallest
disk is placed on the top and they are on rod A. The objective of the puzzle is to move
the entire stack to another rod (here considered C), obeying the following simple rules:

Only one disk can be moved at a time.


Each move consists of taking the upper disk from one of the stacks and placing it on top
of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack.

No disk may be placed on top of a smaller disk.

Equation:

9.“What is the smallest value of n such that an algorithm


whose running time is 100n2 runs faster than an algorithm
whose running time is 2n on the same machine?
Explanation:
For inputs of size n, running time of algorithm A is 100n2 and of B is 2n. For A
to run faster than B, 100n2 must be smaller than 2n.
Calculate: A (quadratic time complexity) will run much faster than B
(exponential time complexity) for very large values of n. Let’s start checking
from n=1 and go up for values of n which are power of 2.
n=1⇒ 100×1^2 = 100 > 2^1
n=2⇒ 100×2^2 = 400 > 2^2
n=4⇒ 100×4^2 = 1600 > 2^4
n=8⇒ 100×8^2 = 6400 > 2^8
n=16⇒ 100×16^2 = 25600 < 2^16
Somewhere between 8 and 16, A starts to run faster than B. Let’s do what we
were doing but now we are going to try middle value of the range, repeatedly
(binary search).
n=8+16/ 2 = 12⇒100×12^2=14400 > 2^12
n=12+16/ 2=14⇒100×14^2=19600 > 2^14
n=14+16/ 2=15⇒100×15^2=22500 < 2^15
So, at n=15, A starts to run faster than B.
10.Differentiate the following: 1. Divide and conquer &
Dynamic Programming 2. Greedy Algorithm & Dynamic
Programming

11. What do you mean by Divide & Conquer approach? List


advantages and disadvantages of it

Divide and Conquer Introduction


Divide and Conquer is an algorithmic pattern. In algorithmic methods, the design is to take a
dispute on a huge input, break the input into minor pieces, decide the problem on each of the
small pieces, and then merge the piecewise solutions into a global solution. This mechanism of
solving the problem is called the Divide & Conquer Strategy.
Divide and Conquer algorithm consists of a dispute using the following three steps.

15. Divide the original problem into a set of subproblems.


16. Conquer: Solve every subproblem individually, recursively.
17. Combine: Put together the solutions of the subproblems to get the solution to the whole
problem

Advantages of Divide and Conquer


• Divide and Conquer tend to successfully solve one of the biggest problems, such as the
Tower of Hanoi, a mathematical puzzle. It is challenging to solve complicated problems
for which you have no basic idea, but with the help of the divide and conquer approach,
it has lessened the effort as it works on dividing the main problem into two halves and
then solve them recursively. This algorithm is much faster than other algorithms.
• It efficiently uses cache memory without occupying much space because it solves simple
subproblems within the cache memory instead of accessing the slower main memory.
• It is more proficient than that of its counterpart Brute Force technique.
• Since these algorithms inhibit parallelism, it does not involve any modification and is
handled by systems incorporating parallel processing.

Disadvantages of Divide and Conquer


• Since most of its algorithms are designed by incorporating recursion, so it necessitates
high memory management.
• An explicit stack may overuse the space.
• It may even crash the system if the recursion is performed rigorously greater than the
stack present in the CPU.

12.Explain with example how games can be formulated using graphs?

13. Define: Acyclic Directed Graph, Articulation Point, Dense Graph,


Preconditioning, Sparse Graph , Graph, Tree
A directed acyclic graph (DAG) is a conceptual representation of a
series of activities. The order of the activities is depicted by a graph,
which is visually presented as a set of circles, each one representing an
activity, some of which are connected by lines, which represent the flow
from one activity to another.
A vertex in an undirected connected graph is an articulation point (or
cut vertex) if removing it (and edges through it) disconnects the
graph. Articulation points represent vulnerabilities in a connected network
– single points whose failure would split the network into 2 or more
components.
14.Explain Breadth First Traversal Method for Graph with
algorithm
Breadth First Search (BFS) algorithm traverses a graph in a breadthward motion and
uses a queue to remember to get the next vertex to start a search, when a dead end
occurs in any iteration.
15.Explain Depth First Traversal Method for Graph with algorithm
Depth First Search (DFS) algorithm traverses a graph in a depthward motion and uses
a stack to remember to get the next vertex to start a search, when a dead end occurs in
any iteration.
16.Differentiate BFS and DFS
17.Explain Backtracking Method. What is N-QueensProblem? Give
solution of 4-Queens Problem using Backtracking Method.
Backtracking is a technique based on algorithm to solve problem. It
uses recursive calling to find the solution by building a solution step by
step increasing values with time. It removes the solutions that doesn't
give rise to the solution of the problem based on the constraints given
to solve the problem.

Define backtracking. State types of constraints used in


backtracking.
Many problems can be solved by backtracking strategy, and that problems satisfy
complex set of constraints, and these constraints are of two types:

• Implicit constraint: It is a rule in which how each element in a tuple is related.


• Explicit constraint: The rules that restrict each element to be chosen from the
given set.
N-Queens Problem: A classic combinational problem is to place n queens
on a n*n chess board so that no two attack, i.,e no two queens are on the
same row, column or diagonal. If we take n=4then the problem is called 4
queens problem. If we take n=8 then the problem is called as 8 queens
problem.
18.Differentiate branch and bound and back tracking algorithm
19. Draw the state space tree Diagram for 4 Queen problem
What are the basic ideas behind string matching? Which
are the two popular algorithms for string matching?
Explain and analyze any one in brief.
String matching algorithms have greatly influenced computer science and play an essential
role in various real-world problems. It helps in performing time-efficient tasks in multiple
domains. These algorithms are useful in the case of searching a string within another string.
String matching is also used in the Database schema, Network systems.
Let us look at a few string matching algorithms before proceeding to their applications in
real world. String Matching Algorithms can broadly be classified into two types of
algorithms –
18. Exact String Matching Algorithms
19. Approximate String Matching Algorithms
Exact String Matching Algorithms:
Exact string matching algorithms is to find one, several, or all occurrences of a defined
string (pattern) in a large string (text or sequences) such that each matching is perfect. All
alphabets of patterns must be matched to corresponding matched subsequence. These are
further classified into four categories:
20. Algorithms based on character comparison:
o Naive Algorithm: It slides 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.
o KMP (Knuth Morris Pratt) Algorithm: The idea is whenever a mismatch is
detected, we already know some of the characters in the text of the next
window. So, we take advantage of this information to avoid matching the
characters that we know will anyway match.
o Boyer Moore Algorithm: This algorithm uses best heurestics of Naive and
KMP algorithm and starts matching from the last character of the pattern.
o Using the Trie data structure: It is used as an efficient information retrieval
data structure. It stores the keys in form of a balanced BST.
21. Deterministic Finite Automaton (DFA) method:
o Automaton Matcher Algorithm: It starts from the first state of the automata
and the first character of the text. At every step, it considers next character of
text, and look for the next state in the built finite automata and move to a
new state.
22. Algorithms based on Bit (parallelism method):
o Aho-Corasick Algorithm: It finds all words in O(n + m + z) time where n is the
length of text and m be the total number characters in all words and z is total
number of occurrences of words in text. This algorithm forms the basis of the
original Unix command fgrep.
23. Hashing-string matching algorithms:
o Rabin Karp Algorithm: It matches the hash value of the pattern with the hash
value of current substring of text, and if the hash values match then only it
starts matching individual characters.
Approximate String Matching Algorithms:
Approximate String Matching Algorithms (also known as Fuzzy String Searching) searches
for substrings of the input string. More specifically, the approximate string matching
approach is stated as follows: Suppose that we are given two strings, text T[1…n] and
pattern P[1…m]. The task is to find all the occurrences of patterns in the text whose edit
distance to the pattern is at most k. Some well known edit distances are – Levenshtein edit
distance and Hamming edit distance.
These techniques are used when the quality of the text is low, there are spelling errors in
the pattern or text, finding DNA subsequences after mutation, heterogeneous databases,
etc. Some approximate string matching algorithms are:

• Naive Approach: It slides the pattern over text one by one and check for
approximate matches. If they are found, then slides by 1 again to check for
subsequent approximate matches.
• Sellers Algorithm (Dynamic Programming)
• Shift or Algorithm (Bitmap Algorithm)
Applications of String Matching Algorithms:

• Plagiarism Detection: The documents to be compared are decomposed into string


tokens and compared using string matching algorithms. Thus, these algorithms are
used to detect similarities between them and declare if the work is plagiarized or
original.

• Bioinformatics and DNA Sequencing: Bioinformatics involves applying information


technology and computer science to problems involving genetic sequences to find
DNA patterns. String matching algorithms and DNA analysis are both collectively
used for finding the occurrence of the pattern set.

• Digital Forensics: String matching algorithms are used to locate specific text strings
of interest in the digital forensic text, which are useful for the investigation.
• Spelling Checker: Trie is built based on a predefined set of patterns. Then, this trie is
used for string matching. The text is taken as input, and if any such pattern occurs, it
is shown by reaching the acceptance state.

• Spam filters: Spam filters use string matching to discard the spam. For example, to
categorize an email as spam or not, suspected spam keywords are searched in the
content of the email by string matching algorithms. Hence, the content is classified
as spam or not.

• Search engines or content search in large databases: To categorize and organize data
efficiently, string matching algorithms are used. Categorization is done based on the
search keywords. Thus, string matching algorithms make it easier for one to find the
information they are searching for.

• Intrusion Detection System: The data packets containing intrusion-related keywords


are found by applying string matching algorithms. All the malicious code is stored in
the database, and every incoming data is compared with stored data. If a match is
found, then the alarm is generated. It is based on exact string matching algorithms
where each intruded packet must be detected.

20.Explain Rabin Karp method of string matching.


Rabin-Karp algorithm is an algorithm used for searching/matching patterns in the
text using a hash function. Unlike Naive string matching algorithm, it does not travel
through every character in the initial phase rather it filters the characters that do not
match and then performs the comparison.

A hash function is a tool to map a larger input value to a smaller output value. This
output value is called the hash value.
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
How Rabin-Karp Algorithm Works?
A sequence of characters is taken and checked for the possibility of the presence of
the required string. If the possibility is found then, character matching is performed.

Let us understand the algorithm with the following steps:

24. Let the text be:


Text
And the string to be searched in the above text be:
Pattern
25. Let us assign a numerical value(v)/weight for the characters we will be using in
the problem. Here, we have taken first ten alphabets only (i.e. A to J).
Text
Weights
26. n be the length of the pattern and m be the length of the text. Here, m = 10 and
n = 3.
Let d be the number of characters in the input set. Here, we have taken input
set {A, B, C, ..., J}. So, d = 10. You can assume any suitable value for d.
27. Let us calculate the hash value of the pattern. Hash
value of text
hash value for pattern(p) = Σ(v * dm-1) mod 13
= ((3 * 102) + (4 * 101) + (4 * 100)) mod 13
= 344 mod 13
= 6

In the calculation above, choose a prime number (here, 13) in such a way that we
can perform all the calculations with single-precision arithmetic.

The reason for calculating the modulus is given below.

28. Calculate the hash value for the text-window of size m.


For the first window ABC,
hash value for text(t) = Σ(v * dn-1) mod 13
= ((1 * 102) + (2 * 101) + (3 * 100)) mod 13
= 123 mod 13
= 6

29. Compare the hash value of the pattern with the hash value of the text. If they
match then, character-matching is performed.
In the above examples, the hash value of the first window (i.e. t) matches with
p so, go for character matching between ABC and CDD. Since they do not

match so, go for the next window.


30. We calculate the hash value of the next window by subtracting the first term
and adding the next term as shown below.
t = ((1 * 102) + ((2 * 101) + (3 * 100)) * 10 + (3 * 100)) mod 13
= 233 mod 13
= 12

In order to optimize this process, we make use of the previous hash value in the
following way.
t = ((d * (t - v[character to be removed] * h) + v[character to be added] ) mod
13
= ((10 * (6 - 1 * 9) + 3 )mod 13
= 12
Where, h = dm-1 = 103-1 = 100.

31. For BCC, t = 12 (≠6). Therefore, go for the next window.


After a few searches, we will get the match for the window CDA in the text.
Hash
value of different windows

21. Explain KMP string matching methods.


22.Explain naïve string matching algorithm with example.
Example:
32. Suppose T = 1011101110
33. P = 111
34. Find all the Valid Shift

23.Explain finite automata for string matching with example


24.Define P, NP, NP complete and NP-Hard problems. Give examples
of each.
25.Explain Traveling salesman problem with example.

Traveling-salesman Problem
In the traveling salesman Problem, a salesman must visits n cities. We can say that salesman
wishes to make a tour or Hamiltonian cycle, visiting each city exactly once and finishing at the
city he starts from. There is a non-negative cost c (i, j) to travel from the city i to city j. The goal is
to find a tour of minimum cost. We assume that every two cities are connected. Such problems
are called Traveling-salesman problem (TSP).
We can model the cities as a complete graph of n vertices, where each vertex represents a city.
It can be shown that TSP is NPC.
If we assume the cost function c satisfies the triangle inequality, then we can use the following
approximate algorithm.

Triangle inequality
Let u, v, w be any three vertices, we have

One important observation to develop an approximate solution is if we remove an edge from H*,
the tour becomes a spanning tree.

35. Approx-TSP (G= (V, E))


36. {
37. 1. Compute a MST T of G;
38. 2. Select any vertex r is the root of the tree;
39. 3. Let L be the list of vertices visited in a preorder tree walk of T;
40. 4. Return the Hamiltonian cycle H that visits the vertices in the order L;
41. }
Traveling-salesman Problem

Intuitively, Approx-TSP first makes a full walk of MST T, which visits each edge exactly two times.
To create a Hamiltonian cycle from the full walk, it bypasses some vertices (which corresponds
to making a shortcut)

26.What do you mean by polynomial reduction?


A polynomial-time Turing reduction from a problem A to a problem B is an algorithm
that solves problem A using a polynomial number of calls to a subroutine for
problem B, and polynomial time outside of those subroutine calls. Polynomial-time
Turing reductions are also known as Cook reductions, named after Stephen Cook.

You might also like