Graph in Data Structure

Prof Ansari
Prof AnsariProf Ansari

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
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
V3
V1l
l l
l
l
V2
e1
e2
V4
V3
e3
V5
en
e6
e5
Simple
Out degree (V1) = 3
In degree (V1) = 0
l
l
l
l
Undirected
l l
l
l
Directed
ll
l l
l
l
V1
V2 V4
V5 V6 V7
V8l
V8 is isolated node
V1
V2
V3
V4
e4
l
l
ll
V8 and V4 have loops
V1
V2
V3
V7
l
l
ll
Weighted graph
l
l
l
V6
V5
V4
l
l
ll
l
Null graph
4
3
5
9
7
8
l
V3
V1l
l l
l
l
l
l l
l
l
V2
e1
e2
V4
V3
e3
V5
en
e6
e5
Simple
Out degree (V1) = 3
In degree (V1) = 0
l
l
l
l l
l
l
l
Undirected
l l
l
l
Directed
ll
l l
l
l
V1
V2 V4
V5 V6 V7
V8l
V8 is isolated node
V1
V2
V3
V4
e4
l
l
ll
V8 and V4 have loops
V1
V2
V3
V7
l
l
ll
Weighted graph
l
l
l
V6
V5
V4
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 ith
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
V1 l
V3 V2
l l
V1 l
V3 V2
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
V1
V2 V3
V4 V7
V8
V5 V6
V1V1
V2V2 V3V3
V4V4 V7V7
V8V8
V5V5 V6V6
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(N2
).
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(N2
).
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
V4
10V5
16
V1
V2
V6
V3
9
7
7
19.5
9.512
l
l
ll
l
l
17
8
11
V4
10V5
16
V1
V2
V6
V3
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
33 14
18
19 6
5
10
11
55
66
22
44
33
16
21 11
33 14
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

Recomendados

Binary Tree Traversal por
Binary Tree TraversalBinary Tree Traversal
Binary Tree TraversalDhrumil Panchal
8.5K vistas18 diapositivas
Data Structures- Part5 recursion por
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursionAbdullah Al-hazmy
9.4K vistas28 diapositivas
Linear search algorithm por
Linear search algorithmLinear search algorithm
Linear search algorithmNeoClassical
25.9K vistas8 diapositivas
Sparse matrix and its representation data structure por
Sparse matrix and its representation data structureSparse matrix and its representation data structure
Sparse matrix and its representation data structureVardhil Patel
7.5K vistas9 diapositivas
linear search and binary search por
linear search and binary searchlinear search and binary search
linear search and binary searchZia Ush Shamszaman
4K vistas24 diapositivas
Queues in C++ por
Queues in C++Queues in C++
Queues in C++Vineeta Garg
5.2K vistas24 diapositivas

Más contenido relacionado

La actualidad más candente

Sparse matrix por
Sparse matrixSparse matrix
Sparse matrixdincyjain
564 vistas10 diapositivas
linked list in data structure por
linked list in data structure linked list in data structure
linked list in data structure shameen khan
39.3K vistas63 diapositivas
Queue in Data Structure por
Queue in Data Structure Queue in Data Structure
Queue in Data Structure Janki Shah
13.6K vistas32 diapositivas
Introduction to stack por
Introduction to stackIntroduction to stack
Introduction to stackvaibhav2910
2.5K vistas10 diapositivas
Linear and Binary search por
Linear and Binary searchLinear and Binary search
Linear and Binary searchNisha Soms
507 vistas31 diapositivas
Queue data structure por
Queue data structureQueue data structure
Queue data structureanooppjoseph
47K vistas12 diapositivas

La actualidad más candente(20)

