6.1. Introduction To Graphs

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 28

Chapter 6

Graphs
6.1. Introduction to Graphs:

Graph G is a pair (V, E), where V is a finite set of vertices and E is a finite set of edges.
We will often denote n = |V|, e = |E|.

A graph is generally displayed as figure 6.5.1, in which the vertices are represented by
circles and the edges by lines.

An edge with an orientation (i.e., arrow head) is a directed edge, while an edge with no
orientation is our undirected edge.

If all the edges in a graph are undirected, then the graph is an undirected graph. The
graph in figure 6.5.1(a) is an undirected graph. If all the edges are directed; then the
graph is a directed graph. The graph of figure 6.5.1(b) is a directed graph. A directed
graph is also called as digraph. A graph G is connected if and only if there is a simple
path between any two nodes in G.

A graph G is said to be complete if every node a in G is adjacent to every other node v


in G. A complete graph with n nodes will have n(n-1)/2 edges. For example, Figure
6.5.1.(a) and figure 6.5.1.(d) are complete graphs.

A directed graph G is said to be connected, or strongly connected, if for each pair (u, v)
for nodes in G there is a path from u to v and also a path from v to u. On the other
hand, G is said to be unilaterally connected if for any pair (u, v) of nodes in G there is a
path from u to v or a path from v to u. For example, the digraph shown in figure 6.5.1
(e) is strongly connected.

B D v1
A B

E
A C E G v4 v2
C D

(a) (b) v3 (c)


F

v1 v1 v1 v1

v2 v3
v4 v2 v4 v2 v4 v2

(d) (e) (f) (g)


v3 v3 v3 v4 v5 v6 v7

Figure 6.5.1 Various Graphs

We can assign weight function to the edges: wG(e) is a weight of edge e  E. The graph
which has such function assigned is called weighted graph.

The number of incoming edges to a vertex v is called in–degree of the vertex (denote
indeg(v)). The number of outgoing edges from a vertex is called out-degree (denote
outdeg(v)). For example, let us consider the digraph shown in figure 6.5.1(f),
Lecture Notes 185 Dept. of Information
Technology
indegree(v1) = 2 outdegree(v1) = 1
indegree(v2) = 2 outdegree(v2) = 0

A path is a sequence of vertices (v1, v2, . . . . . , v k), where for all i, (vi, vi+1)  E. A path
is simple if all vertices in the path are distinct. If there is a path containing one or more
edges which starts from a vertex V i and terminates into the same vertex then the path
is known as a cycle. For example, there is a cycle in figure 6.5.1(a), figure 6.5.1(c) and
figure 6.5.1(d).

If a graph (digraph) does not have any cycle then it is called acyclic graph. For
example, the graphs of figure 6.5.1 (f) and figure 6.5.1 (g) are acyclic graphs.

A graph G’ = (V’, E’) is a sub-graph of graph G = (V, E) iff V’  V and E’  E.

A Forest is a set of disjoint trees. If we remove the root node of a given tree then it
becomes forest. The following figure shows a forest F that consists of three trees T1, T2
and T3.

D B

A Forest F

P X

Y
Q R

Z
C F T2 T3 T1

A graph that has either self loop or parallel edges or both is called multi-graph.

Tree is a connected acyclic graph (there aren’t any sequences of edges that go around
in a loop). A spanning tree of a graph G = (V, E) is a tree that contains all vertices of V
and is a subgraph of G. A single graph can have multiple spanning trees.

Let T be a spanning tree of a graph G. Then

Lecture Notes 186 Dept. of Information


Technology
1. Any two vertices in T are connected by a unique simple path.

2. If any edge is removed from T, then T becomes disconnected.

3. If we add any edge into T, then the new graph will contain a cycle.

4. Number of edges in T is n-1.

6.2. Representation of Graphs:

There are two ways of representing digraphs. They are:

 Adjacency matrix.
 Adjacency List.
 Incidence matrix.

Adjacency matrix:

In this representation, the adjacency matrix of a graph G is a two dimensional n x n


matrix, say A = (ai,j), where

 1 if there is an edge from v i to v j


a i, j  
 0 otherwise

The matrix is symmetric in case of undirected graph, while it may be asymmetric if the
graph is directed. This matrix is also called as Boolean matrix or bit matrix.

1 1 2 3 4 5
1 0 1 1 0 1
2 3
G1: 2 0 0 1 1 1
3 0 0 0 1 0
4 0 0 0 0 0
(a) 4 5 (b)
5 0 0 1 1 0

Figure 6.5.2. A graph and its Adjacency matrix

Figure 6.5.2(b) shows the adjacency matrix representation of the graph G1 shown in
figure 6.5.2(a). The adjacency matrix is also useful to store multigraph as well as
weighted graph. In case of multigraph representation, instead of entry 0 or 1, the entry
will be between number of edges between two vertices.

In case of weighted graph, the entries are weights of the edges between the vertices.
The adjacency matrix for a weighted graph is called as cost adjacency matrix. Figure
6.5.3(b) shows the cost adjacency matrix representation of the graph G2 shown in
figure 6.5.3(a).

Lecture Notes 187 Dept. of Information


Technology
G2: 4
B D A B C D E F G
3 1 4 A 0 3 6    
2 2
B 3 0 2 4   
4 1
A C E G C 6 2 0 1 4 2 
6
2 D  4 1 0 2  4
2 1
(a) E   4 2 0 2 1
(b)
F F   2  2 0 1
G    4 1 1 0

Figure 6.5.3 Weighted graph and its Cost adjacency matrix

Adjacency List:

