ComputationalMathematics - Chapter 3

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

Lecture Notes on Computational Mathematics

Dr. K. Manjunatha Prasad


Professor of Mathematics,
Department of Data Science, PSPH
Manipal Academy of Higher Education, Manipal, Karnataka-576 104
kmprasad63@gmail.com, km.prasad@manipal.edu
Lecture Notes on Computational Mathematics

Dr. K. Manjunatha Prasad


Department of Data Science, PSPH
Manipal Academy of Higher Education, Manipal, India

M.Sc Data Science/Biostatistics/Digital Epidemiology (I sem), Batch 2021-2022

1
Graphs and algorithms

1. Graphs and algorithms

1.1 Preliminaries

Definition 1.1. A graph G consists of a finite nonempty set V (G) of n elements called vertices (or points)
together with a prescribed set E(G) of m unordered pairs of distinct elements of V (G).

Sometimes, we simply use the notations V and E for V (G) and E(G), respectively. Each pair e = { i, j }
of elements in E(G) is an edge (or line) of G, and e is said to join i and j. In this case we write i ∼ j to
say that i and j are adjacent vertices and we also say that vertex i and edge e are incident with each
other, as are j and e. If two distinct edges e i and e j are incident with a common vertex, then they are
said to be adjacent edges. A graph with n vertices and m edges is called an (n, m) graph.
Note: The definition of graph permits no loop, that is, no edge joining a vertex to itself. In a
multigraph, no loops are allowed but more than one edge can join two vertices, such edges are called
multiple edges. If both loops and multiple edges are permitted, we call it a pseudograph.
Remark: It is customary to represent a graph by a diagram and refer to the diagram itself as the
graph. Each vertex is represented by a small dot and each edge is represented by a line segment joining
the two vertices with which the edge is incident. Thus a diagram of the graph depicts the incidence
relation holding between its vertices and edges. While drawing a graph it is immaterial whether the
edges are drawn straight or curved, long or short.

Definition 1.2. A graph in which any two distinct vertices are adjacent is called a complete graph.

The complete graph with n vertices is denoted by K n .

Definition 1.3. A graph whose edge set is empty is called a null grpah or a totally disconnected graph.

Definition 1.4. A graph G is called a bigraph or bipartite graph if V can be partitioned into two disjoint
subsets V1 and V2 such that every edge of G joins a vertex of V1 to a vertex of V2 . (V1 , V2 ) is called a
bipartition of G. If further G contains every edge joining the vertices of V1 to the vertices of V2 then G is
called a complete bigraph.

If V1 conains m vertices and V2 contains n vertices then the complete bigraph G is denoted by K m,n .

Definition 1.5. The degree of a vertex i in a graph G is the number of edges incident with i.

The degree of i is denoted by de g(i). A vertex i of degree 0 is called an isolated vertex. A vertex i of
degree 1 is called an end vertex.

Theorem 1.1. The sum of the degrees of the vertices of a graph G is twice the number of edges.

Proof. Every edge of G is incident with two vertices. Hence every edge contributes 2 to the sum of the
degrees of the vertices. Hence the result follows.

Lecture Notes 2
1.1 Preliminaries

Corollary 1.1. In any graph G, the number of vertices of odd degree is even.

Proof. The sum of degrees of the vertices of odd degree and even degree equals the sum of the degrees
of all the vertices of G and hence is an even number (by previous theorem). Thus the sum of the degrees
of the vertices of odd degree must be an even number and hence the number of such vertices must be
even.

Definition 1.6. A graph G in which all the vertices have same degree is called a regular graph of degree
r.

Definition 1.7. A graph H = (V1 , E 1 ) is called a subgraph of G = (V , E) if V1 ⊂ V and E 1 ⊂ E. If H is a


subgraph of G we say that G is a supergraph of H. H is called a spanning subgraph of G if V1 = V . H is
called an induced subgraph of G if H is the maximal subgraph of G with vertex V1 .

