DAA Unit3 Notes and QBank
DAA Unit3 Notes and QBank
DAA Unit3 Notes and QBank
The greedy approach suggests constructing a solution through a sequence of steps, each
expanding a partially constructed solution obtained so far, until a complete solution to the
problem is reached. On each step—and this is the central point of this technique—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
Prim’s Algorithm
A spanning tree of an undirected connected graph is its connected acyclic subgraph (i.e.,
a tree) that contains all the vertices of the graph.
If such a graph has weights assigned to its edges, a minimum spanning tree is its
spanning tree of the smallest weight, where the weight of a tree is defined as the sum of the
weights on all its edges.
The minimum cost spanning tree problem is the problem of finding a minimum spanning tree
for a given weighted connected graph.
1
16IT4202 – DAA UNIT 3 NOTES
ALGORITHM Prim(G)
//Prim’s algorithm for constructing a minimum spanning tree
//Input: A weighted connected graph G = <V, E>
//Output: ET , the set of edges composing a minimum spanning tree of G
VT = {V0} //the set of tree vertices can be initialized with any vertex
ET = 𝝓
for i = 1 to |V| − 1 do
find a minimum-weight edge e* = (v*, u*) among all the edges (v, u)
such that v is in VT and u is in V − VT
VT = VT ∪ {u*}
ET = ET ∪ {e*}
return ET
If a graph is represented by its weight matrix and the priority queue is implemented as an
unordered array, the algorithm’s running time will be in 𝚯(|V |2).
If a graph is represented by its adjacency lists and the priority queue is implemented
as a min-heap, the running time of the algorithm is in O(|E| log |V |).
2
16IT4202 – DAA UNIT 3 NOTES
3
16IT4202 – DAA UNIT 3 NOTES
KRUSKAL’S ALGORITHM
ALGORITHM Kruskal(G)
//Kruskal’s algorithm for constructing a minimum spanning tree
//Input: A weighted connected graph G = <V, E>
//Output: ET , the set of edges composing a minimum spanning tree of Gsort E in nondecreasing
order of the edge weights w(ei1) ≤ . . . ≤ w(ei|E|)
ET = 𝜙;
ecounter = 0 //initialize the set of tree edges and its size
k = 0 //initialize the number of processed edges
while ecounter < |V| − 1 do
k=k+1
if ET ∪ {eik } is acyclic
ET = ET ∪ { eik };
ecounter = ecounter + 1
return ET
4
16IT4202 – DAA UNIT 3 NOTES
5
16IT4202 – DAA UNIT 3 NOTES
Dijkstra’s Algorithm
The single-source shortest-paths problem is defined as for a given vertex called the source in a
weighted connected graph, find shortest paths to all its other vertices
Applications.
Most widely used applications are transportation planning and packet routing in communication
networks, including the Internet. Multitudes of less obvious applications include finding shortest
paths in social networks, speech recognition, document formatting, robotics, compilers, and
airline crew scheduling. In the world of entertainment, one can mention path finding in video
games and finding best solutions to puzzles using their state-space graphs
The best-known algorithm for the single-source shortest-paths problem, called Dijkstra’s
algorithm.
This algorithm is applicable to undirected and directed graphs with nonnegative weights only
Dijkstra’s algorithm finds the shortest paths to a graph’s vertices in order of their distance from a
given source. First, it finds the shortest path from the source to a vertex nearest to it, then to a
second nearest, and so on. In general, before its ith iteration commences, the algorithm has
already identified the shortest paths to
i − 1 other vertices nearest to the source.
The set VT of vertices for which a shortest path has already been found and the priority queue Q
of the fringe vertices.
6
16IT4202 – DAA UNIT 3 NOTES
ALGORITHM Dijkstra(G, s)
//Dijkstra’s algorithm for single-source shortest paths
//Input: A weighted connected graph G = <V, E>with nonnegative weights and its vertex s
//Output: The length dv of a shortest path from s to v and its penultimate vertex pv for every
vertex v in V
Initialize(Q) //initialize priority queue to empty
for every vertex v in V
dv = ∞; pv = null
Insert(Q, v, dv) //initialize vertex priority in the priority queue
ds = 0; Decrease(Q, s, ds) //update priority of s with ds
VT = 𝝓
for i = 0 to |V| − 1 do
u* = DeleteMin(Q) //delete the minimum priority element
VT = VT ∪ {u*}
for every vertex u in V − VT that is adjacent to u* do
if du* + w(u*, u) < du
du = du* + w(u*, u); pu = u*
Decrease(Q, u, du)
The time efficiency of Dijkstra’s algorithm depends on the data structures used for
implementing the priority queue and for representing an input graph itself in Θ(|V |2) for graphs
represented by their weight matrix and the priority queue implemented as an unordered array.
For graphs represented by their adjacency lists and the priority queue implemented as a min-
heap, it is in O(|E| log |V |).
7
16IT4202 – DAA UNIT 3 NOTES
8
16IT4202 – DAA UNIT 3 NOTES
A tree constructed by the above algorithm is called a Huffman tree. It defines—in the
manner described above—a Huffman code.
It finds the optimal coding in O(N. log N) time.
EXAMPLE
Consider the five-symbol alphabet {A, B, C, D, _} with the following occurrence frequencies in
a text made up of these symbols:
symbol A B C D _
frequency 0.35 0.1 0.2 0.2 0.15
9
16IT4202 – DAA UNIT 3 NOTES
10
16IT4202 – DAA UNIT 3 NOTES
1. Among the subsets that do not include the ith item, the value of an optimal subset is,
F(i − 1, j).
2. Among the subsets that do include the ith item (hence, j – wi ≥ 0), an optimal subset is made
up of this item and an optimal subset of the first i − 1 items that fits into the knapsack of
capacity j − wi . The value of such an optimal subset is vi + F(i − 1, j − wi).
Thus, the value of an optimal solution among all feasible subsets of the first i items is the
maximum of these two values. Of course, if the ith item does not fit into the knapsack, the value
of an optimal subset selected from the first i items is the same as the value of an optimal subset
selected from the first i − 1 items. These observations lead to the following recurrence:
F(i, j) = max{F(i − 1, j ), vi+ F(i − 1, j − wi)} if j – wi ≥ 0, F(i − 1, j) if j − wi < 0.
It is convenient to define the initial conditions as follows:
F(0, j) = 0 for j ≥ 0 and F(i, 0) = 0 for i ≥ 0.
Our goal is to find F(n, W), the maximal value of a subset of the n given items that fit into the
knapsack of capacity W, and an optimal subset itself.
11
16IT4202 – DAA UNIT 3 NOTES
Memory functions
Dynamic programming deals with problems whose solutions satisfy a recurrence relation with
overlapping subproblems. The direct top-down approach to finding a solution to such a
recurrence leads to an algorithm that solves common subproblems more than once and hence is
very inefficient The classic dynamic programming approach, on the other hand, works bottom
up: it fills a table with solutions to all smaller subproblems, but each of them is solved only once.
An unsatisfying aspect of this approach is that solutions to some of these smaller subproblems
are often not necessary for getting a solution to the problem given. Since this drawback is not
present in the top-down approach, The goal is to get a method that solves only subproblems that
are necessary and does so only once. Such a method exists; it is based on using memory
functions.
This method solves a given problem in the top-down manner but, in addition, maintains a table
of the kind that would have been used by a bottom-up dynamic programming algorithm. Initially,
all the table’s entries are initialized with a special “null” symbol to indicate that they have not yet
been calculated. Thereafter, whenever a new value needs to be calculated, the method checks the
corresponding entry in the table first: if this entry is not “null,” it is simply retrieved from the
table; otherwise, it is computed by the recursive call whose result is then recorded in the table.
The following algorithm implements this idea for the knapsack problem. After initializing the
table, the recursive function needs to be called with i = n (the number of items) and j = W (the
knapsack capacity).
12
16IT4202 – DAA UNIT 3 NOTES
ALGORITHM MFKnapsack(i, j )
//Implements the memory function method for the knapsack problem
//Input: A nonnegative integer i indicating the number of the first items being considered and a
nonnegative integer j indicating the knapsack capacity
//Output: The value of an optimal feasible subset of the first i items
//Note: Uses as global variables input arrays Weights[1..n], V alues[1..n], and table F[0..n, 0..W ]
whose entries are initialized with −1’s except for row 0 and column 0 initialized with 0’s
if F[i, j ]< 0
if j <Weights[i]
value = MFKnapsack(i − 1, j)
else
value = max(MFKnapsack(i − 1, j),Values[i] + MFKnapsack(i − 1, j −Weights[i]))
F[i, j ] = value
return F[i, j ]
13
16IT4202 – DAA UNIT 3 NOTES
14
16IT4202 – DAA UNIT 3 NOTES
15
16IT4202 – DAA UNIT 3 NOTES
16
16IT4202 – DAA UNIT 3 NOTES
17
16IT4202 – DAA UNIT 3 NOTES
Warshall’s Algorithm
The transitive closure of a directed graph with n vertices can be defined as the n*n boolean
matrix T = {tij}, in which the element in the ith row and the jth column is 1 if there exists a
nontrivial path (i.e., directed path of a positive length) from the ith vertex to the jth vertex;
otherwise, tij is 0.
Warshall’s algorithm constructs the transitive closure through a series of n * n boolean matrices: R(0), . . .
, R(k−1), R(k), . . . R(n). Each of these matrices provides certain information about directed paths in the
digraph. Specifically, the element r(k) ij in the ith row and jth column of matrix R(k) (i, j = 1, 2, . . . , n, k =
0, 1, . . . , n) is equal to 1 if and only if there exists a directed path of a positive length from the ith vertex
to the jth vertex with each intermediate vertex, if any, numbered not higher than k.
R(1) contains the information about paths that can use the first vertex as intermediate; In general, each
subsequent matrix in series has one more vertex to use as intermediate for its paths than its predecessor
and hence may, but does not have to, contain more 1’s. The last matrix in the series, R(n), reflects paths
that can use all n vertices of the digraph as intermediate and hence is nothing other than the digraph’s
transitive closure.
Let r(k) ij , the element in the ith row and jth column of matrix R(k), be equal to 1. This means that there
exists a path from the ith vertex vi to the jth vertex vj with each intermediate vertex numbered not higher
than k: vi, a list of intermediate vertices each numbered not higher than k, vj .
This formula implies the following rule for generating elements of matrix R(k) from elements of matrix
R(k−1), which is particularly convenient for applyingWarshall’s algorithm by hand:
If an element rij is 1 in R(k−1), it remains 1 in R(k).
18
16IT4202 – DAA UNIT 3 NOTES
If an element rij is 0 in R(k−1), it has to be changed to 1 in R(k) if and only if the element in its row i and
column k and the element in its column j and row k are both 1’s in R(k−1).
19
16IT4202 – DAA UNIT 3 NOTES
20
16IT4202 – DAA UNIT 3 NOTES
Given a weighted connected graph (undirected or directed), the all-pairs shortest paths problem
asks to find the distances—i.e., the lengths of the shortest paths— from each vertex to all other
vertices.
The distance matrix matrix D is an n*n where the element dij in the ith row and the jth
column of this matrix indicates the length of the shortest path from the ith vertex to the jth
vertex.
Floyd’s algorithm computes the distance matrix of a weighted graph with n vertices through a
series of n *n matrices: D(0), . . . , D(k−1), D(k), . . . , D(n). the element d(k) ij in the ith row and
the jth column of matrix
D(k) (i, j = 1, 2, . . . , n, k = 0, 1, . . . , n) is equal to the length of the shortest path among all paths
from the ith vertex to the jth vertex with each intermediate vertex, if any, numbered not higher
than k. hence,D(0) is simply the weight matrix of the graph. The last matrix in the series, D(n),
contains the lengths of the shortest paths among all paths that can use all n vertices as
intermediate
21
16IT4202 – DAA UNIT 3 NOTES
22
16IT4202 – DAA UNIT 3 NOTES
23
16IT4202 – DAA UNIT 3 NOTES
24
16IT4202 – DAA UNIT 3 NOTES
25
16IT4202 – DAA UNIT 3 NOTES
26
16IT4202 – DAA UNIT 3 NOTES
The algorithm’s space efficiency is clearly quadratic; the time efficiency of this
algorithm is cubic i.e. Θ(N3)
27
16IT4202 – DAA UNIT 3 NOTES
28
16IT4202 – DAA UNIT 3 NOTES
29
16IT4202 – DAA UNIT 3 NOTES
30
16IT4202 – DAA UNIT 3 NOTES
31
16IT4202 – DAA UNIT 3 NOTES
32
16IT4202 – DAA UNIT 3 NOTES
S.No Questions
What do you mean by optimal feasible solution?
1.
An optimal feasible solution is one that either minimizes or maximizes the objective function.
What is Transitive Closure?
The transitive closure of a directed graph with n vertices can be defined as the n by n Boolean
2.
matrix T = {tij}, in which the element in the ith row (1<i<n) and the jth column (l<j<n) is 1 if
there exists a non trivial directed path from the ith vertex to jth vertex ; otherwise , tij is 0.
State Principal of Optimality.
3. An optimal solution to any instance of an optimization problem is composed of optimal
solutions to its subinstances.
Compare Dynamic Programming and Greedy Approach..
Greedy Method Dynamic Programming
It is used for obtaining Optimal Solution It is used for obtaining Optimal Solution
In this method from a set of feasible solution There is no set of feasible solutions.
we pick up the optimum solution
4.
The optimum selection is without revising It considers all possible sequences in order to
previously generated solutions obtain the optimum solution
33
16IT4202 – DAA UNIT 3 NOTES
algorithm MST forms a single tree. The safe edge added to MST is always a least-weight
edge connecting the tree to a vertex not in the tree.
How can one use Prim’s algorithm to find a spanning tree of a connected graph with no
weights on its edges?
7.
Since Prim’s algorithm requires weights on the graphs edges, some weights have to be
assigned so that we can employ the algorithm to find spanning tree.
Does Kruskal’s algorithm work correctly on graphs that have negative edge weights?
Yes, Kruskal’s algorithm work correctly on graphs that have negative edge weights. In
8. Kruskal's algorithm the safe edge added to A (subset of a MST) is always a least weight edge
in the graph that connects two distinct components. So, if there are negative weight edges
they will not affect the evolution of the algorithm.
What is All-pair shortest-paths problem?
Given a weighted connected graph (undirected or directed), the all pairs shortest paths
9.
problem asks to find the distances(the lengths of the shortest path) from each vertex to all
other vertices.
What is Minimum Cost Spanning Tree?
10. A minimum spanning tree of a weighted connected graph is its spanning tree of the smallest
weight, where the weight of a tree is defined as the sum of the weights on all its edges.
1. What do you mean by Huffman code?
11. A Huffman code is a optimal prefix free variable length encoding scheme that assigns bit
strings to characters based on their frequencies in a given text.
2. What is Optimal Binary Search Trees?
12. An optimal binary search tree is a binary search tree for which the nodes are arranged on
levels such that the tree cost is minimum.
3.
4. What are the drawbacks of dynamic programming?
13. • Time and space requirements are high, since storage is needed for all level.
• Optimality should be checked at all levels
What is Knapsack problem?
14. Given n items of known weights w1, . . . , wn and values v1, . . . , vn and a knapsack of
capacity W, find the most valuable subset of the items that fit into the knapsack.
34
16IT4202 – DAA UNIT 3 NOTES
1. CO 3
Apply Prim’s algorithm to find a minimum spanning tree of the following graphs. 14
CO 3
2.
35
16IT4202 – DAA UNIT 3 NOTES
Write the Dijktra’s algorithm for finding the single source shortest path 14
3. CO 3
Construct the optimal binary search tree with the following data 14 CO 3
5. Keys A B C D E
Solve the all pairs source shortest path for the digraph with the weight matrix 14 CO 3
A 0 ∞ ∞ 7 ∞
B 3 0 4 ∞ ∞
1. 10 CO 3
C ∞ ∞ 0 ∞ 6
D ∞ 2 5 0 ∞
E ∞ ∞ ∞ 4 0
Character A B C D E F
Frequency 5 9 12 13 16 45
2. 10 CO 3
36
16IT4202 – DAA UNIT 3 NOTES
Solve the following instance of the knapsack problem given the knapsack capacity is
W=5
Apply the Warshall’s algorithm to find the transitive closure of the digraph defined by the
following adjacency matrix
4. 10 CO 3
37