In this representation, the n rows of the adjacency matrix are represented as n linked
lists. An array Adj[1, 2, . . . . . n] of pointers where for 1 < v < n, Adj[v] points to a
linked list containing the vertices which are adjacent to v (i.e. the vertices that can be
reached from v by a single edge). If the edges have weights then these weights may
also be stored in the linked list elements. For the graph G in figure 6.5.4(a), the
adjacency list in shown in figure 6.5.4 (b).

1 2 3

1 1 1 2 3
1 1 1

1 2 3
2 0 0

3 0 1 0 3 2

(a) Adjacency Matrix (b) Adjacency List

Figure 6.5.4 Adjacency matrix and adjacency list

Incidence Matrix:

In this representation, if G is a graph with n vertices, e edges and no self loops, then
incidence matrix A is defined as an n by e matrix, say A = (ai,j), where

 1 if there is an edge j incident to v i


a i, j  
 0 otherwise

Here, n rows correspond to n vertices and e columns correspond to e edges. Such a


matrix is called as vertex-edge incidence matrix or simply incidence matrix.

Lecture Notes 188 Dept. of Information


Technology
c a b c d e f g h i j k l
B D
a d f A 1 0 0 0 0 0 1 0 0 0 0 0
b e B 1 1 1 0 0 0 0 0 0 0 0 0
h i C 0 1 0 1 0 0 1 1 0 0 1 0
A C E G
g D 0 0 1 1 1 1 0 0 0 0 0 0
j
k l E 0 0 0 0 1 0 0 1 1 1 0 0
(a)
F (b) F 0 0 0 0 0 0 0 0 0 1 1 1
G 0 0 0 0 0 1 0 0 1 0 0 1

Figure 6.5.4 Graph and its incidence matrix

Figure 6.5.4(b) shows the incidence matrix representation of the graph G1 shown in
figure 6.5.4(a).

6.3. Minimum Spanning Tree (MST):

A spanning tree for a connected graph is a tree whose vertex set is the same as the
vertex set of the given graph, and whose edge set is a subset of the edge set of the
given graph. i.e., any connected graph will have a spanning tree.

Weight of a spanning tree w(T) is the sum of weights of all edges in T. Minimum
spanning tree (MST) is a spanning tree with the smallest possible weight.

Example:

G:

A gr a p h G:
T hr e e ( of ma n y p o s s i b l e) s p a n n i n g t r e e s f r o m gr a p h G:

2 2
4
G: 3 5 3
6

1 1

A w e i g ht e d gr a p h G: T h e mi n i m a l s p a n n i n g t r e e f ro m w e i g ht e d gr a p h G:

Let's consider a couple of real-world examples on minimum spanning tree:

 One practical application of a MST would be in the design of a network. For


instance, a group of individuals, who are separated by varying distances,
wish to be connected together in a telephone network.  Although MST cannot

Lecture Notes 189 Dept. of Information


Technology
do anything about the distance from one connection to another, it can be
used to determine the least cost paths with no cycles in this network,
thereby connecting everyone at a minimum cost.

 Another useful application of MST would be finding airline routes. The


vertices of the graph would represent cities, and the edges would represent
routes between the cities.  MST can be applied to optimize airline routes by
finding the least costly paths with no cycles.

Minimum spanning tree, can be constructed using any of the following two algorithms:

1. Kruskal’s algorithm and


2. Prim’s algorithm. 

Both algorithms differ in their methodology, but both eventually end up with the
MST. Kruskal's algorithm uses edges, and Prim’s algorithm uses vertex connections in
determining the MST. In Prim’s algorithm at any instance of output it represents tree
whereas in Kruskal’s algorithm at any instance of output it may represent tree or not.

6.3.1. Kruskal’s Algorithm

This is a greedy algorithm. A greedy algorithm chooses some local optimum (i.e.
picking an edge with the least weight in a MST).

Kruskal's algorithm works as follows: Take a graph with 'n' vertices, keep on adding the
shortest (least cost) edge, while avoiding the creation of cycles, until (n - 1) edges
have been added. Sometimes two or more edges may have the same cost.

The order in which the edges are chosen, in this case, does not matter. Different MST’s
may result, but they will all have the same total cost, which will always be the
minimum cost. 

Kruskal’s Algorithm for minimal spanning tree is as follows:

1. Make the tree T empty.

2. Repeat the steps 3, 4 and 5 as long as T contains less than n - 1 edges and E is
not empty otherwise, proceed to step 6.

3. Choose an edge (v, w) from E of lowest cost.


4. Delete (v, w) from E.

5. If (v, w) does not create a cycle in T

then Add (v, w) to T


else discard (v, w)

6. If T contains fewer than n - 1 edges then print no spanning tree.

Example 1:

Construct the minimal spanning tree for the graph shown below:
Lecture Notes 190 Dept. of Information
Technology
10 50
1 2
45 40 3
30 35

4 25 5
55
20 15
6

Arrange all the edges in the increasing order of their costs:

Cost 10 15 20 25 30 35 40 45 50 55

Edge (1, 2) (3, 6) (4, 6) (2, 6) (1, 4) (3, 5) (2, 5) (1, 5) (2, 3) (5, 6)

The stages in Kruskal’s algorithm for minimal spanning tree is as follows:

STAGES IN KRUSKAL’S
EDGE COST REMARKS
ALGORITHM

(1, 2) 10 The edge between vertices 1 and 2 is


1 2 the first edge selected. It is included in
the spanning tree.
3

4
5
6

(3, 6) 15 Next, the edge between vertices 3 and 6


is selected and included in the tree.
1 2
3

4
5
6

(4, 6) 20 The edge between vertices 4 and 6 is


1 2
next included in the tree.
3

4
5
6

Lecture Notes 191 Dept. of Information


