Daa Endsem Paper Sol Unit III
Daa Endsem Paper Sol Unit III
Tech V Sem
Design and Analysis of Algorithms
Unit-III
(2017-18)
In Dynamic Programming, we make a choice at each step, but the choice usually
depend on the solutions to the subproblems. Consequently we solve dynamic
programming problems in a bottom up manner, progressing from smaller
subproblems to larger subproblems.
In a greedy algorithm, we make whatever choice seems best at the moment and
then solves the subproblem arising after the choice is made. Thus, unlike
dynamic programming, which solves the subproblems bottom up, a greedy
strategy usually progresses in a top down fashion, making one greedy choice
after another.
Not all optimization problems can be solved with greedy approach. For eg.,
Fractional Knapsack problem can be solved using greedy approach, whereas, we
cannot solve 0-1 knapsack using greedy approach.
The Single-Source Shortest Path (SSSP) problem consists of finding the shortest
paths between a given vertex v and all other vertices in the graph. Algorithms
such as Dijkstra is used to solve this problem. The Single-Pair Shortest Path
(SPSP) problem consists of finding the shortest path between a single pair of
vertices.
A = {1,3}
A = {1,3} {4,6}
MST = cost = 20 1
2 4
2
3
6
4 3
5
5 6
The convex hull of a set of S points in the plane is defined to be the smallest
convex polygon containing all the points of S.
A polygon is said to be convex if for any two points P1 & P2 inside the polygon,
the directed segment from P1 to P2 <P1,P2> is fully contained in the polygon.
The vertices of the convex hull of a set S of points form a (not necessarily
proper) subset of S. Given a finite set of points {P1,P2,…,Pn} the convex hull
of P is the smallest convex set C such that P ⊂ 𝑪.
There are two variants of the convex hull problem :
Obtain the vertices of the convex hull (also referred to as extreme points)
Obtain the vertices of the convex hull in some order.
Convex Hull of Q is denoted by CH(Q).
Algorithms used to compute the convex hull of a set of n points.
GRAHAM-SCAN
DIVIDE-AND-CONQUER METHOD
Both algorithms runs in O(n lg n) time.
Not a Convex
Hull
5. Find the shortest path in the below graph from the source vertex 1 to all other
vertices by using Dijkstra’s algorithm.
10
50
30
60
6. Given the six items in the table below and a Knapsack with Weight 100, what is
the solution to the Knapsack problem in all concepts. I.e. explain greedy all
approaches and find the optimal solution
Knapsack
Optimal Solution :-
So Optimal Solution = 65
Greedy Solution :-
Greedy Solution :-
So Greedy Solution = 66
2018-19
7. Compare adjacency list and adjacency matrix representations of a graph with
suitable example and diagram.
i. Adjacency Matrix:
Pros: Representation is easier to implement and follow. Removing an edge takes O(1)
time. Queries like whether there is an edge from vertex ‘u’ to vertex ‘v’ are efficient
and can be done O(1).
Cons: Consumes more space O(V^2). Even if the graph is sparse(contains less number
of edges), it consumes the same space. Adding a vertex is O(V^2) time. Computing all
neighbors of a vertex takes O(V) time (Not efficient).
An array of lists is used. The size of the array is equal to the number of vertices. Let the
array be an array[]. An entry array[i] represents the list of vertices adjacent to the ith
vertex. This representation can also be used to represent a weighted graph. The weights
of edges can be represented as lists of pairs.
In Real-life problems, graphs are sparse(|E| <<|V|2). That’s why adjacency lists Data
structure is commonly used for storing graphs.
8. Consider the weights and values of the item listed below. Note that there is only one unit
of each item. The task is to pick a subset of these items such that their total weight is no
more than 11. And their total value is maximized. Moreover, no item can be split. The
total value of items picked by an optimal algorithm is Vopt. A greedy algorithm gives the
maximum value as Vgreedy. Find the value of Vopt – Vgreedy, where max. capacity of
knapsack is 11.
Item ID A B C D
Weight 10 7 4 2
Value 60 28 20 24
Vi/Wi. 6 4 5 12
Greedy Solution :-
Vopt – Vgreedy = 60 – 44 = 16
Let G =(E,V) is a given graph and A is a minimum spanning tree of G and is not unique.
Let B = another MST with equal weight.
Let (u,v) be an edge in A but not in B.
Then B should have another edge (x,y) which is not in A.
Let w(u,v) < w(x,y).
As B is a MST, {u,v} must contain a cycle.
Replacing (x,y) with (u,v) in B will yield a spanning tree with smaller weight compared to B.
This is in contradiction with above assumption that B is a MST, but it is not.
1. Q ←
2. for each u V
3. do key[u] ← ∞
4. π[u] ← NIL
5. INSERT(Q, u)
6. DECREASE-KEY(Q, r, 0) ► key[r] ← 0
7. while Q
8. do u ← EXTRACT-MIN(Q)
9. for each v Adj[u]
10. do if v Q and w(u, v) < key[v]
11. then π[v] ← u
12. DECREASE-KEY(Q, v, w(u, v))
• Time Complexity of the above program is O(V^2) ,if the input graph is
represented using adjacency list,
10. Find the shortest path in the below graph from the source vertex A to other vertices
by using Bellman Ford algorithm. When do Dijkstra’s and the Bellman-Ford
algorithm both fail to find the shortest path.
A B C D E F G H
0
- 2 1 4 12 4 8 7
2 1 4 11 4 7 7
i AB AD AC BF BC BE CA CE DC EG ED FH GE GF HG
1
2
2 4
2
8
4
11
11. Given an integer x and a positive number n, use divide and conquer approach to write
a function that computes xn with complexity O(lgn).
2019-20
12. What are greedy algorithms? Explain their characteristics.
13. Define Spanning Tree. Write Kruskal’s Algorithm for finding minimum cost
spanning tree. Describe how Kruskal’s algorithm is different from Prim’s Algorithm
for finding minimum cost spanning tree.
Answer 3 (2017-18)
Both Prim’s and Kruskal’s algorithm finds the Minimum Spanning Tree and follow the
Greedy approach of problem-solving. However, there are few differences :
Prim’s Algorithm starts to build the Minimum Spanning Tree from any vertex in the
graph. Whereas Kruskal builds the Minimum Spanning Tree from the vertex carrying
minimum weight in the graph.
Prim traverses one node more than one time to get the minimum distance. Kruskal
traverses one node only once.
Prim’s algorithm has a time complexity of O(V2), V being the number of vertices and
can be improved up to O(E log V) using Fibonacci heaps. Kruskal’s algorithm’s
time complexity is O(E log V), V being the number of vertices.
Prim’s algorithm gives connected component as well as it works only on connected
graph. Kruskal’s algorithm can generate forest(disconnected components) at any
instant as well as it can work on disconnected components
Prim’s algorithm runs faster in dense graphs. Kruskal’s algorithm runs faster in sparse
graphs.
Prim’s generates the minimum spanning tree starting from the root vertex. Kruskal’s
generates the minimum spanning tree starting from the least weighted edge.
14. Compare the various programming paradigms such as Divide and Conquer, Dynamic
Programming and Greedy approach.
Used to solve
Used to solve Used to solve optimization
optimization problem decision problem problem
It is used to obtain a
solution to the given
It may or may not problem, it does not It always
generate an optimal aim for the optimal generates
solution. solution optimal solution.
Recursive in
Iterative in nature. Recursive in nature. nature.
More efficient
Efficient and fast than Less efficient and and faster than
divide and conquer. slower. greedy.
More memory is
required to store
Extra memory is not Some memory is subproblems for
required. required. later use.
Divide and conquer Algorithm: is an algorithmic paradigm in which the problem is solved
using the Divide, Conquer, and Combine strategy. It solves a problem using the following
three steps:
Divide: This involves dividing the problem into smaller sub-problems.
Conquer: Solve sub-problems by calling recursively until solved.
Combine: Combine the sub-problems to get the final solution of the whole problem.
Difference between the Greedy Algorithm and the Divide and Conquer Algorithm:
Divide and conquer Greedy Algorithm
Divide and conquer is used to obtain a The greedy method is used to obtain
solution to the given problem, it does not an optimal solution to the given
aim for the optimal solution. problem.
Each recursive invocation of the algorithm takes as input a subset P ⊆ Q and arrays X
and Y, each of which contains all the points of the input subset P.
The points in array X are sorted so that their x-coordinates are monotonically in-
creasing.
Similarly, array Y is sorted by monotonically increasing y-coordinate.
Suppose we know the convex hull of the left half points and the right half points, then
the problem now is to merge these two convex hulls and determine the convex hull
for the complete set.
This can be done by finding the upper and lower tangent to the right and left convex
hulls.
Let the left convex hull be A and the right convex hull be B. Then the lower and
upper tangents are named as 1 and 2 respectively, as shown in the figure.
Then the red outline shows the final convex hull.
Divide:
o It finds a vertical line l that bisects the point set P into two sets PL and PR such
that |PL| = ⌈|P|/2⌉, |PR| = ⌊|P|/2⌋, all points in PL are on or to the left of line l,
and all points in PR are on or to the right of l.
o The array X is divided into arrays XL and XR, which contain the points of PL
and PR respectively, sorted by monotonically increasing x-coordinate.
o Similarly, the array Y is divided into arrays YL and YR, which contain the
points of PL and PR respectively, sorted by monotonically increasing y-
coordinate.
o The division terminates if |P| <= 3.
Conquer:
o Having divided P into PL and PR , it makes two recursive calls, one to find the
closest pair of points in PL and the other to find the closest pair of points in PR.
o The inputs to the first call are the subset PL and arrays XL and YL; the second
call receives the inputs PR, XR, and YR.
o Let the closest-pair distances returned for PL and PR be δL and δR ,
respectively, and let δ = min(δL , δR ).
Binary Search is a searching algorithm used in a sorted array by repeatedly dividing the
search interval in half. The idea of binary search is to use the information that the array is
sorted and reduce the time complexity to O(Log n).
The basic steps to perform Binary Search are:
1. Begin with the mid element of the whole array as a search key.
2. If the value of the search key is equal to the item then return an index of the search key.
3. Or if the value of the search key is less than the item in the middle of the interval,
narrow the interval to the lower half.
4. Otherwise, narrow it to the upper half.
5. Repeatedly check from the second point until the value is found or the interval is empty.
18. Discuss greedy approach to an activity selection problem of scheduling several competing
activities. Solve the following activity selection problem
S = {A1, A2, A3, A4, A5, A6, A7, A8, A9, A10}
Si = { 1, 2, 3, 4, 7, 8, 9, 9, 11, 12 }
Fi = { 3, 5, 4, 7, 10, 9, 11, 13, 12, 14 }
Scheduling several competing activities that require exclusive use of a common resource
, with a goal of selecting a maximum size set of mutually compatible activities.
Definition –
o S={1, 2,…, n} – activities that wish to use a resource
o Each activity has a start time si and a finish time fi , where si fi
GREEDY-ACTIVITY-SELECTOR(s, f)
o n length[s]
o a {a1}
o i1
o for m 2 to n
o do if sm fi
o then A A {am}
o im
o return A
S = {A1, A3, A2, A4, A6, A5, A7, A8, A9, A10}
Si = { 1, 3, 2, 4, 8, 7, 9, 9, 11, 12 }
Fi = { 3, 4, 5, 7, 9, 10, 11, 13, 12, 14 }
i N M Output
0 10 1 {a1}
1 10 3, {a1,a3}
2 10 2,4 {a1,a3,a4}
4 10 6 {a1,a3,a4, a6}
5 10 5, 7 {a1,a3,a4, a6 a7}
8 10 8,9 {a1,a3,a4, a6 ,a7,a9}
9 10 {a1,a3,a4, a6 ,a7 a9,a10}
19. Define Minimum Spanning Tree. Write Prim’s Minimum Spanning Tree Algorithm
in detail. (2018-19)
20. Explain Dijkstra’s algorithm to solve single source shortest path problem with
suitable example.
Answer 5 2017-18
2021-22
A thief robbing a store finds n items: the i th item is worth vi dollars and weighs
wi pounds, where both vi and wi are integers.
The thief wants to take as valuable a load as possible, but he can carry only W
pounds in his knapsack, where W is some integer.
Problem: Which items should the thief take?
Two versions of the problem:
o 0-1 : For each item, the thief must either take it or leave it, in other
words, he cannot take a fraction of an item.
o Fractional : The thief can take fractions of items.
Item I1 I2 I3 I4
Profit 45 30 45 10
Weight 3 5 9 5
Pi/Wi 15 6 5 2
16 0
I1 16-3=13 45
I2 13-5=8 45+30=75
1. INITIALIZE-SINGLE-SOURCE(G, s)
2. for i = 1 to |V[G]| –1
3. for each edge (u, v) E[G]
4. Relax(u, v, w)
5. for each edge (u, v) E[G]
6. if d[v] > d[u] + w(u, v)
7. return false
8. return true