Graph in Data Structure

profansari 764 views 16 slides May 09, 2017
Slide 1
Slide 1 of 16
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16

About This Presentation

A graph G consists of a non empty set V called the set of nodes (points, vertices) of the graph, a set E, which is the set of edges of the graph and a mapping from the set of edges E to a pair of elements of V.
Any two nodes, which are connected by an edge in a graph are called "adjacent nodes&...


Slide Content

For More Visit: Https://www.ThesisScientist.com


Unit 6
Graph
Definition and Terminology
A graph G consists of a non empty set V called the set of nodes (points, vertices) of the graph,
a set E, which is the set of edges of the graph and a mapping from the set of edges E to a pair
of elements of V.
Any two nodes, which are connected by an edge in a graph are called "adjacent nodes".
In a graph G(V,E) an edge which is directed from one node to another is called a "directed edge",
while an edge which has no specific direction is called an "undirected edge". A graph in which
every edge is directed is called a "directed graph" or a "digraph". A graph in which every edge is
undirected is called an "undirected graph".
If some of edges are directed and some are undirected in a graph then the graph is called a "mixed
graph".
Any graph which contains some parallel edges is called a "multigraph".
If there is no more than one edge but a pair of nodes then, such a graph is called "simple graph."
A graph in which weights are assigned to every edge is called a "weighted graph".
In a graph, a node which is not adjacent to any other node is called "isolated node".
A graph containing only isolated nodes is called a "null graph". In a directed graph for any node v
the number of edges which have v as initial node is called the "outdegree" of the node v. The
number of edges to have v as their terminal node is called the "Indegree" of v and Sum of
outdegree and indegree of a node v is called its total degree.

For More Visit: Https://www.ThesisScientist.com


l
V
3
V
1l
l l
l
l
V
2
e
1
e
2
V
4
V
3
e
3
V
5
e
n
e
6
e
5
Simple
Out degree (V
1
) = 3
In degree (V
1
) = 0
l
l
l
l
Undirected
l
l
l
l
Directed
ll
l l
l
l
V
1
V
2
V
4
V
5
V
6
V
7
V
8l
V
8
is isolated node
V
1
V
2
V
3
V
4
e
4
l
l
ll
V
8
and V
4
have loops
V
1
V
2
V
3
V
7
l
l
ll
Weighted graph
l
l
l
V
6
V
5
V
4
l
l
ll
l
Null graph
4
3
5
9
7
8
l
V
3
V
1l
l l
l
l
l
l l
l
l
V
2
e
1
e
2
V
4
V
3
e
3
V
5
e
n
e
6
e
5
Simple
Out degree (V
1
) = 3
In degree (V
1
) = 0
l
l
l
l l
l
l
l
Undirected
l
l
l
l
Directed
ll
l l
l
l
V
1
V
2
V
4
V
5
V
6
V
7
V
8l
V
8
is isolated node
V
1
V
2
V
3
V
4
e
4
l
l
ll
V
8
and V
4
have loops
V
1
V
2
V
3
V
7
l
l
ll
Weighted graph
l
l
l
V
6
V
5
V
4
l
l
ll
l
Null graph
4
3
5
9
7
8


Figure 6.1

For More Visit: Https://www.ThesisScientist.com


Definition and Terminology
A GRAPH G, consists of two sets V and E. V is a finite non-empty set of vertices. E is a set of pairs of
vertices, these pairs are called edges. V(G) and E(G) will represent the sets of vertices and edges of graph
G. In an undirected graph the pair of vertices representing any edge is unordered. Thus, the pair (V1 , V2)
and (V2, V1) represent the same edge.
In a directed graph each edge is represented by a directed pair <V1, V2>, V1 is the tail and V2 the head of
edge. Thus <V2, V1> and <V1, V2> represent two different edges.