Technology
(2, 6) 25 The edge between vertices 2 and 6 is
1 2
considered next and included in the
3
tree.
4
5
6

(1, 4) 30 Reject The edge between the vertices 1 and 4


is discarded as its inclusion creates a
cycle.

(3, 5) 35 Finally, the edge between vertices 3 and


1 2
3 5 is considered and included in the tree
built. This completes the tree.
4 5
The cost of the minimal spanning tree is
6 105.

Example 2:

Construct the minimal spanning tree for the graph shown below:

1 28
10
2
14
6 16
7
24 3
25
5 18
12
22 4

Solution:

Arrange all the edges in the increasing order of their costs:

Cost 10 12 14 16 18 22 24 25 28
Edge (1, 6) (3, 4) (2, 7) (2, 3) (4, 7) (4, 5) (5, 7) (5, 6) (1, 2)

The stages in Kruskal’s algorithm for minimal spanning tree is as follows:

STAGES IN KRUSKAL’S
EDGE COST REMARKS
ALGORITHM

(1, 6) 10 The edge between vertices 1 and 6 is


the first edge selected. It is included in
Lecture Notes 192 Dept. of Information
Technology
the spanning tree.

(3, 4) 12 1 Next, the edge between vertices 3 and 4


2 is selected and included in the tree.
6
3
7

5
4

(2, 7) 14 1 The edge between vertices 2 and 7 is


2 next included in the tree.
6
3
7

5
4

(2, 3) 16 1 The edge between vertices 2 and 3 is


2 next included in the tree.
6
3
7

5
4

The edge between the vertices 4 and 7


(4, 7) 18 Reject is discarded as its inclusion creates a
cycle.

(4, 5) 22 1 The edge between vertices 4 and 7 is


2 considered next and included in the
tree.
6
3
7

5
4

(5, 7) 24 Reject The edge between the vertices 5 and 7


is discarded as its inclusion creates a
cycle.

(5, 6) 25 Finally, the edge between vertices 5 and


6 is considered and included in the tree
built. This completes the tree.

The cost of the minimal spanning tree is


99.

Lecture Notes 193 Dept. of Information


Technology
6.3.2. MINIMUM-COST SPANNING TREES: PRIM'S ALGORITHM

A given graph can have many spanning trees. From these many spanning trees, we
have to select a cheapest one. This tree is called as minimal cost spanning tree.

Minimal cost spanning tree is a connected undirected graph G in which each edge is
labeled with a number (edge labels may signify lengths, weights other than costs).
Minimal cost spanning tree is a spanning tree for which the sum of the edge labels is as
small as possible

The slight modification of the spanning tree algorithm yields a very simple algorithm for
finding an MST. In the spanning tree algorithm, any vertex not in the tree but
connected to it by an edge can be added. To find a Minimal cost spanning tree, we
must be selective - we must always add a new vertex for which the cost of the new
edge is as small as possible.

This simple modified algorithm of spanning tree is called prim's algorithm for finding an
Minimal cost spanning tree. Prim's algorithm is an example of a greedy algorithm.

Prim’s Algorithm:

E is the set of edges in G. cost [1:n, 1:n] is the cost adjacency matrix of an n vertex
graph such that cost [i, j] is either a positive real number or  if no edge (i, j) exists. A
minimum spanning tree is computed and stored as a set of edges in the array t [1:n-1,
1:2]. (t [i, 1], t [i, 2]) is an edge in the minimum-cost spanning tree. The final cost is
returned.

Algorithm Prim (E, cost, n, t)


{
Let (k, l) be an edge of minimum cost in E;
mincost := cost [k, l];
t [1, 1] := k; t [1, 2] := l;
for i :=1 to n do // Initialize near
if (cost [i, l] < cost [i, k]) then near [i] := l;
else near [i] := k;
near [k] :=near [l] := 0;
for i:=2 to n - 1 do // Find n - 2 additional edges for t.
{
Let j be an index such that near [j]  0 and
cost [j, near [j]] is minimum;
t [i, 1] := j; t [i, 2] := near [j];
mincost := mincost + cost [j, near [j]];
near [j] := 0
for k:= 1 to n do // Update near[].
if ((near [k]  0) and (cost [k, near [k]] > cost [k, j]))
then near [k] := j;
}
return mincost;
}

Lecture Notes 194 Dept. of Information


Technology
EXAMPLE:

Use Prim’s Algorithm to find a minimal spanning tree for the graph shown below
starting with the vertex A.

4
B D

4
3 2 1 2
4 E 1

A C 2 G
6
2 F 1

Solution:
The cost adjacency matrix is
 0 3 6  

 3 0 2 4 
 6 2 0 1 4

  4 1 0 2

   4 2 0
   2  2


    4 1

The stepwise progress of the prim’s algorithm is as follows:

Step 1:

B 3  D Vertex A B C D E F G
Status 0 1 1 1 1 1 1
 E Dist. 0 3 6    
A 0 6  G Next * A A A A A A
 F
Step 2: C

44 D Vertex A B C D E F G
B 3
Status 0 0 1 1 1 1 1
 E Dist. 0 3 2 4   
A 0 Next * A B B A A A
2  G
 F
C
Step 3:
1 D
B 3

4 E Vertex A B C D E F G
Status 0 0 0 1 1 1 1
A 0 2  G
Dist. 0 3 2 1 4 2 
Step 4: C 2 F Next * A B C C C A

1 D Vertex A B C D E F G
Status 0 0 0 0 1 1 1
Dist. 0 3 2 1 2 2 4
2 E
A 0 2 4 G Next * A B C D C D

C
2 F

Step 5:

