0% found this document useful (0 votes)
18 views

MODULE-4 - Trees and Graph Algorithm

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

MODULE-4 - Trees and Graph Algorithm

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

22CDT45A

MODULE-4
Trees and Graph Algorithms
Trees – properties, pendant vertex, Distance and centres in a tree - Rooted and binary trees,
counting trees, spanning trees, Prim’s algorithm and Kruskal’s algorithm, Dijkstra’s shortest
path algorithm, Floyd-Warshall shortest path algorithm.

A graph G is said to be a tree if it is connected and has no cycles.

A graph which is a tree is usually denoted by T (instead of G) to emphasize the structure

The graphs shown in figure 3.2 are not trees. Observe that the first of these contains a cycle
whereas the second is not connected. However, each component of the second (disconnected)
graph is a tree. Such a graph is called a forest.

Figure 3.2

Pendent vertex
A pendant vertex is defined as a vertex of degree one. A pendant vertex of a tree is also called
a leaf.

Some Properties of Trees


Theorem 1. In a tree, there is one and only one path between every pair of vertices.
Proof: Let T be a tree. Then T is a connected simple graph. Since T is connected, there must
be at least one path between every two vertices. If there are two paths between a pair of vertices
of T, the union of the paths will become a cycle, and T cannot be a tree. Thus, between every
pair of vertices in a tree there must exist one and only one path.
22CDT45A

Theorem 2. If in a graph G there is one and only one path between every pair of vertices, then
G is a tree.

Proof: Since there is a path between every pair of vertices in G, it is obvious that G is
connected. Since there is only one path between every pair of vertices, G cannot have a cycle.
Because, if there is a cycle, then there exist two paths between two vertices on the cycle. Thus,
G is a connected graph containing no cycles. This means that G is a tree.
Theorem 3. A tree with n vertices has n -1 edges.
Proof: We prove the theorem by induction on n.
The theorem is obvious for n=1, n=2, and n=3; see the trees in below figure.

Assume that the theorem holds for all trees with n vertices where n ≤ k, for a specified positive
integer k.
Consider a tree T with k+1 vertices. In T, let e be an edge with end vertices u and v. Since T is a
tree, it has no cycles and therefore there exists no other edge or path between u and v. Hence,
deletion of e from T will disconnect the graph and T – e consists of exactly two components, say
T1 and T2. Since T does not contain any cycle, the components T1 and T2 too do not contain any
cycles. Hence, T1 and T2 are trees in their own right. Both of these trees have less than k + 1vertices
each, and therefore, according to the assumption made, the theorem holds for these tress; that is
each of T1 and T2 contains one less edge than the number of vertices in it. Therefore, since the
total number of vertices in T1 and T2 (taken together) is (k+1) -2 = k-1. But T1 and T2 taken
together is T –e contains k -1 edges. Consequently, T has exactly k edges.

Theorem 4. Any connected graph with n vertices and 𝑛 − 1 edges is a tree.


22CDT45A

Proof: Let G be a connected graph with n vertices and n-1 edges. Assume that G is not a tree.
Then G contains a cycle, say C. Let e be an edge in C. The graph G will not become
disconnected if e is deleted. Thus, G – e is a connected graph. But, on the other hand, G – e has
n vertices and n – 2 edges; therefore, it cannot be connected. This is a contradiction. Hence, G
must not have a cycle; this means that G must be a tree.
This completes the proof of the theorem.
Theorem 5. A graph with 𝑛 vertices, 𝑛 − 1 edges and no cycles is connected.

Proof: Consider a graph 𝐺 which has 𝑛 vertices, 𝑛 − 1 edges and no cycles. Suppose G is not
connected. Let the components of G be 𝐻𝑖 , 𝑖 = 1,2, … , 𝑘. If 𝐻𝑖 has 𝑛𝑖 vertices, we have 𝑛1 +
𝑛2 + ⋯ + 𝑛𝑘 = 𝑛. Since G has no cycles, 𝐻𝑖 ′𝑠 also do not have cycles. Further, they are all
connected graphs. Therefore, they are trees. Consequently, each 𝐻𝑖 must have 𝑛𝑖 − 1 edges.
Therefore, the total number of edged in these 𝐻𝑖 ′𝑠 is (𝑛1 − 1) + (𝑛2 − 1) + ⋯ + (𝑛𝑘 − 1) =
𝑛 − 𝑘. This must be equal to the total number of edges in G; that is 𝑛 − 𝑘 = 𝑛 − 1. This is not
possible, since 𝑘 > 1.
Therefore, G must be connected.

Minimally connected graphs

A connected graph is said to be minimally connected if the removal of any one edge from it
disconnects the graph.

Theorem 6. A connected graph is a tree if and only if it is minimally connected.

Proof: Suppose G is a connected graph which is not a tree. Then, G contains a cycle C. The
removal of any one edge e from this cycle will not make the graph disconnected. Therefore, G
is not a minimally connected. Thus, if a connected graph is not a tree then it is not minimally
connected. This is equivalent to saying that if a connected graph is minimally connected then
it is a tree (contrapositive).
Conversely, suppose G is a connected graph which is not minimally connected, then
there exists an edge e in G such that G-e is connected. Therefore, e must be in some cycle in
G. this implies that G is not a tree. Thus, if a connected graph is not minimally connected then
it is not a tree. This is equivalent to saying that if a connected graph is a tree, then it is minimally
connected (contrapositive).
Hence the theorem.

