You are on page 1of 16

CS2201 Data Structures V Unit II Year CSE

2013-14/CS2201/Unit V/ P.Sujatha 1
UNIT V GRAPHS
Graph:
A graph G=(V,E) consists of a set of vertices V and set of edges E. Each edge is a
pair(v,w), where v,wV.
If the pair is ordered then the graph is directed(digraph). Vertex w is adjacent to v iff
v,wE.
In an undirected graph with edge(v,w) and hence (w,v), w is adjacent to v and v is
adjacent to w.
Indegree of a vertex
The number of edges entering into the vertex V.
Outdegree of a vertex
The number of edges exiting from that vertex V.
An undirected graph is connected if there is a path from every vertex to every other
vertex.
Strongly connected graph
If there is a path from every vertex to every other vertex in a directed graph then it is said be
strongly connected graph.
Weakly connected graph
If there is no path from every vertex to every other vertex in a directed graph then it is said
be weakly connected graph.
Two ways of representing a graph

The two ways of representing a graph are:
1. Adjacency Matrix
2. Adjacency List
Example:
V1 V2
V3 V4
CS2201 Data Structures V Unit II Year CSE
2013-14/CS2201/Unit V/ P.Sujatha 2
Adjacency matrix: Adjacency List:

Topological sort

A topological sort is an ordering of vertices in a directed acyclic graph, such that is there
is a path from V
i
to V
j
, then V
j
appears after V
i
in the ordering.
Implementation of the topological sorting
1. Find the indegree for all vertices.
2. Enqueue the vertices whose indegree is 0.
3. Dequeue the vertex v and decrement the indegrees of all its adjacency vertices.
4. Enqueue the vertices on the queue, if its indegree falls to zero.
5. Repeat from step 3 until the queue is empty.
6. The topological ordering is the order in which the vertices dequeued.
Pseudo code for topological sort
Void topsort(Graph G)
{
Queue Q;
int counter=0;
Vertex V, W;
Q=createqueue(NumVertex); MakeEmpty(Q);
for each vertex V
if(indegree[V]==0)
enqueue(V,Q);
while(!isempty(Q))
{
V=dequeue(Q);
Topnum[V]=++counter;
for each Wadjacent to V
if(--indegree[W]==0)
enqueue(W,Q);
}
if(counter!=NumVertex)
error(Graph has a cycle);
v1 v2 v3 v4
v1 0 1 1 0
v2 0 0 0 1
v3 0 1 0 0
v4 0 0 1 0
v1
v2
v3
V4
V2 V3
V4
V2
V3
CS2201 Data Structures V Unit II Year CSE
2013-14/CS2201/Unit V/ P.Sujatha 3
disposequeue(Q); // Free the Memory
}
Graph traversal methods:
1. Breath First Traversal
2. Depth First Traversal
Breath First Traversal
BFS of a graph G starts from an unvisited vertex u. Then all unvisited vertices v
i
adjacent
to u are visited and then all unvisited vertices w
j
adjacent to v
i
are visited and so on.
Breadth first search uses a queue data structure to keep track of the order of nodes whose
adjacent nodes are to be visited.
ADT Routines for breath first traversal
Input: A graph G and a root v of G
procedure BFS(G,v):
create a queue Q
enqueue v onto Q
mark v
while Q is not empty:
t Q.dequeue()
if t is what we are looking for:
return t
for all edges e in G.adjacentEdges(t) do
u G.adjacentVertex(t,e)
if u is not marked:
mark u
enqueue u onto Q
return none
Implementation
The algorithm uses a queue data structure to store intermediate results as it traverses the graph,
as follows:
1. Enqueue the root vertex
2. Dequeue a vertex and mark it as visited.
3. Using the adjacency matrix of the graph find all the unvisited adjacent vertices and
enqueue them into the queue.
4. Repeat the step 2 and 3 until the queue is empty.
Example
Adjacency Matrix
V
1
V
2
V
3
V
4
CS2201 Data Structures V Unit II Year CSE
2013-14/CS2201/Unit V/ P.Sujatha 4
Implementation
1. Let v1 be the root vertex and mark it as visited.
2. Find the adjacency vertices of v1 and enqueue them into the queue.
V2 V3 V4
3. Now the vertex v2 is dequeued and adjacent vertices are v1 and v4. V1 is already visited
and the vertex v4 is already in the queue.
V3 V4
4. The vertex v3 is dequeued and the adjacency vertices are v1 and v4. V1 is already visited
and the vertex v4 is already in the queue.
V4
5. Now the vertex v4 is dequeued and adjacent vertices are v1 v2 and v3. All are already
visited and the queue is also empty.
Depth First Traversal
DFS by selecting one vertex V of G as a start vertex. V is marked visited. Then each
unvisited vertex adjacent to V is searched in turn using depth first search recursively. This
process continues until a vertex with no adjacent vertices is encountered.
Routines for depth first traversal
void dfs(vertex v)
{
visited[v]=True;
for each w adjacent to v
if(!visited[w])
dfs(w);
}
Implementation
Select any node in the graph and mark is as visited.
v1 v2 v3 v4
v1 0 1 1 1
v2 1 0 0 1
v3 1 0 0 1
v4 1 1 1 0
V
1
V
2
V
4
V
3
CS2201 Data Structures V Unit II Year CSE
2013-14/CS2201/Unit V/ P.Sujatha 5
Recursively call the depth first search on all adjacent vertices that are not already
visited.
Example
Adjacency Matrix
Implementation
1. Let v1 be the source vertex and mark it as visited.
2. Find the immediate adjacent unvisited vertex v2 of v1 and mark it as visited.
3. From v2 the next adjacent vertex is v4 is marked.
4. From v4 the next unvisited vertex is v3 is marked.
Shortest Path Problem
Dijkstras algorithm
To solve the single source shortest path problem for the weighted graph is known as
Dijkstras algortihm. This algortihm uses greedy technique. Greedy algorithm is used to solve a
problem in stages by doing what appears to be the best thing at each stage.
v1 v2 v3 v4
v1 0 1 1 1
v2 1 0 0 1
v3 1 0 0 1
v4 1 1 1 0
V
1
V
2
V
3
V
4
V
1
V
2
V
3
V
4
CS2201 Data Structures V Unit II Year CSE
2013-14/CS2201/Unit V/ P.Sujatha 6
Let the starting vertex is called the initial vertex. Let the distance of vertex v be the distance
from the initial vertex to v. Dijkstra's algorithm will assign some initial distance values and will
try to improve them step by step.
1. Assign to every vertex a tentative distance value: set it to zero for our initial node and to
infinity for all other nodes.
2. Mark all vertices unvisited. Set the initial vertex as current. Create a set of the unvisited
vertices called the unvisited set consisting of all the vertices except the initial vertex.
3. For the current vertex, consider all of its unvisited neighbors and calculate their tentative
distances. For example, if the current vertex v1 is marked with a distance of 4, and the
edge connecting it with a neighbor v2 has length 1, then the distance to v2 (through v1)
will be 4+1=5. If this distance is less than the previously recorded tentative distance of
v2, then overwrite that distance. Even though a neighbor has been examined, it is not
marked as "visited" at this time, and it remains in the unvisited set.
4. Mark the current vertex as visited and remove it from the unvisited set. A visited vertex
will never be checked again.
5. If the destination vertex has been marked visited (when planning a route between two
specific nodes) or if the smallest tentative distance among the vertices in the unvisited set
is infinity (when planning a complete traversal), then stop. The algorithm has finished.
6. Select the unvisited vertex that is marked with the smallest tentative distance, and set it as
the new "current node" then go back to step 3.
Example: 4

