Graphs
[Module – 5]
WCE - SY (CSE) – 1CI 202: Data Structures
Course Teacher: Dr. Smriti H. Bhandari
Graphs
Basic Terminology
Representation of Graphs
Adjacency Matrix
Adjacency List
Adjacency Multilist
Traversals
Depth first
Breadth first
Minimum Spanning Tree
2
Basic Concepts
Non-linear data structure
Definition
Graph is
a collection of nodes, called vertices and
A collection of segments or lines called edges/arcs
connecting pairs of vertices
3
Basic Concepts (2)
Formal definition
Graph G is a pair (V,E) where
V is a set of nodes called vertices
E is a set of pairs of vertices each representing an
edge
4
Basic Concepts (3)
Directed Edge
Ordered pair of vertices (u,v)
First vertex u is the origin
Second vertex v is the destination
Undirected Edge
Unordered pair of vertices (u,v)
5
Basic Concepts (4)
Directed graph or digraph
All the edges are directed
Undirected graph
All the edges are undirected
6
Applications
Electronic circuits
Printed circuit boards
Integrated circuits
Transportation and communication networks
Highway network
Flight network
Computer Networks
Local area network
Internet
Databases
Entity-relationship graph
Modeling any sort of relationships
Between components, processes, people, concepts etc.
7
Example – Flight route network
Edge represents flight route between two
airports
8
Terminology
End vertices or end points of an edge
U and V are end points of edge a
Edges incident on the vertex
a, d and b are incident on V
Adjacent vertices
Vertex v is adjacent to vertex u iff (u,v) e
U and W are adjacent
Degree of a vertex
Number of edges incident to it or number of
adjacent vertices
X has degree 4
Loops
Edge having identical end points
i forms loop at Z
Isolated Node/Vertex
A node which is not adjacent to any other node
Degree of isolated node is zero
S is an isolated node 9
Terminology – (2)
Path
A sequence of vertices v1 ,v2 ,. . .vk such that vi+1 is adjacent to vi
for i = 1 .. k – 1
Simple path
A path with no repeated vertices (i.e. all vertices are distinct)
Cycle
A path except that the last vertex is same as the first vertex
Simple Cycle
A simple path except that the last vertex is same as the first vertex
Cyclic graph
Graph containing a cycle
Acyclic graph
Graph which does not contain a cycle
10
Terminology – (3)
Connected graph
A graph G is connected if and only if there is a simple
path between any two nodes in G
Complete graph
A graph G is said to be complete if every node u in G
is adjacent to every other node v in G.
Always connected
A complete graph with n nodes will have n(n-1)/2
edges.
11
Terminology – (4)
Strongly Connected Graph
A directed graph G is said to be strongly
connected, if for each pair u,v of nodes in G
there is a path from u to v and there is also a
path from v to u
Unilaterally Connected Graph
Graph 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
12
Terminology – (5)
Densely Connected Digraphs
The ratio of number of edges to number of
nodes is large
Sparsely Connected Digraph
The ratio of number of edges to number of
nodes is small
13
Terminology – (6)
n
di
e i 1
2
where
e number of edges
n number of vertices
d i degree of vertex i
14
Terminology – (7)
Tree graph
A connected graph T without any cycles is called a tree
graph or tree
There is a unique simple path P between any two
nodes u and v in T
Null graph
Graph containing only isolated nodes
Graph containing an empty set of edges
Order of Graph
The number of vertices
15
Terminology – (8)
Labeled Graph
A graph G is said to be labeled if its edges are
assigned data
Weighted Graph
A graph G is said to be weighted if each edge
e in G is assigned a nonnegative numerical
value w(e) called the weight or length of e.
16
Terminology – (9)
A graph G is called a simple
graph if it satisfies the following
conditions:
There are no loops in the graph G.
This means, that there does not
exist an edge in E of form (Vi, Vi)
for any Vi in V.
There is not more than one edge
joining any pair of vertices Vi and,
Vj in V. This means, that there
does not exist more than one edge
in E of the form (Vi, Vj).
17
Representation of Graph
Matrix Representation
List Representation
Multilist Representation
18
Adjacency Matrix Representation
Consider a graph G with set of vertices V and
set of edges E. If there are N nodes in V, for
N > = 1, the graph G may be represented by
an adjacency matrix which is a table with N
rows and N columns where
A(i, j ) 1
0
if and only if there is an edge (Vi , Vj )
otherwise
19
Adjacency Matrix
If G is an undirected graph, then
adjacency matrix A of G will be symmetric
matrix
i.e. for every i and j, A(i,j) = A(j,i)
20
Adjacency Matrix – Example 1
A B C D
0 1 1 1 A
1
0 0 0 B
A
1 0 0 1
1 0 1 0 C
21
Adjacency Matrix – Example 2
X Y Z W
Y X
0 0 0 1X
1 0 1
1Y
A
Z W
1 0 0 1 Z
0 0 1 0 W
22
Adjacency Matrix – Observations
X Y Z W
Y X
0 0 0 1X
1 0 1
1Y
A
Z W
1 0 0 1 Z
0 0 1 0 W
i th row in the adjacency matrix is determined by
the edges which start from the vertex Vi.
23
Adjacency Matrix – Observations
X Y Z W
Y X
0 0 0 1X
1 0 1
1Y
A
Z W
1 0 0 1 Z
0 0 1 0 W
The number of elements in the i th row whose
value is 1 is equal to the out degree of the node
Vi. 24
Adjacency Matrix – Observations
X Y Z W
Y X
0 0 0 1X
1 0 1
1Y
A
Z W
1 0 0 1 Z
0 0 1 0 W
The number of elements whose value is 1 in j th
column is equal to the in degree of vertex Vj.
25
Adjacency Matrix – Observations
X Y Z W
Y X
0 0 0 1X
1 0 1
1Y
A
Z W
1 0 0 1 Z
0 0 1 0 W
If there are loops at each node but no other
edge, then the adjacency matrix is the identity
or the unit matrix. 26
Adjacency Matrix – Observations
X Y Z W
Y X
0 0 0 1X
1 0 1
1Y
A
Z W
1 0 0 1 Z
0 0 1 0 W
For a null graph which has no edges, the
adjacency matrix has all its elements as zero.
Therefore, its adjacency matrix is a null matrix. 27
Adjacency Matrix – Example 3
A B C D E F G
A 0 5 0 0 0 0 0
B 0 0 6 5.5 0 0 0
C 0 0 0 0 0 10 0
D 0 0 0 0 7 0 0
E 0 0 0 0 0 7.5 0
F 0 0 0 0 0 0 4
G 0 0 0 0 0 0 0
28
Adjacency Matrix – Example 4
A B C D
A 0 1 0 1
B 1 0 1 1
C 0 1 0 1
D 1 1 1 0
29
An entry of 1 in the i th row and j th column
of A in the adjacency matrix of a graph
shows that there exists an edge (Vi ,Vj)
that means, there exists a path of length 1
from Vi and Vj
30
Proposition
Let A be the adjacency matrix of a graph
G. Then Ak(i,j) i.e the ij entry in the matrix
Ak, gives the number of paths of length K
from vi to vj.
31
Example
X Y Z W
Y X 0 0 0 1X
1 0 1
1Y
A
Z W
1 0 0 1 Z
W
0 0 1 0
Compute A2, A3, A4
32
X Y Z W
Y X 0 0 1 0 1 0 0 1
1 0 1
2 3 1
0 2 2
A2 A
0 0 1 1 1 0 1 1
Z W
1 0 0 1 0 0 1 1
•There is a path of length 2 from W to X 0 0 1 1
2 0 2 3
•There are two paths of length 3 from Y
to Z and from Y to W A4
1 0 1 2
•There are three paths of length 4 from
Y to W
1 0 1 1
33
Proposition
Let us define matrix Bm as below:
B m = A + A2 + A3 + … + Am
The entry ij of matrix Bm gives the number
of paths of length m or less from node vi to
vj
34
Path Matrix / Reachability Matrix
Let G be the simple directed graph with m nodes
v1,v2, … vm. The path matrix or reachability
matrix of G is the m-square matrix P defined as
below:
P(i, j ) 1
0
if and only if there is a path from Vi to Vj
otherwise
35
Proposition
Let A be the adjacency matrix and let P be
the path matrix of a digraph G.
Then P(i,j) = 1 if and only if there is a
nonzero number in the ij entry of the
matrix
B m = A + A2 + A3 + … + Am
36
Example
Using earlier example compute B4 and P
Y X
1 0 2 3 1 0 1 1
5
0 6 8 1 0 1
1
B4 P
3 0 3 5 1 0 1 1
Z W
2 0 3 3 1 0 1 1
Node Y is not reachable from any of the other
nodes
37
Observation
Graph G is strongly connected if and only if
the path matrix P of G has no zero entries
38
Drawbacks of Adjacency matrix
representation
Static implementation
A priori knowledge regarding number of
nodes in the graph is needed
Dynamic changes in the graph are difficult
to model
Wastage of space if matrix is sparse
Not possible to store additional information
about the graph
39
Linked Representation
A linked structure that can make allocation
and deallocations of nodes dynamically
Also known as adjacency structure
40
Linked Representation
Graph is represented using adjacency list
For each node, it’s neighbor’s information is
maintained in a linked list fashion
Adjacency list stores information about only those
edges that exist
The linked representation contains two lists:
A node list
An edge list
41
Example
42
Linked Representation
Node List / Vertex List
Name or Key Pointer to Pointer to Other information (if any)
value of the the next the first e.g. indegree, outdegree,
node node in list element in status etc.
NODE list EDGE
Edge List
Pointer to To link the
the location edges with
in the list the same
NODE of the initial node
destination 43
44
Linked Representation
45
Adjacency Multilists
Adjacency lists maintained as multilists
Multilists lists in which nodes may be
shared among several lists
Exactly one node for each edge but this
node will be in two lists
46
Adjacency Multilists
where
m is a boolean mark field
(may be used to check whether the edge has been
examined)
47
Adjacency Multilists - Example
48
Graph ADT
typedef struct vertex
{
struct vertex* pNextVertex;
void* dataPtr;
struct edge* pEdge;
}VERTEX;
typedef struct edge
{
struct vertex* destination;
struct edge* pNextEdge;
}EDGE;
49
Traversing a Graph
Breadth First Traversal
Uses a queue as an auxiliary structure to hold
nodes / vertices for future processing
Depth First Traversal
Uses a stack as an auxiliary structure to hold
nodes / vertices for future processing
50
Algorithms
During execution of algorithm each node N
of G will be in one of three states
STATUS = 1 (Ready State) – Initial state of N
STATUS = 2 (Waiting State) – N is on the
queue or stack, waiting to be processed
STATUS = 3 (Processed State) – N has been
processed
51
Breadth-first Traversal
General Idea
Process starting node say N
Process all neighbors of N
Process all neighbors of neighbors of N and so
on…
No node should be processed more than once
Use of queue to hold nodes that are waiting to
be processed
52
Breadth First Traversal -
Algorithm
Algorithm BreadthFirstG (graph)
1. Initialize all nodes to the ready state (STATUS=1)
2. Put the starting node N in queue (enqueue N) and
change its status to the waiting state (STATUS=2)
3. loop(queue is not empty)
4. a. Remove the front node N of queue (dequeue N)
b. Process N and change the status of N to the
processed state (STATUS=3)
5. a. Add to the rear of queue all the neighbors of N
that are in ready state (STATUS=1)
b. Change their status to the waiting state
(STATUS=2)
6. end loop
end BreadthFirstG 53
Breadth First Traversal - Example
54
Depth-first Traversal
General Idea
Examine starting node say N
Along path P, Examine a neighbor of N, then a
neighbor of neighbor of N and so on…
After coming to the “dead end” i.e. end of path
P, backtrack on P until we can continue along
another path P’ and so on…
Use of stack to hold nodes that are waiting to
be processed
55
Depth First Traversal - Algorithm
Algorithm DepthFirstG (graph)
1. Initialize all nodes to the ready state (STATUS=1)
2. Push the starting node N onto stack and change to the
waiting state (STATUS=2)
3. loop (stack is not empty)
4. a. Pop the top node N of stack
b. Process N and change the status of N to the
processed state (STATUS=3)
5. a. Push onto stack all the neighbors of N that are still
in the ready state (STATUS=1)
b. Change their status to the waiting state
(STATUS=2)
6. end loop
end DepthFirstG 56
Example
57
Minimum Spanning Tree
Spanning Tree
A tree that contains all of the vertices in the
graph
Minimum Spanning Tree
A spanning tree in which the total weight of
the edges is guaranteed to be the minimum of
all possible trees in the graph
58
MST - Example
59
Exercise - 1
For the diagraph given below obtain
The adjacency matrix
The in-degree and out-degree of each vertex
The adjacency-list representation
The adjacency-multilist representation (assuming
undirected graph)
60
Exercise - 2
Is the graph strongly connected?
List all the simple paths.
Obtain the adjacency-matrix,
adjacency-list and
adjacency-multilist
representations
of the graph
Obtain path matrix for
the graph
64
Exercise - 3
Apply depth first and breadth first traversal
to the complete graph of four vertices A, B,
C, D. List the vertices in the order that they
are visited.
65
Exercise - 4
Suppose a weighted graph G is maintained
in memory by a node array DATA and a
weight matrix W as below. Draw a picture
of G.
DATA: X, Y, S, T
66
WCE - SY (CSE) – 1CI 202: Data Structures
Course Teacher: Dr. Smriti H. Bhandari