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

DFS3

Uploaded by

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

DFS3

Uploaded by

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

Nonlinear Data Structures

Graph
A graph G = (V, E); V is a set of nodes and E is a set of edges.
An edge ’x’ in E is associated with a pair of nodes denoted by ’u’ and
’v’. That is, edge x connects ’u’ and ’v’.

Adjacent nodes
Any two nodes which are connected by an edge in a graph are called
adjacent node.

Directed graph
A graph in which every edge is directed is called directed graph.
Clearly, (u, v ) denoting a pair of nodes in G , there would be a path
from u to v and also a path from v to u.

Undirected graph
A graph in which every edge is undirected is called undirected graph.
Dr. Avatharam Ganivada (SCIS) CBNST March 13, 2018 1 / 23
Mixed Graph
A graph involving the edges which are both directed or undirected is called
mixed graph.

Loop
An edge of graph which joins a node to itself is called a loop (sling).

Parallel Edges
In a graph (directed or un directed), edges between pairs of nodes that
exist in parallel are parallel edges.

Multigraph
Any graph which contains some parallel edges or self loops are multigraph.

Weighted graph
A graph with weights associated with the edges is called weighted graph.

Dr. Avatharam Ganivada (SCIS) CBNST March 13, 2018 2 / 23


Path of graph
A path in a graph is finite sequence of edges, connected to sequence of
nodes in sequel, which are distinct to each other.

Degree of vertex (node)


Degree of vertex, deg(v), is the number of edges which associate with the
node and loops to be counted twice.

Indegree of vertex (node)


The number of incoming edges to a node v is called indegree of the vertex.

Outdegree of vertex (node)


The number of outgoing edges to a node v is called outdegree of the
vertex.

Cyclic graph
A path containing one or more edges in which it starts from a vertex v and
terminates into the same vertex is called cyclic graph.
Dr. Avatharam Ganivada (SCIS) CBNST March 13, 2018 3 / 23
Acyclic graph
A graph which does not hold any cycle is called acyclic graph.

Complete graph
A graph G is said to be completed if every node in G is adjacent to every
other node. A complete graph with n nodes will have n(n − 1)/2 edges.

Representation of graphs
Adjacency matrix
Adjacency list
Incidence matrix
Adjacency matrix: A n × n matrix, (aij ), is in the form of adjacency
matrix aij = 1, if there is an edge from vi to vj
= 0, otherwise.

Dr. Avatharam Ganivada (SCIS) CBNST March 13, 2018 4 / 23


1 2 3 4 5
1 0 1 1 0 1
2 0 0 1 1 1
3 0 0 0 1 0
4 0 0 0 0 0
5 0 0 1 1 0

Note: In weighted graph, entries are weights of edges between the


vertices, zeros indicating diagonals, and infinity indicating rest of them.
This is also called cost adjacency matrix.
Dr. Avatharam Ganivada (SCIS) CBNST March 13, 2018 5 / 23
Adjacency list
Here, n rows of the adjacency matrix are represented as n linked lists.

Figure: Adjacency matrix and adjacency list

Incidence matrix
For a graph G with n vertices, e edges and no self loops, incidence matrix
A of the size n × e is defined as
aij = 1, if there is an edge j incident to node vi
= 0, otherwise.

Dr. Avatharam Ganivada (SCIS) CBNST March 13, 2018 6 / 23


Ex:

a b c d e f g h i j k l
A 1 0 0 0 0 0 1 0 0 0 0 0
B 1 1 1 0 0 0 0 0 0 0 0 0
C 0 1 0 1 0 0 1 1 0 0 1 0
D 0 0 1 1 1 1 0 0 0 0 0 0
E 0 0 0 0 1 0 0 1 1 1 0 0
F 0 0 0 0 0 0 0 0 0 1 1 1
G 0 0 0 0 0 1 0 0 1 0 0 1

Table: Incidence matrix


Dr. Avatharam Ganivada (SCIS) CBNST March 13, 2018 7 / 23
Traversing Techniques
1. Depth first search (DFS) 2. Breadth first search (BFS).
DFS is like preorder traversal of tree.
Traversal can start from any vertex vi .
If v is visited then all adjacent vertices, w, to v are traversed
recursively using DFS.
Since graph can have cycles, we avoid re-visiting a node. So we mark
a visited node v as visited node should not be selected for traversal.
Push visited node, v, into stack. i.e. PUSH(v).
Repeat this step till stack is empty. v < − POP()
if visited[v] < − 0
then visited[v] < − 1
for all w adjacent to v
if visited[w] is 0
then PUSH (w)
end for
end if
Dr. Avatharam Ganivada (SCIS) CBNST March 13, 2018 8 / 23
Current Node Stack Processed Nodes

A
A BCF A
F BCD AF
D BCEJ AF D
J BCEK AF D J
K BCEG AF D J K
G BCE AF D J K G
E BC AF D J K GE
C B AF D J K GEC
B Empty AF D J K GECB

Depth first traversal sequence is: A F D J K G E C B.


Dr. Avatharam Ganivada (SCIS) CBNST March 13, 2018 9 / 23
Example2