B 3 1 D
Lecture Notes 195 Dept. of Information
Technology
Vertex A B C D E F G
Status 0 0 0 0 1 0 1
Dist. 0 3 2 1 2 2 1
A 0 2 2 E 1 G Next * A B C D C E
C
2 F

Step 6:

Vertex A B C D E F G
B 3 1 D
Status 0 0 0 0 0 1 0
Dist. 0 3 2 1 2 1 1
1 G Next * A B C D G E
A 0 2 2
E
C
1 F

Step 7:

Vertex A B C D E F G
B 3 1 D
Status 0 0 0 0 0 0 0
Dist. 0 3 2 1 2 1 1
2 E
A 0 2 1 G Next * A B C D G E

C 1 F

6.4. Reachability Matrix (Warshall’s Algorithm):

Warshall’s algorithm requires knowing which edges exist and which does not. It doesn’t
need to know the lengths of the edges in the given directed graph. This information is
conveniently displayed by adjacency matrix for the graph, in which a ‘1’ indicates the
existence of an edge and ‘0’ indicates non-existence.

A l l P a ir s Rec h a b i l it y
A d j ac e nc y M at r i x W a r s h a l l’ s A l g or it h m
M at r i x

It begins with the adjacency matrix for the given graph, which is called A0, and then
updates the matrix ‘n’ times, producing matrices called A1, A2, . . . . . , An and then
stops.

In warshall’s algorithm the matrix Ai contains information about the existence of


i–paths. A one entry in the matrix A i will correspond to the existence of i–paths and
zero entry will correspond to non-existence. Thus when the algorithm stops, the final
matrix An, contains the desired connectivity information.

Lecture Notes 196 Dept. of Information


Technology
A one entry indicates a pair of vertices, which are connected and zero entry indicates a
pair, which are not. This matrix is called a reachability matrix or path matrix for the
graph. It is also called the transitive closure of the original adjacency matrix.

The update rule for computing Ai from Ai-1 in warshall’s algorithm is:

Ai [x, y] = Ai-1 [x, y] ۷ (Ai-1 [x, i] ٨ Ai-1 [i, y]) ---- (1)

Example 1:

Use warshall’s algorithm to calculate the reachability matrix for the graph:
4
1 4
5 6
7 11

1
2 3
7

We begin with the adjacency matrix of the graph ‘A0’

1 0 1 1 0
 
2 0 0 1 1
A0  0
3 0 0 0
 
4 1 1 1 0
 

The first step is to compute ‘A1’ matrix. To do so we will use the updating rule – (1).

Before doing so, we notice that only one entry in A 0 must remain one in A1, since in
Boolean algebra 1 + (anything) = 1. Since these are only nine zero entries in A 0, there
are only nine entries in A0 that need to be updated.

A1[1, 1] = A0[1, 1] ۷ (A0[1, 1] ٨ A0[1, 1]) = 0 ۷ (0 ٨ 0) = 0


A1[1, 4] = A0[1, 4] ۷ (A0[1, 1] ٨ A0[1, 4]) = 0 ۷ (0 ٨ 0) = 0
A1[2, 1] = A0[2, 1] ۷ (A0[2, 1] ٨ A0[1, 1]) = 0 ۷ (0 ٨ 0) = 0
A1[2, 2] = A0[2, 2] ۷ (A0[2, 1] ٨ A0[1, 2]) = 0 ۷ (0 ٨ 1) = 0
A1[3, 1] = A0[3, 1] ۷ (A0[3, 1] ٨ A0[1, 1]) = 0 ۷ (0 ٨ 0) = 0
A1[3, 2] = A0[3, 2] ۷ (A0[3, 1] ٨ A0[1, 2]) = 0 ۷ (0 ٨ 1) = 0
A1[3, 3] = A0[3, 3] ۷ (A0[3, 1] ٨ A0[1, 3]) = 0 ۷ (0 ٨ 1) = 0
A1[3, 4] = A0[3, 4] ۷ (A0[3, 1] ٨ A0[1, 4]) = 0 ۷ (0 ٨ 0) = 0
A1[4, 4] = A0[4, 4] ۷ (A0[4, 1] ٨ A0[1, 4]) = 0 ۷ (1 ٨ 0) = 0

1 0 1 1 0
 
2 0 0 1 1
A1 
3 0 0 0 0
 
4 
1 1 1 0

Lecture Notes 197 Dept. of Information


Technology
Next, A2 must be calculated from A1; but again we need to update the 0 entries,

A2[1, 1] = A1[1, 1] ۷ (A1[1, 2] ٨ A1[2, 1]) = 0 ۷ (1 ٨ 0) = 0


A2[1, 4] = A1[1, 4] ۷ (A1[1, 2] ٨ A1[2, 4]) = 0 ۷ (1 ٨ 1) = 1
A2[2, 1] = A1[2, 1] ۷ (A1[2, 2] ٨ A1[2, 1]) = 0 ۷ (0 ٨ 0) = 0
A2[2, 2] = A1[2, 2] ۷ (A1[2, 2] ٨ A1[2, 2]) = 0 ۷ (0 ٨ 0) = 0
A2[3, 1] = A1[3, 1] ۷ (A1[3, 2] ٨ A1[2, 1]) = 0 ۷ (0 ٨ 0) = 0
A2[3, 2] = A1[3, 2] ۷ (A1[3, 2] ٨ A1[2, 2]) = 0 ۷ (0 ٨ 0) = 0
A2[3, 3] = A1[3, 3] ۷ (A1[3, 2] ٨ A1[2, 3]) = 0 ۷ (0 ٨ 1) = 0
A2[3, 4] = A1[3, 4] ۷ (A1[3, 2] ٨ A1[2, 4]) = 0 ۷ (0 ٨ 1) = 0
A2[4, 4] = A1[4, 4] ۷ (A1[4, 2] ٨ A1[2, 4]) = 0 ۷ (1 ٨ 1) = 1

