0% found this document useful (0 votes)
25 views

Aph Theory

1) A graph is a non-linear data structure consisting of nodes and edges connecting the nodes. It can be used to model real-world connections. 2) Key elements of a graph include vertices, edges, paths, degrees of vertices, and whether a graph is directed or undirected. 3) Graphs can be represented through adjacency matrices where 1s indicate edges and 0s indicate no edge. Properties of these matrices relate to the graph structure.

Uploaded by

pavan sai teja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Aph Theory

1) A graph is a non-linear data structure consisting of nodes and edges connecting the nodes. It can be used to model real-world connections. 2) Key elements of a graph include vertices, edges, paths, degrees of vertices, and whether a graph is directed or undirected. 3) Graphs can be represented through adjacency matrices where 1s indicate edges and 0s indicate no edge. Properties of these matrices relate to the graph structure.

Uploaded by

pavan sai teja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 46

GRAPH

INTRODUCTION

• A data structure that establishes a relationship


from many parents to many children is graph.
• In real life its used in number of situations like
modelling the interonnection between various
cities,airmap modelling the interconnection
between various countries,
• routing messages over a computer network from
one node to another,flow chart of a program.
Defination:
• A graph is a non-linear data structure that consists of a
collection of nodes(or vertices) and a collection of edges,with
each edge joining one node to another.Edges are sometimes
referred to as arcs.Formally a graph G=(V,E) consist of two sets
(a) V called set of vertices or nodes.
(b) E called set of all edges or arcs.This set E is the set of pair of
elements from V.If there is an edge connecting nodes v1 and
v2 then it is represented as(v1,v2) where v1,v2 ɛV
• In graph,vertices ae represented by circles or points and edges
as line segments or arcs connecting the vertices.
www.csemcq.com
The edge in a graph can be directed or undirected
depending on whether the direction of the edge is
specified or not.
A graph in which each edge is directed is called a
directed graph or digraph.
A graph in which each edge is undirected is called
undirected graph.
DIRECTED GRAPH UNDIRECTED GRAPH
GRAPH TERMINOLOGY

1.ADJACENT VERTEX:
Two vertices in a graph are said to be adjacent if
there exsists an edge between them.if a graph
contains an edge(v1,v2) then vertex v2 is said to be
adjancent to vertex v1.
2.PATH:
• A path is a sequence of vertices traversed by
following the edges between them.A path is simple if
it has no repeating vertices
• One special type of simple path is called cycle. A cycle
is a simple path consisting of sequence of vertices
such that the starting and ending vertex are same.
• A loop is special case of cycle in which an edge begins
and ends with the same vertex.
• If a graph doesnot contain any cycle then it is called
an acyclic graph.
ACYCLIC GRAPH CYCLIC GRAPH
3.DEGREE OF VERTEX:
• In an undirected graph ,the degree of vertex is the number of
edges originating from it.
• In other words,the number of edges connected with vertex vi
is called the degree of vertex vi. It is denoted by degree (vi )
• For directed graph this term is used as:
• Indegree : is the number of edges entering the vertex.The
indegree of vertex is denoted by indegree(vi )
• Outdegree : is the number of edges leaving the vertex. And
represented by outdegree (vi )
• A vertex is pendent if its indegree is 1 and its outdegree is 0
4.COMPLETE GRAPH:
• A graph is said to be complete if there are edges
from any vertex to all other vertices.
• In such type of graph ,each vertex is adjacent to
every other vertex.
5.CONNECTED GRAPH:
• A undirected graph is said to be connected if every
vertex is reachable from others by following some
path.
• In other words an undirected graph is said to be
connected if each pair of distinct vertices (vi ,vj ) ɛ V
has a path between them.
• If this property holds true for directed graph then it is
called strongly connected graph i.e. a digraph is said
to be strongly connected if for every pair of distinct
vertices (vi ,vj ) there exsist a path from vi to vj that
connect two nodes.
UNCONNECTED GRAPH CONNECTED GRAPH
6.WEIGHTED GRAPH:
• A graph is termed as weighted graph if all the edges
in it are labelled with some weights.
• A weight is the measure of the cost of using an
edge to go from one vertex to another.
• In an unweighted graph where the weight is not
mentioned explicitly ,the cost of traversing an edge
is same for all the edges.
7.MULTIGRAPH:
• It may be possible that there are two or more edges
connecting the same vertices of graph.Such a graph
is called multigraph.
REPRESENTATION OF GRAPHS
• Representation of a graph-There are two main ways
of representing a graph in memory. These are:
• 1)Sequential
• 2)Linked List
1.SEQUENTIAL/MATRIX
REPRESENTATION
• This representation can be used for both directed
and undirected graph.
• The graphs can be represented as matrices in
sequential representation.In this representation a
square matrix of order nxn is used where n is
number of vertices in graph.
• There are two most common matrices. These are:
• Adjacency Matrix
• Incidence Matrix
• The adjacency matrix is a sequence matrix with one
row and one column devoted to each vertex.
• The values of the matrix are 0 or 1.
• A value of 1 for row i and column j implies that
edge eij exists between vi and vj vertices.
• A value of 0 implies that there is no edge between
the vertex vi and vj.
• Thus, for a graph with v1,v2,v3………..vn vertices, the
adjacency matrix A=[aij] of the graph G is the n x n
matrix and can be defined as:
• 1 if vi is adjacent to vj (if there is an edge between v i and vj)

