Lecture 11 - Graphs
Lecture 11 - Graphs
Graph
A graph is a data structure that describes a binary relation between elements and has a
webby looking graphical representation. Graph plays a significant role in solving a rich
class of problems.
Definition:
A graph G = (V, E) consists of a finite set of non-empty set of vertices V and set of edges
E.
V = { v1, v2, …, vn}
E = {e1, e2, …en}
Each edge e is a pair (v, w) where v, w є V. The edge is also called arc.
Eg, of graphs:
1 1 1
e1 e2
e1 e2 e1
e6
2 3 2 e3
2 e5 3 e3 e4 e5 e6
e2
e4 e3
4 5 6 7 3
4
G1 G2 G3
V(G1) = {1, 2, 3, 4} V(G3) = {1, 2, 3}
E(G1) = {e1, e2, e3, e4, e5, e6} E(G3) = {e1, e2, e3}
e1 = (1, 2) e1 = (1, 2)
e2 = (1, 3) e2 = (2, 3)
e3 = (3, 4) e3 = (1, 3)
e4 = (2, 4)
e5 = (2, 3)
e6 = (1, 4) Now, specify the vertices
and edges for G2.
1
Graph
The vertices are represented by points or circles and the edges are line segments
connecting the vertices. If the graph is directed, then the line segments have arrow heads
indicating the direction.
Directed Graphs:
If every edge (i, j), in E(G) of a graph G is marked by a direction from i to j, then the
graph is called directed graph. It is often called diagraph.
1
e1 e2
e6
2 e5 3
e4 e3
4
G4
In edge e = (i, j), we say, e leaves i and enters j. In diagraphs, self loops are allowed. The
indegree of a vertex v is the number of edge entering v. The outdegree of a vertex v is the
number of edges leaving v. The sum of the indegrees of all the vertices in a graph equals
to the sum of outdegree of all the vertices.
Undirected Graph:
If the directions are not marked for any edge, the graph is called undirected graph. The
graphs G1, G2, G3 are undirected graphs. In an undirected graph, we say that an edge e =
(u, v) is incident on u and v (u and v are connected).
Undirected graph don’t have self loops. Incidence is a symmetric relation i.e. if e = (u, v)
Then u is a neighbor of v and vice versa. The degree of a vertex, d(v), is the total number
of edges incident on it.
2
Graph
The sum of the degrees of all the vertices in a graph equals twice the number of edges if
d(v) = 0, v is isolated. If d(v) = 1, v is pendent.
Weighted Graphs:
A graph is said to be weighted graph if every edge in the graph is assigned some weight
or value. The weight is a positive value that may represent the cost of moving along the
edge, distance between the vertices etc.
The two vertices with no edge (path) between them can be thought of having an edge
(path) with weight infinite.
1 2 2
6 1
3
3 5 4
3
Graph
If e(i, j) is an edge on E(G), then we say that the edge e(i, j) is incident on vertices i and j.
Eg. in the above figure, e4 is incident on C and D. If (i, j) is directed edge, then i is
adjacent to j and j is adjacent from i.
Path:
A path is a sequence or distinct vertices, each adjacent to the next. For e.g. the sequence
of vertices e1, e3, e4 (i.e (B,A), (A,C), (C,D)) of the above graph form a path from B to D.
The length of the path is the number of edges in the path. So, the path from B to D has
length equal to three.
In a weighted graph, the cost of a path is the sum of the costs of its edges. Loops have
path length of 1.
Cycle:
A cycle is a path containing at least three vertices such that the last vertex on the path is
adjacent to the first. In the above graph G6, A, C, D, A is a cycle.
Connected:
Any graph is connected provided there exists a path (directed or undirected) between any
two nodes. A digraph is said to be strongly connected if, for any two vertices there is a
direct path from i to j.
A digraph is said to be weakly connected if, for any two vertices i and j, there is a direct
path from i to j or j to i. or A weakly-connected graph is a directed graph for which its
underlying undirected graph is connected.
Eg.
A D E E
A D A D
B C
B C B C
Complete graph
A complete graph is a graph in which there is an edge between every pair of vertices.
4
Graph
Representation of Graphs
A graph can be represented in many ways. Some of them are described in this section.
Adjancency matrix
Adjancency matrix A for a graph G = (V, E) with n vertices, is n x n matrix, such that
Aij = 1, if there is an edge from vi to vj
Aij = 0, if there is no such edge
0 otherwise
Eg; An adjacency matrix for the following undirected graph is
V1
V2 V3 V4
V5
A=
V1 V2 V3 V4 V5
V1 0 1 1 1 0
V2 1 0 1 0 1
V3 1 1 0 1 1
V4 1 0 1 0 1
V5 0 1 1 1 0
5
Graph
If the graph is directed, then the adjacency matrix will be as follows. The number in each
row tells the outdegree of that vertex.
V1
V2 V3 V4
V5
A=
V1 V2 V3 V4 V5
V1 0 1 1 1 0
V2 0 0 1 0 1
V3 0 0 0 1 0
V4 0 0 0 0 1
V5 0 0 1 0 0
6
Graph
1 2 1 2 3
2 3 4
3
4 3 4
1 2 3
Graph Traversals
A graph traversal means visiting all the nodes of the graph. Basically, there are two
methods of graph traversals.
1. Breadth First Traversal
2. Depth First Traversal
1. Breadth First Traversal
The Breadth First Traversal begins with a given vertex and then it next visits all the
vertices adjacent to v, putting the vertices adjacent to these in a waiting list to be
traversed after all vertices adjacent to v have visited. Breadth First Traversal uses
queue.
4 8 A B C
2 5 7 D E F
1 3 6 G H I
7
Graph
Algorithm:
void BFTraversal(Graph G)
{
for each vertex v in G
visited[v] = false;
for each vertex v in G
if(visited[v]==false)
{
enqueue(V, Q);
do
{
v=dequeue(q);
visited[v]=true;
visited(v);
for each vertex w adjacent to v
if(visited[w]==false)
enqueue(w, q);
}while(isempty(Q)==false)
}
}
8
Graph
Algorithm:
void DFTraversal(Graph G)
{
Subgraph:
Let H be a graph with vertex set v(H) and edge set E(H) and, similarly,
let G be a graph with vertex set V(G) and edge set E(G). Then H is said
9
Graph
C
C
A B A B A B
(i) (ii) (iii)
Spanning Tree:
Let a graph G=(V,E) be a graph. If T is a subgraph of G and contains all the vertices but
no cycles/circuits, then ‘T’ is said to be spanning tree of G.
In other words, the spanning tree of an undirected graph G is the free tree formed from
graph edges which connects all the vertices of G.
v1 v5
v4
v2 v7
v3 v6
For a given graph G=(V, E), if Breadth-First search and Depth-First search call is made,
then the resultant tree that is generated is the spanning tree of the graph G. Thus, by
making a Depth-First search on G, we get the Depth-First spanning tree. Similarly, on
applying Breadth-First search on graph ‘G’ we get Breadth First spanning tree.
10
Graph
v1 v1
v1
v2 v2 v3 v4
v3 v4
v2 v3 v4
v5 v5
v5
Graph G BS Spanning Tree and DF Spanning Tree of G
2 2 2
A B A B A B A B
2
3 3 4 4 3 3
4
3
C D C D C
1
D C D
1 1
G T1 T2 T3
2 2
A B A B A B
2 3 4 2 3
C D C D C D
1 1
T4 T5 T6
Here, T1 to T6 are the spanning trees of G. Among them, T4 has minimum cost (i. e. 5).
So, T4 is the minimum spanning tree of graph G
11
Graph
There are several algorithms available to determine the minimal spanning tree of a given
weighted graph.
12
Graph
Kruskal’s Algorithm
To determine minimum spanning tree consider a graph G with n vertices.
Step 1: List all the edges of the graph G with increasing weights
Step 2: Proceed sequentially to select one edge at a time joining n vertices of G such that
no cycle is formed until n-1 edges are selected.
Step 3: Draw the n-1 edges that were selected forming a minimal spanning tree T of G.
2
A B
4 1 1
3 0
2 7
C D E
4
5 3
6
F 1 G
We have,
Edges: AD FG AB CD BD AC DG CF EG DE DF BE
Weights 1 1 2 2 3 4 4 5 6 7 8 10
A A
B B
1 1
C D E C D E
F G F G
1
(1) (2)
13
Graph
A B A B
2 2
1 1
C D E C D E
2
F G F G
1 1
(3) (4)
A B A B
2 2
1 1
C D E C D E
2 2
4 4 4
F G F G
1 1
(5) (6)
Hence, the minimum spanning tree is generated.
Round Robin Algorithm
This method provides better performance when the number of edges is low. Initially each
node is considered to be a partial tree. Each partial tree is maintained in a queue Q.
Priority queue is associated with each partial tree, which contains all the arcs ordered by
their weights.
The algorithm proceeds by removing a partial tree, T1, from the front of Q, finding the
minimum weight arc a in T1; deleting from Q, the tree T2, at the other end of arc a;
combining T1 and T2 into a single new tree T3 and at the same time combining priority
queues of T1 and T2 and adding T3 at the rear of priority queue. This continues until Q
contains a single tree, the minimum spanning tree.
14
Graph
Q Priority queue
{A} 1, 2
{B} 2, 2, 3, 3
{C} 1, 3, 4
{D} 2, 4, 5
{E} 3, 5
A 3
2 E
1 B
2 5
3
C D
4
A
E
1 B
C D
Q Priority queue
{B} 2, 2, 3, 3
{D} 2, 4, 5
{E} 3, 5
{A, C} 2, 3, 4
15
Graph
A
E
1 B
C D
Q Priority queue
{E} 3, 5
{A, C} 2, 3, 4
{B, D} 2, 3, 3, 4, 5
A
3 E
1 B
C D
Q Priority queue
{A, C} 2, 3, 4
{E, B, D} 2, 3, 4, 5, 5
Q Priority queue
{A, C, E, B, D} 3, 3, 4, 4, 5, 5
16
Graph
A 2
3 E
1 B
2
C D
Only 1 partial tree is left in the queue, which is the required minimum spanning tree.
All pairs: Find the shortest path from all pair of vertices.
17
Graph
Let source vertex be a. We will find the shortest path to all the other vertices.
3
b c
3
1
3 z
a
2
2
d 3 e
5
b d ∞ 5 ∞
6 b d
4 6 ∞
4
1 z
a 8 2 1
8 2 z
0 a
3
2 3
c e 2
1
c 1 e
0
∞ 0 ∞ 18
Given graph with weights (a)
4(a) 5 Graph
b d ∞ 3(a,c 5 10(a,c)
6 ) b d
4 6 ∞
∞ 4
1 z
0 a 8 2 1 z
0 a 8 2
3
2 3
c e 2
1
∞ c 1 e
2(a) 0
0 12(a,c)
(b) (c)
3(a,c 5 8(a,c,b
) b d )
6
4
1 z
a 8 2
13(a,c,b,d,e
3)
2
c 1 e
2(a) 0 10(a,c,b,d)
(f)
19
Graph
Greedy Algorithm
One of the simplest approaches that often leads to a solution of an optimization problem.
This approach selects the best choice at each step, instead of considering all sequences of
steps that may lead to an optimal solution. Algorithm that make what seems to be the
“best” choice at each step are called greedy algorithm.
Transitive Closure
In all shortest path, we noticed that every time we have to determine whether there is a
path from vertex i to j. This can be accomplished by observing a matrix, say d(n). The path
exists if and only if the entry does not have the value infinity. In matrix d(n), we consider
only path (not the weight). Thus, (dk) can be regarded as Boolean matrix.
If there is a path from i to j, then (i, j)th entry in dk is 1 (which means true) otherwise the
value is 0. If ‘A’ is the adjacency matrix of graph ‘G’, then the transitive closure is
defined to be a Boolean matrix with the above property.
1 2
3 4
Directed
Graph
A+ Matrix has the following property:
1 if there is a path from i to j
0 otherwise
The matrix A+ is called the transitive closure. The Transitive closure helps to determine
which pair or vertices in a digraph are connected by a path. It takes O(n4) time
complexity.
20
Graph
B
C
A E
D
Now, let adj2 is a matrix that stores information about the existence of path of length 2.
The adj2 matrix (path matrix of length 2) can be represented as:
A B C D E
A 0 0 0 1 1
B 0 0 0 1 1
C 0 0 0 1 1
D 0 0 0 1 0
E 0 0 0 0 1
A B C D E
A 0 0 0 1 1
B 0 0 0 1 1
C 0 0 0 1 1
D 0 0 0 0 1
E 0 0 0 1 0
21
Graph
To calculate the path matrix, we just multiply the matrix itself. For eg, to calculate path
matrix 3, the matrix of adj1 is multiplied 3 times itself. While multiplying, the addition
operatin is replaced by ‘||’ operation and multiplication is replaced by ‘&&’ operation.
To find at least one path between two nodes, we can check adj1, adj2, adj3, …,adjmaxnodes-
1. If any of the adjacency matrix give true value for node ‘i’ and ‘j’, then there exist at
least a path between nodes ‘i’ and ‘j’. To store the adjacency values for path, we use a
path matrix as:
A B C D E
A 0 0 0 1 1
B 0 0 0 1 1
C 0 0 0 1 1
D 0 0 0 1 1
E 0 0 0 1 1
The length of path can be 1, 2, 3 or 4… and there is no termination of the order of the
length. Let ‘m’ is the highest order of length and ‘n’ is the no. of total nodes. If the graph
supports m>n, then at least one of the node is visited twice or more times in the path. It
means, we can discard the cycling path from the node to the same node. The removal
process is repeated until no node is repeated in the path. This ensures that the paths that
exist in the graph be less than or equal to n. The final matrix path in which no path exists
that has length greater than ‘n’ is known as transitive closure of the matrix adj.
Warshall’s Algorithm
Transitive closure method of finding path between nodes is quite inefficient because it
processes the adjacency matrix lots of times. An Efficient method for calculating
transitive closure is by Warshall’s algorithm whose time complexity is only O(n3).
a b
d c
We first find the matrices d0, d1, d2, d3, d4, and d4 is the transitive closure.
22
Graph
d1 has 1 as its (i,j)th entry if there is a path from vi to vj that has v1 = a as an interior
vertex.
d2 has 1 as its (i,j)th entry if there is a path from vi to vj that has v1 and v2 as interior
vertex. d3 has 1 as its (i,j)th entry if there is a path fro vi to vj that has v1 v2 and v3 as
interior vertex.
23
Graph
Warshall’s Algorithm
procedure warshall (MR : n x n zero one matrix)
{
D = MR
for k = 1 to n
begin
for i = 1 to n
begin
for j = 1 to n
dij = dij (dik dkj)
end
}
D = [dij] is the transitive closure.
Topological Sorting
A topological sort is an ordering of vertices in a directed acyclic (graph with no cycle)
graph (dag), such that if there is a path from vi to vj, then vi appears before vj in the
ordering. A topological sort of a graph can be viewed as an ordering of its vertices along
a horizontal line so that all directed edges go from left to right.
Applications:
Consider the course available at university as the vertices of a directed graph; where there
is an edge from one course to another if the first is a prerequisite for the second.
A topological ordering is then a listing of all the courses such that all prerequisite for a
course appear before it does.
24
Graph
A B
C D E
F G
G1
0 1 2 3 4
5 6 7 8 9
G2
Fig: Directed Graph with no Directed
Cycle
25
Graph
A B E D C G F
26
Graph
3 6 9 0 2 4 1 5 8 7
9 6 3 4 8 2 0 5 1 7
27
Graph
28