Thus, if H is an induced subgraph of G, two vertices are adjacent in H iff they are adjacent in G. If
V2 ⊂ V , then the induced subgraph of G with vertex set V2 is called the subgraph of G induced by V2 . If
E 2 ⊂ E, then the subgraph of G with edge set E and having no isolated vertices is called the subgraph
edge induced by E 2 .

Definition 1.8. A walk of a graph G is an alternating sequence of vertices and edges v0 , e 1 , v1 , e 1 , · · · , vn−1 ,
xn , vn beginning and ending with vertices such that every edge e i is incident with v i−1 and v i .

We say that the walk joins v0 and vn and it is called a v0 − vn walk. v0 is called the initial vertex and
vn is called the terminal vertex of the walk. The above walk is also denoted by v0 , v1 , · · · , vn the edges
of the walk being self evident. The number of edges (n) in the walk is called the length of this walk. A
single vertex is considered as a walk of length 0.

Definition 1.9. A walk is called a trail if all its edges are distinct and is called a path if all its vertices
are distinct.

A graph consisting of a path with n vertices is denoted by P n .

Definition 1.10. A v0 − vn walk is called closed if v0 = vn . A closed walk v0 , v1 , · · · , vn = v0 in which


n ≥ 3 and v0 , v1 , · · · , vn−1 are distinct is called a cycle of length n.

A graph consisting of a cycle of length n is denoted by C n .

Definition 1.11. Two vertices u and v of a graph G are said to be connected if there exists a u − v path
in G. A graph G is said to be connected if every pair of its vertices are connected. A graph which is not
connected is said to be disconnected.

Definition 1.12. A graph G is said to be minimally connected if removal of any one edge from it discon-
nects the graph.

Lecture Notes 3
1.2 Incidence matrix

A component or connected component of a graph G is a subgraph H of G in which any two


vertices are connected by a path, and is connected to no additional vertices in the supergraph.
Thus, a graph G is connected iff it has exactly one component.
We mention the following theorem without any proof:

Theorem 1.2. A graph G with at least two vertices is bipartite iff all its cycles are of even length.

Definition 1.13. Let G = (V , E) be a graph. The complement G C of G is defined to be the graph which
has V as its set of vertices and two vertices are adjacent in G C iff they are not adjacent in G.

Definition 1.14. A directed graph (or digraph) is a triple containing a vertex set V (G), an edge set
E(G) and a function assigning each edge an ordered pair of vertices.

We usually take V (G) to be {1, · · · , n} and E(G) to be { e 1 , · · · , e m }. We may refer to edges j 1 , j 2 , · · ·


when we actually mean edges e j 1 , e j 2 , · · · . Our emphasis is on undirected graphs. However, we do
consider directed graphs as well.
Further notions will be recalled as and when the need arises.

1.2 Incidence matrix

Definition 1.15. The (vertex-edge) incidence matrix Q(G) of a directed (n, m) graph G is an n × m
matrix whose rows and columns are indexed by V (G) and E(G) respectively, such that




0 vertex i and edge e j are not incident
(Q(G)) i j = 1 e j originates at i


−1 e j terminates at i

Example 1. Let G be the following graph:

e1 e2 e3

2 4
3

e4 e5 e6

Figure 1.1: The graph G

Lecture Notes 4
1.2 Incidence matrix

The incidence matrix of the graph in Figure 1.1 is,

e1 e2 e3 e4 e5 e6
 
1 −1 1 −1 0 0 0
 
2
 1 0 0 −1 0 0 
 
 
Q(G) = 3  0
 −1 0 0 1 0 

 
4  0 0 1 0 0 −1 
 
5 0 0 0 1 −1 1

Exercise 1.1. Find the incidence matrix of the following directed graph G:

v1

e1 e2

v3 v2
e3

Exercise 1.2. Find the incidence matrix of the following directed graph G:

