0% found this document useful (0 votes)
29 views

DCA7104 - Analysis and Design of Algorithms

Uploaded by

Dhananjay Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

DCA7104 - Analysis and Design of Algorithms

Uploaded by

Dhananjay Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Enrolment No: dhananjay.

2114100139

Name: DHANANJAY

Address: B-19 House No 151 Chhawla

New Delhi - 110071

Course Title: Analysis and Design of Algorithms

Course Code: DCA7104

Phone No.: 9711440620

Personal Email ID: [email protected]

University Email ID: [email protected]

Signature:
Assignment Set – 1

Ques 1.
a. What are the properties of an algorithm? Explain branch and bound algorithm with an
example.
Ans: Properties of algorithms
An algorithm must create one or more outputs and can take zero to more external inputs. The
algorithm must also terminate after a predetermined number of steps. The algorithm's
characteristics are:
• Accuracy - Any valid input must result in accurate and correct output.
• Clarity - Every instruction should be explicit, clear, and precise. Clearly specified inputs
and outputs are necessary.
• Finiteness - An algorithm must always come to an end after a finite number of steps when
being followed.
• Effectiveness - All instructions should be as simple as possible so that they may be
completed primarily with a pencil and piece of paper.
• Generality – Algorithms require generic guidelines that can be used in any situation.
Branch and bound algorithm:
For optimization issues, branch and bound techniques are typically used. The branch-and-bound
algorithm can be used to create a tree of subproblems. The "root problem" is what we refer to as
the primary issue. a process for creating upper and lower bounds for a certain problem. Apply the
border technique at each node.
• If the boundaries do not match, the problem represented by that node is split into two
subproblems, which are then transformed into separate nodes.
• If the boundaries do match, a suitable solution is taken into consideration for that specific
subproblem. Resolve the remaining nodes in the tree using the best feasible known
solution.
Branch-and-Bound Algorithm Example: Traveling Salesman's Problem: A salesman needs to
make one trip to each of n cities and is trying to travel as little as possible.
• Visualize the routing problem as locating the quickest route through a collection of cities,
stopping at each one only once.
• Divide the node's difficulties into two minor issues.
1. The quickest way to get to the city A
2. The quickest path that avoids cities a debut
• As the tree gets bigger, split it even further.
• Heuristics: Usual guidelines for choosing which options to investigate first.
Optimization is the process of removing some possibilities without thoroughly examining them.

b. What is the general plan to analyse the efficiency of recursive algorithms?


