Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
3Activity
0 of .
Results for:
No results containing your search query
P. 1
Graphs

Graphs

Ratings: (0)|Views: 37 |Likes:
Published by gdskumar
A graph is a collection of vertices and edges, G =(V, E) where V is set of vertices and E is set of edges. An edge is defined as pair of vertices, which are adjacent to each other. When these pairs are ordered, the graph is known as directed graph.
A graph is a collection of vertices and edges, G =(V, E) where V is set of vertices and E is set of edges. An edge is defined as pair of vertices, which are adjacent to each other. When these pairs are ordered, the graph is known as directed graph.

More info:

Published by: gdskumar on Sep 25, 2010
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

12/06/2010

pdf

text

original

 
Session 6
Graphs
During this session you will learn about:
Graphs.
Adjacency matrix and lists.
Breadth search and depth search on graphs.
Some important functions and definitions regarding graphs.
6.1
.
Introduction
A graph is a collection of vertices and edges, G =(V, E)where V is set of vertices and E is set of edges. An edge isdefined as pair of vertices, which are adjacent to each other.
When these pairs are ordered, the graph is known as
directed graph.
These graphs have many properties and they are veryimportant because they actually represent many practicalsituations, like networks. In our current discussion we areinterested on the algorithms which will be used for most ofthe problems related to graphs like to check connectivity, thedepth first search and breadth first search, to find a pathfrom one vertex to another, to find multiple paths from onevertex to another, to find the number of components of thegraph, to find the critical vertices and edges.The basic problem about the graph is its representationfor programming.
6.2. Adjacency Matrix and Adjacency lists
We can use the adjacency matrix, i.e. a matrix whose rowsand columns both represent the vertices to represent graphs.In such a matrix when the i
th
row, j
th
column element is 1, wesay that there is an edge between the i
th
and j
th
vertex. Whenthere is no edge the value will be zero. The otherrepresentation is to prepare the adjacency lists for eachvertex.Now we will see an example of a graph and see how anadjacency matrix can be written for it. We will also see theadjacency relations expressed in form of a linked list.
Data Structures with ‘c’
114
 
For Example:
Fig 1 Graph
The adjacency matrix for representing this graph is:V1V2V3V4V5V6V7V8V101001100V210011000V300011001V401100000V511100111V610001000V700001000V800101000
Fig 2. Adjacency Matrix representation of graph in fig 1
Adjacency list will be:V1 -> V2 -> V5 -> V6V2 -> V1 -> V4 -> V5V3 -> V4 -> V5 -> V8V4 -> V2 -> V3V5 -> V1 -> V2 -> V3 -> V6 -> V7 -> V8V6 -> V1 -> V5
Data Structures with ‘c’
115V1V4V7V51V81V3V2V6
 
V7 -> V5V8 -> V3 -> V5
Fig 3. Adjacency List representation of graph in fig 1
6.3. Breadth first search and Depth first search
Suppose the graph is represented as an adjacency matrix,and we are required to have the breadth first search of thegraph. Here we will require the starting vertex from whichthis search will begin. First that vertex will be printed,then all the vertices, which are adjacent to it, are printedand so on.If we have a matrix and there are n vertices. Let thestarting vertex be j. Now the j
th
vertex will be printed first,and to find all the vertices adjacent to this vertex, we musttravel along j
th
row of the matrix, and whenever we find 1 wewill print the corresponding column number. Next time we willrequire each of the vertices printed recently so that we cantravel level by level. For the same purpose we will push intoa queue all the columns with the value one in the j
th
row. Nexttime pop the vertex number from the queue and print it.Replace the value of j, which is currently printed. Again pushall the vertices that are adjacent to this vertex into thequeue and continue the above process until all the verticesare dealt with.Remember there will be many vertices, which will beconnected to more than a vertex, and therefore there arechances that we may repeat some of the vertices or there willbe an infinite loop. To avoid this problem, we use what isknown as visited array. It will be initially all zeroes.Whenever a vertex is pushed into the queue the correspondingposition the visited array is changed to 1. Now we use anotherrule that we push only those vertices into the queue whosecorresponding value in the visited array at that point iszero. When all the vertices are printed we will stop.Sometimes it happens that a particular vertex or a group ofvertices is non reachable from the current vertex and in thiscase the graph is ‘
not connected’
.
Therefore a connected graph is the one in which we cantravel through all the vertices starting from a current node.
Algorithm for the breadth first traversal in a graph:1.Initialize the adjacency matrix p to all zeroes.2.Accept the number of vertices from the user, say n.3.Initialize the visited array v to all zeroes.4.Accept the graph.
Data Structures with ‘c’
116

Activity (3)

You've already reviewed this. Edit your review.
1 hundred reads
ggardu liked this
ggardu liked this

You're Reading a Free Preview

Download
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->