v4 v3

e3 e2

v1

e1

v2

Exercise 1.3. Find the incidence matrix of the following directed graph G ≃ K 2,3 :

Lecture Notes 5
1.2 Incidence matrix

v1 v2

e4
e1 e3
e2 e5 e6

v3 v4 v5

1.2.1. Rank of Incidence matrix

For any graph G, the column sums of Q(G) are zero and hence the rows of Q(G) are linearly dependent.
We now proceed to determine the rank of Q(G).

Lemma 1.1. If G is a connected graph on n vertices, then rank Q(G) = n − 1.

Proof. Suppose x is a vector in the left null space of Q := Q(G), that is, x′ Q = 0. Then x i − x j = 0
whenever i ∼ j. It follows that x i = x j whenever there is an i j −path. Since G is connected, x must have
all components equal. Thus, the left null space of Q is at most one-dimensional and therefore the rank
of Q is at least n − 1. Also, as observed earlier, the rows of Q are linearly dependent and therefore rank
Q ≤ n − 1. Hence, rank Q = n − 1.

Theorem 1.3. If G is a graph on n vertices and has k connected components then rank Q(G) = n − k.

Proof. Let G 1 , . . . ,G k be the connected components of G. Then, after a relabeling of vertices(rows) and
edges (columns) if necessary, we have
 
Q(G 1 ) 0 ... 0
 
 0 Q(G 2 ) ... 0
 

Q(G) = 
 .. .. .. ..
.
.

 . . . 
 
0 0 ... Q(G k )

Since G i is connected, rank Q(G i ) is n i − 1 is the number of vertices in G i , i = 1, . . . , k. It follows that

rank Q(G) = rank Q(G 1 ) + · · · + rank Q(G k )

= (n 1 − 1) + · · · + (n k − 1)

= n1 + · · · + n k

= n−k

This completes the proof.

Lecture Notes 6
1.3 Adjacency Matrix

1.2.2. Minor

A matrix is said to be totally unimodular if the determinant of any square submatrix of the matrix
is either 0 or ±1. It is easily proved by induction on the order of the submatrix that Q(G) is totally
unimodular as seen in the next result.

Lemma 1.2. Let G be a graph with incidence matrix Q(G). Then Q(G) is totally unimodular.

Proof. Consider the statement that any k × k submatrix of Q(G) has determinant 0 or ±1. We prove the
statement by induction on k. Clearly the statement holds for k = 1, since each entry of Q(G) is either
0 or ±1. Assume the statement to be true for k − 1 and consider a k × k submatrix B of Q(G). If each
column of B has a 1 and a −1, then detB = 0. Also, if B has a zero column, then detB = 0. Now suppose
B has a column with only one nonzero entry, which must be ±1. Expand the determinant of B along
that column and use induction assumption to conclude that det B must be 0 or ±1.

1.3 Adjacency Matrix

Definition 1.16. The adjacency matrix A(G) of a simple (n, m) graph G is an n × n matrix whose rows
and columns are indexed by V (G) such that

 1 if vertex i and vertex j are adjacent
(A(G)) i j =
 0 otherwise

We often denote A(G) simply by A.

Example 2. Let G be the graph given below.

1
e1 e2 e3

2 3 4
e4 e5 e6

Then the adjacency matrix of G is

1 2 3 4 5
 
1 0 1 1 1 0
 
2
 1 0 0 0 1 
 
 
A(G) = 3  1 0 0
 0 1 

 
4  1 0 0 0 1 
 
5 0 1 1 1 0

Exercise 1.4. Find the adjacency matrix of the following graph G:

Lecture Notes 7
1.3 Adjacency Matrix

v1

e1 e2

v3 v2
e3

Exercise 1.5. Find the adjacency matrix of the following graph G:

v4 v3

e3 e2

v1

e1

v2

Exercise 1.6. Find the adjacency matrix of the following graph G ≃ K 2,3 :