1 2 2
3
3
1
Step 1: Step 2:
It represents the initial configuration, The source vertex v1 is marked as known.
assuming that the source node s is v
1
. The vertices adjacent to v1 are v2 and v3.
Step 3: Step 4:
The vertex v3 is marked as known. Now the vertex v4 is marked as known.
The vertices adjacent to v3 are v2 and v4. The vertices adjacent to v4 are v2 and v5.
v Known d
v
d
w
v1 0 0 0
v2 0 0
v3 0 0
v4 0 0
v5 0 0
v Known d
v
d
w
v1 1 0 0
v2 0 4 v1
v3 0 1 v1
v4 0 0
v5 0 0
v Known d
v
d
w
v1 1 0 0
V
1
V
2
V
3
V
4
V
5
CS2201 Data Structures V Unit II Year CSE
2013-14/CS2201/Unit V/ P.Sujatha 7

Step5: Step 6:
The vertext v2 is marked as known. The vertext v5 is marked as known.
The vertices adjacent to v2 is v5.


Minimum spanning tree
A minimum spanning tree of a weighted connected graph G is its spanning tree of the
smallest weight, where the weight of a tree is defined as the sum of the weights on all its edges.
Prims algortihm
2
4 1 3 10
2 7
5 8 4 6
1
Step 1:
It represents the initial configuration, assuming that the source node s is v
1
.
v Known d
v
d
w
v1 0 0 0
v2 0 0
v3 0 0
v4 0 0
v5 0 0
v6 0 0
v7 0 0
Step 2:
v2 0 3 v3
v3 1 1 v1
v4 0 2 v3
v5 0 0
v Known d
v
d
w
v1 1 0 0
v2 0 3 v3
v3 1 1 v1
v4 1 2 v3
v5 0 5 v4
v Known d
v
d
w
v1 1 0 0
v2 1 3 v3
v3 1 1 v1
v4 1 2 v3
v5 1 5 v4
v Known d
v
d
w
v1 1 0 0
v2 1 3 v3
v3 1 1 v1
v4 1 2 v3
v5 0 5 v4
V
1
V
1
V
3
V
4
V
5
V
6
V
7
CS2201 Data Structures V Unit II Year CSE
2013-14/CS2201/Unit V/ P.Sujatha 8
The source vertex v1 is marked as known. The vertices adjacent to v1 are v2, v3 and v4.
v Known d
v
d
w
v1 1 0 0
v2 0 2 v1
v3 0 4 v1
v4 0 1 v1
v5 0 0
v6 0 0
v7 0 0
Step 3:
The vertex v4 is marked as known. The vertices adjacent to v4 are v2, v3, v5, v6 and v7.
v Known d
v
d
w
v1 1 0 0
v2 0 2 v1
v3 0 2 v4
v4 1 1 v1
v5 0 7 v4
v6 0 8 v4
v7 0 4 v4
Step 4:
Now the vertex v2 is marked as known. The vertices adjacent to v2 are v1, v4 and v5.
v1 and v4 are already visited.
v Known d
v
d
w
v1 1 0 0
v2 1 2 v1
v3 0 2 v4
v4 1 1 v1
v5 0 7 v4
v6 0 8 v4
v7 0 4 v4
4
2
1
V
1
V
2
V
3
V
4
V
5
V
6
V
7