Figure 6.2 : Three Sample Graph
The graph G1 and G2 are undirected. G3 is a directed graph.
l V(G1) = {1, 2, 3, 4,} ; E(G1) = {(1, 2), (1, 3), (1, 4), (2, 3), (2, 4) (3, 4)}
l V(G2) = {1, 2, 3, 4, 5, 6, 7} ; E(G2) = {(1, 2), (1, 3), (2, 4), (2, 5), (3, 6), (3, 7)}
l V(G3) = {1, 2, 3}; E(G3) = {<1, 2>, <2, 1>, <2, 3>}
The graph G1 is also a tree while the graphs G1 and G3 are not. A graph may not have multiple occurrences
of the same edge, when this restriction is removed from a graph, the resulting data object is referred to as a
multigraph, which is not a graph.

Figure 6.3 : Multigraph
.

For More Visit: Https://www.ThesisScientist.com


Representation of Graphs
Three most commonly used representations are :
i) Adjacency matrices
ii) Adjacency lists
iii) Adjacency multilist
Adjacency Matrix
Let G = (V, E) be a graph with n vertices, n  1. The adjacency matrix of (G) is a 2-dimensional nxn array,
say A, with the property that A(ij) = 1 if the edge (Vi, Vj) for a directed graph G is in E(G). The adjacency
matrix for an undirected graph is symmetric as the edge (Vi, Vj) is in E(G) if the edge (Vj, Vi) is also in
E(G).
The adjacency matrix for a directed graph need not be symmetric.

Figure 6.4 : Adjacency Matrix Representation of Graph
A simple digraph which doesn't have any cycle is called "acyclic". Naturally such graphs cannot have any
loop.

Example 1
Let, V ={v1,v2,v3}
vxv ={(v1v2), (v1v3), (v2v1),(v2v3), (v3v1),(v3v2),(v1v1),(v2v2), (v3v3)}
Suppose we have graph with 3 vertices: Consider the following graph with
set V :
v1 v2 v3

For More Visit: Https://www.ThesisScientist.com


v1 0 1 0
A = v2 0 0 0 Direction from Row to Column
v3 1 1 1
This matrix is also called "Adjacency matrix"
0 1 1
1 0 1
1 1 1
For a given digraph G (V,E) an Adjacency Matrix depends upon the ordering of the elements of V. For
different orderings of the elements of V we get different adjacency matrices of the same graph G.
Path Matrix
We can extend the idea of matrix representation to multigraph and weighted graphs. For simple undirected
graphs such an extension simply gives symmetric adjacency matrix. In the case of multigraph or a weighted
graph we write aij=wij, where wij denotes the weight of the edge (Vi,Vj). If (Vi, Vj)E then we write
wij =0
For a null graph, adjacency matrix is a null matrix. If there are loops at each node but no other edges in the
graph then the adjacncy matrix is the identity or unit matrix.
V1 V2 V3 V4
V1 0 1 0 1
Let us denote the elements of A2 by aij2 then A[i] = V2 1 0 0 0
V3 1 1 0 1
V4 0 1 0 0
aij(2)= nk=1aik,akj
for any fixed k aik.akj=1, if both aik and akj equals 1 i.e., (ViVk) (VkVj) are the edges of the graph.
The matrices A2, A3 and A4 for the graph represented by the above matrix are:
V1 V2 V3 V4 V1 V2 V3 V4
V1 1 1 0 0 V1 1 1 0 1
A2 = V2 0 1 0 1 A3= V2 0 1 0 1
V3 1 2 0 1 V3 2 2 0 1
V4 0 0 0 0 V4 1 1 0 0

V1 V2 V3 V4
V1 1 2 0 1
A4 = V2 1 1 0 1

For More Visit: Https://www.ThesisScientist.com


V3 2 3 0 2
V4 1 1 0 0
((1) (2) (3).... can be considered as the length of path)
The matrix representing the paths of length less than or equal to n which exists from Vi to Vj is
Bn = A+A2+......An
if we write dij = Vnr=1(aik  bkj) V i, j = 1,2......n.
V = union of (OR)
 = And