Rooted Trees
Let D be a directed graph and G be its underlying graph. We say that D is a directed tree
whenever G is a tree. Thus, a directed tree is a directed graph whose underlying graph is a tree.
A directed tree T is called a rooted tree if (i) T contains a unique vertex, called the root whose
in-degree is equal to 0, and (ii) the in – degrees of all other vertices of T are equal to 1.
22CDT45A

A vertex v (other than the root r) of a rooted tree is said to be at the k – th level or has level
number k if the path from r to v is of length k. if 𝑣1 and 𝑣2 are two vertices such that 𝑣1 has
a lower level number than 𝑣2 and there is a path from 𝑣1 to 𝑣2 , then we say that 𝑣1 is an ancestor
𝑣2 , or that 𝑣2 is a descendant of 𝑣1 . In particular, if 𝑣1 and 𝑣2 are such that 𝑣1 has a lower
level number than 𝑣2 and there is an edge (- directed edge, actually) from 𝑣1 to 𝑣2 , then 𝑣1 is
called a parent of 𝑣2 , or 𝑣2 is called that child of 𝑣1 . Two vertices with a common parent are
referred to as siblings.
In a rooted tree a vertex whose out – degree is 0 is called a leaf and a vertex which is not a leaf
is called an internal vertex.
For example

1) 𝑣1 and 𝑣2 are at the first level, 𝑣3, 𝑣4 are at the second level, 𝑣5, 𝑣6 𝑣7 are at the third
level, and 𝑣8 and 𝑣9 are at the fourth level.
2) 𝑣1 is the ancestor of 𝑣3, 𝑣5 𝑣6 (or 𝑣3, 𝑣5 , 𝑣6 are the descendants of 𝑣1 ), and 𝑣2 is the
ancestor of 𝑣4, 𝑣7 𝑣8 𝑣9 (or 𝑣4, 𝑣7 𝑣8 𝑣9 are the descendants of 𝑣2 )
3) 𝑣1 is the parent of 𝑣3, (or 𝑣3, is a child of 𝑣1 )
4) 𝑣5 and 𝑣6 are siblings, and 𝑣8 𝑎𝑛𝑑 𝑣9 are siblings.
5) 𝑣5, 𝑣6 𝑣8 𝑣9 are leaves ,and all other vertices are internal vertices.

m-ary Tree
A rooted tree T is called an m-ary tree if every internal vertex of T is of out-degree ≥ m; that
is if every internal vertex of T has at most m children.
A rooted tree T is called a complete m-ary tree if every internal vertex of T is of out-degree m;
that is if every internal vertex of T has exactly m children.
22CDT45A

Binary Tree
An m-ary tree for which m =2 is called a binary tree.
In other words, a rooted tree T is called a binary tree if every vertex of T is of out-degree ≤
2; that is if every vertex has at most two children.
A complete m-ary tree for which m = 2 is called a complete binary tree.
In other words, a rooted tree T is called a complete binary tree if every internal vertex of T is
of out-degree 2; that is if every internal vertex has exactly two children.

Balanced Tree
If T is a rooted tree and h is the largest level number achieved by a leaf of T, then T is said to
have height h. A rooted tree of height h is said to be balanced if the level number of every leaf
is h or h-1.
Full binary Tree
Let T be a complete binary tree of height h. Then T is called a full binary tree if all the leaves
in T are at level h.

Distance and Centers in a tree


In a connected graph G, the distance d (𝑣𝑖 , 𝑣𝑗 ) between two of its vertices 𝑣𝑖 , 𝑎𝑛𝑑 𝑣𝑗 is the
length of the shortest path (i.e., the number of edges in the shortest path) between them.
22CDT45A

Some of the paths between vertices 𝑣1 and 𝑣2 in Fig 1. are (a, e), (a, c, f), (b, c, e), (b, f), (b, g,
h), and (b, g, i, k). There are two shortest paths, (a, e) and (b, f), each of length two. Hence
𝑑(𝑣1 , 𝑣2 ) = 2.

Fig 1

The eccentricity E(𝑣) of a vertex 𝑣 in a graph G is the distance from 𝑣 to the vertex farthest
from 𝑣 in G; that is, E(𝑣) = max 𝑑(𝑣, 𝑣𝑖 ).
𝑣𝑖𝜖𝐺

A vertex with minimum eccentricity in graph G is called a center of G.

Fig 2.
The eccentricities of the four vertices in Fig 2. are E(a) = 2, E (b) = 1, E(c) =2, and E(d) =2.
Hence, vertex b is the center of that tree.

On the other hand consider the tree in Fig. 3-9. The eccentricity of each of its six vertices is
shown next to the vertex. This tree has two vertices having the same minimum eccentricity.
Hence this tree has two centers. Some authors refer to such centers as bicenters;