7
4
8
2
1
V
1
V
2
V
3
V
4
V
5
V
6
V
7
2
2
1
V
1
V
2
V
3
V
4
V
5
V
6
V
7
2
7
8
4
CS2201 Data Structures V Unit II Year CSE
2013-14/CS2201/Unit V/ P.Sujatha 9
Step 5:
The vertext v3is marked as known. The vertices adjacent to v3 are v1, v4 and v6.
The vertices v1 and v4 is already visited.
v Known d
v
d
w
v1 1 0 0
v2 1 2 v1
v3 1 2 v4
v4 1 1 v1
v5 0 7 v4
v6 0 5 v3
v7 0 4 v4
Step 6:
The vertext v7 is marked as known. The adjacent vertices are v4, v5 and v6.
The vertex v4 is already visited.
v Known d
v
d
w
v1 1 0 0
v2 1 2 v1
v3 1 2 v4
v4 1 1 v1
v5 0 6 v7
v6 0 1 v7
v7 1 4 v4
Step 7:
The vertext v6 is marked as known. The adjacent vertices are v3, v4 and v7.
All are already visited.
v Known d
v
d
w
v1 1 0 0
v2 1 2 v1
v3 1 2 v4
v4 1 1 v1
v5 0 6 v7
v6 1 1 v7
v7 1 4 v4
Step 8:
The vertext v5 is marked as known. The adjacent vertices are v2, v4 and v7.
All are already visited.
2
1
V
1
V
2
V
3
V
4
V
5
V
6
V
7
2
2
1
2
4
V
1
V
2
V
3
V
4
V
5
V
6
V
7
6
1
2
4
2
1
V
1
V
2
6
V
3
V
4
V
5
V
6
V
7
1
CS2201 Data Structures V Unit II Year CSE
2013-14/CS2201/Unit V/ P.Sujatha 10
v Known d
v
d
w
v1 1 0 0
v2 1 2 v1
v3 1 2 v4
v4 1 1 v1
v5 1 6 v7
v6 1 1 v7
v7 1 4 v4
The minimum cost of this spanning tree is 16.
Kruskals algorithm
Kruskals algorithm is a way to find out the minimum cost spanning tree. Each stage
selects an edge with minimum weight and it doesnt form a cycle.
Kruskals algorithm maintains a collection of trees. Initially it contains |v| single node
trees. Adding an edge merges two trees into one. When the algorithm terminates, it contains only
one tree and it is a minimum spanning tree.
Routine for Krruskals
Void kruskal(Graph G)
{
int EdgesAccepted;
DisjSet S;
PriorityQueue H;
Vertex U;
Settype Uset, Vset;
Edge E;
Initialize(S);
ReadGraphIntoHeapArray(G, H);
BuildHeap(H);
EdgesAccepted=0;
While(EdgesAccepted<Numvertex-1)
{
E=DeleteMin(H);
Uset=Find(U,S);
Vset=Find(V,S);
if(Uset!=Vset)
{
EdgesAccepted++;
SetUnion(S, Uset, Vset);
}
}
2
1
V
1
V
2
V
3
V
4
V
5
V
6
V
7
2
4
6
1
CS2201 Data Structures V Unit II Year CSE
2013-14/CS2201/Unit V/ P.Sujatha 11
}
Example: 2
4 1 3 10
2 7
5 8 4 6