Dr. Avatharam Ganivada (SCIS) CBNST March 13, 2018 10 / 23


Breadth First Search (BFS)
This method starts from vertex v0 .
v0 is marked as visited. All vertices adjacent to v0 are visited next.
Let vertices adjacent to v0 are v1 , v2 , v3 , and v4 .
v1 , v2 , v3 , and v4 are marked as visited.
All unvisited vertices adjacent to v1 , v2 , v3 , and v4 are visited next.
This method continuous untill all vertices are visited.
The vertices visited but not adjacent vertices visited can be stored in
queue.
Initially, the queue contains the starting vertex.
Every traversal, a vertex is removed from queue and its adjacent
vertices, which are not yet visited, are added to the queue.
This process exists when the queue becomes empty.

Dr. Avatharam Ganivada (SCIS) CBNST March 13, 2018 11 / 23


Current Node Queue Traversed Nodes

A
A FCB A
F CBD AF
C BDEG AF C
B DEG AF C B
D EGJ AF C B D
E GJK AF C B D E
G JK AF C B D EG
J K AF C B D EGJ
K Empty AF C B D EGJK

Breadth first traversal sequence is: A F C B D E G J K.


Dr. Avatharam Ganivada (SCIS) CBNST March 13, 2018 12 / 23
Spanning Tree
A spanning tree of an undirected graph G is a tree (subgraph) which
includes all of the vertices of G with minimum possible number of edges.

Minimum spanning Tree


A minimum spanning tree of an undirected graph G is a spanning tree
with smallest possible weight, the where weight of a spanning tree is the
sum of weights of all edges in tree.

Dr. Avatharam Ganivada (SCIS) CBNST March 13, 2018 13 / 23


Minimum spanning tree is constructed using any of the algorithms: 1.
Kruskals algorithm 2. Prims algorithm.

Kruskals algorithm
Take a graph with ’n’ vertices.
Make the tree T empty.
Sort all the edges in non-decreasing order of their weight.
Repeat the following three steps as T contains n - 1 edges and a set
of edges E is non empty.
Choose an edge (v , w ) from ECBNST
Dr. Avatharam Ganivada (SCIS) with the lowest cost. March 13, 2018 14 / 23
Delete the edge (v , w ) from E .
If the edge (v , w ) does not create a cycle in T then add (v , w ) to T ,
otherwise discard (v , w ).
If T contains less than n - 1 edges then print no spanning tree exists.
Example1:

Cost 10 15 20 25 30 35 40 45 50 55
Edge (1, 2) (3,6) (4,6) (2,6) (1,4) (3,5) (2,5) (1,5) (2,3) (5,6)

Dr. Avatharam Ganivada (SCIS) CBNST March 13, 2018 15 / 23


Note: Only edge (1, 4) is discarded as it creates cycle. Total cost is 35.

Example2:

Dr. Avatharam Ganivada (SCIS) CBNST March 13, 2018 16 / 23


Prim’s algorithm
Assume a set of vertices V = 1, 2, . . . , n.
Remove all loops and parallel edges.
Choose any arbitrary node as root node.
Check outgoing edges and select the one with less weight.
Add the node to new spanning tree.
Take constructed tree as a node and check all outgoing edges again.
Repeat the above two steps till all the nodes in V are included in
minimum spanning tree.
Algorithm:
{
V = 1, 2, . . . , n; Minimum spanning tree T = ∅;
An empty set U = ∅;
while(U 6= V )
{ let (u, v ) denote the lowest weight edge such that u ∈ U, v ∈ {V − U}
T = T ∪ {(u, v )}
U = U∪ {v } } }
Dr. Avatharam Ganivada (SCIS) CBNST March 13, 2018 17 / 23
Example
Remove all loops and parallel edges.

Dr. Avatharam Ganivada (SCIS) CBNST March 13, 2018 18 / 23


Example1
Choose S node as the root node. Check for all edges going out from it.
S,A and S,C are two edges with weights 7 and 8, respectively. We choose
the edge S,A as it is lesser than the other.

Dr. Avatharam Ganivada (SCIS) CBNST March 13, 2018 19 / 23


The tree S-7-A is treated as one node. The out going edges from it are
now checked. The one which has the lowest cost is selected and included
it in the tree.

Dr. Avatharam Ganivada (SCIS) CBNST March 13, 2018 20 / 23


S-7-A-3-C tree is formed. It is treated as node and the out going edges
from it are now checked. Here, C-3-D is the new edge, which is less than
other edges’ cost, C-4-B, A-6-B.

After adding node D to the


spanning tree, There are two edges
going out of it having the same
cost, i.e. D-2-T and D-2-B. Thus,
any of those is added.

Dr. Avatharam Ganivada (SCIS) CBNST March 13, 2018 21 / 23


In next step, the edge D-2-B with
the least cost is added to the
spanning tree.

Dr. Avatharam Ganivada (SCIS) CBNST March 13, 2018 22 / 23


Example2
Use Prim’s algorithm and find minimum spanning tree.

Dr. Avatharam Ganivada (SCIS) CBNST March 13, 2018 23 / 23

You might also like