v1 v2

e4
e1 e3
e2 e5 e6

v3 v4 v5

1.3.1. Properties

1. Clearly A is symmetric matrix with zeros on the diagonal.

Lecture Notes 8
1.3 Adjacency Matrix

2. The sum of the 2 × 2 minors of A equals −|E(G)|.

For i ̸= j the principal submatrix


 ofA formed by the rows and columns i, j is the zero matrix if
0 1
i ≁ j and otherwise it equals  . The determinant of this matrix is −1. Thus the absolute
1 0
value of the sum of all 2 × 2 principal minors of A is the number of edges in G in magnitude. This
fact completes the proof.

3. The sum of the 3 × 3 principal minors of A equals twice the number of triangles in G.

Consider three distinct vertices i, j, k ∈ V (G). The pricipal submatrix formed by the rows and
columns i, j, k will be non-singular if and only if they are adjacent to each other. In which case the
submatrix formed these rows and columns is
 
0 1 1
 
1 0 1 , whose determinant is 2.
 
1 1 0

Proof: (⇐) is clear.


(⇒) Suppose i ≁ j then the submatrix formed by the rows and columns i, j, k is
 
0 0 a
 
0
 0 b
 (1.1)
a b 0

where a, b are 1 or 0 depending on i ∼ k, j ∼ k or not, whose determinant is zero contradicting


to nonsingularity. Hence, two times the number of cycles of length 3 equals the sum of all 3 × 3
principal submatrices of A(G).

Definition 1.17 (distance). Let G be a connected graph with vertices {1, . . . , n}. The distance d(i, j)
between the vertices i and j defined as the minimum length of an (i j)−path.

We set d(i, i) = 0. The maximum value of d(i, j) is the diameter of G.

1.3.2. Eigenvalues of Some Graphs

Let G be a graph with adjacency matrix A. Often we refer to the eigenvalues of A as the eigenvalues of
G.

Theorem 1.4.

(i) For any positive integer n, the eigen values of K n are n − 1 with multiplicity 1 and −1 with multi-
plicity n − 1.
p p
(ii) For any positive integers p, q, the eigenvalues of K p,q are pq, − pq and 0 with multiplicity
p + q − 2.

Lecture Notes 9
1.4 Laplacian Matrix

Proof. First consider Jn , the n × n matrix of all ones. It is a symmetric, rank 1 matrix, and hence it
has only one nonzero eigenvalue, which must equal the trace. Thus, the eigenvalues of Jn are n with
multiplicity 1 and 0 with multiplicity n − 1. Since A(K n ) = Jn − I n , the eigenvalues of A(K n ) must be as
asserted in (i).
To prove (ii), note that  
0 J pq
A(K p,q ) =  ,
Jq p 0
where J pq and Jq p are matrices of all ones of the appropriate size. Now

rank A(K p,q ) = rank J pq + rank Jq p = 2,

and hence A(K p,q ) must have precisely two nonzero eigenvalues. These must be of the form λ and −λ,
since the trace of A(K p,q ) is zero. As noted earlier, the sum of the 2 × 2 principal minors of A(K p,q )
is negative the number of edges, that is, − pq. This sum also equals the sum of the products of the
eigenvalues, taken two at a time, which is −λ2 . Thus, λ2 = pq and the eigenvalues must be as asserted
in (ii).

For any positive integer n ≥ 2, let Q n be the full-cycle permutation matrix of order n. Thus, (i, i + 1)-
element of Q n is 1, i = 1, 2, . . . , n − 1, the (n, 1)-element of Q n is 1, and the remaining elements of Q n are
zero.
2π i
Lemma 1.3. For n ≥ 2, the eigenvalues of Q n are 1, ω, ω2 , . . . , ωn−1 , where ω = e n , is the primitive nth
root of unity.

Proof. The characteristic polynomial of Q n is det(Q n − λ I) = (−1)n (λn − 1). Clearly, the roots of the
characteristic polynomial are the n roots of unity.