Fig. 3-9 Eccentricities of the vertices of a tree


22CDT45A

Finding the center of a tree.

Problems:

1. Which of the following is a rooted tree?

Sol: (a) Rooted tree (b) Not a rooted tree


22CDT45A

2. For the rooted tree shown below, find the levels of the vertices A, H, F, M

Figure 3.12
Sol: A: 1, H: 3, F: 2, M: 4.

3. In the rooted tree shown in Figure 3.12, identify


i) all ancestors of L, ii) all descendants of A,
iii) the children of B, iv) the parent and siblings of C.
Sol: (i) r, B, G (ii) C, D, H
(iii) E, F, G (iv) Parent: A, Sibling: D

4. In the tree shown in Figure 3.13, identify the following:


i) vertices which are leaves ii) the root iii) the parent of g
iv) descendants of c v) siblings of s vi) vertices having the level number-4.

Figure 3.13
Sol: (i) k, p, h, q, s, t, f (ii) a (iii) d
(iv) e, f, j, q, s, t (v) q, t (vi) k, p, q, s, t

Homework:

1. Which of the following is a binary tree? Complete binary tree?


22CDT45A

2. Which of the following is a balanced tree? Full binary tree?

Spanning Trees
Let G be a connected graph. A subgraph T of G is called a spanning tree of G if (i) T is a
tree, and (ii) T contains all vertices of G.
A spanning tree is also called a maximal tree.
The edges of a spanning tree are called its branches.
If G has n vertices, a spanning tree of G must have n vertices and n -1 edges.
If T is a spanning tree of graph G, then the edges of G which are not in T (if any such exist)
are called the chords of G with respect to T. The set of all chords of G is the complement of T
in G. This set is called the chord-set or cotree of T in G and is denoted by 𝑇̅. Evidently,
G=T ∪ 𝑇̅.

Problem 1: Find all the spanning trees of the graph shown below:

Figure 3.23
A spanning tree of a given graph must be a subgraph which is a tree and must contain all
the vertices of the graph. Since there are four vertices, this tree must have three edges. Each
22CDT45A

such tree is got by deleting one edge from the graph. There are four such trees; these are
shown in figures 3.24(a), (b), (c), (d).

Figure 3.23

Problem 2: Find all the spanning trees of the graph shown below:

In the given graph, there are four vertices. Its spanning tree must include all these four
vertices and three of the edges. Each such tree is got by deleting two edges from the graph.
There are eight such trees; these are shown in figures 3.26(a)-(h).

Homework
1. Find all the spanning trees of the graph shown below:
22CDT45A

Minimal Spanning Tree

Let G be a graph and suppose there is a positive real number associated with each edge of G.
Then, G is called a weighted graph and the positive real number associated with an edge e (of G)
is called the weight of the edge e.

The sum of the weights of all the branches of T is called the weight of tree T.

If G is a connected, weighted graph, then the weight of an edge e of G is denoted by wt(e) and
the weight of a spanning tree T of G is denoted by wt(T).

A spanning tree whose weight is the least is called a minimal spanning tree of the graph.

Example:

This graph has three spanning trees as shown below:

We observe that the weights of these three spanning trees are 16, 13 and 15 respectively. Of
these trees, the second one has the minimum weight. This is a minimal spanning tree for the
graph in Figure 4.1.

Algorithm for Minimal Spanning Tree

There are several methods of constructing minimal spanning trees. Below we give the working
rules (algorithm) of two such methods.
22CDT45A

Kruskal’s Algorithm

The working rule for the Kruskal’s method (usually called Kruskal’s algorithm) may be
stated as follows:
Step 1. Given a connected, weighted graph G with n vertices, list the edges of G in the order
of increasing weights.
Step 2. Starting with a smallest weighted edge, proceed sequentially by selecting one edge at
a time such that no cycle is formed.
Step 3. Stop the process of Step 2 when(n-1) edges are selected. These (n-1) edges constitute
a minimal spanning tree of G.
Remarks
(1) If two are more edges have the same weight, there will be more than one listing of
edges in increasing order of weights. Different listings may yield different minimal
spanning trees. As such, Kruskal’s algorithm does not determine a unique minimal
spanning tree.
(2) The process in step 2 is called a Greedy process.

Problem 1: Using the Kruskal’s algorithm, find a minimal spanning tree of the weighted graph
shown below.

Sol:
Edge CR QR BP CQ AB AP CP AC BQ
Weight 3 3 5 5 6 6 7 8 10
Select? Yes Yes Yes No Yes No Yes

Thus, a minimal spanning tree of the given graph contains the five edges CR, QR, BP,AB,
CP. This shown in Figure 4.4 The weight of this tree is 24 units.
22CDT45A

Problem 2: Using the Kruskal’s algorithm, find a minimal spanning tree of the weighted graph
shown below.

Sol:
Edge CR PR QR BQ BR AB BC AQ PQ
Weight 5 7 7 8 9 10 10 11 12
Select? Yes Yes Yes Yes No Yes Yes