Sparse matrix por dincyjain
Sparse matrixSparse matrix
Sparse matrix
dincyjain564 vistas
linked list in data structure por shameen khan
linked list in data structure linked list in data structure
linked list in data structure
shameen khan39.3K vistas
Queue in Data Structure por Janki Shah
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
Janki Shah13.6K vistas
Introduction to stack por vaibhav2910
Introduction to stackIntroduction to stack
Introduction to stack
vaibhav29102.5K vistas
Linear and Binary search por Nisha Soms
Linear and Binary searchLinear and Binary search
Linear and Binary search
Nisha Soms507 vistas
Queue data structure por anooppjoseph
Queue data structureQueue data structure
Queue data structure
anooppjoseph47K vistas
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang... por Umesh Kumar
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
Umesh Kumar3.5K vistas
Binary search tree operations por Kamran Zafar
Binary search tree operationsBinary search tree operations
Binary search tree operations
Kamran Zafar409 vistas
Binary tree por Rajendran
Binary tree Binary tree
Binary tree
Rajendran 1.8K vistas
Binary Search - Design & Analysis of Algorithms por Drishti Bhalla
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla14.6K vistas
Terminology of tree por RacksaviR
Terminology of treeTerminology of tree
Terminology of tree
RacksaviR1.6K vistas
File organization por Ganesh Pawar
File organizationFile organization
File organization
Ganesh Pawar9.3K vistas

Similar a Graph in Data Structure

Chap 6 Graph.ppt por
Chap 6 Graph.pptChap 6 Graph.ppt
Chap 6 Graph.pptshashankbhadouria4
41 vistas115 diapositivas
Graphs por
GraphsGraphs
Graphsamudha arul
178 vistas45 diapositivas
Graph representation por
Graph representationGraph representation
Graph representationDEEPIKA T
45 vistas23 diapositivas
Fallsem2015 16 cp4194-13-oct-2015_rm01_graphs por
Fallsem2015 16 cp4194-13-oct-2015_rm01_graphsFallsem2015 16 cp4194-13-oct-2015_rm01_graphs
Fallsem2015 16 cp4194-13-oct-2015_rm01_graphsSnehilKeshari
374 vistas104 diapositivas
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf por
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdfClass01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdfChristianKapsales1
4 vistas45 diapositivas
Grpahs in Data Structure por
Grpahs in Data StructureGrpahs in Data Structure
Grpahs in Data StructureAvichalVishnoi
34 vistas22 diapositivas

Similar a Graph in Data Structure(20)

Graph representation por DEEPIKA T
Graph representationGraph representation
Graph representation
DEEPIKA T45 vistas
Fallsem2015 16 cp4194-13-oct-2015_rm01_graphs por SnehilKeshari
Fallsem2015 16 cp4194-13-oct-2015_rm01_graphsFallsem2015 16 cp4194-13-oct-2015_rm01_graphs
Fallsem2015 16 cp4194-13-oct-2015_rm01_graphs
SnehilKeshari374 vistas
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf por ChristianKapsales1
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdfClass01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Lecture 5b graphs and hashing por Victor Palmar
Lecture 5b graphs and hashingLecture 5b graphs and hashing
Lecture 5b graphs and hashing
Victor Palmar1.2K vistas
Dsa.PPT por boltkat
Dsa.PPTDsa.PPT
Dsa.PPT
boltkat12 vistas
Discrete maths assignment por Keshav Somani
Discrete maths assignmentDiscrete maths assignment
Discrete maths assignment
Keshav Somani576 vistas
Matrix representation of graph por Rounak Biswas
Matrix representation of graphMatrix representation of graph
Matrix representation of graph
Rounak Biswas3.2K vistas
graph.pptx por hijigaf
graph.pptxgraph.pptx
graph.pptx
hijigaf16 vistas
Graph theory concepts complex networks presents-rouhollah nabati por nabati
Graph theory concepts   complex networks presents-rouhollah nabatiGraph theory concepts   complex networks presents-rouhollah nabati
Graph theory concepts complex networks presents-rouhollah nabati
nabati2.8K vistas
Unit 3 graph chapter6 por DrkhanchanaR
Unit 3  graph chapter6Unit 3  graph chapter6
Unit 3 graph chapter6
DrkhanchanaR935 vistas

Más de Prof Ansari

Sci Hub New Domain por
Sci Hub New DomainSci Hub New Domain
Sci Hub New DomainProf Ansari
4.1K vistas21 diapositivas
Sci Hub cc Not Working por
Sci Hub cc Not WorkingSci Hub cc Not Working
Sci Hub cc Not WorkingProf Ansari
1.4K vistas20 diapositivas
basics of computer network por
basics of computer networkbasics of computer network
basics of computer networkProf Ansari
3.9K vistas29 diapositivas
JAVA INTRODUCTION por
JAVA INTRODUCTIONJAVA INTRODUCTION
JAVA INTRODUCTIONProf Ansari
506 vistas8 diapositivas
Project Evaluation and Estimation in Software Development por
Project Evaluation and Estimation in Software DevelopmentProject Evaluation and Estimation in Software Development
Project Evaluation and Estimation in Software DevelopmentProf Ansari
5.1K vistas9 diapositivas
Stepwise Project planning in software development por
Stepwise Project planning in software developmentStepwise Project planning in software development
Stepwise Project planning in software developmentProf Ansari
9.6K vistas28 diapositivas