1
0 1 1 1
 
2 0 0 1 1
A2 
3 0 0 0 0
 
4 
1 1 1 1

This matrix has only seven 0 entries, and so to compute A 3, we need to do only seven
computations.

A3[1, 1] = A2[1, 1] ۷ (A2[1, 3] ٨ A2[3, 1]) = 0 ۷ (1 ٨ 0) = 0


A3[2, 1] = A2[2, 1] ۷ (A2[2, 3] ٨ A2[3, 1]) = 0 ۷ (1 ٨ 0) = 0
A3[2, 2] = A2[2, 2] ۷ (A2[2, 3] ٨ A2[3, 2]) = 0 ۷ (1 ٨ 0) = 0
A3[3, 1] = A2[3, 1] ۷ (A2[3, 3] ٨ A2[3, 1]) = 0 ۷ (0 ٨ 0) = 0
A3[3, 2] = A2[3, 2] ۷ (A2[3, 3] ٨ A2[3, 2]) = 0 ۷ (0 ٨ 0) = 0
A3[3, 3] = A2[3, 3] ۷ (A2[3, 3] ٨ A2[3, 3]) = 0 ۷ (0 ٨ 0) = 0
A3[3, 4] = A2[3, 4] ۷ (A2[3, 3] ٨ A2[3, 4]) = 0 ۷ (0 ٨ 0) = 0

1 0 1 1 1
 
2 0 0 1 1
A3 
3 0 0 0 0 
 
4 
1 1 1 1

Once A3 is calculated, we use the update rule to calculate A 4 and stop. This matrix is
the reachability matrix for the graph.

A4[1, 1] = A3 [1, 1] ۷ (A3 [1, 4] ٨ A3 [4, 1]) = 0 ۷ (1 ٨ 1) = 0 ۷ 1 = 1


A4[2, 1] = A3 [2, 1] ۷ (A3 [2, 4] ٨ A3 [4, 1]) = 0 ۷ (1 ٨ 1) = 0 ۷ 1 = 1
A4[2, 2] = A3 [2, 2] ۷ (A3 [2, 4] ٨ A3 [4, 2]) = 0 ۷ (1 ٨ 1) = 0 ۷ 1 = 1
A4[3, 1] = A3 [3, 1] ۷ (A3 [3, 4] ٨ A3 [4, 1]) = 0 ۷ (0 ٨ 1) = 0 ۷ 0 = 0
A4[3, 2] = A3 [3, 2] ۷ (A3 [3, 4] ٨ A3 [4, 2]) = 0 ۷ (0 ٨ 1) = 0 ۷ 0 = 0
A4[3, 3] = A3 [3, 3] ۷ (A3 [3, 4] ٨ A3 [4, 3]) = 0 ۷ (0 ٨ 1) = 0 ۷ 0 = 0
A4[3, 4] = A3 [3, 4] ۷ (A3 [3, 4] ٨ A3 [4, 4]) = 0 ۷ (0 ٨ 1) = 0 ۷ 0 = 0
Lecture Notes 198 Dept. of Information
Technology
1 1 1 1 1
 
2 1 1 1 1
A4 
3 0 0 0 0
 
4 
1 1 1 1

Note that according to the algorithm vertex 3 is not reachable from itself 1. This is
because as can be seen in the graph, there is no path from vertex 3 back to itself.

6.5. Traversing a Graph

Many graph algorithms require one to systematically examine the nodes and edges of a
graph G. There are two standard ways to do this. They are:

 Breadth first traversal (BFT)


 Depth first traversal (DFT)
The BFT will use a queue as an auxiliary structure to hold nodes for future processing
and the DFT will use a STACK.

During the execution of these algorithms, each node N of G will be in one of three
states, called the status of N, as follows:

1. STATUS = 1 (Ready state): The initial state of the node N.

2. STATUS = 2 (Waiting state): The node N is on the QUEUE or STACK, waiting to


be processed.

3. STATUS = 3 (Processed state): The node N has been processed.

Both BFS and DFS impose a tree (the BFS/DFS tree) on the structure of graph. So, we
can compute a spanning tree in a graph. The computed spanning tree is not a
minimum spanning tree. The spanning trees obtained using depth first search are
called depth first spanning trees. The spanning trees obtained using breadth first
search are called Breadth first spanning trees.

6.5.1. Breadth first search and traversal:

The general idea behind a breadth first traversal beginning at a starting node A is as
follows. First we examine the starting node A. Then we examine all the neighbors of A.
Then we examine all the neighbors of neighbors of A. And so on. We need to keep track
of the neighbors of a node, and we need to guarantee that no node is processed more
than once. This is accomplished by using a QUEUE to hold nodes that are waiting to be
processed, and by using a field STATUS that tells us the current status of any node.
The spanning trees obtained using BFS are called Breadth first spanning trees.

Breadth first traversal algorithm on graph G is as follows:

This algorithm executes a BFT on graph G beginning at a starting node A.

Initialize all nodes to the ready state (STATUS = 1).

1. Put the starting node A in QUEUE and change its status to the waiting
state (STATUS = 2).
Lecture Notes 199 Dept. of Information
Technology
2. Repeat the following steps until QUEUE is empty:

a. Remove the front node N of QUEUE. Process N and change the


status of N to the processed state (STATUS = 3).

b. Add to the rear of QUEUE all the neighbors of N that are in the
ready state (STATUS = 1), and change their status to the waiting
state (STATUS = 2).

