SCHEME FOR VALUATION
QP CODE: 23123488
B.Sc/BCA DEGREE (CBCS) REGULAR EXAMINATIONS, MAY 2023
Fourth Semester
CORE COURSE- CS4CRT09 - DESIGN AND ANALYSIS OF ALGORITHMS
Part A
Answer any ten questions-2 marks each
1. How do you analyze an algorithm?
Analysis of algorithms or performance analysis refers to the task of determining how
much computing time and storage an algorithm requires.
2. What is worst-case complexity?
The worst-case complexity measures the resources (e.g. running time, memory) that
an algorithm requires given an input of arbitrary size (commonly denoted
as n in asymptotic notation). It gives an upper bound on the resources required by the
algorithm. the case of running time, the worst-case time complexity indicates the
longest running time performed by an algorithm given any input of size n, and thus
guarantees that the algorithm will finish in the indicated period of time.
3. Define control abstraction.
A control abstraction is a procedure whose flow of control is clear but whose primary
operations are specified by other procedures whose precise meanings are left undefined.
4. Describe the divide and conquer maximin problem.
Let P = (n, a [i] ,……,a [j]) denote an arbitrary instance of the problem. Here ‘n’ is the
no. of elements in the list (a [i],….,a[j]) and we are interested in finding the maximum
and minimum of the list. If the list has more than 2 elements, P has to be divided into
smaller instances. For example, we might divide ‘P’ into the 2 instances, P1=([n/2],
a[1],……..a[n/2]) & P2= ( n-[n/2], a[[n/2]+1],….., a[n]) After having divided ‘P’ into 2
smaller sub problems, we can solve them by recursively invoking the same divide-and-
conquer algorithm.
5. Explain the method of strassen's matrix multiplication.
Strassen’s Matrix Multiplication is the divide and conquer approach to solve the matrix
multiplication problems. The usual matrix multiplication method multiplies each row
with each column to achieve the product matrix. The time complexity taken by this
approach is O(n3), since it takes two loops to multiply. Strassen’s method was
introduced to reduce the time complexity from O(n3) to O(nlog 7).
Using Strassen’s Algorithm compute the following −
6. Explain the method of greedy algorithm.
A greedy algorithm, as the name suggests, always makes the choice that seems to be the
best at that moment. This means that it makes a locally-optimal choice in the hope that
this choice will lead to a globally-optimal solution. Assume that we have an objective
function that needs to be optimized (either maximized or minimized) at a given point. A
Greedy algorithm makes greedy choices at each step to ensure that the objective
function is optimized. The Greedy algorithm has only one shot to compute the optimal
solution so that it never goes back and reverses the decision.
7. Write the control abstraction of th greedy strategy.
Algorithm GreedyMethod (a, n)
{
// a is an array of n inputs
Solution: =Ø;
for i =1 to n do
{
s = select (a);
if (feasible (Solution, s)) then
Solution: = union (Solution, s);
}
return solution;
}
8. Write any four examples of Dynamic Programming.
Multistage Graphs, All Pairs shortest Paths, Single Source Shortest Paths, 0/1 Knapsack
problem, Traveling Salesperson Problem. (Write any 4)
9. Explain single source shortest path.
The single source shortest path problem (SSSP) finds the shortest path from a single
vertex (the source) to every other vertex in the graph. In a Single Source Shortest Paths
Problem, we are given a Graph G = (V, E), we want to find the shortest path from a
given source vertex s ∈ V to every vertex v ∈ V.
10. What is 0/1 knapsack problem?
The 0/1 knapsack problem means that the items are either completely or no items are
filled in a knapsack. For example, we have two items having weights 2kg and 3kg,
respectively. If we pick the 2kg item then we cannot pick 1kg item from the 2kg item
(item is not divisible); we have to pick the 2kg item completely. This is a 0/1 knapsack
problem in which either we pick the item completely or we will pick that item. The 0/1
knapsack problem is solved by the dynamic programming.
11. Explain biconnected graph.
An undirected graph is called Biconnected if there are two vertex-disjoint paths
between any two vertices. In a Biconnected Graph, there is a simple cycle through any
two vertices.
A graph is said to be Biconnected if:
It is connected, i.e. it is possible to reach every vertex from every other vertex,
by a simple path.
Even after removing any vertex the graph remains connected.
12. Draw a graph that contain hamiltonian circuit.
Part B
Answer any six questions-5 marks each
13. What are the conditions to be satisfied by an algorithm?
Input: An algorithm should have zero or more but should be a finite number of inputs.
Output: An algorithm must give at least one required result from the given set of input
values.
Definiteness: Each step must be clear, unambiguous, and precisely defined.
Finiteness: Finiteness means Algorithm should be terminated after a finite number of
steps.
Effectiveness: Each step of the Algorithm must be feasible i.e., it should be practically
possible to perform the action. Every Algorithm is generally expected to be effective.
14. Differentiate between time complexity and space complexity.
Time Complexity Space Complexity
Space complexity is a measure of how
Time complexity is a measure of how much memory an algorithm needs a
much time an algorithm takes a problem to problem to resolve as the size of the input
resolve as the size of the input increases. increases.
The number of operations an algorithm The amount of memory required to store
performs on the input data is typically used the input data and any additional data
to measure time complexity. structures created by the algorithm is
usually used to measure space complexity.
Time complexity indicates how efficient an Space complexity indicates how efficient
algorithm is in terms of time usage. an algorithm is in terms of memory usage.
An algorithm with a lower time complexity will An algorithm with a lower space complexity
generally execute faster than one with a will generally consume less memory than one
higher time complexity. with a higher space complexity.
15. Consider the numbers given below. Show how partitioning algorithm of Quick Sort will
place 106 in its correct position. Show all steps clearly. 106,117,128,134,141,91,84,63,42
(1) (2) (3) (4) (5) (6) (7) (8) (9) (10) i p
106 117 128 134 141 91 84 63 42 + 2 9
106 42 128 134 141 91 84 63 117 + 3 8
106 42 63 134 141 91 84 128 117 + 4 7
106 42 63 84 91 141 134 128 117 + 5 6
106 42 63 84 91 141 134 128 117 + 6 5
91 42 63 84 106 141 134 128 117
16. Compare and contrast the complexities of Prim's and Kruskal's algorithm.
Similarities
Both Prim's and Kruskal's algorithms are developed for discovering the minimum
spanning tree of a graph.
Both the algorithms are popular and follow different steps to solve the same kind of
problem.
Differences
Prim’s Algorithm Kruskal’s Algorithm
Prim’s algorithm has a time complexity of Kruskal’s algorithm’s time complexity is
O(V2), V being the number of vertices O (E log V), V being the number of
vertices.
Prim’s algorithm runs faster in dense Kruskal’s algorithm runs faster in sparse
graphs. graphs.
17. With an example, explain the concept behind Prim's algorithm.
Prim’s algorithm follows greedy algorithm to find minimum spanning tree. In Prim’s
algorithm, we start from one vertex and keep adding next vertices that is connected
with the lowest edge weight until we reach our goal. The steps for implementing Prim’s
algorithm are as follows:
Remove all loops and parallel edges.
Initialize the minimum spanning tree with any vertex of the graph.
Find all the edges that connect the tree to new unvisited vertices, find the
minimum edges and add them to the tree.
Repeat step 2 until we get a minimum spanning tree.
Example
Step 1 – Remove all loops and parallel edges
After removing parallel edge from the graph, graph will look like below.
Step 2 – Choose any vertex for minimum spanning tree
In this case, we choose A vertex as the root node for Prim’s minimum spanning tree.
We can choose any vertex and initialize it as minimum spanning tree node when next
vertex with minimum weight will be added.
Step 3 – Find all outgoing edges from the selected vertex and select the one with less
cost
After choosing the A as root node, we see that (A,B), (A,C) and (A,D) are three edges
with weight 7, 3, and 12, respectively. We choose the edge (A,C) as it has the minimum
edge weight.
Now, the tree A-3-C is treated as one node and we check the all-outgoing edges from A
and C to find the minimum cost edge.
Here we found (A,B) and (C,E) has same weight. So we can select both.
Now again find the all connected outgoing edges from the visited vertex to find the
minimum cost edge. Here we got (E,D) has minimum edge weight so we will add (E,D)
in the tree.
So this is the final minimum spanning tree with weight total edge weight 22.
18. Explain All pairs Shortest path problem with example.
The all-pairs shortest path problem is the determination of the shortest graph distances
between every pair of vertices in a given graph. The problem can be solved using Floyd-
Warshall and Johnson’s algorithms. The all-pairs shortest path algorithm is also known
as Floyd-Warshall algorithm is used to find all pair shortest path problem from a given
weighted graph. The time complexity of this algorithm is O(V3), here V is the number
of vertices in the graph.
For example, consider the following graph:
The adjacency matrix containing the shortest distance is:
0 -1 -2 0
4 0 2 4
5 1 0 2
3 -1 1 0
The shortest path from:
• vertex 0 to vertex 1 is [0 —> 2 —> 3 —> 1]
• vertex 0 to vertex 2 is [0 —> 2]
• vertex 0 to vertex 3 is [0 —> 2 —> 3]
• vertex 1 to vertex 0 is [1 —> 0]
• vertex 1 to vertex 2 is [1 —> 0 —> 2]
• vertex 1 to vertex 3 is [1 —> 0 —> 2 —> 3]
• vertex 2 to vertex 0 is [2 —> 3 —> 1 —> 0]
• vertex 2 to vertex 1 is [2 —> 3 —> 1]
• vertex 2 to vertex 3 is [2 —> 3]
• vertex 3 to vertex 0 is [3 —> 1 —> 0]
• vertex 3 to vertex 1 is [3 —> 1]
• vertex 3 to vertex 2 is [3 —> 1 —> 0 —> 2]
19. Describe Travelling Sales Persons problem.
The travelling salesman problem is a graph computational problem where the salesman
needs to visit all cities (represented using nodes in a graph) in a list just once and the
distances (represented using edges in the graph) between all these cities are known. The
solution that is needed to be found for this problem is the shortest possible route in
which the salesman visits all the cities and returns to the origin city.
Explanation with example.
20. Explain DFS algorithm.
Algorithm DFS(v)
// Given an undirected graph GG=(V, E) with n vertices and an array visited[] initially
//set to zero, this algorithm visits all vertices reachable from v.
{
visited[v]=1;
for each vertex w adjacent from v do
{
If (visited[w]=0) then
DFS(w);
}
} (Give full marks for correct explanation)
21. Explain sum of subset problem with algorithm. And solve the data using sum of subset, S=
{3,5,6,7}, d = 15.
Given positive numbers wi, 1<=i<=n, and m, this problem calls for finding all subsets
of the wi whose sums are m. For example, if n=4, (w1, w2, w3, w4) = (11,13,24,7),
and m=31, the desired subsets are (11,13,7) and (24,7). The explicit constraints for
this problem are to require xi element of {j / j is an integer and 1<=j<=n}. The
implicit constraints require that no two be the same and that the sum of the
corresponding wi’s be m.
Above question consist of 4 weights, n=4 (w1, w2, w3, w4) with values (3,5,6,7) and
d=15.So we find the desired subset is (3, 5,7).
Write recursive backtracking algorithm for sum of subsets problem.
Part C
Answer any Two questions-15 marks each
22. What are different algorithm design techniques? Explain any two techniques wih example.
1.Divide and conquer 2. Greedy Method 3. Dynamic Programming
4. Backtracking (1 mark)
Explain any two with example (14 marks)
23. Explain mergesort algorithm with an example.
Merge sort is similar to the quick sort algorithm as it uses the divide and conquer
approach to sort the elements. It is one of the most popular and efficient sorting
algorithm. It divides the given list into two equal halves, calls itself for the two halves
and then merges the two sorted halves.
To understand merge sort, we take an unsorted array as the following −
Merge sort first divides the whole array iteratively into equal halves. We see here that
an array of 8 items is divided into two arrays of size 4.
This does not change the sequence of appearance of items in the original. Now we
divide these two arrays into halves.
We further divide these arrays and we achieve atomic value which can no more be
divided.
Now, we combine them in exactly the same manner as they were broken down.
first compare the element for each list and then combine them into another list in a
sorted manner. We see that 14 and 33 are in sorted positions. We compare 27 and 10
and in the target list of 2 values we put 10 first, followed by 27. We change the order of
19 and 35 whereas 42 and 44 are placed sequentially.
In the next iteration of the combining phase, we compare lists of two data values, and
merge them into a list of found data values placing all in a sorted order.
After the final merging, the list becomes sorted and is considered the final solution.
Explanation (5 mark ) 2. Example (5 mark ) 3. Write recursive
algorithm for merge sort ( 5 mark)
24. Write a note on greedy technique. Explain about Knapsack algorithm and solve the data
using knapsack problem. M=40, N=4, weights (20,25,10,15), profit (20,40,35,45).
Greedy technique is basically used to determine the feasible solution that may or may
not be optimal. The feasible solution is a subset that satisfies the given criteria. The
optimal solution is the solution which is the best and the most favorable solution in the
subset. In the case of feasible, if more than one solution satisfies the given criteria then
those solutions will be considered as the feasible, whereas the optimal solution is the
best solution among all the solutions. One of the examples of greedy technique is
knapsack problem.
The knapsack problem states that − given a set of items, holding weights and profit
values, one must determine the subset of the items to be added in a knapsack such that,
the total weight of the items must not exceed the limit of the knapsack and its total
profit value is maximum.
Solution
Given:
n = 4, m = 40, (p1, p2… p4) = (20,40,35,45), (w1, w2… w4) = (20,25,10,15)
To prove: Optimal solution that gives maximum profit.
Proof:
Step 1: (To find profit/ weight ratio)
p1/w1 = 20/2 0= 1
p2/w2 = 40/25 = 1.6
p3/w3 = 35/10= 3.5
p4/w4 =45/15=3
Step 2: Arrange all the items in descending order based on Pi/Wi
Weights (in kg) 10 15 25 20
Profits 35 45 40 20
Pi/Wi 3.5 3 1.6 1
Step 3:
Without exceeding the knapsack capacity, insert the items in the knapsack with
maximum profit.
Knapsack = {3,4}
However, the knapsack can still hold 15 kg weight, but the next item having 25 kg
weight will exceed the capacity. Therefore, only 15kg weight of the 25 kg will be
added in the knapsack.
Weights (in kg) 10 15 25 20
Profits 35 15 10 20
Knapsack 1 1 15/25 0
Hence, the knapsack holds the weights = [(1 * 10) + (1 * 15) + (15/25 * 25)] = 40. With
maximum profit of [(1 * 35) + (1 * 15) + (15/25 * 10)] =56.
Explanation (3 mark) Knapsack Problem (4 mark) Knapsack algorithm and
solution ( 8 mark)
25. How does backtracking works on 8-Queen's problem? Explain with suitable example
Backtracking is an algorithmic technique for solving problems recursively by trying to
build a solution incrementally, one piece at a time, removing those solutions that fail to satisfy
the constraints of the problem at any point in time. One of the examples of backtracking
concepts is 8-Queen’s problem.
The eight queens puzzle is the problem of placing eight chess queens on an 8×8 chessboard so
that no two queens threaten each other; thus, a solution requires that no two queens share the
same row, column, or diagonal.
One possible solution for 8 queens problem is shown below:
8 queens Problem with algorithm (8 Mark) Example (7 mark)
Scheme prepared By:
Sheena Thomas
Asst.Professor, Department of Computer Science & Applications
Mar Elias College, Kottappady
Phone:9961838735