Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Daa Cia 1 Key

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

JCT COLLEGE OF ENGINEERING AND TECHNOLOGY

PICHANUR, COIMBATORE – 641105

AD3351- DESIGN AND ANALYSIS OF ALGORITHMS

ANSWER KEY

PART-A

1. Demonstrate notion of algorithm with diagram.

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.

2. Recall about omega notation.

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

F(n) >= c* g9n) For all n >= n0

It is denoted as f(n) Є Ω (g(n)).

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 (jo; j<n; j++)
{
C[i][j] = A[i][j] + B[i][j]
}
}

4. Name the applications of brute force method.

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

PICHANUR, COIMBATORE – 641105

5. Outline the Knapsack problem.

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

6(a).Explain the steps involved in problem solving.


ANS:

Algorithm design and analysis process.


1. Understanding the Problem.

•This is the first step in designing of algorithm.


•The first thing you need to do before an algorithm is to understand completely the problem given.
•Read the problem’s description carefully to understand the problem statement completely. Ask questions
for clarifying the doubts about the problem.
•Identify the problem types and use existing algorithm to find solution. •Input (instance) to the problem and
range of the input get fixed.
•Read the problem’s description carefully and ask questions if you any doubts about the problem, do a few
small examples by hand, think about special cases, and ask question s again if needed.
•It helps to understand how such an algorithm works and to know its strengths and weaknesses, especially
if you have to choose among several available algorithms.
•It is very important to specify exactly the range of instances the algorithm needs to handle.
JCT COLLEGE OF ENGINEERING AND TECHNOLOGY

PICHANUR, COIMBATORE – 641105

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

Algorithm Design Techniques


•An algorithm design technique (or “strategy” or “paradigm”) is a general approach to solving
problems algorithmically that is applicable to a variety of problems from different areas of computing.
•Though Algorithms and Data Structures are independent, but they are combined together to
develop program. Hence the choice of proper data structure is required before designing the algorithm.
•Implementation of algorithm is possible only with the help of Algorithms and Data Structures
•Algorithmic strategy / technique / paradigm are a general approach by which many problems can
be solved algorithmically. E.g., Brute Force, Divide and Conquer, Dynamic Programming, Greedy
Technique and so on.

Methods of Specifying an Algorithm


There are three ways to specify an algorithm. They are:
 Natural language
 Pseudocode
 Flowchart

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

PICHANUR, COIMBATORE – 641105

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.

Proving an Algorithm’s Correctness


•Once an algorithm has been specified then its correctness must be proved.
•An algorithm must yields a required result for every legitimate input in a finite amount of time.
•For example, the correctness of Euclid’s algorithm for computing the greatest common divisor stems from the
correctness of the equality gcd(m, n) = gcd(n, m mod n).
•A common technique for proving correctness is to use mathematical induction because an algorithm’s iterations
provide a natural sequence of steps needed for such proofs.
•The notion of correctness for approximation algorithms is less straightforward than it is for exact algorithms.
Analyzing an Algorithm
•For an algorithm the most important is efficiency. In fact, there are two kinds of algorithm efficiency. They are:
•Time efficiency, indicating how fast the algorithm runs, and
•Space efficiency, indicating how much extra memory it uses.
•The efficiency of an algorithm is determined by measuring both time efficiency and space efficiency. •So factors to
analyze an algorithm are:
▪Time efficiency of an algorithm
▪Space efficiency of an algorithm
▪Simplicity of an algorithm
▪Generality of an algorithm
JCT COLLEGE OF ENGINEERING AND TECHNOLOGY

PICHANUR, COIMBATORE – 641105

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.

General plan for Empirical Analysis of Algorithm


A. Understand the purpose of experiment of given algorithm.
B. Decide the efficiency metric M. Also decide the measurement unit.
C. Decide on characteristic of input
D. Prepare program implementing the algorithm for the experimentation.
E. Generate a sample of inputs.
F. Run the algorithm (or algorithms) on the sample’s inputs and record the data observed.
G. Analyze the data obtained.
H. Understand the purpose of experiment of given algorithm.

A).Understand the purpose of experiment of given algorithm.


• To ensure theoretical assertion about the algorithm’s efficiency
• comparing the efficiency of several algorithms for solving the same problem or different
implementations of the same algorithm
• developing a hypothesis about the algorithm’s efficiency class
• ascertaining the efficiency of the program implementing the algorithm on a particular
machine.

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

Line 1 : int sum element (int a[10], int n)


Line 2 : {
Line 3 : int i, sum = 0;
Line 4 : for (i=0;i<n;i++)
Line 5 : {
Line 6 : sum=sum+a[i];
Line 7 : }
Line 8 : return sum;
Line 9 : }
JCT COLLEGE OF ENGINEERING AND TECHNOLOGY

PICHANUR, COIMBATORE – 641105

The execution starts from for loop. The delaration part can be neglected. Now

Statement Frequency count


i=0 1
i<n This statement executes for (n+1).
i++ n times
sum=sum+a[i] n times
Return sum 1
Total (3n+3)
We get the frequency count to be (3n+3). We can neglect the constant terms an in terms of asymptotic
notation the efficiency of an algorithm can be denoted as O(n).

C)Decide on characteristic of input