Más de Prof Ansari(20)

Sci Hub New Domain por Prof Ansari
Sci Hub New DomainSci Hub New Domain
Sci Hub New Domain
Prof Ansari4.1K vistas
Sci Hub cc Not Working por Prof Ansari
Sci Hub cc Not WorkingSci Hub cc Not Working
Sci Hub cc Not Working
Prof Ansari1.4K vistas
basics of computer network por Prof Ansari
basics of computer networkbasics of computer network
basics of computer network
Prof Ansari3.9K vistas
JAVA INTRODUCTION por Prof Ansari
JAVA INTRODUCTIONJAVA INTRODUCTION
JAVA INTRODUCTION
Prof Ansari506 vistas
Project Evaluation and Estimation in Software Development por Prof Ansari
Project Evaluation and Estimation in Software DevelopmentProject Evaluation and Estimation in Software Development
Project Evaluation and Estimation in Software Development
Prof Ansari5.1K vistas
Stepwise Project planning in software development por Prof Ansari
Stepwise Project planning in software developmentStepwise Project planning in software development
Stepwise Project planning in software development
Prof Ansari9.6K vistas
Database and Math Relations por Prof Ansari
Database and Math RelationsDatabase and Math Relations
Database and Math Relations
Prof Ansari686 vistas
Normalisation in Database management System (DBMS) por Prof Ansari
Normalisation in Database management System (DBMS)Normalisation in Database management System (DBMS)
Normalisation in Database management System (DBMS)
Prof Ansari536 vistas
Entity-Relationship Data Model in DBMS por Prof Ansari
Entity-Relationship Data Model in DBMSEntity-Relationship Data Model in DBMS
Entity-Relationship Data Model in DBMS
Prof Ansari1.4K vistas
A Detail Database Architecture por Prof Ansari
A Detail Database ArchitectureA Detail Database Architecture
A Detail Database Architecture
Prof Ansari1K vistas
INTRODUCTION TO Database Management System (DBMS) por Prof Ansari
INTRODUCTION TO Database Management System (DBMS)INTRODUCTION TO Database Management System (DBMS)
INTRODUCTION TO Database Management System (DBMS)
Prof Ansari576 vistas
Master thesis on Vehicular Ad hoc Networks (VANET) por Prof Ansari
Master thesis on Vehicular Ad hoc Networks (VANET)Master thesis on Vehicular Ad hoc Networks (VANET)
Master thesis on Vehicular Ad hoc Networks (VANET)
Prof Ansari1.6K vistas
Master Thesis on Vehicular Ad-hoc Network (VANET) por Prof Ansari
Master Thesis on Vehicular Ad-hoc Network (VANET)Master Thesis on Vehicular Ad-hoc Network (VANET)
Master Thesis on Vehicular Ad-hoc Network (VANET)
Prof Ansari2.4K vistas
INTERFACING WITH INTEL 8251A (USART) por Prof Ansari
INTERFACING WITH INTEL 8251A (USART)INTERFACING WITH INTEL 8251A (USART)
INTERFACING WITH INTEL 8251A (USART)
Prof Ansari582 vistas
HOST AND NETWORK SECURITY by ThesisScientist.com por Prof Ansari
HOST AND NETWORK SECURITY by ThesisScientist.comHOST AND NETWORK SECURITY by ThesisScientist.com
HOST AND NETWORK SECURITY by ThesisScientist.com
Prof Ansari2.5K vistas
SYSTEM NETWORK ADMINISTRATIONS GOALS and TIPS por Prof Ansari
SYSTEM NETWORK ADMINISTRATIONS GOALS and TIPSSYSTEM NETWORK ADMINISTRATIONS GOALS and TIPS
SYSTEM NETWORK ADMINISTRATIONS GOALS and TIPS
Prof Ansari2.3K vistas
INTRODUCTION TO VISUAL BASICS por Prof Ansari
INTRODUCTION TO VISUAL BASICS INTRODUCTION TO VISUAL BASICS
INTRODUCTION TO VISUAL BASICS
Prof Ansari146 vistas
introduction to Blogging ppt por Prof Ansari
introduction to Blogging pptintroduction to Blogging ppt
introduction to Blogging ppt
Prof Ansari469 vistas
INTRODUCTION TO SOFTWARE ENGINEERING por Prof Ansari
INTRODUCTION TO SOFTWARE ENGINEERINGINTRODUCTION TO SOFTWARE ENGINEERING
INTRODUCTION TO SOFTWARE ENGINEERING
Prof Ansari1.9K vistas
Introduction to E-commerce por Prof Ansari
Introduction to E-commerceIntroduction to E-commerce
Introduction to E-commerce
Prof Ansari974 vistas