For example :
d11=(a11 b11) V(a12 b21) V ........(a1n Vbn1)
If for any k, the kth element in the row for A and the kth element in the column for B are both 1 then dij=1;
otherwise dij =0. Let us write AA =A(2), AA(x-1)=A(x) for any r=2,3........ the entry in the i
th
row and jth
column of A(x) is 1 if there is at least one path of length r from Vi to Vj. The path matrix P is given by:
P=AVA(2)VA(3)V...VA(n)=Vnk=VnRs1A(k)
Warshall Algorithm
Given the adjacency matrix A , this matrix produces the path matrix P.
1. [Initialization]
PA
2 [Perform a pass]
Repeat three step A for k=1(1)n
3 [Process Rows]
Repeat step 4 for i=1(1) n.
4 [Across column]
Repeat for j=1(1)n
Pijpij V(Vik ^ Pkj)
5. [Exit]
Minimal Algorithm
Given the adjacency matrix B in which the zero elements are replaced by infinity or by some large number,
the matrix produced by the following algorithm shows the minimum length of paths between the nodes
MIN is a function that selects the algebraic minimum of its two arguments.
[1.] cB
[2.] Repeat thru step 4 for k =1(1) n

For More Visit: Https://www.ThesisScientist.com


[3.] Repeat thru step 4 forj =1(1) n
cij= MIN (cij, cik+ ckj)
[5.] exit

Example (Warshall)
Suppose we have 3 vertices v 1v2 v3
V1 V2 V3
Adjacency matrix A = V1 0 1 0
V2 0 0 0 = p
V3 1 1 1
V
1
l
V
3
V
2
l l
V
1
l
V
3
V
2
l l


I Iteration
 R=1, i=1, j=1,2,3 for p(1)
P11=P11V(P11 P11) =0V(00)=0
P12=P12V(P11 P12) =1V(01)=1
P13=P13V(P11 P13) =0V(00)=0
 k=1, i=2, j=1,2,3
P21=P21V(P21^P22)=0
P22=P22V(P21^ P22)=0
P23 =0
 k=1, i=3, j=1,2,3
P31=P31 V (P31^P31)=1
P32=P32 V (P31^P32)=1
p33=P33 V (P31 ^ P33) = 1
Hence,

For More Visit: Https://www.ThesisScientist.com


0 1 0
Pv = 0 0 0
1 1 1
II Insertion
for k=2 for p(2)
i=1
j=1,2,3
P11=P11V(P12 ^ P21)=0
P12=P12V(P12 ^ P22)=0
P13=P13V(P12 ^ P23)=
. .
. .
. .
Similarly, we can calculate Matrix for
k=3 and so on


Adjacency List
In this representation the n rows of the adjacency matrix are represented as n linked lists. There is one list
for each vertex in G. The nodes in list i represent the vertices that are adjacent from vertex i.
Each node has two fields, VERTEX and LINK. The vertex field contains the indices of the vertices
adjacent to vertex i.
Each node has a head node. Head nodes are sequential providing easy random access to the adjacency list
for any particular vertex.

For More Visit: Https://www.ThesisScientist.com



Figure 6.5
Adjancency Multi-list
In the adjacency list representation of an undirect graph each edge (Vi, Vj) is represented by two
entries, one on the list for Vi and the other on the list for Vj. In some situations it is necessary to be
able to determine the second entry for a particular edge and mark that edge as already having been
examined. This can be accomplished easily if the adjacency lists are actually maintained as
multilist (i.e., lists in which nodes may be shared among several lists).
For each edge there will be exactly one node, but this node will be in two lists, i.e., the adjacency
list for each of the two nodes it is incident to. The node structure will be:



where M is a one bit mark field that may be used to indicate whether or not the edge has been
examined.
e.g., Graph
M