1.4 Laplacian Matrix

Definition 1.18. The Laplacian matrix L(G) of a simple (n, m) graph G is an n × n matrix whose rows
and columns are indexed by V (G) such that for i ̸= j,

 0 if vertex i and vertex j are not adjacent
(L(G)) i j =
 −1 otherwise

and for i = j,
L(G) ii = d i , the degree of vertex i, i = 1, 2, . . . , n.

Let D(G) be the diagonal matrix of vertex degrees. If A(G) is the adjacency matrix of G, then note
that L(G) = D(G) − A(G).
Suppose each edge of G is assigned an orientation, which is arbitrary but fixed. Let Q(G) be the
incidence matrix of G. Then observe that L(G) = Q(G)Q(G)′ . This can be seen as follows. The rows of
Q(G) are indexed by V (G). The (i, j)−entry of Q(G)Q(G)′ is the inner product of the rows i and j of Q(G).
If i = j then the inner product is clearly d i , the degree of the vertex i. If i ̸= j, then the inner product is
−1 if the vertices i and j are adjacent, and zero otherwise.

Lecture Notes 10
1.4 Laplacian Matrix

Example 3. Consider the graph

K4 :
•1 •2 •3

•4 •5 •6

The laplacian matrix is given by


 
3 −1 0 −1 −1 0
 
−1 3 −1 0 −1 0
 
 
 0 −1 2 0 −1 0 
L(G) =  .
 
−1 0 0 2 −1 0 
 
 
−1 −1 −1 −1 5 −1
 
0 0 0 0 −1 1

Exercise 1.7. Find the Laplacian matrix of the following graph G:

v1

e1 e2

v3 v2
e3

Exercise 1.8. Find the Laplacian matrix of the following graph G:

v4 v3

e3 e2

v1

e1

v2

Lecture Notes 11
1.5 Tree

Exercise 1.9. Find the Laplacian matrix of the following graph G ≃ K 2,3 :

v1 v2

e4
e1 e3
e2 e5 e6

v3 v4 v5

1.4.1. Properties of Laplacian Matrix

Let G be a graph with V (G) = {1, . . . , n} and E(G) = { e 1 , . . . , e m }. Then the following are true.

(i) L(G) is a symmetric, positive semidefinite matrix.

(ii) The rank of L(G) equals n − k, where k is the number of connected components of G.

(iii) For any vector x,


x′ L(G)x = (x i − x j )2 .
X
i∼ j

(iv) The row and the column sums of L(G) are zero.

(v) The cofactors of any two elements of L(G) are equal.

1.5 Tree

Definition 1.19. A tree is a connected graph without any cycles.

1.5.1. Some properties of Trees

• A graph G is a tree if and only if there exists one and only one path between every pair of vertices
of G.

• A tree with n vertices has n − 1 edges.

• Any connected graph with n vertices and n − 1 edges is a tree.

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

Thus a graph G with n vertices is called a tree if

1. G is connected and is without cycles, or

2. G is connected and has n − 1 edges, or

Lecture Notes 12
1.6 Graph theoretic algorithms for shortest spanning tree

3. G is without cycles and has n − 1 edges, or

4. There is exactly one path between every pair of vertices in G, or

5. G is a minimally connected graph.

1.5.2. Spanning Tree

Definition 1.20. A tree T is said to be a spanning tree of a connected graph G if T is subgraph of G


and T contains all the vertices of G.

In other words, spanning tree in a graph G is a minimal subgraph connecting all the vertices of G.

1.6 Graph theoretic algorithms for shortest spanning tree

Definition 1.21. A weighted graph is a graph having a weight, or a number, associated with each edge.

If a graph G is a weighted graph, then the weight of a spanning tree T is the sum of the weights
of all the edges in T. In general, different spanning trees of G will have different weights. Among all
spanning trees of G the one with the smallest weight is of practical significance.