Thus, a minimal spanning tree of the given graph contains the five edges CR, PR, QR, BQ,
AB. This tree is shown in Figure 4.6. The weight of the tree is 37 units.

Problem 3: Using the Kruskal’s method, determine a minimal spanning tree for the graph
shown below.

Sol: The given graph has 9 vertices and therefore a spanning tree thereof will have 8 edges.
Let us put the edges of the graph in a non-decreasing order of their weights and go on selecting
8 edges one by one in such a way that no cycle is created. The scheme is summarized below.
Edge eh ab bc de ef ad ae be bf cf dg hi fi gh ei dh
Weight 1 2 2 2 2 3 3 3 3 3 3 3 3 3 4 5
Select? Yes Yes Yes Yes Yes Yes No No No No Yes Yes
22CDT45A

Thus, the edges eh, ab, bc, de, ef, ad, dg, and hi constitute a minimal spanning tree for the
given graph. The weight of this tree is 18 units. The tree is shown in Figure 4.10.

Problem 4: Eight cities A,B,C,D,E,F,G,H are required to be connected by a new railway


network. The possible tracks and the cost of involved to lay them (in crores of rupees) are
summarized in the following Table:

Track between Cost Track between Cost


A and B 155
D and F 100
A and D 145
E and F 150
A and G 120
F and G 140
B and C 145
F and H 150
C and D 150
G and H 160
C and E 95
Determine a railway network of minimal cost that connects all these cities.

Sol:
Edge CF DF AG FG AD BC CD EF FH AB GH
Weight 95 100 120 140 145 145 150 150 150 155 160
Select? Yes Yes Yes Yes No Yes Yes No Yes
Thus, a minimal spanning tree of the given graph consists of the branches CE, DF, AG, FG,
BC, CD, FH. This tree represents the required railway network. The network is shown in
Figure 4.12.
22CDT45A

The cost involved is


95+100+120+140+145+150+150=900(in crores of Rupees).

H.W Apply the Kruskal’s algorithm to find a minimal spanning tree for the weighted graph
shown below.

Prim’s Algorithm

The working rules for the prim’s method (usually called Prim’s algorithm) may be stated as
follows.
Step 1. Given a connected, weighted graph G with n vertices, assign n names (say: 𝑣1 𝑣2 …𝑣𝑛
or A B C and so on) to these vertices, and prepare a n × n Table in which the weights
of all edges are shown. The entries in the table will be symmetric with respect to the
diagonal and no entries appear on the diagonal. Indicate the weights of the non-existing
edges as ∞.

Step 2. Start from the vertex 𝑣1 (or A, as the case may be) and connect it to its nearest neighbor
(i.e., to the vertex which has the smallest entry) in the 𝑣1 – row, say 𝑣𝑘 . Now, consider
the edge {𝑣1 , 𝑣𝑘 } and connect it to its closest neighbor (i,e,. to a vertex, other than
𝑣1 𝑎𝑛𝑑 𝑣𝑘 , that has the smallest entry among all entries in 𝑣1 𝑎𝑛𝑑 𝑣𝑘 rows). Let this
vertex be 𝑣𝑚.

Step 3. Start from the vertex 𝑣𝑚 and repeat the process of step 2. Stop the process when all the
n vertices have been connected by n-1 edges. These n -1 edges constitute a minimal
spanning tree.
22CDT45A

Remarks
1. In the process of connecting an edge to its nearest neighbor as explained above, care
has to be taken that cycles are not created by the connections.
2. Like in the Kruskal’s method, a minimal spanning tree determined by the Prim’s
method is not unique.

Problem 1: Using Prim’s algorithm, find a minimal spanning tree for the weighted graph
shown below.

Sol: Let us tabulate the weights of the edges between every pair of vertices as shown below:
𝒗𝟏 𝒗𝟐 𝒗𝟑 𝒗𝟒 𝒗𝟓
𝒗𝟏 - 4 ∞ ∞ 5
𝒗𝟐 4 - 3 6 1
𝒗𝟑 ∞ 3 - 6 2
𝒗𝟒 ∞ 6 6 - 7
𝒗𝟓 5 1 2 7 -

Accordingly, the edges {𝑣1 𝑣2 }, {𝑣2 𝑣5 }, {𝑣5 𝑣3 } together with the edge {𝑣2 𝑣4 } or the edge
{𝑣3 𝑣4 } constitute a minimal spanning tree. Thus, for the given graph, there are two minimal
spanning trees as shown in figure 4.14(a), (b). The weight of each of these trees is 13 units.
22CDT45A

Problem 2: Using Prim’s algorithm, find a minimal spanning tree for the weighted graph
shown below.

Sol: The given graph has 6 vertices. Therefore, a minimal spanning tree thereof has 5 edges.
We first prepare the following table of weights of edges.
A B C D E F
A - 2 4 ∞ ∞ 2
B 2 - 1 ∞ 4 ∞
C 4 1 - 3 3 1
D ∞ ∞ 3 - 3 5
E ∞ 4 3 3 - 4
F 2 ∞ 1 5 4 -
The edges {A,B}, {B,C}, {C,F}, {C,D} belong to a minimal tree. The vertex left over is E
which is joined to B,C,D,F in the given graph. Among the edges that contain E, the edges EC
and ED have equal minimum weights, 3. Therefore, we can include either of these edges in the
minimal spanning tree.
Thus, for the given graph we get two minimal spanning trees shown in Figures 4.16(a),(b).