3. Exit.
6.5.2. Depth first search and traversal:

Depth first search of undirected graph proceeds as follows: First we examine the
starting node V. Next an unvisited vertex 'W' adjacent to 'V' is selected and a depth
first search from 'W' is initiated. When a vertex 'U' is reached such that all its adjacent
vertices have been visited, we back up to the last vertex visited, which has an unvisited
vertex 'W' adjacent to it and initiate a depth first search from W. The search terminates
when no unvisited vertex can be reached from any of the visited ones.

This algorithm is similar to the inorder traversal of binary tree. DFT algorithm is similar
to BFT except now use a STACK instead of the QUEUE. Again field STATUS is used to
tell us the current status of a node.

The algorithm for depth first traversal on a graph G is as follows.

This algorithm executes a DFT on graph G beginning at a starting node A.

1. Initialize all nodes to the ready state (STATUS = 1).

2. Push the starting node A into STACK and change its status to the waiting state
(STATUS = 2).

3. Repeat the following steps until STACK is empty:

a. Pop the top node N from STACK. Process N and change the status of N to
the processed state (STATUS = 3).

b. Push all the neighbors of N that are in the ready state (STATUS = 1), and
change their status to the waiting state (STATUS = 2).
4. Exit.

Example 1:

Consider the graph shown below. Traverse the graph shown below in breadth first
order and depth first order.

Lecture Notes 200 Dept. of Information


Technology
Node Adjacency List
A
A F, C, B
B A, C, G
F C B C A, B, D, E, F, G
D C, F, E, J

D E G
E C, D, G, J, K
F A, C, D
G B, C, E, K
J K
J D, E, K
A Gr a p h G
K E, G, J
Adjacency list for graph G

Breadth-first search and traversal:

The steps involved in breadth first traversal are as follows:

Current Status
QUEUE Processed Nodes
Node A B C D E F G J K
1 1 1 1 1 1 1 1 1
A 2 1 1 1 1 1 1 1 1
A FCB A 3 2 2 1 1 2 1 1 1
F CBD AF 3 2 2 2 1 3 1 1 1
C BDEG AFC 3 2 3 2 2 3 2 1 1
B DEG AFCB 3 3 3 2 2 3 2 1 1
D EGJ AFCBD 3 3 3 3 2 3 2 2 1
E GJK AFCBDE 3 3 3 3 3 3 2 2 2
G JK AFCBDEG 3 3 3 3 3 3 3 2 2
J K AFCBDEGJ 3 3 3 3 3 3 3 3 2
K EMPTY AFCBDEGJK 3 3 3 3 3 3 3 3 3

For the above graph the breadth first traversal sequence is: A F C B D E G J K.

Depth-first search and traversal:

The steps involved in depth first traversal are as follows:

Current Status
Stack Processed Nodes
Node A B C D E F G J K
1 1 1 1 1 1 1 1 1
A 2 1 1 1 1 1 1 1 1
A BCF A 3 2 2 1 1 2 1 1 1
Lecture Notes 201 Dept. of Information
Technology
F BCD AF 3 2 2 2 1 3 1 1 1
D BCEJ AFD 3 2 2 3 2 3 1 2 1
J BCEK AFDJ 3 2 2 3 2 3 1 3 2
K BCEG AFDJK 3 2 2 3 2 3 2 3 3
G BCE AFDJKG 3 2 2 3 2 3 3 3 3
E BC AFDJKGE 3 2 2 3 3 3 3 3 3
C B AFDJKGEC 3 2 3 3 3 3 3 3 3
B EMPTY AFDJKGECB 3 3 3 3 3 3 3 3 3

For the above graph the depth first traversal sequence is: A F D J K G E C B.

Example 2:

Traverse the graph shown below in breadth first order, depth first order and construct
the breadth first and depth first spanning trees.
A H I Node Adjacency List
A F, B, C, G
B C G B A
C A, G
D E, F
J K E G, D, F
D
F A, E, D
E G A, L, E, H, J, C
F
H G, I
L M
The G ra ph G I H
J G, L, K, M
K J
L G, J, M
TheM L, list
adjacency J for the graph G

If the depth first traversal is initiated from vertex A, then the vertices of graph G are
visited in the order: A F E G L J K M H I C D B. The depth first spanning tree is shown
in the figure given below:

Lecture Notes 202 Dept. of Information


Technology
A

F B

G D

L H C

J I

K M
Depth first Traversal

If the breadth first traversal is initiated from vertex A, then the vertices of graph G are
visited in the order: A F B C G E D L H J M I K. The breadth first spanning tree is
shown in the figure given below:

F B C G

E D L H J

M I K

Breadth first traversal


Example 3:

Traverse the graph shown below in breadth first order, depth first order and construct
the breadth first and depth first spanning trees.

2 3

4 5 6 7

Graph G

Lecture Notes 203 Dept. of Information


Technology
He a d No d e s

1 2 3

2 1 4 5

3 1 6 7

4 2 8

5 2 8

6 3 8

7 3 8

8 4 5 6 7

A dj ac e nc y list fo r g r a p h G

Depth first search and traversal:

If the depth first is initiated from vertex 1, then the vertices of graph G are visited in
the order: 1, 2, 4, 8, 5, 6, 3, 7. The depth first spanning tree is as follows:

2 3

4 5 6 7

Depth First Spanning Tree

Breadth first search and traversal:

If the breadth first search is initiated from vertex 1, then the vertices of G are visited in
the order: 1, 2, 3, 4, 5, 6, 7, 8. The breadth first spanning tree is as follows:

2 3

4 5 6 7

Breadth First Spanning Tree

Lecture Notes 204 Dept. of Information


