Unit V Graphs: Definition of Graph
Unit V Graphs: Definition of Graph
Unit V
Graphs
Definition of graph
A graph is a collection of two sets V and E where V is a finite non empty set
of vertices and E is a finite non empty set of edges.
Vertices are nothing but the nodes in the graph and the two adjacent vertices
are joined by edges. The graph is thus a set of two sets. Any graph G is
denoted by G = {V,E}
Types of graph:
Basically graphs are classified in to two types
1. Directed graphs
2. Un directed graphs
In the directed graph the directions are shown on the edges. As shown in the
following figure the edges between the vertices are ordered.
V1
E1
E2
V2 3
E3 V4
E4
Directed Graph
In this type of graph, the edge E1 is in between vertices V1 and V2. the V1
is called head and the V2 is called the tail. Similarly for V1 head the tail is
V3 and so on.
We can say E1 is the set of (V1,V2) and not of (V2,V1).
1
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.
Similarly, in an undirected graph, the edges are not ordered. In this type of
Graph the edge E1 is the set of (V1,V2) or (V2,V1).
V1
E1
V2 V3
V4
Properties of Graph:
Complete graph:
If an undirected graph of n vertices consists of n(n-1)/2 number of edges
then it is called as complete graph.
The graph shown in following figure is a complete graph.
V1 V2
V4 V3
Complete graph
Sub Graph:
A sub graph G’ of graph is a graph such that the set of vertices and set of
edges of G’ are proper subset of the set of edges of G.
V1 V1
V2 V3
V2
G G’
2
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.
Connected Graph:
An undirected graph is said to be connected if for every pair of distinct
vertices Vi and Vj in V(G) there is a graph from Vi to Vj in G.
V1
V2 V3
V4
Representation of Graphs:
There are several representation of graph, but we will discuss the two
commonly used representations called
Adjacency matrix
Adjacency lists
Adjacency matrix representation:
The adjacency matrix of G is a 2-dimensional array of size n*n ( where n is
the number of vertices in the graph). Consider a graph G of n vertices and
the matrix M. if there is an edge present between vertices Vi and Vj then
M[I][j]=1 else M[I][j]=0. note that for an undirected graph if M[I][j]=1 then
M[j][I] is also 1. Here are some graphs shown by adjacency matrix.
1 2 3 4
V1
1 0 1 1 0
2 0 0 0 0
V2 V3
3 0 0 0 0
4 0 1 1 0
V4
3
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.
1 2 3 4
V1
1 0 1 1 0
2 1 0 0 1
V2 V3
3 1 0 0 1
V4 4 0 1 1 0
As can be seen from above, the adjacency matrix for an undirected graph is
symmetric. The adjacency matrix for a directed graph need not be
symmetric. The space needed to represent a graph using its adjacency matrix
is n2 locations. About half this space can be saved in the case of undirected
graphs by storing only the upper or lower triangle of the matrix.
Adjacency Lists:
In this representation the n rows of the adjacency matrix are
represented as n linked lists. There is one list for each vertex in the graph.
The nodes in list I represent the vertices that are adjacent from vertex i. Each
list has a head node.
4
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.
The head nodes are sequential providing easy random access to the
adjacency list for any particular vertex. The adjacency lists for graphs G1
and G2 are shown below.
Vertex 1 2 3 4 N
Vertex 2 1 3 4 N 5 N
Vertex 3 1 2 4 N
Vertex 4 1 2 3 5 N
Vertex 5 2 4 N
Vertex 1 2 N
Vertex 2 1 3 N
Vertex 3 2 N
Adjacency lists.
Given the root node of a binary tree, one of the most common operations
performed is visiting every node of the tree in some order. Similarly, given a
vertex in a directed or undirected graph we may wish to visit all vertices in
two ways_ using the Depth First Search and the Breadth First Search
algorithm.
5
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.
2 3
In DFS the basic data structure for storing the adjacent nodes is stack. In the
algorithm we have used a recursive call to DFS function. When a recursive
call is invoked actually push operations get performed. When we exit form
the loop pop operation will be performed. Let us see how our algorithm
works.
Step1: start with vertex 1, print it. So ‘1’ gets printed. Mark 1 as visited.
Visited
Output screen
0 0
1
1 1
2 0
3 0
6
4 0
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.
Step2: Find adjacent vertex to 1, say i.e. 2 if it is not visited, 2 will get
inserted in the stack, mark it as visited.
Visited Stack
Output screen
0 0 2 Top After exiting the
loop 2 will be 1 2
1 1 popped print 2
2 1
3 0
4 0
Step3: Find adjacent to 2 i.e. vertex 4 if it is not visited, 4 will get pushed on
the stack mark it as visited.
Visited Stack
Output screen
0 0 4 Top After exiting the
loop 4 will be 1 2 4
1 1 popped print 4
2 1
3 0
4 1
Visited Stack
Output screen
0 0 3 Top After exiting the
loop 3 will be 1 2 4 3
1 1 popped print 3
2 1
3 1
Since all the nodes are coverer stop the procedure.
4 1 So output of DFS is: 1 2 4 3
7
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.
2 3
In BFS queue is maintained for storing the adjacent nodes and an array
‘visited’ is maintained for keeping the track of visited nodes. i.e., once a
particular node is visited it should not be revisited again.
8
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.
Visited Queue
-1 0 1 2 3 4
0 0
Inserted vertex 1 in queue
1 1 and marked the index 1 of
visited array by 1
2 0
Front Rear
3 0
4 0
Step 2:
Output Screen
0 1 2 3 4 Delete 1 and 1
print it. So 1
gets printed
Front
Rear
Step 3: Find adjacent vertices of vertex 1 and mark them as visited, insert
those in queue. The adjacent vertices of vertex 1 are 2 and 3.
Visited
0 0 0 1 2 3 4
Increment front by 1 delete
1 1 1 2 3 2 from queue and print it so
2 gets printed
2 1
Font Rear
3 1
4 0
9
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.
Step 4: Find adjacent to 2 and insert those nodes in queue as well as mark
them as visited.
Visited
Output Screen
0 0 0 1 2 3 4
1 2
1 1 1 2 3 4
2 1
Font Rear
3 1
4 1
Output Screen
0 1 2 3 4
1 2 3
1 2 3 4 So 3 gets
printed
Font Rear
Visited
Output Screen
0 0 0 1 2 3 4
1 2 3 4
1 1 1 2 3 4
2 1
Front
3 1 Rear
4 1
Increment front, delete the node form the queue and print it.
So output will be- BFS for above graph is: 1 2 3 4
10
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.
Applications of Graph
1. In computer networking such as local area network (LAN), wide area
networking (WAN), internetworking.
2. In telephone cabling graph theory is effectively used.
3. In job scheduling algorithms, graphs are used.
Since graph is nothing but the collection of nodes (vertices) and edges. One
can find the best-suited distance within the vertices. If the distance between
the two vertices is reduced then the cost of cabling and the time in traveling
through the nodes will get reduced. .
Definition of spanning tree
A spanning tree is a subset of a tree T in which all the vertices of tree T are
present but it may not contain all the edges.
The minimum spanning tree of a weighted connected graph G is called
minimum spanning tree if its weight is minimum. There are two popular
algorithms are used to construct minimum spanning tree, namely prim’s
algorithm and kruskal’s algorithm.
1. Prim’s algorithm: in prim’s algorithm the pair with the minimum
weight is to be chosen. Then adjacent to these vertices which ever is
the edge having minimum weight is selected. This process will be
continued till all the vertices are not been covered.
For example consider the following graph:
V1 2 V2
4 1 3 3
V3 V4 V5
2 7
5 8 4
1 6
V6 V7
11
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.
Prim’s algorithm can be explained with the help of prim’s table. The initial
configuration is shown below.
V Known dv pv
V1 0 0 0
V2 0 0
V3 0 0
V4 0 0
V5 0 0
V6 0 0
V7 0 0
12
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.
The next vertex selected is v4. Every vertex adjacent to v4. v1 is not
examined because it is known. V2 is unchanged, because it has dv = 2 and
the edge cost from v4 to v2 is 3; all the rest are updated. This shown in the
below table.
V Known dv pv
V1 1 0 0
V2 0 2 V1
V3 0 2 V4
V4 1 1 V1
V5 0 7 V4
V6 0 8 V4
V7 0 4 V4
The table after v4 s declared known
The next vertex chosen is v2. This does not affect any distances. Then v3 is
chosen, which affects the distance in v6, producing results from the selection
of v7, which forces v6 and v5 to be adjusted. Finally V6 and v5 are selected.
V Known dv pv
V1 1 0 0
V2 1 2 V1
V3 1 2 V4
V4 1 1 V1
V5 0 7 V4
V6 0 5 V3
V7 0 4 V4
The table after v2 and then v3 are declared known
13
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.
V Known dv pv
V1 1 0 0
V2 1 2 V1
V3 1 2 V4
V4 1 1 V1
V5 0 6 V7
V6 0 1 V7
V7 1 4 V4
The table after v7 is declared known
V Known dv pv
V1 1 0 0
V2 1 2 V1
V3 1 2 V4
V4 1 1 V1
V5 0 6 V7
V6 0 1 V7
V7 0 4 V4
The table after v6 and v5 are selected ( prim’s algorithm terminates)
The edges in the spanning tree can be read from the table: (v2,v1), (v3,v4),
(v4,v1), (v5,v7), (v6,v7), (v7,v4). The total cost is 16.
V1
2 V2
1
V3 2 V4 V5
4 6
1
V6 V7
14
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.
8 1
(1)
2 3 1
13
12 2 8 (2)
7
1
12
2
10 9
(3) (4)
1 8
2 1
2
3 3
Weight = 21
15
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.
a z
16
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.
17
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.
Dist (e) =
Dist (f) =
Dist (z) =
So minimum node is selected in p i.e. node d
Step 2: v = d
P = {a,d} T = {b,c,e,f,z}
Dist (b) = min{old dist(b),dist(d)+w(b,d)}
Dist (b)=min{22,8+}
Dist (b)=22
Dist(c) =min{16,8+10}=16
Dist (e)=min{,8+}=8
Dist (f)=min{,8+6}=14
Dist (z)=min{,8+}=8
Step 3: v=f
P={a,d,f} T={b,c,e,z}
Dist (b) = min{22,14+7} = 21
Dist(c) = min{16,14+3} = 16
Dist (e) = min{,14+} =
Dist (z) = min(,14+9} = 23
Step 4: v = c
P = {a,d,f,c} T = {b,e,z}
Dist (b) = min{21,16+20} = 21
Dist (e) = min(,16+4} = 20
Dist (z) = min{23,16+10} = 23
18
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.
Step 5: v= e
P={a,d,f,c,e} T={b,z}
Dist (b) = min{21,20+2} = 21
Dist (z) = min{23,20+4} = 23
Step 6: v=b
P={a,d,f,c,e,b} T = {z}
Dist (z) = min{23,21+2} = 23.
Now the target vertex for finfing the shortest path is z. hence the length of
the shortest path from the vertex a to z is 23.
The shortest path is as shown below:
Bi-connectivity
Bi-connected graphs are the graphs which can not be broken into two
disconnected pieces (graphs) by connecting single edge. For example
V1 E1 V3 E5
E2 V5
E3
V2 V4 E6
E4
19
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.
In the given figure even if we remove any single edge the graph does not
become disconnected.
V1 V3
E5
E2 E3 V5
E6
V2 V4
E4
For example even if we remove an edge E1 the graph does not become
disconnected. We do not get two disconnected components of graph.
Properties of bi-connected graph:
1. There are two disjoint paths between any two vertices
2. There exists simple between two vertices
3. There should not be any cut vertex (cut vertex is a vertex which if we
remove then the graph becomes disconnected)
Introduction to NP completeness problem
For obtaining solution to any problem there are two classes defined
Problem Solution
P class NP class
20
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.
V1 V3 V6
V2 V4 V5
21
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.
1 2 3
8 7 6
22