We note that the weight of each of the above trees is 10 units.


22CDT45A

H.W 1: Using Prim’s algorithm, find a minimal spanning tree for the weighted graph shown
below.

H.W 2: Apply Prim’s algorithm to find a minimal spanning tree for the graph shown in Figure
4.7

Algorithm for the Shortest Path

Let D =D(V, E) where V = {1,2,3,…n} is the vertex set, be a weighted, directed network in
which the weight of every directed edge (arc) is non negative. Suppose we wish to find a path
with minimum weight from any chosen vertex, say vertex 1, to any other vertex s of D, if there
is a directed path from 1 to s. Such a path is called a shortest path.

There are several methods of constructing shortest path. Below we give the working rules
(algorithm) of two such methods.

Dijkstra’s Algorithm

Dijkstra’s algorithm that enables us to find the paths of minimum weight, directed networks.

Problem 1: Using the Dijkstra’s algorithm, obtain the shortest path from vertex 1 to each of
the other vertices in the weighted, directed network shown in Figure . Indicate the weights of
these shortest paths.
22CDT45A

Sol:
→ First Iteration
For this iteration, P = P{1} and 𝑡𝑗 = 𝑞1𝑗 for j =2, 3, 4, 5, 6, 7. By examining the figure, we
find that**

𝑡2 =4, 𝑡3 = 6, 𝑡4 = 8, 𝑡5 = ∞, 𝑡6 = ∞, 𝑡7 = ∞.

Step 1. We note that 𝑡𝑗 is minimum for j=2, the minimum being 𝑡2 = 4, Therefore, we label
the arc (1.2) as 𝑝1. Also, we adjoin 2 to P, so that P = {1,2} for the next step.

Step 2. We have P = {1, 2} and 𝑡2 = 4 (from step 1). We choose

new 𝑡3 = min {𝑡3 , 𝑡2 + 𝑞23 }


= min {6, 4 + 1} = 5,
new 𝑡4 = min {𝑡4 , 𝑡2 + 𝑞24 }
= min {8, 4 + ∞} = 8,
new 𝑡5 = min {𝑡5 , 𝑡2 + 𝑞25 }
= min {∞, 4 + 7} = 11,
new 𝑡6 = min {𝑡6 , 𝑡2 + 𝑞26 } = ∞,
new 𝑡7 = min {𝑡7 , 𝑡2 + 𝑞27 } = ∞.

→ Second Iteration
For this iteration, P = {1,2}, and 𝑡2 = 4. Also, 𝑡3 = 5; 𝑡4 = 8, 𝑡5 = 11, 𝑡6 = ∞, 𝑡7 = ∞, (found
above – new ones).

Step 1. We note that among the new 𝑡𝑗 ’s, the minimum 𝑡𝑗 occurs for j =3, the minimum being
𝑡3 = 5, We observe that among the arcs from vertices in P to the vertex 3, the arc (2,
3) has the least weight. We therefore label the arc (2, 3) as 𝑝2 . Also, we adjoin 3 to P,
so that P = {1, 2, 3} for the next step.
Step 2. We have P = {1, 2, 3}, 𝑡2 = 4, 𝑡3 = 5. We choose.
new 𝑡4 = min {𝑡4 , 𝑡3 + 𝑞34 } = min {8, 5 + 2} = 7,
new 𝑡5 = min {𝑡5 , 𝑡3 + 𝑞35 } = min {11, 5 + 5} = 10,
new 𝑡6 = min {𝑡6 , 𝑡3 + 𝑞36 } = min {∞, 5 + 4} = 9,
new 𝑡7 = min {∞, 5 + 𝑞37 } = ∞
→ Third Iteration
For this iteration P = {1, 2, 3}, 𝑡3 = 5. Also, 𝑡4 = 7, 𝑡5 = 10, 𝑡6 = 9, 𝑡7 = ∞ (Found
above – new ones)
22CDT45A

