You are on page 1of 24

GRAPHS

Dr.A.Ashok Kumar
Department Of Computer Science
Alagappa Government Arts College
Karaikudi
• A graph G consists of two sets V and E. The set
V is a finite, nonempty set of vertices
• The set E is a set of pairs of vertices; these
pairs are called edges
• The notations V(G) and E(G) represent the sets
of vertices and edges, respectively, of graph G
• We write G = (V, E) to represent a graph
• In an undirected graph the pair of vertices
representing any edge is unordered.
– The pairs (u,v) and (v, u) represent the same edge
• In a directed graph each edge is represented
by a directed pair (u,v); u is the tail and v the
head of the edge
– Therefore, (v, u) and (u,v) represent two different
edge
• The set representations of these graphs are
– V(G1) = {1,2,3,4}, E(G1)= {(1,2),(1, ,3), (1,4),
(2,3), (2,4), (3,4)}
– V(G2)= {1,2,3,4,5,6,7}, E(G2)= {(1,2), (1,3), (2,
,4), (2,5), (3,6), (3,7)}
– V(G3)= {1,2,3} , E(G3) = {(1,2),(2,1),(2,3)}
• Restrictions on graphs:
1. A graph may not have an edge from a vertex v back
to itself
• That is, edges of the form (v, v) and <v,v> are not legal
• Such edges are known as self-edges or self-loops
• If we permit self-edges , we obtain a data object referred
to as a graph with self-edges
2. A graph may not have multiple occurrences of the
same edge
• If we remove this restriction, we obtain a data object
referred to as a multigraph
• If (u,v) is an edge in E(G),then we say
vertices u and v are adjacent and edge (u,v)
is incident on vertices u and v
• If (u,v) is a directed edge, then vertex u is
adjacent to v, and v is adjacent from u
• A subgraph of G is a graph G‘ such that V(G')
⊆ V(G) and E(G') ⊆ E(G)
• A path from vertex u to vertex v in graph G
is a sequence of vertices u,i1, i2, ...., ik,v such
that (u,i1), (i1,i2), ..., (ik,v) are edges in E(G)
• The length of a path is the number of edges
on it
• A simple path is a path in which all vertices
except possibly the first and last are distinct
• A cycle is a simple path in which the first
and last vertices are the same
• In an undirected graph G, two vertices u and
v are said to be connected iff there is a path
in G from u to v
• An undirected graph is said to be connected
iff for every pair of distinct vertices u and v
in V(G), there is a path from u to v in G
• A tree is a connected acyclic (i.e., has no cycles)
graph
• A directed graph G is said to be strongly
connected iff for every pair of distinct vertices u
and v in V(G), there is a directed path from u to
v and also from v to u
• The degree of a vertex is the number of edges
incident to that vertex
• If G is a directed graph, we define the in-degree
of a vertex v to be the number of edges for
which v is the head
• The out-degree is defined to be the number of
edges for which v is the tail
• A graph with weighted edges is called a network
• Some of these applications are
1. The analysis of electric circuits,
2. Finding shortest routes,
3. Project planning,
4. Identification of chemical compounds,
5. Statistical mechanics,
6. Genetics - The scientific study of heredity,
7. Cybernetics - cybernetic systems are various
kinds of automatic control devices in engineering,
8. Linguistics - The science of language, including
phonetics, phonology, morphology, syntax, and
semantics
9. Social sciences,
10. Mathematical structures, and so on
Graph Representations
• Most commonly representations for graphs
are
– Adjacency matrices,
– Adjacency lists, and
– Adjacency multilists
Adjacency Matrix
• Let G = (V,E) be a graph with n vertices, n ≥ 1
• The adjacency matrix of G is a two-dimensional n
x n array, say a, with the property that a[i, j] = 1 iff
the edge (i,j) ((i,j)for a directed graph) is in E(G)
• The element a[i, j] = 0 if there is no such edge in G
Adjacency Lists
• In this representation of graphs, 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 at least two fields:
– vertex and
– link
• The vertex field contains the indices of the
vertices adjacent to vertex i
Adjacency Multilists
• In some applications it is necessary to be able to
determine the second entry for a particular edge
and mark that edge as having been examined
• This can be accomplished easily if the adjacency
lists are maintained as multilists
• Each edge there is exactly one node, but this node
is in two lists
• The new node structure is

• Where m is a one-bit mark field that can be used


to indicate whether the edge has been examined
TECHNIQUES FOR GRAPHS

• In its simplest form it requires us to determine


whether there exists a path in the given graph
G = (V, E) such that this path starts at vertex v
and ends at vertex u
• This problem can be solved by starting at
vertex v and systematically searching the graph
G for vertices that can be reached from v
• Two search methods
– Breadth First Search and Traversal
– Depth First Search and Traversal
Breadth First Search and Traversal
• In breadth first search we start at a vertex v and mark it
as having been reached (visited)
• The vertex v is at this time said to be unexplored
• A vertex is said to have been explored by an algorithm
when the algorithm has visited all vertices adjacent
from it
• All unvisited vertices adjacent from v are visited next
• These are new unexplored vertices
• Vertex v has now been explored
• The newly visited vertices haven't been explored and
are put onto the end of a list of unexplored vertices
• The first vertex on this list is the next to be explored
• Exploration continues until no unexplored vertex is left
• The list of unexplored vertices operates as a queue
Algorithm BFS(w)
//A breadth first search of G is carried out beginning
// at vertex v. For any node i, visited[i]= 1if i has
// already been visited. The graph G and array visited[]
// are global; visited[] is initialized to zero.
{
u :=v; // q is a queue of unexplored vertices.
visited[v] :=1;
Repeat
{ for all vertices w adjacent from u do
{
if (visited[w]= 0) then
{
Add w to q; // w is unexplored.
visited[w] :=1;
}
}
if q is empty then return;// No unexplored vertex.
Delete u from q; // Get first unexplored vertex.
}until(false);
}
• A complete traversal of the graph can be made
by repeatedly calling BFS each time with a new
unvisited starting vertex
• The resulting traversal algorithm is known as
breadth first traversal (BFT)

Algorithm BFT(G,n)
// Breadth first traversal of G
{
for i :=1to n do // Mark all vertices unvisited.
visited[i]:=0;
for i :=1to n do
if (visited[i]= 0) then BFS(i);
}
Depth First Search and Traversal
• A depth first search of a graph differs from a
breadth first search in that the exploration of
a vertex v is suspended as soon as a new
vertex is reached
• At this time the exploration of the new
vertex u begins
• When this new vertex has been explored, the
exploration of v continues
• The search terminates when all reached
vertices have been fully explored
Algorithm DFS(w)
// Given an undirected (directed) graph G = (V,E) with
// n vertices and an array visited[] initially set
//to zero, this algorithm visits all vertices
// reachable from v. G and visited[] are global.
{
visited[v] :=1;
for each vertex w adjacent from v do
{
if (visited[w]= 0) then DFS(w);
}
}
• The algorithm for this (DFT) differs from BFT
only in that the call to BFS(i) is replaced by a call
to DFS(i)

You might also like