Technology
Program to implement Breadth First Search and Traversal:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<alloc.h>

struct graph
{
int status;
int data;
struct graph *next;
};
typedef struct graph node;

int ctr;
node *start[40];
node *queue;

node* getnode()
{
node *newnode;
newnode=(node*)malloc(sizeof(node));
printf("Enter data:");
scanf("%d",&newnode->data);
newnode->status=1;
newnode->next=NULL;
return(newnode);
}

node* makenode(int data1)


{
node *newnode;
newnode=(node*)malloc(sizeof(node));
newnode->data=data1;
newnode->next=NULL;
return(newnode);
}

void createadj_list(int n)
{
int i;
node *newnode,*temp;
for(i=1;i<n;i++)
{
printf("enter data for %d node",i);
while(1)
{
newnode=getnode();
if(newnode->data==-999)
break;
if(start[i]==NULL)
{
start[i]=newnode;
}
else
{
temp=start[i];
while(temp->next!=NULL)
Lecture Notes 205 Dept. of Information
Technology
temp=temp->next;
temp->next=newnode;
}
}
}
}

void main()
{
int i,n,k;
node *temp,*temp1,*temp2,*newnode;
int processednode[20];
int currentnode,startnode;
clrscr();
printf("enter number of nodes in a graph: ");
scanf("%d",&n);
printf("Enter adjacency list of a graph:");
createadj_list(n);
printf("enter starting node:");
scanf("%d",&startnode);
for(k=1;k<n;k++)
{
if(start[k]->data==startnode)
{
temp=start[k];
newnode=makenode(temp->data);
queue=newnode;
break;
}
else
printf("Given starting node is not present");
}
i=0;
do
{
currentnode=queue->data;
processednode[i]=currentnode;
i++;
temp=queue;
queue=queue->next;
free(temp);
for(k=1;k<n;k++)
{
temp=start[k];
if(temp->data==currentnode)
{
temp1=temp;
while(temp1!=NULL)
{
if(temp1->status==1)
{
newnode=makenode(temp1->data);
if(queue==NULL)
queue=newnode;
else
{
temp2=queue;
while(temp2!=NULL)
temp2=temp2->next;
temp2->next=newnode;

Lecture Notes 206 Dept. of Information


Technology
}
}
temp1->status=2;
temp1=temp1->next;
}
}
temp=start[k];
while(temp!=NULL)
{
if(temp->data==currentnode)
temp->status=3;
temp=temp->next;
}
}
}while(queue==NULL);
processednode[i]=-999;
printf("processed node is:");
for(i=0;processednode[i]!=-999;i++)
printf("%d,",processednode[i]);
getch();
}

EXCERCISES

1. Show that the sum of degrees of all vertices in an undirected graph is twice the
number of edges.

2. Show that the number of vertices of odd degree in a finite graph is even.

3. How many edges are contained in a complete graph of “n” vertices.

4. Show that the number of spanning trees in a complete graph of “n” vertices is 2 n-1
– 1.

5. Prove that the edges explored by a breadth first or depth first traversal of a
connected graph from a tree.

6. Explain how existence of a cycle in an undirected graph may be detected by


traversing the graph in a depth first manner.

7. Write a “C” function to generate the incidence matrix of a graph from its
adjacency matrix.

8. Give an example of a connected directed graph so that a depth first traversal of


that graph yields a forest and not a spanning tree of the graph.

9. Rewrite the algorithms “BFSearch” and “DFSearch” so that it works on adjacency


matrix representation of graphs.

10. Write a “C” function to find out whether there is a path between any two vertices
in a graph (i.e. to compute the transitive closure matrix of a graph)

11. Write a “C” function to delete an existing edge from a graph represented by an
adjacency list.

12. Construct a weighted graph for which the minimal spanning trees produced by
Kruskal’s algorithm and Prim’s algorithm are different.
Lecture Notes 207 Dept. of Information
Technology
13. Describe the algorithm to find a minimum spanning tree T of a weighted graph G.
Find the minimum spanning tree T of the graph shown below.

6 5
A B C

1 8
4 2

D E
3

14. For the graph given below find the following:


a) Linked representation of the graph.
b) Adjacency list.
c) Depth first spanning tree.
d) Breadth first spanning tree.
e) Minimal spanning tree using Kruskal’s and Prim’s algorithms.

8 6

1 1 5 7

2 4 6 2 7 9

3 3 8 10

4 10 9 5

15. For the graph given below find the following:


f) Linked representation of the graph.
g) Adjacency list.
h) Depth first spanning tree.
i) Breadth first spanning tree.
j) Minimal spanning tree using Kruskal’s and Prim’s algorithms.

4
1

2 3 7 8

5 6

16. For the graph given below find the following:


k) Linked representation of the graph.
l) Adjacency list.
m) Depth first spanning tree.
n) Breadth first spanning tree.
o) Minimal spanning tree using Kruskal’s and Prim’s algorithms.

Lecture Notes 208 Dept. of Information


Technology
5
1

6
2 4

8
3
7

Multiple Choice Questions

1. How can the graphs be represented? [ D ]


A. Adjacency matrix C. Incidence matrix
B. Adjacency list D. All of the above

2. The depth-first traversal in graph is analogous to tree traversal: [ C ]


A. In-order C. Pre-order
B. Post-order D. Level order

3. The children of a same parent node are called as: [ C ]


A. adjacent node C. Sibblings
B. non-leaf node D. leaf node

4. Complete graphs with n nodes will have__________ edges. [ C ]


A. n - 1 C. n(n-1)/2
B. n/2 D. (n – 1)/2

5. A graph with no cycle is called as: [ C ]