Step 1. We note that among the new 𝑡𝑗 ’s, the minimum 𝑡𝑗 occurs for j = 4, the minimum
being 𝑡4 = 7. We label the arc (3, 4) as 𝑝3 , which has the least weight among all
arcs having initial vertices in P and final vertex as 4. Further, we adjoin 4 to P, so that
P ={1, 2, 3, 4} for the next step.
Step 2. We have P = {1, 2, 3, 4}, 𝑡2 = 4, 𝑡3 = 5, 𝑡4 = 7. We choose
new 𝑡5 = min {𝑡5 , 𝑡4 + 𝑞45 } = min {10, 7 + ∞} = 10,
new 𝑡6 = min {𝑡6 , 𝑡4 + 𝑞46 } = min {9, 7 + 5} = 9,
new 𝑡7 = min {𝑡7 , 𝑡4 + 𝑞47 } = ∞.
→ Fourth Iteration
For the iteration, P = {1, 2, 3, 4}, 𝑡2 = 4, 𝑡3 = 5, 𝑡4 = 7. Also, 𝑡5 = 10, 𝑡6 = 9, 𝑡7 = ∞
(Found above – new ones)
Step 1. We note that among the new 𝑡𝑗 ’s, the minimum 𝑡𝑗 occurs for j = 6, the minimum
being 𝑡6 = 9. We label the arc (3, 6) as 𝑝4 , which has the least weight among all
arcs having initial vertices in P and terminal vertex as 6. Also, we adjoin 6 to P, so
that P = {1, 2, 3, 4, 5, 6}for the next step.
Step 2. We have P = {1, 2, 3, 4, 6}, 𝑡2 = 4, 𝑡3 = 5, 𝑡4 = 7, 𝑡6 = 9. We choose
new 𝑡5 = min {𝑡5 , 𝑡6 + 𝑞65 } = min {10, 9 + 1} = 10,
new 𝑡7 = min {𝑡7 , 𝑡6 + 𝑞67 } = min {∞, 9 +8} = 17.
new 𝑡7 = min {𝑡7 , 𝑡4 + 𝑞47 } = ∞.
→ Fifth Iteration
For this iteration, P = {1, 2, 3, 4, 6}, 𝑡2 = 4, 𝑡3 = 5, 𝑡4 = 7, 𝑡6 = 9. Also, 𝑡5 = 10, 𝑡7
=17, (Found above – new ones)
We note that among the new 𝑡𝑗 ’s, the minimum 𝑡𝑗 occurs for j = 5, the minimum being 𝑡5 =
10. We label the arc (6, 5) as 𝑝5 , which has the least weight among all arcs from the vertices
in P to the vertex 5. Also, we adjoin 5 to P, so that P = {1, 2, 3, 4, 6, 5,} for the next step.

→ Final Step
At this stage, we note that only one 𝑡𝑗 is left over in P (namely 𝑡7 ). We observe that
among all arcs from the vertices in P to the vertex 7, the arc (5, 7) has the least weight.
Therefore, we label the arc (5,7) as 𝑝6 . Also we adjoin 7 to P.
Thus we have P = {1, 2, 3, 4, 5, 6, 7,} which is the vertex set. We stop the process.
The arcs labeled as 𝑝1 to 𝑝6 are
(1, 2), (2, 3), (3, 4), (3, 6), (6, 5), (5, 7).
The shortest path arboresence formed by these arcs is shown below:

From the above figure, we note that the following are the shortest paths (and their weights)
from vertex 1 to all other vertices.
22CDT45A

1) Path from 1 to 2: 1 → 2; weight 4.


2) Path from 1 to 3: 1 → 3; weight 5.
3) Path from 1 to 4: 1 → 2 → 3 → 4; weight 7.
4) Path from 1 to 5: 1 → 2 → 3 → 6 → 5 ; weight 10.
5) Path from 1 to 6: 1 → 2 → 3 → 6; weight 9.
6) Path from 1 to 7: 1 → 2 → 3 → 6 → 5 → 7 ; weight 16.

Problem 2: Apply Dijkstra’s algorithm, to the weighted digraph shown in Figure 4.55 in order
to find the shortest distance from the vertex c to each of the other vertices.

Sol:
→ First, let us rename the vertices as indicated below:
c : 1, a : 2, b : 3, f : 4, g :5, h : 6.
→ First Iteration
For this iteration, P = {1} and 𝑡𝑗 = 𝑞1𝑗 𝑓𝑜𝑟 𝑗 =2, 3, 4, 5, 6. By examining the figure, we find
that
𝑡2 = ∞, 𝑡3 = ∞, 𝑡4 = 6, 𝑡5 = ∞, 𝑡6 = 11.

Step 1. We note that 𝑡𝑗 is minimum for 𝑗 = 4, the minimum being 𝑡4 = 6, Therefore, label the
arc (1, 4) as 𝑝1. Also we adjoin 4 to P so that P = {1, 4} for the next step.

Step 2. We have P = {1, 4}, and 𝑡4 = 6.(from step). We choose.


new 𝑡2 = min {𝑡2 , 𝑡4 + 𝑞42 } = min {∞, 6 + ∞} = ∞,
new 𝑡3 = min {𝑡3 , 𝑡4 + 𝑞43 } = min {∞, 6 + ∞} = ∞,
new 𝑡5 = min {𝑡5 , 𝑡4 + 𝑞45 } = min {∞, 6 + 9} = 15,
new 𝑡6 = min {𝑡6 , 𝑡4 + 𝑞46 } = min {11, 6 + 4} = 10,
22CDT45A

→ Second Iteration
For this iteration, P = {1, 4,} and 𝑡4 = 6. Also, 𝑡2 = ∞, 𝑡3 = ∞, 𝑡5 = 15, 𝑡6 = 10
(found above – new ones)
Step 1. We note that among the new 𝑡𝑗 ’s, the minimum j = 6, the minimum being 𝑡6 = 10.
We observe that among the arcs from the vertices in P to the vertex 6, the arc (4, 6)
has the least weight. Let us label this arc (4, 6) as 𝑝2 , Also, we adjoin 6 to P so that P
= {1, 4, 6} for the next step.