Definition 1.22. A spanning tree with the smallest weight in a weighted graph is called a shortest
spanning tree or shortest-distance spanning tree or minimal spanning tree.

For a given graph, such a spanning tree may not be unique. For example, if G is a weighted graph
with n vertices in which every edge has unit weight, then all the spanning trees have a weight of n − 1
units.
A practical application of finding the shortest spanning tree is the following: Suppose that we have
to connect n cities v1 , v2 , ..., vn through a network of roads, given that c i j is the cost of building a direct
road between the cities v i and v j . Then the problem of finding the least expensive network that connects
all the cities is nothing but the problem of finding a shortest spanning tree.

1.6.1. Kruskal's Algorithm

The following algorithm to find the shortest spanning tree of the given graph is due to Joseph Kruskal.
It was published in 1956.

Let G be a graph with n vertices. Then the shortest spanning tree is found as follows:

• Step I: Remove all the self loops and multiple edges of G.

• Step II: List all the edges of the graph G in the order of nondecreasing weight.

• Step III: Select a smallest edge of G, say e k .

Lecture Notes 13
1.6 Graph theoretic algorithms for shortest spanning tree

• Step IV: Select another smallest edge e l such that e l makes no cycle with e k .

• Step V: Now select another smallest edge, say e m such that e m makes no cycle with e k and e l .

• Step VI: Continue this process of selecting the smallest edges (from all the remaining edges of G)
which make no cycle with previously selected edges until all n − 1 edges have been selected. These
edges constitute the desired shortest spanning tree.

Exercise 1.10. Use the algorithm of Kruskal, to find a shortest spanning tree in the following graph:

1.6.2. Prim's algorithm

This algorithm was developed in 1930 by Czech mathematician Vojtech Jarnik and later rediscovered
and republished by Robert C. Prim and Edsger W. Dijkstra. Unlike Kruskal’s algorithm, Prim’s al-
gorithm does not require listing of edges and checking at each step if a newly selected edge forms a
cycle.
Let G be a graph with n vertices. Then the shortest spanning tree is found as follows:

• Step I: Tabulate the given weights of the edges of G in an n by n table. (Then the table is
symmetric with respect to the diagonal, and the diagonal is empty.). Set the weights of non-
existent edges as very large.

• Step II: Draw n isolated vertices and label them v1 , v2 , v3 , ..., vn .

• Step III: Start from vertex v1 and connect it to the vertex, say vk , which has the smallest entry
in row 1 of the table.

Lecture Notes 14
1.7 Dijkstra’s Shortest Path Algorithm

• Step IV: Consider v1 and vk as one subgraph, and connect this subgraph to a vertex, other than
v1 and vk , which has the smallest entry among all the entries of the rows 1 and k. Let this new
vertex be v i .

• Step V: Regard v1 , vk , v i as one subgraph, and continue this procedure until all n vertices have
been connected by n − 1 edges.

Exercise 1.11. Find the shortest spanning tree of the following graph G using Prim’s algorithm.

1.7 Dijkstra's Shortest Path Algorithm

Definition 1.23. Given a simple weighted digraph G of n vertices is described by an n × n matrix D =


[d i j ] , with


 d , if there is an edge from i to j
 ij
(D) i j = 0, if i = j


∞, if there is no edge from i to j

where d i j = length (i.e., weight) of the directed edge from vertex i to vertex j. The matrix D is called the
distance matrix of G.

Note

1. If the given digraph is not simple, then it is made simple by removing all the self-loops, and
replacing every set of parallel edges by the shortest (least weight) edge among them.

Lecture Notes 15
1.7 Dijkstra’s Shortest Path Algorithm

2. Also, the graph need not be directed. For an undirected graph d i j = d ji , and effectively each
undirected edge is replaced by two oppositely directed edges of the same weight.