A. Sub-graph C. Acyclic graph
B. Directed graph D. none of the above

6. The maximum number of nodes at any level is: [ B ]


A. n C. n + 1
B. 2n D. 2n

20 Node Adjacency List


A B
A BCD
23 4 15
1 B ADE

36 9 C ADF
C D E
D ABCEFG
25 16 E BDG
28
3
F CDG
F G
17 G FDE

FIGURE 1 and its adjacency list


7. For the figure 1 shown above, the depth first spanning tree visiting [ B ]
sequence is:
A. A B C D E F G C. A B C D E F G
B. A B D C F G E D. none of the above

8. For the figure 1 shown above, the breadth first spanning tree visiting [ B ]
sequence is:
A. A B D C F G E C. A B C D E F G
B. A B C D E F G D. none of the above

9. Which is the correct order for Kruskal’s minimum spanning tree algorithm [ B ]
to add edges to the minimum spanning tree for the figure 1 shown
Lecture Notes 209 Dept. of Information
Technology
above:
A. (A, B) then (A, C) then (A, D) then (D, E) then (C, F) then (D, G)
B. (A, D) then (E, G) then (B, D) then (D, E) then (F, G) then (A, C)
C. both A and B
D. none of the above

10. For the figure 1 shown above, the cost of the minimal spanning tree is: [ A ]
A. 57 C. 48
B. 68 D. 32
11. A simple graph has no loops. What other property must a simple graph [ D ]
have?
A. It must be directed. C. It must have at least one vertex.
B. It must be undirected. D. It must have no multiple edges.

12. Suppose you have a directed graph representing all the flights that an [ D ]
airline flies. What algorithm might be used to find the best sequence of
connections from one city to another?
A. Breadth first search. C. A cycle-finding algorithm.
B. Depth first search. D. A shortest-path algorithm.

13. If G is an directed graph with 20 vertices, how many boolean values will [ D ]
be needed to represent G using an adjacency matrix?
A. 20 C. 200
B. 40 D. 400

14. Which graph representation allows the most efficient determination of [ B ]


the existence of a particular edge in a graph?
A. An adjacency matrix. C. Incidence matrix
B. Edge lists. D. none of the above

15. What graph traversal algorithm uses a queue to keep track of vertices [ A ]
which need to be processed?
A. Breadth-first search. C Level order search
B. Depth-first search. D. none of the above

16. What graph traversal algorithm uses a stack to keep track of vertices [ B ]
which need to be processed?
A. Breadth-first search. C Level order search
B. Depth-first search. D. none of the above

17. What is the expected number of operations needed to loop through all [ D ]
the edges terminating at a particular vertex given an adjacency matrix
representation of the graph? (Assume n vertices are in the graph and m
edges terminate at the desired node.)
A. O(m) C. O(m²)
B. O(n) D. O(n²)

18. What is the expected number of operations needed to loop through all [ A ]
the edges terminating at a particular vertex given an adjacency list
representation of the graph? (Assume n vertices are in the graph and m
edges terminate at the desired node.)
A. O(m) C. O(m²)
B. O(n) D. O(n²)

Lecture Notes 210 Dept. of Information


Technology
19. [ B ]
3
A D

2 1 5 5

B 3 G 4 E FIGURE 3

1 4 6 1

C F
3

For the figure 3, starting at vertex A, which is a correct order for Prim’s
minimum spanning tree algorithm to add edges to the minimum
spanning tree?
A. (A, G) then (G, C) then (C, B) then (C, F) then (F, E) then (E, D)
B. (A, G) then (A, B) then (B, C) then (A, D) then (C, F) then (F, E)
C. (A, G) then (B, C) then (E, F) then (A, B) then (C, F) then (D, E)
D. (A, G) then (A, B) then (A, C) then (A, D) then (A, D) then (C, F)

20. For the figure 3, which is a correct order for Kruskal’s minimum spanning [ C ]
tree algorithm to add edges to the minimum spanning tree?
A. (A, G) then (G, C) then (C, B) then (C, F) then (F, E) then (E, D)
B. (A, G) then (A, B) then (B, C) then (A, D) then (C, F) then (F, E)
C. (A, G) then (B, C) then (E, F) then (A, B) then (C, F) then (D, E)
D. (A, G) then (A, B) then (A, C) then (A, D) then (A, D) then (C, F)

21. Which algorithm does not construct an in-tree as part of its processing? [ ]
A. Dijkstra’s Shortest Path Algorithm
B. Prim’s Minimum Spanning Tree Algorithm
C. Kruskal’s Minimum Spanning Tree Algorithm
D. The Depth-First Search Trace Algorithm

22. The worst-case running time of Kruskal’s minimum-cost spanning tree [ ]


algorithm on a graph with n vertices and m edges is:
A. C.
B. D.

23. An adjacency matrix representation of a graph cannot contain [ D ]


information of:
A. Nodes C. Direction of edges
B. Edges D. Parallel edges

A Node Adjacency List


A D
B D B AC
C GDF
G F
D ----
E CD
C E
F EA
G B

FIGURE 4 and its adjacency list

24. For the figure 4, which edge does not occur in the depth first spanning [ B ]
tree resulting from depth first search starting at node B:
A. F  E C. C  G
B. E  C D. C  F
Lecture Notes 211 Dept. of Information
Technology
25. The set of all edges generated by DFS tree starting at node B is: [ A ]
A. B A D C G F E C. B A C D G F E
B. A D D. Cannot be generated

26. The set of all edges generated by BFS tree starting at node B is: [ C ]
A. B A D C G F E C. B A C D G F E
B. A D D. Cannot be generated

Lecture Notes 212 Dept. of Information


Technology

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