Step 2. We have P = {1, 4, 6}, 𝑡4 = 6, 𝑡6 = 10, We choose


new 𝑡2 = min {𝑡2 , 𝑡6 + 𝑞62 } = min {∞, 10 + ∞} = ∞,
new 𝑡3 = min {𝑡3 , 𝑡6 + 𝑞63 } = min {∞, 10 +∞} = ∞,
new 𝑡5 = min {𝑡5 , 𝑡6 + 𝑞65 } = min {15, 10 + 4} = 14.

→ Third Iteration
For this iteration, P = {1, 4, 6}, 𝑡4 = 6, 𝑡6 = 10. Also, 𝑡2 = ∞, 𝑡3 = ∞, 𝑡5 = 14, (found
above – new ones)

Step 1. We note that among the new 𝑡𝑗 ’s, 𝑡𝑗 is minimum for j = 5, the minimum being 𝑡5 =
14.
Among the arcs from the vertices in P to the vertex 5, the arc (6, 5) has the least
weight.
Let us label this arc (6, 5) as 𝑝3 . Also, we adjoin 5 to P so that P = {1, 4, 6, 5} for the
next step.

Step 2. We have P = {1, 4, 6, 5}, 𝑡4 = 6, 𝑡6 = 10, 𝑡5 = 14, We choose


new 𝑡2 = min {𝑡2 , 𝑡5 + 𝑞52 } = min {∞, 14 + ∞} = ∞,
new 𝑡3 = min {𝑡3 , 𝑡5 + 𝑞53 } = min {∞, 14 +∞} = ∞,

→ Fourth Iteration
For this iteration, P = {1, 4, 6, 5}, 𝑡4 = 6, 𝑡6 = 10, 𝑡5 = 14. Also, 𝑡2 = ∞, 𝑡3 = ∞,
(found above – new ones)
Since both of the new 𝑡𝑗 ’s are equal, we chose one of the j’s, say j = 2. Among the
arcs from the vertices in P to the vertex 2, the arc (4, 2) has least weight. Let us label
this arc (4, 2) as 𝑝4 . Also, we adjoin 2 to P so that P = {1, 4, 6, 5, 2} for the next
step.
22CDT45A

Final Step
At this stage, the vertex left over in P is 3. Among the arcs from the vertices in P to
the vertex 3, the arc (2, 3) has the least weight. Let us label this arc (2, 3) as 𝑝5 .
Further, we adjoin 3 to P so that P = {1, 4, 6, 5, 2, 3}. Now P covers all vertices.
Therefore, we stop the process.

The arcs that we have labeled as 𝑝1, 𝑝2 …. 𝑝5 are


(1, 4), (4, 6), (6, 5), (4, 2), (2, 3).
In the notation of Figure 4.55, these arcs read
(c,f), (f, h), (h, g), (f, a), (a, b).
The shortest path arborescence formed by these arcs is shown below:

From the above figure, we note that the following are the shortest path (and their weights
(distances) from c to other vertices:
1) Path from c to f: c → f; distance 6,
2) Path from c to h: f → h; distance 10,
3) Path from c to g: c → f → h → 𝑔; distance 14,
4) Path from c to a: c → f → a; distance 17,
5) Path from c to b: c → f → a → 𝑏; distance 22.

H.W 1: Obtain the shortest distance from vertex 1 to vertex 7 in the network shown in Figure 8.3.
by Dijkstra’s Algorithm.

Figure 8.3
22CDT45A

H.W 2: Obtain the shortest distance from the vertex a to vertex z in the network shown in Figure by
Dijkstra’s Algorithm.

Floyd-Warshall Shortest Path Algorithm

Given a directed network D (V, E), where V = {1, 2, ….n} is the vertex set, let us define 𝑞𝑖𝑗 as
in Dijkstra’s algorithm. Then the matrix 𝒬 = [𝑞𝑖𝑗 ] is called the weight matrix of D. The initial
path matrix P [𝑝𝑖𝑗 ] is the n × n matrix defined by 𝑝𝑖𝑗 = j. The algorithm consists of n iterations,
and iteration r (based at vertex 1) begins with two matrices 𝒬𝑟−1 and 𝑃𝑟−1 and ends with 𝑄𝑟
and 𝑃𝑟 . Initially, 𝑄0 = 𝑄 and 𝑃0 = P. The (i, j) entries in 𝑄𝑟 and 𝑃𝑟 are denoted by 𝑞𝑟 (i, j) and
𝑃𝑟 (i, j) respectively. For a specified r, the matrices 𝑄𝑟 and 𝑃𝑟 are obtained from 𝒬𝑟−1 and 𝑃𝑟−1
by applying the following rule, known as the triangle (triple) operation:
If 𝑞𝑟−1 (i, j) ≤ 𝑞𝑟−1 (i, r) + 𝑞𝑟−1 ( r, j) , then 𝑞𝑟 (i, j) = 𝑞𝑟−1 (i, j) and 𝑃𝑟 (i, j) = 𝑃𝑟−1 (i, j).
Otherwise,
𝑞𝑟 (i, j) = 𝑞𝑟−1 (i, r) + 𝑞𝑟−1 ( r, j) and 𝑃𝑟 (i, j) = 𝑃𝑟−1 (i, r).
When the algorithm terminates, we are left with the SD (shortest distance) matrix 𝑄𝑛 / 𝐷𝑛 and
the SP (shortest path) matrix 𝑃𝑛 . The (i, j) entry in 𝑄𝑛 is the shortest distance between the
vertices i and j, and the (i, j) entry in 𝑃𝑛 is the first vertex after i in a shortest path from i to j.