1
Step 1:
Initial configuration
Step 2: Edge v1,v4 is accepted. Step 3: Edge v6,v7 is accepted.

Step 4: Edge v1,v2 is accepted Step 5: Edge v1,v3 is accepted
V
1
V
1
V
3
V
4
V
5
V
6
V
7
V
1
V
1
V
3
V
4
V
5
V
6
V
7
V
1
V
1
V
3
V
4
V
5
V
6
V
7
1
V
1
V
1
V
3
V
4
V
5
V
6
V
7
1
1
V
1
V
1
V
3
V
4
V
5
V
6
V
7
1
2
2
2
V
1
V
1
V
3
V
4
V
5
V V
1
CS2201 Data Structures V Unit II Year CSE
2013-14/CS2201/Unit V/ P.Sujatha 12
Step 6: Edge v2,v4 forms a cycle. Step 7: Edge v1,v3 forms a cycle.
So, that edge is rejected. So, that edge is rejected.
Step 8: Edge v4,v7 is accepted Step 9: Edge v3,v6 forms a cycle. So, that
edge is rejected.
Step 10: Edge v7,v5 is accepted
V
1
V
1
V
3
V
4
V
5
V
6
V
7
1
1
2
2
V
1
V
1
V
3
V
4
V
5
V
6
V
7
1
1
2
2
V
1
V
1
V
3
V
4
V
5
V
6
V
7
1
1
2
2 4
V
1
V
1
V
3
V
4
V
5
1
2
2 4
V
1
V
1
V
3
V
4
V
5
V
6
V
7
1
1
2
2 4
CS2201 Data Structures V Unit II Year CSE
2013-14/CS2201/Unit V/ P.Sujatha 13
Edge Weight Action
(v1,v4)
(v6,v7)
(v1,v2)
(v3,v4)
(v2,v4)
(v1,v3)
(v4,v7)
(v3,v6)
(v5,v7)
1
1
2
2
3
4
4
5
6
Accepted
Accepted
Accepted
Accepted
Rejected
Rejected
Accepted
Rejected
Accepted
Articulation points and biconnected components
A connected undirected graph is biconnected if there are no vertices whose removal
disconnects the rest of the graph.
If a graph is not biconnected the vertices whose removal would disconnect the graph are
known as articulation points.
Steps to find the articulation points
1. Perform the depth first search, starting at any vertex.
2. Number the vertex as they are visited as Num(v).
3. Compute the lowest numbered vertex for every vertex v in the depth first spanning
tree, which we call as low(w), that is reachable from v by taking zero or more tree
edges and then possibly one back edge. By definition, low(v) is the minimum of
i. Num(v)
ii. The lowest Num(w) among all back edges(v,w)
iii. The lowest low(w) among all tree edges(v,w)
4. (i) The root is an articulation iff it has more than two children.
(ii) Any vertex v other than root is an articulation point iff v has same child w such
that low(w)>=Num(v).
Example:
B A
C D
6
CS2201 Data Structures V Unit II Year CSE
2013-14/CS2201/Unit V/ P.Sujatha 14
Depth First Spanning Tree
(1/1)
(2/1)
(3/1)
(4/1) (7/7)
(5/4)
(6/4)
Low value can be computed by performing a postorder traversal of the depth first spanning tree.
Low(v) is the minimum of
1. Num(v)
2. The lowest Num(w) among all back edges(v,w)
3. The lowest Low(w) among all tree edges(v,w)
A B C D E F G
A 0 1 0 1 0 0 0
B 1 0 1 0 0 0 0
C 0 1 0 1 0 0 1
D 1 0 1 0 1 1 0
E 0 0 0 1 0 1 0
F 0 0 0 1 1 0 0
G 0 0 1 0 0 0 0
B
A
C
D G
E
F
CS2201 Data Structures V Unit II Year CSE
2013-14/CS2201/Unit V/ P.Sujatha 15
ie., Low[v] = min{ Num(V), min{Num(w)}, min{Low(w)}}
Low[F]= min(Num(F), Num(D)) = min(6,4) = 4
Low[E]= min(Num(E), Low(F)) = min(5,4) = 4
Low[D]= min(Num(D), Low(E), Num(A)) = min(4,4,1) = 1
Low(G)= min(Num(G))=7
Low(C)= min(Num(C), Low(D), Low(G)) = min(3,1,7) = 1
Low(B)= min(Num(B), Low(C)) = min(2,1) = 1
Low(A)= min(Num(A), Low(B)) = min(1,1) = 1
If Low(w)>=Num(v) then v is an articulation point
Low(G)>=Num(C) ie., 7>=3
C is an articulation point
Low(E)>=Num(D) ie., 4>=4
D is an articulation point
.
Euler circuit
A graph is said to be containing an Euler circuit if it can traced in 1 sweep without lifting
the pencil from the paper and without tracing the same edge more than once. Vertices may be
passed through more than once. The starting and ending points must be the same.
Condition for Euler circuit
Every point in the graph have an even number of paths that connect it. An Eulerian
circuit is possible for any graph at which every vertex has 2, 4 or similar number of lines
connecting to it.
Example
Odd vertices the vertices which contains odd number of edges is called odd vertices.
Even vertices the vertices which contains even number of edges is called even vertices.
Procedure to be followed to find the Euler circuit and Euler graph are as follows
A graph with all vertices being even vertices contains an Euler circuit.
A graph which contains 2 or less than 2 odd vertices and any even vertices contains an
Euler path.
Graph 1
Graph 2
Graph 3
CS2201 Data Structures V Unit II Year CSE
2013-14/CS2201/Unit V/ P.Sujatha 16
A graph with more than 2 odd vertices that does not contain Euler path or Euler circuit.
Graph
No.
Number of vertices
connected to an odd
number of edges
Number of vertices
connected to an even
number of edges
Is the graph contains
Euler path? or Euler
circuit or not both
1 0 6 Euler Circuit
2 2 6 Euler Path
3 4 1
Not Euler circuit &
not Euler path
Applications of graph:
1. The link structure of a website could be represented by a directed graph
2. To model and analyze traffic networks
3. Study molecules in chemistry and physics.

You might also like