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

DFS Unit 4

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

DFS Unit 4

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

DATA STRUCTURE

UNIT – IV
Graphs
Graphs: Definition
• A graph G = (V,E) is composed of:
V: set of vertices
E: set of edges connecting the vertices in V
• An edge e = (u,v) is a pair of vertices
• Example:

V= {a,b,c,d,e}
E= {(a,b),(a,c),(a,d),(b,e),(c,d),(c,e),(d,e)}

Types of Graph:-
• An undirected graph is one in which the pair of vertices in a edge is unordered,
(v0, v1) = (v1,v0)
Example, Graph 1 shown above.
• A directed graph is one in which each edge is a directed pair of vertices, <v0,
v1> != <v1,v0>
Example,

Terminology Used in Graphs:-


• Adjacent and Incident vertices:
– If (v0, v1) is an edge in an undirected graph,
• v0 and v1 are adjacent
• The edge (v0, v1) is incident on vertices v0 and v1
– If <v0, v1> is an edge in a directed graph
• v0 is adjacent to v1, and v1 is adjacent from v0
• The edge <v0, v1> is incident on v0 and v1
• Degree of a vertex is the number of edges incident to that vertex for undirectedgraphs.
– For directed graph,
• the in-degree of a vertex v is the number of edgesthat have v as the head
the out-degree of a vertex v is the number of edgesthat have v as the tail
• if di is the degree of a vertex i in a graph G with n vertices and e edges, the number of edges

• Path: Sequence of vertices v1,v2,. . .vk such that consecutive vertices vi and
vi+1 are adjacent.
Example, consider same graph 1. The paths can be:-

Cycle: is a simple path, except that the last vertex is the same as the firstvertex.
Example, consider same graph 1. A cycle can be:-

Complete graph: one in which all pairs of vertices are adjacent. Total numberof edges in
complete graph is - m = n(n -1)/2, where m is number of edges andn are vertices.

n=5
m = 5 * (5-1)/2 = 10

Graph Representation:-
Adjacency Matrix
oLet G = (V, E) be a graph with n vertices.
oThe adjacency matrix of G is a two-dimensionaln by n array, say adj_mat
oIf the edge (vi, vj) is in E(G), adj_mat[i][j]=1
oIf there is no such edge in E(G), adj_mat[i][j]=0
The adjacency matrix for an undirected graph is symmetric; the
adjacency matrix for a digraph need not be symmetric
oExample,

Pros: Representation is easier to implement and follow. Removing


an edge takes O(1) time. Queries like whether there is an edge from
vertex ‘u’ to vertex ‘v’ are efficient and can be done O(1).
oCons: Consumes more space O(V^2). Even if the graph is
sparse(contains less number of edges), it consumes the same space.
Adding a vertex is O(V^2) time.

Adjacency Lists
oAn array of linked lists is used. Size of the array is equal to number
of vertices. Let the array be array[]. An entry array[i] represents the
linked list of vertices adjacent to the ith vertex. This representation
can also be used to represent a weighted graph. The weights of
edges can be stored in nodes of linked lists.
oExample,
This representation takes O(|V | + |E|) space.
Traversal in Graphs
BFS (Breadth First Traversal):-
oIn this method, After visiting a vertex v, we must visit all its
adjacent vertices w1, w2, w3, ..., before going down next level to
visit vertices adjacent to w1 etc.
oThe method can be implemented using a queue.
oA boolean array is used to ensure that a vertex is enqueued only
once.
oAlgorithm:-
enqueue the starting vertex
while(queue is not empty){
dequeue a vertex v from the queue;
visit v.
enqueue vertices adjacent to v that were never enqueued;
}
DFS
DFS (Depth First Traversal)
o In this method, After visiting a vertex v, which is adjacent to w1,w2, w3….., Next we visit
one of v's adjacent vertices, w1 say.Next, we visit all vertices adjacent to w1 before coming
back tow2, etc.
oMust keep track of vertices already visited to avoid cycles.
oThe method can be implemented using recursion or iteration.
oThe iterative preorder depth-first algorithm is:
push the starting vertex onto the stack
while(stack is not empty){
pop a vertex off the stack, call it v
if v is not already visited, visit it
push vertices adjacent to v, not visited, onto the stackt
}

You might also like