Problem 1. By using the Warshall – Floyd algorithm, obtain the SD matrix and the SP matrix
for the network shown in Figure 8.5.

Figure 8.5.
22CDT45A

Sol: We first write down the matrices 𝒬 and P. These are


0 4 −3 ∞ 1 2 3 4
−3 0 −7 ∞ 1 2 3 4]
𝒬= [ ] P= [
∞ 10 0 3 1 2 3 4
5 6 6 0 1 2 3 4

Iteration 1. We begin with 𝑄0 = 𝑄 and 𝑃0 = P. In performing the triangle operation based at


the vertex 1, the only change is at the (4,3) entry:
𝑞1 (4,3) = min {𝑞0 (4, 3), 𝑞0 (4, 1) + 𝑞0 (1, 3) }
= min {6, 5 − 3} = 2.
Then, 𝑝1 (4,3) = 𝑝0 (4, 1) = 1. Thus, we get

0 4 −3 ∞ 1 2 3 4
−3 0 −7 ∞ 1 2 3 4]
𝑄1 = [ ], 𝑃1 = [
∞ 10 0 3 1 2 3 4
5 6 2 0 1 2 1 4

Iteration 2. We begin with 𝑄1 and 𝑃1 , and construct 𝑄2 and 𝑃2 by using the triangle operation
based at vertex 2. The changes occur at places (3, 1), (4, 1) and (4, 3) in 𝑄1 and at places (3,
1), (4, 1) and (4, 3) in 𝑃1 . We find that
𝑞2 (3, 1) = 7, 𝑞2 (4, 1) =3, 𝑞2 (4, 3) = -1,
𝑝2 (3, 1) = 2, 𝑝2 (4, 1) = 2, 𝑝2 (4, 2) = 2.
Thus, we obtain

0 4 −3 ∞ 1 2 3 4
−3 0 −7 ∞ 1 2 3 4
𝑄2 = [ ], 𝑃2 = [ ]
7 10 0 3 2 2 3 4
3 6 −1 0 2 2 2 4
Iteration 3. We begin with 𝑄2 and 𝑃2 , and construct 𝑄3 and 𝑃3 by using the triangle
operation based at vertex 3. We find that

0 4 −3 0 1 2 3 3
−3 0 −7 −4] , 1 2 3 3]
𝑄3 = [ 𝑃3 = [
7 10 0 3 2 2 3 4
3 6 −1 0 2 2 2 4

Iteration 4. Beginning with 𝑄3 and 𝑃3 , we obtain the following matrices with the use of the
triangle operation.

0 4 −3 0 1 2 3 3
−3 0 −7 −4] , 1 2 3 3]
𝑄3 = [ 𝑃3 = [
7 10 0 3 2 2 3 4
3 6 −1 0 2 2 2 4

0 4 −3 0 1 2 3 3
−3 0 −7 −4] , 1 2 3 3]
𝑄4 = [ 𝑃4 = [
6 9 0 3 4 4 3 4
3 6 −1 0 2 2 2 4
22CDT45A

Since the given network has 4 vertices, we stop the process with the 4th iteration.
The matrix 𝑄4 is the SD matrix and the matrix 𝑃4 is the SP matrix, for the given network.
The SD matrix 𝑄4 gives the shortest distance between every pair of vertices in the network.
[ For example, the shortest distance from vertex 1 to vertex 4 is given by the (1, 4) entry in 𝑄4 ,
namely 0. By looking at Figure 8.5, we find that this shortest distance corresponds to the path 1
→ 3 → 4. Similarly, the entry (3, 2) in 𝑄4 is 9; therefore, the shortest distance from vertex 3 to
vertex 2 is 9. This corresponds to the path 3 → 4 → 2. (Observe that the edge from 3 to 2 does
not correspond to the shortest distance). The entry (2, 3) in 𝑄4 is -7; this is the shortest distance
from vertex 2 to vertex 3, and the corresponding path is the edge (2, 3) itself.
The SP matrix 𝑃4 gives the shortest path between every pair of vertices in the network. For
example, the (1, 4) entry in 𝑃4 is 3. This means that in an SP from 1 to 4, the first vertex after 1 is
3. The (3, 4) entry in 𝑃4 is 4. So, an SP from 1 to 4 is 1 → 3 → 4.

H.W 1: Using the Warshall – Floyd algorithm, find the SD matrix and the SP matrix for the
network shown in Figure 8.8.

Figure 8.8

*****

You might also like