DAA5
DAA5
1.
A. Give an example of a digraph with negative weights for which Floyd’s
algorithm does not yield the correct results.
B. Considering an abstract example with three nodes Vi, Vj and Vk discuss in detail
how Floyd’s algorithm works.
All paths are made up of paths from vi to vk with each intermediate vertex not higher than
k - 1 and a path from vk to vj with each intermediate vertex not higher than k - 1.
That gives the minimum length between two vertices vi and vj,
dij(k) = MIN (dij(k-1), dik(k-1) + dkj(k-1)) for k ≥ 1
C. Solve the all-pairs shortest path problem for the digraph with the following
weight matrix:
2. Apply the bottom-up dynamic programming algorithm to the following instance of
the knapsack problem and find the optimal subset. Capacity W=6. (Neatly show all
the steps).
1 3 25
2 2 20
3 1 15
4 4 40
5 5 50
ITEMS
0 1 2 3 4 5
(0, 0) (3, 25) (2, 20) (1, 15) (4, 40) (5, 50)
0 0 0 0 0 0 0
1 0 0 0 15 15 15
CAPACITY
2 0 0 20 20 20 20
3 0 25 25 35 35 35
4 0 25 25 40 40 40
5 0 25 45 45 55 55
6 0 25 45 60 60 65
T0 consists of a single vertex and hence must be a part of any minimum spanning tree.
To prove : Ti generated from Ti-1 by Prim’s algorithm, is also a part of a minimum spanning
tree.
Let ei = (v, u) be the minimum weight edge from a vertex Ti-1 to a vertex not in Ti-1
used by Prim’s algorithm to expand Ti-1 to Ti.
This cycle must contain another edge (v’, u’) connecting a vertex v’ ∈ Ti-1 to a vertex
u’ that is not in Ti-1.
If we now delete the edge (v’, u’) from this cycle, we will obtain another spanning
tree of the entire graph whose weight is less than or equal to the weight of T since
the weight of ei is less than or equal to the weight of (v’, u’).
Hence, this spanning tree is a minimum spanning tree, which contradicts the
assumption that no minimum spanning tree contains Ti. This completes the
correctness proof of Prim’s algorithm.
A greedy algorithm is an algorithm that follows the problem solving heuristic of making
the locally optimal choice at each stage with the hope of finding a global optimum.
B. If the given graph is a complete graph then which graph representation (weight
matrix or adjacency list) is more suitable to implement Dijkstra’s algorithm?
Justify your answer. (Assume that most suitable priority queue is used)
Dijkstra's algorithm finds the shortest paths from one source to all other nodes in the
graph, for weighted graphs. It operates on dense graphs (matrix representation) in O(V2)
time, and on sparse graphs (adjacency list representation) in O(E log(V)) time.
Matrix representation would be more suitable to implement since for dense graphs, cause
in terms of running time, Adjacency Matrix would almost always outperform lists. The List
implementation would use less memory (proportional to number of edges) to store the
Graph, for dense graphs both representations would use the same amount of memory.
5.
A. Construct a Huffman code for the following data:
Character A B C D _
Character A B C D _
ABACABAD = 0100011101000101
B - A - D - _ - A - D - A
= B A D _ A D A