Último

Pitchbook Repowerlab.pdf por
Pitchbook Repowerlab.pdfPitchbook Repowerlab.pdf
Pitchbook Repowerlab.pdfVictoriaGaleano
9 vistas12 diapositivas
Robotics in construction enterprise por
Robotics in construction enterpriseRobotics in construction enterprise
Robotics in construction enterpriseKhalid Abdel Naser Abdel Rahim
5 vistas1 diapositiva
sam_software_eng_cv.pdf por
sam_software_eng_cv.pdfsam_software_eng_cv.pdf
sam_software_eng_cv.pdfsammyigbinovia
19 vistas5 diapositivas
Trust Metric-Based Anomaly Detection via Deep Deterministic Policy Gradient R... por
Trust Metric-Based Anomaly Detection via Deep Deterministic Policy Gradient R...Trust Metric-Based Anomaly Detection via Deep Deterministic Policy Gradient R...
Trust Metric-Based Anomaly Detection via Deep Deterministic Policy Gradient R...IJCNCJournal
5 vistas25 diapositivas
BCIC - Manufacturing Conclave - Technology-Driven Manufacturing for Growth por
BCIC - Manufacturing Conclave -  Technology-Driven Manufacturing for GrowthBCIC - Manufacturing Conclave -  Technology-Driven Manufacturing for Growth
BCIC - Manufacturing Conclave - Technology-Driven Manufacturing for GrowthInnomantra
22 vistas4 diapositivas
2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptx por
2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptx2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptx
2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptxlwang78
314 vistas19 diapositivas

Último(20)

