Ada Ans
Ada Ans
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.
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.
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.
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.
• Big-O notation
• Omega notation
• Theta notation
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.
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.
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.
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.
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:
Equation:
• 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:
• 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.
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.
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.
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
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.
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.
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)