Daa Cia 1 Key
Daa Cia 1 Key
Daa Cia 1 Key
ANSWER KEY
PART-A
ANS: The algorithm is defined as a collection of unambiguous instructions occurring in some specific sequence and
such an algorithm should produce output for given set of input in finite amount of time.
ANS: A function f(n) is said to be in Ω (g(n)) if f(n) is bounded below by some positive content multiple of g(n)
such that
3. Show the time complexity, when 2 m*n matrices are added by Using the step count method.
ANS: For computing the step count for the matrix addition, consider the following code –
For ( i 0; i< m; i++);
{
For (jo; j<n; j++)
{
C[i][j] = A[i][j] + B[i][j]
}
}
ANS: The following applications in which the Brute Force method is used –
1. Bubble sort
2. Sequential Search
3. Brute force String matching Algorithm
4. Traveling Sales man problem
5. Knapsack problem
JCT COLLEGE OF ENGINEERING AND TECHNOLOGY
ANS: Suppose that there are n objects from I = 1, 2, 3 …… n. Each object I has some weight Wi and value Vi
associated with each object and capacity of knapsack is W. A thief has to pick up the most valuable objevt o fill the
knapsack to its capacity.
PART-B
.
Decision making
The Decision making is done on the following:
As certaining the Capabilities of the Computational Device
•In random-access Memory (RAM), instructions are executed one after another (The central
assumption is that one operation at a time). Accordingly, algorithms designed to be executed on
such machines are called sequential algorithms.
•In some newer computers, operations are executed concurrently, i.e., in parallel. Algorithms that take
advantage of this capability are called parallel algorithms.
•Choice of computational devices like Processor and memory is mainly based on space and time
efficiency
Choosing between Exact and Approximate Problem Solving
•The next principal decision is to choose between solving the problem exactly or solving it
approximately.
•An algorithm used to solve the problem exactly and produce correct result is called an exact algorithm.
•If the problem is so complex and not able to get exact solution, then we have to choose an algorithm
called an approximation algorithm. i.e., produces an approximate answer. E.g., extracting square roots,
solving nonlinear equations, and evaluating definite integrals.
Pseudocode and flowchart are the two options that are most widely used nowadays for specifying algorithms.
Natural Language
It is very simple and easy to specify an algorithm using natural language. But many times specification of
algorithm by using natural language is not clear and thereby we get brief specification.
Example: An algorithm to perform addition of two numbers.
JCT COLLEGE OF ENGINEERING AND TECHNOLOGY
Such a specification creates difficulty while actually implementing it. Hence many programmers prefer to
have specification of algorithm by means of Pseudocode.
Pseudocode
•Pseudocode is a mixture of a natural language and programming language constructs. Pseudocode is
usually more precise than natural language
•For Assignment operation left arrow “←”, for comments two slashes “//”,if condition, for, while loops are
used.
Flowchart
In the earlier days of computing, the dominant method for specifying algorithms was a flowchart, this
representation technique has proved to be inconvenient.
Flowchart is a graphical representation of an algorithm. It is a a method of expressing an algorithm by a
collection of connected geometric shapes containing descriptions of the algorithm’s steps.
Coding an Algorithm
•The coding / implementation of an algorithm is done by a suitable programming language like C, C++, JAVA.
•The transition from an algorithm to a program can be done either incorrectly or very inefficiently. Implementing
an algorithm correctly is necessary. The Algorithm power should not reduced by inefficient implementation.
•Standard tricks like computing a loop’s invariant (an expression that does not change its value) outside the loop,
collecting common sub expressions, replacing expensive operations by cheap ones, selection of programming
language and so on should be known to the programmer.
•Typically, such improvements can speed up a program only by a constant factor, whereas a better algorithm can
make a difference in running time by orders of magnitude.
•It is very essential to write an optimized code (efficient code) to reduce the burden of compiler.
6(b).Define empirical analysis and list the general plan for Empirical Analysis of Algorithm.
ANS:
Definition : Empirical analysis of algorithm meanbs observing behavior of an algorithm, for certain set of
input.
B). Decide the efficiency metric M. Also decide the measurement unit
The efficiency of an algorithm can be measured by following different methods
Insert a counter in the algorithm in order to count the number of times the basic operation is executed . This
is a straight forward method of counting an efficiency of algorithm
For example : If we write a function for calculating sum of n number in an array then we can find the
efficiency of that function by frequency count.The frequency count is a that denotes how many times the
particular statement is executed
The execution starts from for loop. The delaration part can be neglected. Now
7(a). Solve the following using Brute-Force algorithm:Find whether the given string follows the specified
pattern and return 0 or 1 accordingly. Examples:
1) Pattern: "abba" input: " redblueredblue" should return 12
2) Pattern: "aaaa" , input: " asdasdasdasd" should return 1
3) Pattern: "aabb", input: " xyzabcxyzabc" should return 0
JCT COLLEGE OF ENGINEERING AND TECHNOLOGY
Ans:
The Brute-Force approach of string matching algorithm is very simple and straightforward. According to this
approach, each character of pattern is compared with each corresponding character of text.
In given text/pattern pair 1) If we map 'r' of string "red" with 'a' of pattern and 2) If we map b' of string "blue"
with 'b' of pattern then the algorithm will return 1.
ALGORITHM
int Brute Force Algo(char t[14],char p[4], int n) {
//array t[] contains "redbluebluered"
//array pl] contains "abba" //n
represents length of text t[]
int i,j,flag=1;
i=0;
j=0;
while(j<n)
{
if((t[j]=='')&&(p[i]=='a'))//map ' of "red" to 'a'
{
i=i+1;
j-j+3;
} else if((t[j]=='b') && (p[i]=='b'))//map 'b' of "blue" to 'b' {
i=i+1
j-j+4;
}
else
{ flag=0;
j++;
}
}
retum flag;
}
Step 1:
0 1 2 3 4 5 6 7 8 9 10 11 12 13
r e d b l u e b l u e r e d Length n
↕
a b b a
P[ ]
Step 2:
0 1 2 3 4 5 6 7 8 9 10 11 12 13
r e d b l u e b l u e r e d
↕
a b b a
JCT COLLEGE OF ENGINEERING AND TECHNOLOGY
Step 3:
0 1 2 3 4 5 6 7 8 9 10 11 12 13
r e d b l u e b l u e r e d
↕
a b b a
Step 4:
0 1 2 3 4 5 6 7 8 9 10 11 12 13
r e d b l u e b l u e r e d
↕ return 1
a b b a
We will map ‘x’ of “xyz” nstring with “a” and “abc” string with “b”. The Algorithm will be
int I,j,flag=1;
i=0;
j=0;
while(j<n)
{
If((t[j]==’x’&&(p[i]==’a’))//map ‘x’ to “xyz” to “a”
{
i=i+1;
j=j+3;
}
else if((t[j]==’a’)&&(p[i]==’b’)//map ‘a’ of ‘abc’ to ‘a’
{
i=i+1;
j=j+3;
}
Else
{
Flag=0;
J++;
}
}
Return flag;
}
7(b). Find and analyze the optimal solution for the assignment problem given below
Job 1 job 2 Job 3 Job 4
Person 1 9 2 7 8
Person 2 6 4 3 7
Person 3 5 8 1 8
Person 4 7 6 9 4
Ans:
It seems like you've listed numbers 1, 2, 3, and 4. Are you looking for different combinations of these
numbers? If so, I can generate various combinations for you. Here they are:
From all the above feasible solutions, clearly the solution <2,1,3,4> is optional gives minimum cost =
2+6+1+4 = 13
PART-C
Sure, let's discuss in detail the three primary asymptotic notations: Big O, Big Omega, and Big Theta.
Definition: Big O notation is used to describe the upper bound or worst-case scenario of the growth rate of
a function. It characterizes the asymptotic behavior of an algorithm when the input size approaches infinity.
Formal Definition:
- \( f(n) = O(g(n)) \) if there exist constants \( c \) and \( n_0 \) such that for all \( n \geq n_0 \), \( f(n) \leq
c \cdot g(n) \).
Properties:
- Upper Bound: It represents the maximum growth rate of the function.
JCT COLLEGE OF ENGINEERING AND TECHNOLOGY
- Simplicity: Big O notation ignores constant factors and lower-order terms, focusing on the dominant term
that determines the growth rate.
- Comparative Analysis: Enables easy comparison of algorithms to determine their efficiency.
- Transitivity: If \( f(n) = O(g(n)) \) and \( g(n) = O(h(n)) \), then \( f(n) = O(h(n)) \).
- Reflexivity: \( f(n) = O(f(n)) \).
- Symmetry: If \( f(n) = \Theta(g(n)) \), then \( g(n) = \Theta(f(n)) \).
- Composition: If \( f(n) = O(g(n)) \) and \( h(n) = O(k(n)) \), then \( f(n) \cdot h(n) = O(g(n) \cdot k(n)) \).
Example: If an algorithm has a time complexity of \( O(n^2) \), it means the algorithm's running time grows
no faster than \( n^2 \) as the input size \( n \) increases.
Definition: Big Omega notation describes the lower bound or best-case scenario of the growth rate of a
function. It represents the minimum growth rate of a function.
Formal Definition:
- \( f(n) = \Omega(g(n)) \) if there exist constants \( c \) and \( n_0 \) such that for all \( n \geq n_0 \), \
( f(n) \geq c \cdot g(n) \).
Properties:
- Lower Bound: Represents the minimum growth rate of the function.
- Comparative Analysis: Useful for identifying the best-case scenario among algorithms.
- Transitivity: If \( f(n) = \Omega(g(n)) \) and \( g(n) = \Omega(h(n)) \), then \( f(n) = \Omega(h(n)) \).
Example: If an algorithm has a time complexity of \( \Omega(n) \), it means the algorithm's running time is
at least linear as the input size \( n \) increases.
Definition: Big Theta notation provides both upper and lower bounds or tight bounds for the growth rate of
a function. It characterizes the asymptotic behavior when the function grows at the same rate from above
and below.
Formal Definition:
- \( f(n) = \Theta(g(n)) \) if there exist constants \( c_1 \), \( c_2 \), and \( n_0 \) such that for all \( n \geq
n_0 \), \( c_1 \cdot g(n) \leq f(n) \leq c_2 \cdot g(n) \).
Properties:
- Tight Bound: Represents both the upper and lower bounds of the function's growth rate.
- Comparative Analysis: Identifies algorithms with optimal performance.
- Symmetry: If \( f(n) = \Theta(g(n)) \), then \( g(n) = \Theta(f(n)) \).
Example: If an algorithm has a time complexity of \( \Theta(n \log n) \), it means the algorithm's running
time grows at the same rate as \( n \log n \) as the input size \( n \) increases.
Summary:
JCT COLLEGE OF ENGINEERING AND TECHNOLOGY
8(b). Summarize the general plan for Analyzing the time efficiency of non-Recursive algorithm
1. Identify Basic Operations: Identify the fundamental operations performed by the algorithm, such as
arithmetic operations, comparisons, and assignments.
2. Count Basic Operations: Analyze the number of times each basic operation is executed as a function of the
input size.
3. Loop Analysis: If the algorithm contains loops, analyze the number of iterations and the operations
performed within each loop.
4. Summation Formulas: If the algorithm involves nested loops or repetitive patterns, use mathematical
techniques such as summation formulas to express the total number of operations.
5. Express Time Complexity: Summarize the time complexity using Big O notation, indicating the growth
rate of the algorithm as a function of the input size.
Insertion Sort is a simple non-recursive sorting algorithm that builds the final sorted array one element at a
time by repeatedly moving elements that are out of order.
python
def insertion_sort(arr):
for i in range(1, len(arr)):
key = arr[i]
j=i-1
while j >= 0 and key < arr[j]:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
```
- There is one comparison and one assignment within the while loop.
- The number of comparisons and assignments depends on the input size and the state of the array.
3. Loop Analysis:
- The outer loop iterates \( n - 1 \) times, where \( n \) is the length of the array.
- The inner loop iterates at most \( i \) times for each iteration of the outer loop, where \( i \) ranges from 1
to \( n - 1 \).
4. Summation Formulas:
- The total number of comparisons and assignments can be expressed as a sum of arithmetic series.
Summary:
- The mathematical analysis of non-recursive algorithms involves identifying basic operations, counting their
occurrences, analyzing loops, using summation formulas, and expressing the time complexity.
- In the example of Insertion Sort, the time complexity is \( O(n^2) \), indicating a quadratic growth rate with
respect to the input size \( n \).