14-Graphs Chapter14
14-Graphs Chapter14
GRAPHS
• A graph is an abstract data structure that is used to implement the graph concept from
mathematics. A graph is basically, a collection of vertices (also called nodes) and edges that
connect these vertices. A graph is often viewed as a generalization of the tree structure, where
instead of a having a purely parent-to-child relationship between tree nodes, any kind of
complex relationships between the nodes can be represented.
D E
A graph can be directed or undirected. In an undirected graph, the edges do not have any
direction associated with them. That is, if an edge is drawn between nodes A and B, then the
nodes can be traversed from A to B as well as from B to A. The above figure shows an
undirected graph because it does not gives any information about the direction of the edges.
The given figure shows a directed graph. In a directed graph, edges form an ordered pair. If
there is an edge from A to B, then there is a path from A to B but not from B to A. The edge (A,
B) is said to initiate from node A (also known as initial node) and terminate at node B (terminal
node).
A B C
Path: A path P, written as P = {v0, v1, v2,….., vn), of length n from a node u to v is defined as a sequence of
(n+1) nodes. Here, u = v0, v = vn and vi-1 is adjacent to vi for i = 1, 2, 3, …, n.
Closed path: A path P is known as a closed path if the edge has the same end-points. That is, if v0 = vn.
Simple path: A path P is known as a simple path if all the nodes in the path are distinct with an exception that
v0 may be equal to vn. If v0 = vn, then the path is called a closed simple path.
A B e4
2
e2 7 1
e3 D E F
C e6 D B
e5 e7 © Oxford University Press 2011. All rights3 reserved.
Directed Graph
A directed graph G, also known as a digraph, is a graph in which every edge has a direction assigned
to it. An edge of a directed graph is given as an ordered pair (u, v) of nodes in G. For an edge (u, v)-
• the edge begins at u and terminates at v
• u is known as the origin or initial point of e. Correspondingly, v is known as the destination or
terminal point of e
• u is the predecessor of v. Correspondingly, v is the successor of u
• nodes u and v are adjacent to each other.
e6
C e3 D
C D
e5
A B C D E A B C D E
Graph G
Transitive Closure G*
Since an adjacency matrix contains only 0s and 1s, it is called a bit matrix or a Boolean matrix. The
entries in the matrix depend on the ordering of the nodes in G. therefore, a change in the order of
nodes will result in a different adjacency
© Oxford Universitymatrix.
Press 2011. All rights reserved.
A B C D E
A 0 0 0 1 0
A B C B 1 0 1 1 0
C 0 0 0 0 0
D 0 0 0 0 1
E 0 0 1 0 0
D E
Directed Graph
A B C D
A 0 1 0 1
B 0 1 1 1
A B C 1 0 0 1
D 0 0 1 0
C D
A B C D E
A B C
A 0 1 0 1 0
B 1 0 1 1 0
C 0 1 0 0 1
D 1 1 0 0 1
E 0 0 1 1 0
D E
Undirected Graph
Weighted Graph
From adjacency matrix A1, we have learnt that an entry 1 in the ith row and jth column means that there
exists a path of length 1 from vi to vj. Now consider, A2, A3 and A4
aij 2 = ∑ aik akj
Any entry aij = 1 if aik = akj = 1. That is, if there is an edge (vi, vk) and (vk, vj). This implies that there is a
path from vi to vj of length 2.
Similarly, every entry in the ith row and jth column of A3 gives the number of paths of length 3 from
node vi to vj.
In general terms, we can conclude that every entry in the ith row and jth column of An (where n is the
number of nodes in the graph) gives the number of paths of length n from node vi to vj.
A B C D
A 0 1 1 0
B 0 0 1 1
A B C 0 0 0 1
D 1 1 0 0
C
© Oxford University
D Press 2011. All rights reserved.
A2 = A1 X A1, therefore,
A2 = A B C D
A B C D A 0 1 1 0
A 0 1 1 0 B 0 0 1 1
B 0 0 1 1 C 0 0 0 1
X C 0 0 0 1 D 1 1 0 0
D 1 1 0 0
A2 = A B C D
A 0 0 1 2
B 1 1 0 1
A3 =
C 1 1 0 0
D 1 1 2 1 A B C D
A B C D A 0 1 1 0
A 0 1 1 2 B 0 0 1 1
B 1 1 0 1 C 0 0 0 1
X C 1 1 0 0 D 1 1 0 0
D 1 1 2 1
A3 = A B C D
A 2 2 0 1
B 1 2 2 1
C 0 1 2 1
D 1 2 2 3
Pij
0 otherwise
Let us now calculate matrix B and matrix P using the above discussion.
B= A B C D A B C D A B C D A B C D
A 0 1 1 0 A 0 1 1 2 A 2 2 0 1 A 1 3 4 2
B 0 0 1 1 B 1 2 3 4
C 0 0 0 1 B 1 1 0 1 + B 1 2 2 1 +
C 0 1 2 1 C 1 1 1 3
D 1 1 0 0 + C 1 1 0 0 D 1 2 2 3 D 3 4 3 4
D 1 1 2 1
ADJACENCY LIST
The adjacency list is another way in which graphs can be represented in computer’s memory. This
structure consists of a list of all nodes in G. Furthermore, every node is in turn linked to its own list
that contains the names of all other nodes that are adjacent to itself.
The key advantage of using an adjacency list includes:
It is easy to follow, and clearly shows the adjacent nodes of a particular node
It is often used for storing graphs that have a small to moderate number of edges. That is an
Adjacency list is preferred for representing sparse graphs in computer’s memory; otherwise, an
adjacency matrix is a good choice.
Adding new nodes in G is easy and straightforward when G is represented using an Adjacency list.
Adding new nodes in an Adjacency matrix is a difficult task as size of the matrix needs to be
changed and existing nodes may have to be reordered.
B D X
B
C D C D X
D
A B X
For a directed graph, the sum of lengths of all adjacency lists is equal to the number of edges in G.
However, for an undirected graph, the sum of lengths of all adjacency lists is equal to twice the number of
edges in G because an edge (u, v) means an edge from node u to v as well as an edge v to u. The
adjacency list can also be modified to store weighted graphs.
A B D X
A B C
B A C D X
D E C B E X
D A B E X
E D X
C
Example: Consider the graph G given below. The adjacency list of G is also given. Assume that
G represents the daily flights between different cities and we want to fly from city A to H with
minimum stops. That is, find the minimum path P from A to H given that every edge has length
= 1.
Adjacency Lists
A: B, C, D
B C D B: E
C: B, G
D: C, G
E: C, F
F: C, H
E F G G: F, H, I
H: E, I
I: F
H I
Dequeue a node by setting FRONT = FRONT + 1 (remove the FRONT element of QUEUE) and enqueue the
neighbors of A. Also add A as the ORIG of its neighbors, so
FRONT = 2 QUEUE = A B C D
REAR = 4 ORIG = \0 A A A
Dequeue a node by setting FRONT = FRONT + 1 and enqueue the neighbors of B. Also add B as the ORIG of its
neighbors, so
FRONT = 3 QUEUE = A B C D E
REAR = 5 ORIG = \0 A A A B
Dequeue a node by setting FRONT = FRONT + 1 and enqueue the neighbors of C. Also add C as the ORIG of its
neighbors. Note that C has two neighbors B and G. Since B has already been added to the queue and it is not in the
Ready state, we will not add B and add only G, so
FRONT = 4 QUEUE = A B C D E G
REAR = 6 © Oxford University
ORIG = \0Press
A A A 2011.
B C All rights reserved.
Dequeue a node by setting FRONT = FRONT + 1 and enqueue the neighbors of D. Also add D as the ORIG of its neighbors. Note
that D has two neighbors C and G. Since both of them have already been added to the queue and they are not in the Ready state, we
will not add them again, so
FRONT = 5 QUEUE = A B C D E G
REAR = 6 ORIG = \0 A A A B C
Dequeue a node by setting FRONT = FRONT + 1 and enqueue the neighbors of E. Also add E as the ORIG of its neighbors. Note
that E has two neighbors C and F. Since C has already been added to the queue and it is not in the Ready state, we will not add C
and add only F, so
FRONT = 6 QUEUE = A B C D E G F
REAR = 7 ORIG = \0 A A A B C G
Dequeue a node by setting FRONT = FRONT + 1 and enqueue the neighbors of G. Also add G as the ORIG of its neighbors. Note
that G has three neighbors F, H and I.
FRONT = 7 QUEUE = A B C D E G F H I
REAR = 10 ORIG = \0 A A A B C G G G
Since I is our final destination, we stop the execution of this algorithm as soon as it is encountered and added to the QUEUE.
Now backtrack from I using ORIG to find the minimum path P. thus, we have P as
A -> C -> G -> I.
Example: Consider the graph G given below. The adjacency list of G is also given. Suppose we want
to print all nodes that can be reached from the node H (including H itself). One alternative is to use a
Depth- First Search of G starting at node H. the procedure can be explained as below.
Adjacency Lists
A
A: B, C, D
B: E
C: B, G
B C D D: C, G
E: C, F
F: C, H
G: F, H, I
E F G H: E, I
I: F
PRINT: I STACK: E, F
Pop and Print the top element of the STACK, that is, F. Push all the neighbors of F on to the stack that are in the ready state. (Note F
has two neighbors C and H. but only C will be added as H is not in the ready state). The STACK now becomes:
PRINT: F
STACK: E, C
Pop and Print the top element of the STACK, that is, C. Push all the neighbors of C on to the stack that are in the ready state. The STACK
now becomes:
PRINT: C STACK: E, B, G
Pop and Print the top element of the STACK, that is, G. Push all the neighbors of G on to the stack that are in the ready state. Since there
are no neighbors of G that are in the ready state no push operation is performed. The STACK now becomes:
PRINT G:
STACK: E, B
Pop and Print the top element of the STACK, that is, B. Push all the neighbors of B on to the stack that are in the ready state. Since
there are no neighbors of B that are in the ready state no Push operation is performed. The STACK now becomes:
PRINT e:
STACK: E