V1 V2
LINK 1
For V1
LINK 2
For V2

For More Visit: Https://www.ThesisScientist.com



The lists are :
Vertex 1 : N1  N2  N3
Vertex 2 : N1  N4  N5
Vertex 3 : N2  N4  N6
Vertex 4 : N3  N5  N6
Figure 6.7 : Adjacency Multilist for the given Graph
Traversal of a Graph
l Depth first search
l Breadth first search
Depth First Search
DFS algorithm is analogous to preorder traversal of a tree. Given an undirected graph g= (V,E) and a vertex
V in V(G), we are interested in visiting all vertices in G that are reachable from (i.e. all vertices connected
to V).

For More Visit: Https://www.ThesisScientist.com


DFS of an undirected graph precedes as follows:
The start vertex C is visited Next an unvisited vertex w adjacent to V is selected and a depth first search
initiated. (Adjacent means connected by an Arc from the search node.)
Algorithm DFS (V)
1. Given an adjacent graph G=(V,E) with n vertices an array VISITED [N] initially set to zero
this algorithm visits all vertices reachable from V and visited are global.
VISITED (V) 1
2. Check for adjacent vertex W to V and call recursively
for each vertex W adjacent to V do
if (visited (W)=0) then call DFS(W)
end
3. [exit]

Breadh First Search
Starting at vertex V and making is as visited BFS differs from DFS in that all unvisited vertices
adjacent to V are visited next. Then unvisited vertices adjacent to these vertices are visited and so
on.
Algorithm BFS (V)
1. [A BFS of G is carried out beginning at vertex V. All vertices visited are marked as
VISTED(I)=1. The graph G and array VISITED are global and VISITED is initialized to 0]
VISITED (V) 1
initialize Q to be empty [Q is queue]
2. [Perform Iterations]
loop
for all vertices W adjacent to V do
if VISITED (W) =0
then [Add (W,Q); VISITED (W) 1]
end
if Q is empty then return;
DELETE (V,Q)
forever

For More Visit: Https://www.ThesisScientist.com


<exit>
GRAPH G V
1
V
2
V
3
V
4
V
7
V
8
V
5
V
6
V
1
V
1
V
2
V
2
V
3
V
3
V
4
V
4
V
7
V
7
V
8
V
8
V
5
V
5
V
6
V
6

Traversing by DFS : V1, V2 , V4, V8, V5, V3, V6, V7
Traversing by BFS :V1, V2, V3, V4, V5, V6, V7, V8
Analysis of Algorithms
1. DFS
If G is represented by its adjacency matrix, then the time to determine all vertices adjacent to V is
O(N). Since at most n vertices are visited, the total time is O(N
2
).
2. BFS
Each vertex visited gets into the queue exactly once, so the loop.....forever is iterated at most n
times.
If an adjacency matrix is used then the for loop takes O(N) times for each vertex visited.
The total time is therefore O(N
2
).
Weighted Graphs
A graph in which weights are assigned to every edge is called a Weighted graph.
Shortest Path in Weighted Graphs
Let G= (v,e,w) be a weighted graph where w is weight (in terms of time or cost) assigned to that
edge.
The length of a path in G is defined as the sum of lengths of the edges in the path.
Dijkstra's Shortest Path Algorithm

For More Visit: Https://www.ThesisScientist.com


Let TV with AT (A= starting node). Let P be the subset of vertices V-T, let L(T) denote the
length of a shortest path among all paths from A to T that does not include any other vertex in T.
We call L(T) the index of T with respect to P.
Let X be a vertex in T. Let P' be PU{X} and T' be T-{X}. Let L'(T) denote the index of a
vertex T inT' with respect to P'. We claim that
L'(T) =Min[L(T), L(X), L(W,T)]-----------(i)
Algorithm
1. Initially let P= [a] and T =V-[a] , for every vertex t in t, let l(t)= w (A,t).
2. Select the vertex in T that has the smallest index with respect to P, let it denote it vertex x.
3. If x is the vertex we wish to reach from A stop. If not let P'=PU{x} and T=T-{x}. For
every vertex t in T compute its index with respect to P' according to 1.
4. Repeat steps 2 and 3 using P' as P and T' as T.
5. Stop.
Example
Find the shortest path from A to Z for the given graph:
1. B D
C E
A Z
1
2
4
7
5
1
2
3
6
BB DD
CC EE
AA ZZ
1
2
4
7
5
1
2
3
6

