Unit V Graph
Unit V Graph
UNIT – 5
GRAPH
Airlines
Source – destination network
Transportation problem
Graph Terminologies:
A graph has a set of vertices V, often labeled V1, V2 . . . etc and a set of edges E,
labeled e1, e2… Each edge is a pair (U,V) of vertices. In general, Graph G = (V, E) for
the graph with vertex set V and edge set E.
In applications, where pair (u, v) is distinct from pair (v, u) the graph is directed.
Otherwise the graph is undirected.
Undirected Graph:
Weighted Graph:
A graph (or digraph) is termed weighted graph if all the edges in it are labeled
with some weights
Adjacent vertices:
Parallel edges:-
If there is more than one edge between the same pair of vertices, then they are
known as parallel edges.
Simple graph:
A graph (digraph) if it does not have any self loop or parallel edges is called
simple graph.
Complete graph:
Acyclic graph:
If there is a path containing one or more edges which starts from a vertex vi and
terminates into the same vertex then the path is known as a cycle.
If a graph does not have any cycle, then it is called acyclic graph.
Isolated vertex:
A vertex is isolated if there is no edge connected from any other vertex to the vertex
Degree of vertex:
The number of edges connected with vertex vi is called the degree of vertex vi and
degree (Vi) = 3.
In degree of Vi = number of edges incident to Vi.
Out degree of Vi = number of edges emanating from Vi.
indegree (V1) = 1
Prepared By M. Raja, CSE,KLU 6
CSE 103 - Unit V- Graph
outdegree (V1) = 2
Representation of Graphs:-
1. Set representation
2. Linked representation
3. Matrix representation
Set representation:
Topological sorting:-
algorithm to find a topological ordering is to find out any vertex with in degree zero, that
is, a vertex without any predecessor. We can then add this vertex in an ordering set
(initially which is empty) and remove it along with its edges from the graph. Then we
repeat the same strategy on the remaining graph until it is empty.
Example:
Given a graph G:
Visited vertices: V1, V2, V5, V7, V6, V4, V3.
Here V1has no incoming edges. Visit V1 first and remove all the edges connected to it.
Here V2 has no in degree. Visit V2 next and remove all edged connected to V2.
V3 is visited finally.
Visited vertices: V1, V2, V5, V7, V6, V4, V3.
It is related to weighted graph, where we find a spanning tree so that the sum of all
the weights of all the edges in the tree is minimum.
A Spanning Tree:
A Spanning Tree of a connected graph is its connected acyclic sub graph that
contains all the vertices of the graph
A Minimum Spanning Tree of a weighted connected graph is its spanning tree of
the smallest weight, where the weight of a tree is defined as the sum of the weights on all
its edges.
The minimum spanning tree problem is the problem of finding a minimum
spanning tree for a given weighted connected graph.
Consider a graph:
Prepared By M. Raja, CSE,KLU 10
CSE 103 - Unit V- Graph
1. The number of spanning trees grows exponentially with the graph size.
2. Generating all spanning trees for a given graph is not easy.
To overcome this drawback, we make use of some efficient algorithms such as
Prim’s algorithm
Kruskal’s algorithm
Prim’s Algorithm:
b(a,3) c(b,1),d(_,∞)
e(a,6), f(b ,4)
c(b,1) d ( c , 6 ) , e ( a , 6 ),
f(b,4)
f(b,4) d(f,5),e(f,2)
e(f,2)
VT Å VT {u*}
ET Å ET {e*}
return ET
Example 2:
Kruskals algorithm:-
(V1,V4) 1 Accepted
(V6,V7) 1 Accepted
(V1,V2) 2 Accepted
(V3,V4) 2 Accepted
(V2,V4) 3 Rejected
(V1,V3) 4 Rejected
(V4,V7) 4 Accepted
(V3,V6) 5 Rejected
(V5,V7) 6 Accepted
Minimum spanning tree is:
This problem of a graph is about finding a path between two vertices in such a way
that this path will satisfy some criteria of optimization.
E.g. For non-weighted graph, the number of edges will be minimum and for a weighted
graph, the sum of weights on all its edges in the path will be minimum.
Dijkstra’s algorithm
Warshalls algorithm.
Floyd’s algorithm
Here, there is a distinct vertex, called the source vertex and it requires to find the
shortest path from this source vertex to all other vertices.
It is a single- source shortest path problem: for a given vertex called the source in
a weighted connected graph, find shortest path to all its other vertices. The best-known
algorithm for the single – source shortest path problem called Dijkstra’s algorithm.
Example 1:
Consider the graph:
V4(V1 , 1 ) V2( V1 , 2 ),
V3 ( V4 ,2+1 ),
V5(V4 , 2+1 ) ,
V6( V4 ,8+1)
V7(V4 , 1+4)
V6(V7,6)
//…
Operation on Graph:
Insertion:
a) To insert a vertex and hence establish connectivity with other vertices in the
existing graph.
b) To insert an edge between two vertices in the graph.
Deletion:
Merging:
Warshall’s algorithm:
from any vertex Vi to another vertex Vj either directly or through one or more
intermediate vertices. In other words, we can test the reach ability of all pairs of vertices
in a graph.
We can determine whether there is a path from any vertex vi to another vertex vj
either directly or through one or more intermediate vertices. First, we find the adjacency
matrix from the digraph. Then we compute path matrix.
Prepared By M. Raja, CSE,KLU 22
CSE 103 - Unit V- Graph
Consider the unweighted graph: Find its reach ability of all pair of vertices.
Solution:
First find its adjacency matrix.
Algorithm:
Warshall (A[1…….N,1……N])
R(0)Å A
for kÅ1 to n do
for iÅ1 to n do
for jÅ1 to n do
R(0) =
R(1) =
R(2) =
R(3) =
R(4) =
R(5) =
Prepared By M. Raja, CSE,KLU 24
CSE 103 - Unit V- Graph
R(6) =
Example 2:
0 1 0 0
0 0 0 1
Adjacency matrix =
0 0 0 0
1 0 1 0
0 1 0 0
0 0 0 1
R(0) =
0 0 0 0
1 0 1 0
0 1 0 0
0 0 0 1
R(1) =
0 0 0 0
1 1 0
0 1 0
0 0 0 1
R(2) =
0 0 0 0
1 1 1
0 1 0 1
0 0 0 1
R(3) =
0 0 0 0
1 1 1 1
1 1 1 1
1 1 1 1
R(4) =
0 0 0 0
1 1 1 1
• The path matrix obtained using warshall’s algorithm shows the presence or
absence of any path between a pair of vertices. It does not take into account, the
weights of edges.
• If weights are to be taken into account and if we are interested in the length of the
shortest path between any pair of vertices, then Floyd’s algorithm is used.
• The goal is to find the distances from each vertex to all other vertices.
• A Distance matrix D is a n-by-n matrix indicating the length of the shortest path in
a graph.
• The element dij in the ith row and the jth column of this matrix indicates the length
of the shortest path in a graph, from the ith vertex to the jth vertex.
DÅW
for KÅ 1 to n do
for iÅ 1 to n do
for jÅ 1 to n do
D[i , j] Åmin {D[ i,j ] , D[ i,k ], D[ k,j ]}
return D
Example:
2
a b
3 7
6
c d
1
0 3
2 0
Distance matrix D =
7 0 1
6 0
Perform ADD operation on row and column values to change the α Æ some
distance values. Moreover if possible change the values to small values.
0 3
2 0
D(0) =
7 0 1
6 0
0 3
2 0
D(1) =
7 0 1
6 0
0 3
2 0
D(2) =
7 0 1
6 0
0 3
2 0
D(3) =
7 0 1
6 0
Graph Traversal:
Traversing a graph means visiting all the vertices in the graph exactly once.
Several methods are known to traverse a graph systematically, out of them two methods
are accepted as standard. They are:
• Depth- first search traversal is similar to the in order traversal of a binary tree.
• Starting from a given node, this traversal visits all the nodes up to the deepest level
and so on.
• Another Standard graph traversal method is BFS. This traversal is very similar to the
level –by-level traversal of a tree. Here, any vertex in the level I will be visited only
after the visit of all the vertices in its preceding level, that is, at i-1. It is roughly
analogous to the preorder traversal of the tree.
c. Push all the unvisited vertices adjacent to the popped vertex into the
stack.
• Visit V6
• Push all unvisited vertices adjacent to v6 into the stack.
• There are no unvisited vertices adjacent to V6
• Pop a vertex, V3 from the stack.
• Visit V3
• Push all unvisited vertices adjacent to V3 into the stack.
• Pop a vertex, V5 from the stack.
• Visit V5
• Push all unvisited vertices adjacent to V5 into the stack.
• There are no unvisited vertices adjacent to V5.
• Pop a vertex, V4 from the stack
• Visit V4
• Push all unvisited vertices adjacent to V4 into the stack.
• There are no unvisited vertices adjacent toV4.
• The stack is now empty.
• Therefore traversal is complete and visited vertices are:
V1, V2, V6, V3, V5, V4.
Example:
Visit V1:-
• Insert V1 into the queue.
• Visit all unvisited vertices adjacent to V2 and insert them in the queue.
• Visit all unvisited vertices adjacent to V4 and insert them in the queue.
• Visit all unvisited vertices adjacent to V6 and insert them in the queue.V6 does not
have any unvisited adjacent vertices.
• Remove a vertex V5 from the queue.
• Visit all unvisited vertices adjacent to V5 and insert them in the queue.
• V5 does not have any unvisited adjacent vertices.
Visited[v]=true;
For each w adjacent to V
If (| visited[w])
DFS(w):
}
If we perform this process we need to be careful to avoid cycles. To do this, when
we visit each node mark it as “visited” and recursively call DFS an all adjacent vertices
that are not already marked.
This DFS algorithm continues the procedure until there is no unmarked node found.
Undirected graph:
An undirected graph is connected if and only if a DFS starting from any node visits every
node.
Biconnectivity:
Euler circuit problem, which finds a path that touches every edge exactly one, is
solvable in linear time.
Hamilton cycle problem ask for a simple cycle that contain every vertex. No linear
algorithm for this problem.
The single source unweighted problem for directed graph is also solvable in linear
time. No linear time algorithm for corresponding longest simple path problem.
But there are no known algorithm that are guaranteed to run polynomial time
Some important problems that are roughly equivalent in complexity.
These problems form a class called NP – Complete problems.
NP stands for non deterministic polynomial time.
NP completes problems:
Among all the problems known to be in NP, there is a subset known as NP-
complete problems which contain hardest.
NP-complete problem has some property that any problem in NP can be
polynomially reduced to it.
Examples:
*******