Dijkstra’s algorithm is an algorithm to find the shortest path between vertices of a weighted graph.
This algorithm was conceived by computer scientist Edsger W. Dijkstra in 1956 and published three
years later.
Let G be a weighted graph and let r be a vertex of G from which we shall find the shortest paths to
all other vertices of G. We denote by bestd(i), the shortest path so far formed from r to vertex i. Also,
let tree(i) denote the next vertex to i on the current shortest path.

• Step I: Write the distance matrix for G.

• Step II: Let K = { r } and and U = {all other vertices of G except r }. For all vertices other than r,
set bestd(i) = d ri and tree(i) = r.

• Step III: Find a vertex s in U for which bestd is minimum. Transfer s from U to K .

• Step IV: For each vertex u in U , find bestd(s) + d su and if bestd(s) + d su < bestd(u), replace
bestd(u) by bestd(s) + d su and let tree(u) = s (i.e., a shorter path to u has been found by going via
vertex s).

• Step V: Stop the process if U has only one point, otherwise go back to step III.

Example 4. Let G be the digraph given in Figure 3.2. Find the shortest path from the vertex B to the
remaining vertices of G using Dijkstra’s algorithm.

Figure 3.2

Solution. The distance matrix of G is

Lecture Notes 16
1.7 Dijkstra’s Shortest Path Algorithm

A B C D E F G
A 0 ∞ ∞ 8 2 ∞ ∞
B 7 0 1 ∞ ∞ ∞ ∞
C 3 ∞ 0 ∞ 4 3 ∞
D 2 ∞ ∞ 0 1 ∞ ∞
E ∞ ∞ ∞ ∞ 0 ∞ 2
F ∞ ∞ ∞ 10 4 0 7
G ∞ ∞ ∞ 2 ∞ ∞ 0

Since we have to find the shortest path from B to remaining vertices, we have K = {B} and U =
{ A, C, D, E, F,G } with the arrays

A C D E F G
bestd 7 1 ∞ ∞ ∞ ∞
tree B B B B B B

• 1st iteration: Minimum of bestd is 1, corresponding to the vertex C. Transfer C from U to K . So,
K = {B, C } and U = { A, D, E, F,G }. Find 1 + d Cu for each u ∈ U . If 1 + d Cu < bestd(u), then replace
bestd(u) by this new value. Note that 1 + d C A = 4 < 7. Hence, bestd(A) = 4 and tree(A) = C.

A D E F G
bestd 4 ∞ 5 4 ∞
tree C B C C B

• 2nd iteration: Minimum of bestd is 4, corresponding to A and F. We shall transfer A from U


to K . So, K = {B, C, A } and U = {D, E, F,G }. Find 4 + d Au for each u ∈ U and replace the bestd
value by the minimum of bestd and 4 + d Au .

D E F G
bestd 12 5 4 ∞
tree A C C B

• 3rd iteration: Minimum of bestd is 4, corresponding to F and tree(F) = C. So, K = {B, C, A, F }


and U = {D, E,G } with the arrays

D E G
bestd 12 5 11
tree A C F

• 4 th iteration: Minimum of bestd is 5, corresponding to E and tree(E) = C. So, K = {B, C, A, F, E }


and U = {D,G } with the arrays

Lecture Notes 17
1.7 Dijkstra’s Shortest Path Algorithm

D G
bestd 12 7
tree A E

• 4 th iteration: Minimum of bestd is 7, corresponding to G and tree(G) = E. So, K = {B, C, A, F, E,G }


and U = {D } with the array

D
bestd 9
tree G

There is only one vertex in U and hence the algorithm ends. The final arrays are

A C D E F G
bestd 4 1 9 5 4 7
tree C B G C C E

Exercise 1.12. Consider the vertex B of the following graph G as the source vertex. Find the shortest
paths from the source vertex to all the remaining vertices of G using Dijkstra’s algorithm.

Figure 3.3

Lecture Notes 18

You might also like