Ans: The following is a general method for evaluating the effectiveness of recursive algorithms:
1) Based on the n parameter, determine the input size.
2) Recognize and examine the fundamental procedures of recursive algorithms.
3. Count the number of simple operations carried out on several inputs of the same size.
4) If it differs, we must obtain the best, worst, and average input size n situations. (It must be
studied independently if the underlying operation depends on it.)
5) To solve basic processes, set up iterative relationships with appropriate initial conditions.
6) To determine the sequence of increase, solve the recurrence relation using forward and
backward substitution. We can demonstrate the correct answer using mathematical induction. A
few examples help to clarify these steps.
Example 1: calculating the factorial of n. By carrying out repeated multiplication, we can get the
factorial of the number n. If n is 5, then the n factorial (n!) is calculated as follows steps:
1) n! = 5!
2) 4! * 5
3) 3! * 4 * 5
4) 2! * 3 * 4 * 5
5) 1! * 2 * 3 * 4 * 5
6) 0! * 1 * 2 * 3 * 4 * 5
7) 1 * 1 * 2 * 3 * 4 * 5
The recursive algorithm for finding factorial of a number is given as:
Algorithm Tracing to Find the Factorial of a Number
n=2
Pass 1 : factorial(2)// this is a recursive function which traces to itself
{
If(2=0)//this condition is not met
{
return 1
}
else return factorial(2-1) * 2 // recursively calls factorial (1)
Pass 2 : factorial (1)
{
If(1=0) // condition is false
{
Return 1

Ques 2. Differentiate between bottom-up and top-down heap construction with example.
Ans:
Bottom-up heap construction
By arranging keys in the specified sequence, it prepares the almost complete binary tree with n
nodes and then "heapifies" the tree as follows. The algorithm tests whether the parental domination
still holds for the key at this node from the root to the last parental node.
2 2
9 9
7 8

6 5 6 5
8 7

If not, the algorithm replaces the node's key K with its children's larger keys and tests to see if the
parental domination still holds for K in its new position.

9
2
8

6 5
7

This process continues as long as K meets the condition for parental dominance. The process
continues to "heapify" the sub-tree rooted at the current parental node after it has finished doing
so for that node's immediate predecessor. Once this is completed, the algorithm halts, producing
the final heap in the figure for the tree's root.
The positions of the nodes in the tree are indicated by numbers above them in the array below:
0
1
[0] 9
9 2 [1] 6
6 [2] 8
3 4 8 [3] 2
2 5 [4] 5
7 5 [5] 7

In order to move a node down the tree, its key value does not change, hence it is not necessary for
it to participate in intermediary swaps. Up until a point where it finally accepts the "erased" value
once more, the empty nodes are switched out for larger keys in its offspring.

Algorithm for bottom-up heap construction:


Algorithm: Heap Bottom-up (H [1...n])
//A heap from the elements of a given array by the bottom-up algorithm
//Input: An array H[1..n] of orderable items
//Output: A heap H[1..n]
for i ←n/2down to 1 do
k←i; v←H[k] heap←false
while not heap and 2 * k ≤ n do j ←2 * k
if j <n //there are two children
if H[ j ]<H[ j + 1]
j ←j + 1
if v ≥ H[j ]
heap←true
else H[k]←H[j ];
k←j
H[k]←v

Top-down heap construction algorithm


The top-down heap construction process (less effective) creates a heap by repeatedly inserting a
new key into an already-built heap, as seen below.
8
5
7

2 4
3 6

Attaching a new node with key K within it next to the last leaf of an existing heap is the first step
in adding a new key to it. Move K then to its proper position in the new heap as shown below.
8
5
7

2 4
3 6 9

Utilize the parent's key to evaluate K. Stop if the parent key (the structure is a heap) is more than
or equal to K; otherwise, swap these two keys and evaluate K with its new parent. Up until K is
equal to its most recent parent or until it reaches the root, this switching continues. An empty node
can be moved up in this procedure until it reaches the right location, where it will receive K's value.

8 9
5 5
8
9

2 4 2 4
3 7 3 6 7
6

Key comparisons for this insertion operation are limited to the height of the heap. The temporal
efficiency of insertion is in O because a heap with n nodes will have a height of around log2 n.
(log n).
BASIS FOR TOP-DOWN APPROACH BOTTOM-UP APPROACH
COMPARISON
Basic Breaks the massive problem into smaller solves the underlying low-level
subproblems. issue and incorporates it into a
bigger issue.
Process Submodules are solitarily analyzed. Consider what information is to be
enclosed and how the idea of
information concealing is implied.
Communication Not required in the top-down approach. Needs a specific amount of
communication.
Redundancy Hold redundant information. Redundancy can be eliminated.
Programming Structure/procedural oriented Object-oriented programming
languages programming languages (i.e., C) follows languages (like C++, Java, etc.)
the top-down approach. follows the bottom-up approach.
Mainly used in Module documentation, test case creation, Testing
code implementation and debugging.
Ques 3.
a. How is Divide and Conquer a better method for sorting?
Ans: A straightforward algorithm that uses the divide and conquer strategy is insertion sort.
Although the insertion sort takes O(n2) time to complete, it is normally twice as quick as the bubble
sort and somewhat quicker than the selection sort. Although slightly more sophisticated than the
bubble and selection sorts, it is still not overly complicated. It frequently serves as the last phase
of more complex sorts, such rapid sort.
There are many ways to sort an unsorted array in insertion sort, such as by starting with:
• The left
• The right
• Any desired place where the marker is placed.
Algorithm Worst-Case running Best-Case running Average-case running
time time time
Selection Sort Θ (n2) Θ (n2) Θ (n2)
Insertion Sort Θ (n2) Θ (n) Θ (n2)
Merge Sort Θ (n log n) Θ (n log n) Θ (n log n)
Quick Sort Θ (n2) Θ (n log n) Θ (n log n)
b. What are the best, worst and average cases in an insertion sort?
Ans: The ideal input would be an array that has already been sorted. In this situation, the running
time of the insertion sort is linear, or O(n). Every cycle, the first input element still present is
compared to the element that is further to the right in the array's sorted subsection.
An array that is arranged in reverse order is the worst-case input. In this instance, before inserting
the subsequent element, each cycle of the inner loop thoroughly analyses the entire sorted sub-
section of the array. In this situation, the running time of insertion sort is quadratic, or O(n2).
Insertion sort is not a practical method for sorting huge arrays because the average case is also
quadratic. For arrays with fewer than ten entries, insertion sort is the fastest algorithm.
Assignment Set – 2

Ques 4.
a. Explain distribution counting with an example.
Ans: By storing the data produced during the sorting process in a separate array, distribution
counting is an input enhancement technique that improves the sorting process. The text is
compared with the pattern in Horspool's and Boyer-Moore methods for string matching, and the
pattern is shifted by computing the shift size. As a result, searching is expedited.
A hash key is used in the hashing technique to locate objects. When two items' hash key values
match, it is said that a collision has occurred. The two collision resolution techniques, independent
chaining and open addressing, correct this. The B-Tree technique's branching factor reduces access
time. Thus, by utilising all of the aforementioned methods, we may reduce the execution time of
an algorithm.

b. Explain the algorithm to solve the Knapsack problem using the dynamic programming
method.
Ans: Given a knapsack with following:
M: The knapsack's capacity, n: The number of items
w is an array of weights (w1, w2,... wn).
p is an array of profits, p1, p2,..., pn.
x is an array of 0 or 1, where 0 represents that xi has not been chosen and 1 represents
It has been chosen to use that xi item.
The primary objective is to pack the items into the backpack to make the most money possible.
obtained and the weights of the goods picked shouldn't be heavier than the knapsack's capacity.
This issue can be stated as follows:

Design Methodology:
Case 1: If there are no objects (i.e., i = 0) or capacity of knapsack itself is 0 i.e., j=0, then
profit will be 0. The total profit obtained as:
V [ i, j ] = 0 if (i=0 or j=0) -------------------------------------------- Eq 1
Case 2: Suppose we have a situation that weight of the ith object wi is greater than
remaining capacity j, in such case ith object cannot be selected, so the profit obtained is
V [ i, j ] = V [ i-1, j ] if wi > j -----------------------------------------------------Eq 2
Case 3: Suppose we have a situation where wi is less than remaining capacity j, in such case
we have two alternatives:

• We reject ith object, if so, profit obtained is V[ i,j ] = V [ i-1, j ]


• We select ith object, if so, profit obtained is : V [ i, j ]= V [ i-1, j-wi ] + Pi
Out of these situations which ever yields, maximum profit, that must be considered. So, we
must select maximum of these two. So, profit obtained if Wi <= j is given by
V [ i, j ] = max { V [ i -1 , j ] , V [ i-1, j - Wi] +Pi } if wi <= j ------------------------ Eq 3
Considering all the cases, the final recurrence relation to get solution to knapsack problem
can be written as below:

Profit Table:
Profit table for the above example is shown below
Ques 5. Explain the dynamic programming approach to find binomial coefficients
Ans: Let p1, p2...pn be the probability of searching the items, and let a1, a2...an be the distinct
elements listed in ascending order.
In a binary search tree with elements ai....aj, let c[i,j] be the fewest average number of comparisons
performed, where i,j are some integer indices, and 1 I j n.
Now determine the values for C[i,jsub ]'s instances.
In order to use dynamic programming to determine the recurrence relation, we must select a root
ak for keys ai....aj. A binary search tree of this type has the key ak as its root, a left sub-tree Ti k1
holding keys ai...ak- 1 in an ideal arrangement, and a right sub-tree Tk+1 j holding keys ak+1...aj
in an optimal arrangement.
In this case, we are benefiting from the Principle of Optimality. If we start counting tree levels at
1 then we can derive the following recurrence relation:

In the recurrence relation given by Eq: 1, let us assume that C[i, j − 1] ≈ 0 for 1 ≤ i ≤ n + 1. This
we can interpret as the number of comparisons in the empty tree. The figure shows the values
required to compute the C[i,j] formula.
The values at row I and the columns to the left of column j, as well as in column j and the rows
beneath row I are shown above. The pair of entries with the smallest value, as indicated by the
arrows, is recorded as the value of C[i,j]. We must populate the table along its diagonal, beginning
on the main diagonal with zeroes and probabilities denoted by pi, 1 I n, and working our way up
to the upper right corner. With the aid of this approach, we can determine C[1,n], which represents
the typical number of comparisons made by successful searches in the ideal binary search tree.
Another two-dimensional table must be kept up to date with the value of k for which the minimum
is achieved. The table will be same as the one above and will be filled in the same manner. The
table entries will start at R[i,i] for 1 ≤ i ≤ n and is used to find the optimal solution.

Ques 6.
a. Describe greedy choice property.
Ans: Greedy Choice Property
Using a locally optimal (greedy) decision, greedy algorithms arrive at the best answer worldwide.
Without taking into account the outcomes from subproblems, the solution that appears optimal in
the current problem is chosen. A greedy algorithm can make decisions based on past decisions,
but it cannot make decisions based on future decisions or on the answers to subproblems. This is
where dynamic programming and greedy algorithms diverge.
At each stage of dynamic programming, decisions are taken, although they typically depend on
the answers to smaller problems. By working from smaller subproblems to larger subproblems,
dynamic programming tackles issues from the bottom up. Consequently, a greedy approach
typically develops from the top down, making one greedy choice after another, reducing each
given problem instance to a smaller one.
b. Explain the sorting problem with the help of a decision tree
Ans: A decision tree is a software that takes the form of a branching tree. Each node in this diagram
represents a choice. Depending on the test's outcome, the root node is tested first, and then control
is transferred to one of its subtrees. This flow is maintained until it reaches the leaf node containing
the subject matter.
Figure provides a real-world decision tree example. Assume for the purposes of this discussion
that we are salaried citizens under the age of 60 who pay income tax.

First, we determine whether the citizen is a man or a woman. Check to see if the citizen's salary is
less than $2,000 if he is a man. If so, the citizen is not liable for paying taxes. If not, ascertain if
the revenue is below $500,000 If so, a 10% tax will apply. If not, see if it is less than $100,000. If
so, the tax is 20%; otherwise, it is 30%. If it is a female, we first determine if there are fewer than
2,50,000, and then we follow the same procedure as for male citizens.
The input parts of some algorithms, such as sorting and searching, must be compared. We employ
decision trees to research these techniques.
The problem of sorting can be viewed as following.
Input: A sequence of n numbers <a1, a2, . . . , an>.
Output: A permutation (reordering) <a‘1, a‘2, . . . , a‘n> of the input sequence such
that a‘1 <= a‘2 ….. <= a’n.
A sorting algorithm is comparison based if it uses comparison operators to find the order
between two numbers. Comparison sorts can be viewed abstractly in terms of decision trees. A
decision tree is a full binary tree that stands for the comparisons between elements that are
performed by a particular sorting algorithm operating on an input of a given size. The execution
of the sorting algorithm corresponds to tracing a path from the root of the decision tree to a leaf.
At each internal node, a comparison ai <= aj is made. The left subtree then dictates next
comparisons for ai <= aj, and the right subtree dictates subsequent comparisons for ai > aj.
When we come to a leaf, the sorting algorithm has proven the ordering. So we can say following
about the decision tree.
1) Each of the n! permutations on n elements must appear as one of the leaves of the decision
tree for the sorting algorithm to sort properly.
2) Let x be the maximum number of comparisons in a sorting algorithm. The maximum height
of the decision tree would be x. A tree with maximum height x has at most 2^x leaves.
After combining the above two facts, we get following relation.
n! <= 2^x
Taking Log on both sides.
log2(n!) <= x
Since log2(n!) = Θ(n Log n), we can say
x = Ω(nLog2n)
Therefore, any comparison-based sorting algorithm must make at least nLog 2n comparisons to
sort the input array, and Heapsort and merge sort are asymptotically best comparison sorts.

You might also like