Solution:
Initially P={A} and
T={B,C,E,D,Z}
The lengths of different vertices (with respect to P) are :
L(B)=1 , L(C)=4, L(D)=L(E)= L(Z)= 
l so L(B)= 1 is the shortest value
Now let P={A,B} and T={C,D,E,Z}

For More Visit: Https://www.ThesisScientist.com


so, L(C)=3, L(D)=8, L(E)=6, L(Z)= 
lSo L(C) =3, is the shortest value.
Now P={A,B, C} and T'={D,E,Z}
L'(D)= Min{8,}=8. L'(E)= Min{6,3+1}=4
L'(Z) = Min {,3+}= 
Successive observations are shown in following figures:








Thus the shortest path from A to Z is (A, B, C, E, D, Z) of length 9. B D
C E
A Z
DD
ZZ

2.

4 (a)
B D
C E
A Z
DD
ZZ

2.

4 (a)

For More Visit: Https://www.ThesisScientist.com


Spanning Trees
It is a tree obtained from a Graph, which covers all its vertices.
Minimal Spanning Tree
It is a tree from the set of spanning trees, which has minimum weight.
There are two methods to find minimum spanning tree.
l Prim's Method
lKruskal's method

1. Prim's Method
Draw n isolated vertices and label them as V1,V2.....Vn. Tabulate the given weights of the
edges of G in an nxn table. Set the weights of the non-existing edges as very large ()
l
l
ll
l
l
17
8
11
V
4
10
V
5
16
V
1
V
2
V
6
V
3
9
7
7
19.5
9.512
l
l
ll
l
l
17
8
11
V
4
10
V
5
16
V
1
V
2
V
6
V
3
9
7
7
19.5
9.512

V V2 V3 V4 V5 V6

V1 - 10 16 11 10 17
V2 10 - 9.5   19.5
V3 16 9.5 - 7  12
V4 11  7 - 8 7
V5 10   8 - 9
V6 17 19.5 12 7 9 -

For More Visit: Https://www.ThesisScientist.com



Start from vertex V1 and connect it to its nearest neighbor say Vk. Now consider V1 and Vk as one subgraph
and connect this subgraph to its closest neighbor (i.e. to a vertex other than V1 and Vk that has the smallest
entry among all entries in rows 1 and k.)
2. Kruskal's Method
In this approach Minimum cost spanning tree T is built edge by edge. Edges are considered for
inclusion in T in ascending order. An edge is included in T if it doesn't form a cycle with edges
already in T. Since G is connected and has n>0 vertices exactly (n-1) edges will be selected for
inclusion in T. 1
5
6
2
4
3
16
21
11
3314
18
19 6
5
10
11
55
66
22
44
33
16
21
11
3314
18
19 6
5
10

Edge Cost Action Resulting Tree
.... ...... ......... .......................
(2,3) 5 Accept
(2,4) 6 Accept



(3,4) 10 Reject "
(2,6) 11 Accept


(4,6) 14 Reject "
(1,2) 16 Accept


(4,5) 18 Accept



1 2 3 4 5 6
1 3 4 5 62
1 3 5 62
4
11 22 33 44 55 66
11 33 44 55 6622
11 33 55 6622
44
1 5
43 6
211 55
4433 66
22
5
43 6
21 55
4433 66
2211
43 6
21
5
4433 66
2211
55