• aij =

• 0 if there is no edge between vi and vj

• Such a matrix that contains entries of only 0 or 1 is called


a bit matrix or Boolean matrix.
• The adjacency matrix of the graph G does depend on the
ordering of the nodes in G that is different ordering of the
nodes may result in a different adjacency matrix.
• An adjacency matrix representing a directed graph
with n vertices requires memory space of O(nxn)
• Suppose G is an undirected graph. Then the
adjacency matrix A of G will be a symmetric matrix
i.e one in which aij=aji for every i and j.
• So in some applications we need to store entries of
only the upper or lower triangular portion of the
adjacency matrix ,thereby reducing the memory
space needed almost half i.e. O(n2 /2)
Vertices taken 1-6
Adjacency Matrix of Directed and
Undirected Graph
• The adjacency matrix can also be used to represent
weighted graph,such matrix is known as weighted
adjacency matrix.
• In this if an edge with the given weight exist
between two vertices then we store that weight as
the entry in the weighted adjacency matrix instead
of 1.
• However if there doesnot exist an edge between
two vertices then it is represented by 0 or ∞
PROPERTIES OF ADJANCENCY MATRIX
• PROPERTY 1: In the AM of an undirected graph,the
degree of a vertex is the sum of the entries in a row
or a column corresnponding to it.
• PROPERTY 2: In the AM of the undirected
graph ,the sum of all entries of the matrix is twice
the number of edges in the graph.
• PROPERTY 3: In AM of directed graph,the
outdegree of a vertex is equal to the sum of the
entries of the row corresponding to it and the
indegree is sum of entries in column
• PROPERTY 4:In AM of directed graph ,the sum of all
entries of the adjacency matrix is equal to number
of edges in graph.
• PROPERTY 5: Suppose A be an adjacency matrix of a
directed graph with n vertices
• An=[aij n].
• It gives the number of path of length n from vertex
Vi to vertex vj
Consider an Adjacency matrix A representing a graph.
Then A2, A3……..Ak of Adjacency matrix A represent
the matrices with path lengths 2,3……… k
respectively. In other words, if
ak(i,j)= the ijth entry of matrix Ak
then this entry represents the number of paths of
length k from node vi to vj. .
PATH MATRIX

• If now we represent a matrix Bn as


• Bn =A+A2+A3………Ak
• then each entry of Br represent the number of
paths of lengths r or less than r from node vi to vj.
• If an entry in the ith row and jth column of matrix B n
is non zero then it means that vertex vj. is reachable
from vertex vi .
• But inorder to determine whether a vertex is
reachable from any other vertex,we only need to
know the presence of a path and not the number of
path between two vertices
• This information regarding whether there exists a
path between any two vertices or not is maintained
by path matrix.
• The path matrix can be calculated from the B n by
choosing Pij using following simple rule
• Pij =0 if entry in the ith row and jth column of matrix Bn is 0
• Pij = 1 if the entry in ith row and jth column of matrix B n is non
zero
Path Matrix- Let G be a simple directed graph with m
nodes, v1,v2,v3………..vm. The path matrix or reachability
matrix of G is the m-square matrix P=(pij) defined as:

1 if there is a path from vi to vj


Pij=
0 Otherwise

Suppose there is a path from vi to vj. Then there must be a simple


path from vi to vj when vi ≠ vj or there must be a cycle from vi to vj
when vi = vj. Since G has only m nodes such a simple path must be
of length m-1 or less or such a cycle must have length m or less.
WARSHALL ALGORITHM

• A better and relatively simple method to compute


the path matrix of a given graph is using warshall
algorithm.
• Warshall algorithm determines whether for
directed graph G=(V,E) with vertex set V
={1,2……..n},there exists a path in G from i to j for
all vertex pairs(i,j)ɛ V.
• This algorithm tests the reachibility of all pair of
vertices in a graph
The basic idea behind this algorithm is that path
vertex i to vertex j exists(i.e. P[i,j]=1) if either of two
conditions hold true:
I. There exists a direct path from vertex I to vertex j.
II.There exists an indirect path from vertex i to vertex
j through one or more intermediate vertices.
In order to compute the path for any pair of vertices
of a graph G using warshall algorithm we first need
to compute sequence of nxn boolean matrices.
• One might remember that matrix P0 contains direct edges with
no intermediates so P0 =A,adjacency matrix of G.
• Warshall observed that Pk [i,j]=1 if and only if there is a simple
path from vertex i to vertex j by going through vertex i to
vertex k and a path from vertex k to vertex j,each involving
intermediate vertices in the set{1,2……(k-1)}
• With this information,the entry (i,j) of the boolean matrix is
obtained by looking at entries in the matrix Pk using condition

• Pk [i,j]=Pk-1 [i,j] V(Pk-1 [i,k] Ʌ Pk-1 [k,j])


Warshall Algorithm

WARSHALL(A):Given an adjacency matrix A of order n


by n for the graph G with n vertices labelled as 1,2………
n. The algorithm generates the path matrix P of order n by
n.
[initialize P with A i.e. computing P0 ]
1.Repeat step 2 for I=1,2,……N
2. Repeat for J=1,2,………..N
P[I,J]A[I,J]
[End of step 2 loop]
[End of step 1 loop]
[Computing P1 , P2,………………. and finally PN i.e. P]
3. Repeat steps 4,5 for K=1,2……..N
4. Repeat steps 5 for I=1,2……….N
5. Repeat for J=1,2…………..N
P[I,J]P[I,J] V (P[I,K] AND P[K,J])
[End of step 5 loop]
[End of step 4 loop]
[End of step 3 loop]
6. Return
LINKED REPRESENTATION OF GRAPH

The sequential representation of the graph in


memory i.e the representation of graph by
adjacency matrix has a number of major drawbacks
It may be difficult to insert and delete nodes in Graph.
This is because the size of array may need to be
changed and the nodes may need to reordered
which can lead to many changes in the matrix.
Also if the number of edges are very less, then, the
matrix will be sparse and there will be wastage of
memory.
• In linked representation of a graph ,two linked lists
a vertex list and adjacency list are maintained.
• The vertex list is a singly linked list mainaining all
the vertices in the list irrespective of any order.
• From each node of this vertex list another linked list
known as adjancency list eminates that maintains
all the vertices adjancent to the vertex stores in
source node irrespective of any order
• Each node of the vertex list contain of three parts:
• The first part VERT_INFO that stores the vertex name.
• The second part NEXTLINK that points to the next node in the
vertex list.
• The third part ADJ_LINK that points to the first node of its
adjaceny list.
• A node of the adjacency list consists of two parts:
• The first part LINK_VERT that points to the node of the vertex
list which is adjancent to it.
• The second part LINK that points to the next node in the same
adjacency list.
A D
E

B C
Node Adjacency List
A B,C,D
C

C,E
B
C

C
D
E
www.csemcq.com

You might also like