Even though the efficiency of basic operation is measured by frequency count or by time clocking it is
necessary to consider some set of inputs for experiment
For certain sample size of input the behavior of an algorithm is to be observed. For example if we double the
sample size then we can compute the ratio M(2n)/M(n) of observed metric M. The metric can be time or a
frequency count. From the observed radio we can identify the basic efficiency class of that algorithm.
Sometimes based on random input the empirical analysis of algorithm is done.Thus range of the input is
decided to analyze the algorithm.
D)Generate a sample of inputs.
For the decided range of input the various samples are obtained.
E)Run the algorithm (or algorithms) on the sample’s inputs and record the data observed.
Using some suitable programming language the program is to be written for given algorithm and for some
set of input sample the program is run. The result obtained for certain set of input is to be recorded. This
recorded result is then presented for analysis.
F)Analyze the data obtained.
While analyzing the resultant data arrange it in tabular or in graphical form.
For Example:

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

PICHANUR, COIMBATORE – 641105

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.

1) Consider pattern: abba text: redblueredblue

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

PICHANUR, COIMBATORE – 641105

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

2)Consider Pattern: "aaaa" , and input text: " asdasdasdasd"


The simple logic to match pattern against text is that match first letter “a” of string “asd” with letter ‘a’ of
pattern. The algorithm will be
Int BruteForceAlgor(char[14],char p[4], int n)
{
//array t[] contains “asdasdasdasdasd”
//array p[] contains “aaaa”
//n represents length of text[]
int I,j,flag = 1;
i=0;
j=0;
while(j<n)
{
=if ((t[j]==’a’)&&(p[i]==’a’)//map’a’ of “asd” to ‘a’
{
i=i+1;
j=-j+3;
}
else
{
flag=0;
j++;
}
}
return flag;
}
JCT COLLEGE OF ENGINEERING AND TECHNOLOGY

PICHANUR, COIMBATORE – 641105

3) similarly for Pattern: "aabb and input text: "xyzabcxyzabc

We will map ‘x’ of “xyz” nstring with “a” and “abc” string with “b”. The Algorithm will be

int BruteForceAlgo(char t[20],char p[10],int n)


{
//array t[] contains “xyzabcxyzabc”
//array p[] contains “aabb”
//n represents length of text t[]

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:

The cost obtained by assigning the job in various combinations as


JCT COLLEGE OF ENGINEERING AND TECHNOLOGY

PICHANUR, COIMBATORE – 641105

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:

< 1, 2, 3, 4> 9+4+1+4 = 18


<1, 2, 4, 3> 9+4+8+9=30
<1, 3, 2, 4> 9+3+8+4= 24
<1, 3, 4, 2> 9+3+8+6=26
<1, 4, 2, 3> 9+7+8+9 = 33
<1, 4, 3, 2> 9+7+1+6=23
<2, 1, 3, 4>2+6+1+4 = 13
<2, 1, 4, 3> 2+6+8+9 = 25
<2, 3, 1, 4> 2+3+5+4 = 14
<2, 3, 4, 1> 2+3+8+7 = 20
<2, 4, 1, 3> 2+7+1+7 = 17
<2, 4, 3, 1> 2+7+5+9 = 24
<3, 1, 2, 4> 7+6+8+4= 25
<3, 1, 4, 2> 7 + 6 + 8 + 6 = 27
<3, 2, 1, 4> 7 + 4 + 5 + 4 = 20
<3, 2, 4, 1> 7 + 4 + 8 + 7 = 26
<3, 4, 1, 2> 7 + 7 + 5 + 6 = 25
<3, 4, 2, 1> 7 + 7 + 8 + 7 = 29
<4, 1, 2, 3> 8 + 6 + 8 + 9 = 31
<4, 1, 3, 2> 8 + 6 + 1 + 8 = 23
<4, 2, 1, 3> 8 + 4 + 5 + 9 = 26
<4, 2, 3, 1> 8 + 4 + 1 + 7 = 20
<4, 3, 1, 2> 8 + 3 + 8 + 7 = 26
<4, 3, 2, 1> 8 + 3 + 54 * 7 7 = 22

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

8(a). Discuss in detail all the asymptotic notation

Sure, let's discuss in detail the three primary asymptotic notations: Big O, Big Omega, and Big Theta.

1. Big O Notation (O):

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

PICHANUR, COIMBATORE – 641105

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

2. Big Omega Notation (Ω):

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.

3. Big Theta Notation (Θ):

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

PICHANUR, COIMBATORE – 641105

- Big O notation provides an upper bound on the growth rate.


- Big Omega notation provides a lower bound on the growth rate.
- Big Theta notation provides tight bounds on the growth rate.
- These notations are crucial for analyzing the efficiency of algorithms and comparing their performance.
They help in understanding how algorithms behave as the input size grows, which is essential for making
informed decisions in algorithm design and implementation.

8(b). Summarize the general plan for Analyzing the time efficiency of non-Recursive algorithm

General Plan for Mathematical Analysis of Non-Recursive Algorithms:

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.

Example: Analysis of Insertion Sort Algorithm

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

General Plan Application:

1. Identify Basic Operations:


- Comparison (`key < arr[j]`).
- Assignment (`arr[j + 1] = ...`).
- Arithmetic operations (e.g., `j = i - 1`).

2. Count Basic Operations:


JCT COLLEGE OF ENGINEERING AND TECHNOLOGY

PICHANUR, COIMBATORE – 641105

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

5. Express Time Complexity:


- The time complexity of Insertion Sort is \( O(n^2) \) in the worst-case scenario, where \( n \) is the number
of elements in the array.

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 \).

You might also like