Trust Metric-Based Anomaly Detection via Deep Deterministic Policy Gradient R... por IJCNCJournal
Trust Metric-Based Anomaly Detection via Deep Deterministic Policy Gradient R...Trust Metric-Based Anomaly Detection via Deep Deterministic Policy Gradient R...
Trust Metric-Based Anomaly Detection via Deep Deterministic Policy Gradient R...
IJCNCJournal5 vistas
BCIC - Manufacturing Conclave - Technology-Driven Manufacturing for Growth por Innomantra
BCIC - Manufacturing Conclave -  Technology-Driven Manufacturing for GrowthBCIC - Manufacturing Conclave -  Technology-Driven Manufacturing for Growth
BCIC - Manufacturing Conclave - Technology-Driven Manufacturing for Growth
Innomantra 22 vistas
2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptx por lwang78
2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptx2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptx
2023Dec ASU Wang NETR Group Research Focus and Facility Overview.pptx
lwang78314 vistas
Design of Structures and Foundations for Vibrating Machines, Arya-ONeill-Pinc... por csegroupvn
Design of Structures and Foundations for Vibrating Machines, Arya-ONeill-Pinc...Design of Structures and Foundations for Vibrating Machines, Arya-ONeill-Pinc...
Design of Structures and Foundations for Vibrating Machines, Arya-ONeill-Pinc...
csegroupvn16 vistas
Unlocking Research Visibility.pdf por KhatirNaima
Unlocking Research Visibility.pdfUnlocking Research Visibility.pdf
Unlocking Research Visibility.pdf
KhatirNaima11 vistas
IRJET-Productivity Enhancement Using Method Study.pdf por SahilBavdhankar
IRJET-Productivity Enhancement Using Method Study.pdfIRJET-Productivity Enhancement Using Method Study.pdf
IRJET-Productivity Enhancement Using Method Study.pdf
SahilBavdhankar10 vistas
Integrating Sustainable Development Goals (SDGs) in School Education por SheetalTank1
Integrating Sustainable Development Goals (SDGs) in School EducationIntegrating Sustainable Development Goals (SDGs) in School Education
Integrating Sustainable Development Goals (SDGs) in School Education
SheetalTank113 vistas
AWS Certified Solutions Architect Associate Exam Guide_published .pdf por Kiran Kumar Malik
AWS Certified Solutions Architect Associate Exam Guide_published .pdfAWS Certified Solutions Architect Associate Exam Guide_published .pdf
AWS Certified Solutions Architect Associate Exam Guide_published .pdf
ASSIGNMENTS ON FUZZY LOGIC IN TRAFFIC FLOW.pdf por AlhamduKure
ASSIGNMENTS ON FUZZY LOGIC IN TRAFFIC FLOW.pdfASSIGNMENTS ON FUZZY LOGIC IN TRAFFIC FLOW.pdf
ASSIGNMENTS ON FUZZY LOGIC IN TRAFFIC FLOW.pdf
AlhamduKure10 vistas
MongoDB.pdf por ArthyR3
MongoDB.pdfMongoDB.pdf
MongoDB.pdf
ArthyR351 vistas
REACTJS.pdf por ArthyR3
REACTJS.pdfREACTJS.pdf
REACTJS.pdf
ArthyR339 vistas
Design_Discover_Develop_Campaign.pptx por ShivanshSeth6
Design_Discover_Develop_Campaign.pptxDesign_Discover_Develop_Campaign.pptx
Design_Discover_Develop_Campaign.pptx
ShivanshSeth656 vistas
Ansari: Practical experiences with an LLM-based Islamic Assistant por M Waleed Kadous
Ansari: Practical experiences with an LLM-based Islamic AssistantAnsari: Practical experiences with an LLM-based Islamic Assistant
Ansari: Practical experiences with an LLM-based Islamic Assistant
M Waleed Kadous12 vistas

Graph in Data Structure

  • 1. 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.
  • 2. For More Visit: Https://www.ThesisScientist.com l V3 V1l l l l l V2 e1 e2 V4 V3 e3 V5 en e6 e5 Simple Out degree (V1) = 3 In degree (V1) = 0 l l l l Undirected l l l l Directed ll l l l l V1 V2 V4 V5 V6 V7 V8l V8 is isolated node V1 V2 V3 V4 e4 l l ll V8 and V4 have loops V1 V2 V3 V7 l l ll Weighted graph l l l V6 V5 V4 l l ll l Null graph 4 3 5 9 7 8 l V3 V1l l l l l l l l l l V2 e1 e2 V4 V3 e3 V5 en e6 e5 Simple Out degree (V1) = 3 In degree (V1) = 0 l l l l l l l l Undirected l l l l Directed ll l l l l V1 V2 V4 V5 V6 V7 V8l V8 is isolated node V1 V2 V3 V4 e4 l l ll V8 and V4 have loops V1 V2 V3 V7 l l ll Weighted graph l l l V6 V5 V4 l l ll l Null graph 4 3 5 9 7 8 Figure 6.1
  • 3. 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 .
  • 4. 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
  • 5. 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
  • 6. 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 ith 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
  • 7. 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 V1 l V3 V2 l l V1 l V3 V2 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,
  • 8. 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.
  • 9. 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
  • 10. 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).
  • 11. 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
  • 12. For More Visit: Https://www.ThesisScientist.com <exit> GRAPH G V1 V2 V3 V4 V7 V8 V5 V6 V1V1 V2V2 V3V3 V4V4 V7V7 V8V8 V5V5 V6V6 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(N2 ). 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(N2 ). 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
  • 13. 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}
  • 14. 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)
  • 15. 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 V4 10V5 16 V1 V2 V6 V3 9 7 7 19.5 9.512 l l ll l l 17 8 11 V4 10V5 16 V1 V2 V6 V3 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 -
  • 16. 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 33 14 18 19 6 5 10 11 55 66 22 44 33 16 21 11 33 14 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