ADA Module 4 - Full
ADA Module 4 - Full
1. Kruskal Algorithm
2. Prims Algorithms
Kruskal’s Algorithm
3
4
d c
7
e
Time complexity of Kruskal’s algorithm is: O(E log E)
Apply Kruskal’s algorithm to obtain minimum spanning tree
Prims algorithm
• Prim’s algorithm constructs a minimum spanning tree through a
sequence of expanding subtrees.
• The algorithm stops after all the graph’s vertices have been
included in the tree being constructed.
Time complexity of prims algorithm is: O(E log V)
Apply Prim’s algorithm to obtain minimum spanning tree
Apply both Kruskal’s and Prim’s algorithm to below graphs:
99 units
26
Dijkstra’s Algorithm
• It is also called as single-source shortest-path algorithm:
for a given vertex called the source in a weighted connected
graph, find shortest paths to all its other vertices
2. In the next run, the next node with the smallest dist value is
chosen.
e(-,ꝏ)
3 5
b(a,1) c(b,6) 5+1
d(a,3) d c
e(-,ꝏ) 7
d(a,3) c(b,6) e
e(d,10) 7+3
c(b,6) e(d,10)
e(d,10) ---
The shortest distances from node a to all other nodes are
ab=1
abc=6
ad=3
a d e = 10
Character A B C D E _
D_ B E A C 0.65
0.3
0.3 0.35 0.4 0.5 0.5
0.9 C 0.65
E A C D_B B
D _
0.35 0.5
0.1 0.2 0.4 0.5 0.5 0.65
E A B
0.3
C D_B EA 0.4 0.5 0.35
0.5 0.65 0.9
0.3
D _
0.1 0.2
D _
0.1 0.2
1.15 0.9
C D_B EA
0.5 0.65 0.9
E A
C
0.65 0.4 0.5
0.5
EA CD_B B
0.3
0.9 1.15 0.35
D _
0.1 0.2
Give numbering- 1) Encode the text BAD_AC
Left branch numbered with 0 and right 2) Decode the text whose encoding is 1100110110
2.05
branch numbered with 1
0 1
0.9 1.15
0 1 0 1
E A C
0.65
0.4 0.5 0.5
0 1
A B C D E _
01 111 10 1100 00 1101 B
0.3
0.35
0 1
BAD_AC: 11101110011010110
D _
1100110110: D_C 0.1 0.2
Let F(n) be the maximum amount that can be picked up from the row
of n coins.
To derive a recurrence for F (n), we partition all the allowed coin selections into
two groups:
The largest amount we can get from the first group is equal to cn + F (n − 2) — the value
of the nth coin plus the maximum amount we can pick up from the first n − 2 coins.
The maximum amount we can get from the second group is equal to F (n − 1) by the
definition of F (n).
Thus we have:
The application of the
algorithm to the coin row
of denominations
5, 1, 2, 10, 6, 2 is shown
in figure beside:
Using the CoinRow to find F(n), the largest amount of money that can
be picked up takes (n) time and (n)space.
Several coins are placed in cells of an n × m board, no more than one coin per cell.
A robot, located in the upper left cell of the board, needs to collect as many of the coins as
possible and bring them to the bottom right cell.
On each step, the robot can move either one cell to the right or one cell down from its current
location.
When the robot visits a cell with a coin, it always picks up that coin.
Design an algorithm to find the maximum number of coins the robot can collect and a path it
needs to follow to do this
Let F (i, j ) be the largest number of coins the robot can collect and bring to the cell
(i, j ) in the ith row and jth column of the board.
It can reach this cell either from the adjacent cell (i − 1, j) above it
or from the adjacent cell (i, j − 1) to the left of it.
The largest numbers of coins that can be brought to these cells are
F (i − 1,j) and F (i, j − 1), respec vely.
Base Case: There are no adjacent cells above the cells in the first row,
and there are no adjacent cells to the left of the cells in the first column.
For those cells, we assume that F (i − 1,j) and F (i, j − 1) are equal to 0
for their nonexistent neighbors.
Therefore, the largest number of coins the robot can bring to cell (i, j ) is the maximum
of these two numbers plus one possible coin at cell (i, j ) itself.
Example of solving an instance of the knapsack problem by the memory function algorithm.
Introduction to Warshall’s algorithm
Transitive Closure
Definition: 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.
Introduction to Warshall’s algorithm
Transitive Closure
Example:
(a) Digraph. (b) Its adjacency matrix. (c) Its transitive closure.
Warshall’s algorithm works as follows
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.
Introduction to Warshall’s algorithm
Transitive Closure
Specifically, the element R(k) ij in the ith row and jth column of matrix
R(k) (for i, j = 1, 2, . . . , n, and 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(0), Boolean matrix which contains the information about paths
with no intermediate vertices in its paths.
R(1) Boolean matrix which contains the information about paths that
can use the first vertex as intermediate, it may contain more 1’s
than R(0).
R(2) Boolean matrix which contains the information about paths that
can use the first two vertices as intermediate, it may contain more
1’s than R(1).
Transitive Closure
R(n) Boolean matrix which contains the information about paths that
can use the n vertices as intermediate, it may contain more 1’s
than R(n-1).
Finding path from vertex i to j if there exist path via intermediate
vertex k, i.e. if R(k-1) ik = 1 and R(k-1) kj = 1 then R(k) ij = 1 as shown
in figure below
Application of Warshall’s
algorithm to the digraph
shown. 1’s in bold are
changed from 0 (R(k-1)).
Dynamic Programming: Floyd’s algorithm
All-Pairs Shortest Paths problem
Finding the shortest distance path from each vertex to all other vertices
of a given directed or undirected graph.
Floyd’s algorithm for finding all-pairs shortest paths
It is applicable to both undirected and directed weighted graphs
which do not contain a cycle of a negative length.
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).
Each of these matrices contains the lengths of shortest paths with
certain constraints i.e.
for k = 0, matrix contains lengths of shortest paths with zero
intermediate vertex
for k = 1, matrix contains lengths of shortest paths with one
intermediate vertex.
All-Pairs Shortest Paths problem
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
The recurrence equation for for generating the elements of matrix
D(k) from the elements of matrix D(k-1) is
The above equation tells that the element in row i and column j of the
current distance matrix D(k−1) is replaced by the sum of the elements in
the same row i and the column k and in the same column j and the row
k if and only if the sum is smaller than its current value (in D(k-1)).
All-Pairs Shortest Paths problem