0% found this document useful (0 votes)
17 views7 pages

DAA5

The document discusses various graph algorithms like Floyd's algorithm, Prim's algorithm, Kruskal's algorithm and Dijkstra's algorithm. It also discusses Huffman coding. Examples are given to explain the working of the algorithms and to solve problems related to shortest paths, minimum spanning trees and encoding/decoding using Huffman codes.

Uploaded by

sucheta panda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views7 pages

DAA5

The document discusses various graph algorithms like Floyd's algorithm, Prim's algorithm, Kruskal's algorithm and Dijkstra's algorithm. It also discusses Huffman coding. Examples are given to explain the working of the algorithms and to solve problems related to shortest paths, minimum spanning trees and encoding/decoding using Huffman codes.

Uploaded by

sucheta panda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

DAA Assignment V

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.

We compute all the elements of each matrix


D(k) from its immediate predecessor D(k-1) in
series.

Let dij(k) be the element in ith row and jth


column.

That means, dij(k) = length of the shortest


path among all paths from the ith vertex vi to
the jth vertex vj with their intermediate
vertices numbered not higher than k.

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

Item Weight Value

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

Maximum value of the optimal subset is 65, taking items 3 and 5.


3.
A. With a neat drawing, explain the correctness proof of Prim’s algorithm. For the
inductive step, use proof by contradiction to prove that the sub tree Ti
generated by Prim’s algorithm is a sub graph of some minimum spanning tree.

T0 consists of a single vertex and hence must be a part of any minimum spanning tree.

Let us assume Ti-1 is is part of some minimum spanning tree T.

To prove : Ti generated from Ti-1 by Prim’s algorithm, is also a part of a minimum spanning
tree.

Let no minimum spanning tree of the graph can contain Ti.

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.

By our assumption ei cannot belong to any minimum spanning tree, including T,


hence if we add ei to T, a cycle will be formed.

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.

B. Explain greedy technique illustrating the property of choices made at each


step.

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.

In general, greedy algorithms have five components: (Optional)

- A candidate set, from which a solution is created


- A selection function, which chooses the best candidate to be added to the solution
- A feasibility function, that is used to determine if a candidate can be used to
contribute to a solution
- An objective function, which assigns a value to a solution, or a partial solution, and
- A solution function, which will indicate when we have discovered a complete solution
On each step the choice made must be:

- feasible, i.e., it has to satisfy the problem’s constraints.


- locally optimal, i.e., it has to be the best local choice among all feasible choices
available on that step.
- irrevocable, i.e., once made, it cannot be changed on subsequent steps of the
algorithm.

C. Analyze the efficiency of Kruskal’s algorithm.

T(n) = O(1) + O(V) + O(E log E) + O(V log V)


= O(E log E) + O(V log V)

as |E| >= |V| - 1

T(n) = E log E + E log E


= E log E
4.
A. Using Dijkstra’s algorithm solve the following instance of single source shortest
path problem considering ‘a’ as the source vertex:

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 _

Probability 0.4 0.1 0.2 0.15 0.15

Character A B C D _

Probability 0.4 0.1 0.2 0.15 0.15

Codeword 0 100 111 101 110

B. Encode the text ABACABAD using the code of question 5(A).

ABACABAD = 0100011101000101

C. Decode the text whose encoding is 100010111001010 in the code of question


5(A).

100 - 0 - 101 - 110 - 0 - 101 - 0

B - A - D - _ - A - D - A

= B A D _ A D A

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy