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

Ds- Unit 4- Problem on Graph

The document discusses Breadth First Search (BFS) and Depth First Search (DFS) algorithms for graph traversal, detailing their processes and data structures used (queue for BFS and stack for DFS). It also compares the two algorithms, highlighting their efficiency and applications. Additionally, it covers minimum spanning tree algorithms, including Prim's and Kruskal's, and Dijkstra's shortest path algorithm with examples.

Uploaded by

rajanayaki
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

Ds- Unit 4- Problem on Graph

The document discusses Breadth First Search (BFS) and Depth First Search (DFS) algorithms for graph traversal, detailing their processes and data structures used (queue for BFS and stack for DFS). It also compares the two algorithms, highlighting their efficiency and applications. Additionally, it covers minimum spanning tree algorithms, including Prim's and Kruskal's, and Dijkstra's shortest path algorithm with examples.

Uploaded by

rajanayaki
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/ 21

4.4.

2 Breadth First Traversal of Graph


Consider, a vertex V, in the graph will be visited first, then all the vertices
to V, will be traversed suppose adjacent to V1 are (V2 V3V4.V). So V,V,..V adacemtw
be printed first. Then again from V, the adjacent vertices will be printed.
will be continued for all the This process
vertices to get encounterd. To keep track of all the verhcs
and their adjacent vertices we will make use of Queue data structure. Also we will male
use of an array for visited nodes. The nodes which are get visited are
set to 1. Thus we
can keep track of visited nodes.
In short in BFS traversal we follow the path in
breadthwise fashion. Let us see the
algorithm for breadth first search.
Algorithm:
1. Create a graph. Depending on the type of graph ie. directed or undirected Ser u
value of the flag as either 0or 1
2. Read the vertex from respectively.
which you want to traverse the
3. Initialize the graph say Vi
visited array to 1 at the index of V;.
4. Insert the
visited vertex V; in the
5. Visit the
vertex which is at the queue. queueand
place its adjacent front of the queue. Delete it from the
6. Repeat the nodes in the queue.
step 5, till the
7. Stop. queue is notot empty.
Explanation of logic of BFS
In BFSthe queue is maintained for
storing the adjacent nodes and an
array 'visited' is maintained for
keeping thetrack of visited nodes. i.e.
once a particular node is visited it
should not be revisited again. Let us
see how our program works : 4

Fig. 4.4.2 Graph


4- 13 Graphs
Structuresand Algorithms

Start with vertex 1


Step 1 :
Queue
Visited
Inserted vertex 1 in queue
-1 and marked the index 1of
11
visited array by 1
3 Front Rear

Step 2:
1 Delete '1' and print it
So 1'gets printed

Front
rear
in
vertex 1 and mark them as visited, insert those
vertices of
Step 3 : Find adjacent
Queue.
Visited
from
0 1 2 3 Increment front by 1delete '2'printed.
1 Queue and print it so '2' gets
1
Front Rear

nodes in Queue as well as mark them as


'2' and insert those
Step 4 : Find adjacent to
visited.

Visited

front and delete the node print it.


Step 5: Increment
3 4 So '3'gets printed

Front Rear
visited. If it is
whether it is marked as
check
Step 6: Find adjacent to '3' i.e. 4
queue.
marked as visited do not insert in the
4 - 14
Algorithns
Siructuresand
Data

delete the node from Queue and print it.


Inerement front,
Visited
01 2 3 4
|1|23 4 So '4' gets printed since
11
1
stop the procedure. front =rear
2 1
3 1 Front
4|1 rear

So output will be - BPS for above graph as


1 2 3 4
Explanation Of Logic For Depth First Traversal
In DES the basic data structure for storing
the adjacent nodes is stack. In our program we
have used a recursive call to DFS function. 3
When a recursive call is invoked actually push
operation gets performed. When we exit from
the loop pop operation will be performed. Let 4

uS see how our program works. Fig. 4.4.3 Graph

Step 1 : Start with vertex 1, print it so "1 gets printed. Mark 1 as visited.
Visited

1 1

2
3
4

TECHNICAL PUBLICATIONS-An up thrust for knowledge


4-18
Data Structures and Algorithms

adjacent vertex to 1, say i.e. 2 if it is not visited, call


cet
Step 2
inserted
: Find
in the stack, mnark is as visited. DFS2) ie.tn
Visited Stack
0 2Top
11 After exiting the loop 2 will be
2 1 popped print 2
3 0
4

Step 3:Find adjacent to "2' i.e. vertex 4 if it is not visited call DES4) ie. Awill
ushed on to the stack mark it as visited.
Visited Stack
00 4 -Top
2 1 After exiting the loop 4will be
popped print '4'
3 0
4 1

Step 4: Find adjacent to '4 i.e. vertex 3 if it is not visited call DFS(3) i..ee. 3il
shed onto the stack mark it
visited.
Visited Stack
|3 Top After exiting the loop 3 will be
2
popped print '3'
31
4

Since all the nodes are


So output of
DFS is
covered stop the procedure.
2 4 3

niversity Questions
Data Structures and Algorithms 4-30
Graphs
|4.6.4 Comparison between DFS and BFS
Sr. Depth first traversal Breadth first traversal
No.

1. This traversal is done with the help of stack This traversal is done with the help of
data structure. queue data structure.
2. It works using two ordering The first order is It works using one ordering. The order
the order in which the vertices are reached for in which the vertices are reached in the
the first time(i.e. the visited vertices are pushed same order they get removed from the
onto the stack) and the second order in which queue.
the vertices become dead end(the vertices are
popped off the stack)
3. The DFS sequence is composed of tree edges The DFS sequence is composed of tree
and back edges. edges and cross edges.
4. The efficiency of The adjacency matrix graph is The efficiency of The adjacency matrix
graph is O(V2)
5. The efficiency of the adjacency list graph is O
(|V|+|E|) The efficiency of the adjacency list
6.
graph is e(|V+|E)
Application : To check connectivity,
a graph and to find articulation pointacyclicity
of Applications: To check connectivity,
used.
DFS is acyclicity of a graph and to find
shortest path between two vertices BFS
is used. wwwwwwwwwwnos
Structuresand Algor1thms
Dala

4.8 Spanning Tree Dec.-09,10,11,12, May-10,11,12

tree S is a subset of a tree T in which all the vertices of tree T are


Sppanning
A but it may not contain all the edges.
present
minimum
The minimum spanning tree of a weighted connected graphG is called
spanning tree if its weight is minimum.
We are going to discuss the two popular algorithms of minimum spanning tree,
namely Prim's algorithm and Kruskal's algorithm.
4.8.1 Prim's Algorithm
is to be chosen. Then
In Prim's algorithm the pair with the minimum weight
minimum weight is selected.
adiacent to these vertices which ever is the edge havingnot be covered. The necessary
are
This process will be continued till all the vertices will
formed. From Fig. 4.8.1 we
condition in this case is that the circuit should not be
build the minimum spanning tree as follows
Example 4.8.1 Construct minimum spanning tree for following graph.

1 2
b

Fig. 4.8.1 Graph 'g'

Solution:
Step 1 Step 2 Step 3 Step 4 Step 5
a
a

3. 3
3
C 1 C 1 2
1 b -o d
b b o

Fig. 4.8.2 Prim's minimum spanning tree for 'g'


The minimum weight is 7.

TECHNICAL PUBLICATIONS- An up thrust for knowledge


Data Structures and Algorithms
4-32

Example 4.8.2 Ayply Prim's algorithm to find the


minimum
graph spanning tree in the Graphs
4 May-12, Mafolrotkwsing14
5

6 6

3 4

Fig. 4.8.3
Solution :

Step 1
Step 2:
b

3
e

4
4
b
2

b Total weight = 15
2
d

Minimum spanning tree

Fig. 4.8.3(a)

TECHNICAL PUBLICATIONS"- An up thrust for knowledge


Structures and Algorithms
4 -33 Graphs
Data

A82 Kruskal's Algorithm


Kruskal's algorithm the minimum weight is obtained. In this algorithm also the
circuit should not be formed. Each time the edge of minimum weight has to be
selected, from the graph.It is not necessary in this algorithm to have edges of minimum
weightsto be adjacent. Let us solve one example by Kruskal's algorithm.
Example 4.8.2 Obtain MST for graph in Fig. 4.8.4 using Kruskal's algorithm.

Solution:

(1) (2)

1 1

2
8 1

V6 3 V
2 13
(3) (4)
12 8 1 8

12 V6 2
3 3 V2
V3

Graph G
V3 V5d

V V4
Minimum weight=21

Fig. 4.8.4 Graph G


Pseudocode
void spanning tree()
nt count,k,v1,v2,i,j,tree[10J[10],pos,parent[10};
int sum;
nt Find(int v2,int
Void parent |);
Union(int i,int j,int parent|);

TECHNICAL PUBLICATIONS- An up thrust for knowledge


SiuctureSand Algorithms 4-37 Graphs

Dijkstra'sshortest
path algorithm
2 shortest path algorithm suggests the shortest path from some source
Dijkstra's
The Some other destination node. The source node or the node from we start
nodetothe
distance is called the start node and the destination node is called the end
measuringthe we start finding the distance from the start node and find all the
algorithm
node.In this
tromn it to neighbouring nodes.
Among those which ever is the nearest node that
process of finding the nearest node is repeated till the end node.
paths
path isselected. This
Then
whichever is the path that path is called the shortest path.
choosing the shortest
Since in this algorithm all the paths are tried and then we are
them, this algorithm is solved by a greedy algorithm. One more thing is
Pathamong
doesn't
hat we are having all the vertices in the shortest path and therefore the graph
give the spanning tree.
example by this algorithm.
For clear understanding let us see stepwise solving of one
Example :
b 2 2
A
22 20
16 10
7

10
6
Fig. 4.9.2
already selected
P= Set which is for nodes which have
T= Remaining node
Step 1: V=a
P = {al, T = (b,c,d,e,f,z}
dist(b) = minfold dist(b),dist(a)+w(a,b)}
dist(b) = min{o,0+22)
dist(b) = 22
dist(c) = 16
dist(d) = 8 - minimum node
dist(e) = o
dist(f) = o
dist(z) =
Jo mininmum node is selected in P i.e. node d

TECHNICAL PUBLICATIONS - An up thrust for knowledae


Data Structures and Algorithms 4-38

Step 2: v= d Graçha
P= la,d) T = (bc,e,f,z)
dist(b) = min{old dist(b), dist(d)+w(b,d))
dist(b) = min{22,8+oo)
dist(b) = 22
dist(c) = min{16,8-+10) = 16
dist(e) = minlo,8too} = 8
dist(f) = minfo,8+6} = 14
dist(z) = minfo,8+oo} = oo
Step 3: V=f

P= (a,d,f} T = (b,c,e,z)
dist(b) = min(22,14+7} = 21
dist(c) = min{16,14+3) = 16
dist(e) = minfoo ,14 +oo =0
dist(z) = minfo, 14+9} = 23
Step 4: V=c

P= fa,d,f,c} T = (b,e,z)
dist(b) = min(21,16+20} = 21
dist(e) = minfo 16+4} = 20
dist(z) = min{23,16+10} = 23

Step 5: V= e

P= {a,d,f,c,e} T ={b,z}
dist(b) =min{21,20+2) = 21
dist(z) =min{23,20+4) = 23

Step 6: V =b

P= (a,d,f,c,e,b) T = (z)
dist(z) = min{23,21+2) = 23 of the
Hence the length
Now the target vertex for finding the shortest path is z.
shortest path from the vertex a to z is 23.

TECHNICAL PUBLICATIONS- An up thrust for knowledge


4-39
Structures and Algorithms Graphs
Data

path is as shown below :


The shortest
a
8

Fig. 4.9.3
Dala Structures and Algorithns 4- 42

enter The edge Of V5 TO V4.1


enter The odu 0f V5T) V 9's

EntorThe Soues1

Enter The Destination: 5

Shortest path: 1 3 5
Exampl 4.4 Tind the shortest path from node 1 to 7 using shortest path
algorithm.
May-11, Marks 3
8 5

3 4
6
5

Fig. 4.9.4
Solution:

Step 1:P=1 T=2,3,4,5, 6,7}


dist(1, 2) =8
dist(1,3) =3+Select vertex 3 with min length
dist(1,4) = 6
dist(1,5) =
dist(1,6) =
dist(1,7) =a
Step 2: P =1, 3} T=(2,4,5, 6,7}
V=3

dist(2) = min{dist(1, 2), dist(1, 3) +dist (3, 2)}


= min {8,3 + 3)
dist(2) = 6
dist(4) =min dist(1,4), dist(1,3) +dist(3,4)}
=min {6,5}
dist(4) = 5-Select it.
TECHNICAL PUBLICATIONS - An up thrust for knowledge
4- 43 Graphs
SrvctureSandAlgorithms

dist(5)
dist(6) = o
dist(7) =
T=2,5,6,7}
Sep3:P= 3, 4}
V=4
dist(2) = 6
min fo,5 + 6}
dist(5) = min{ dist(1,5),. dist(1, 4) +dist(4,5)}=
dist(5) = 11
= min fo, 5 +1}
dist(6) =min dist(1, 6), dist(1, 4) +dist(4, 6)}
=64-Select it.

dist(7) = oo

Step 4 :P={1, 3, 4, 6}, T=(2,5,7}


dist(2) = 6
dist(5) = 11
min{dist(1,7), dist(1,6) + dist(6,7)}= min foo, 6 +5}
dist(7) =
dist(7) = 11 11.
shortest path as 1 -3-4-6-7 with the path length
Thus we obtain
path finding algorithm with the following graph to
Lam ple 4.9.1Explain Dijikstra's shortest May:f2
travel from S to D. 4
1

2 5
1
3

3
6
Fig. 4.9.5

Solution :
Step 1::P= (S), T= la, b, c, e, f, g, h, i, j, k, D}
= o
dist(S, a) = 3-Min dist(S, g) = o
dist(S, e)
dist(S, b) = 5 for knowledge
PUBLICATIONS- An up thrust
TECHNICAL
Data Structures and Algorithms 4-44

dist(S, c) = 7 dist(S, f) = co
Graphs
dist(S, h) = oo dist(S, k) = o

dist(S, i) = oo dist(S, D) =
dist(S, j) = oo

Step 2: P= (S, a}, T = (b, c, e, f, g, h, i, j, k, D}


V= a
dist(b) = 5 dist(g) = 3+1 = ’ Min
dist(b) = 7 dist(e)
dist(f) = 3+3 = 6 dist(h) = oo
dist(i) = dist{i) = dist(k) = dist(D) =
Step 3: P= (S, a, gl. T = {b, c, e, f, g, h, i, j, k, D}

dist(b) = 5 Min dist(e)


dist(c) = 7 dist(f) =6
dist(h) = dist(s, a) + dist(a, g) + dist(g, h)
dist(h) = 3+ 1 +5 = 9
dist(i) = dist(s, a)+dist(a, g)+dist(g, i)
=3+ 1+ 4
dist(i)
dist(i) = o0, dist(k) = o0, dist(D) = o
Step 4 : P= (S, a, g, b}, T = {c, e, f,
h, i, j, k, D}
V= b
dist(c) = 7
dist(e) dist(s, b) + dist(b, e)
dist(e) = 5+4 =9
dist(f) = 6 ’ Min
dist(h) = 9
dist(i) = 8
dist(i) = dist(k) =
dist(D)
= ,
TECHNICAL PUBLICATIONS
Date Structures and Algorithms 4- 45 Graphs

step 5: P= (5, a, & b, f}, T= {c, e, h, i, j, k, DI


V= f
dist(e) = 9
dist(c) = 7’Min
dist(h) = 9
dist(i) = 8
dist(j)
dist(k) = dist(S, c) + dist(c, F) + dist(f, k)
dist(K) = 7+ 6 +3 = 16
dist(D)

Step 6 : P = {S, a, g, b, f, c}, T= {e, h, i, j, k, D}


V= c
dist(e) = 9
dist(h) = 9
dist(i) = 8 Min

dist(j)
dist(k) = 16
dist(D)
Step 7: P= (S, a, g, b, f, c, i), T = (e, h, j, k, D}
V = i

dist(e) = 9 ’ Min
dist(h) = 9
dist(j)
dist(k) = 16
dist(D) = 9
Step 8: P= (S, a, 8, b, f, c, i, e), T =(h, j, k, D}
V= e

TECHNICAL PUBLICATIONS- An up thrust for knowledge


Data Structures and Algorithms 4- 46

dist(h) = 9 Min Graphs


dist(;)
dist(k) = 16
dist(D) = 9

Step 9 : P= (S, a, g, b, f, c, i, e, h), T = {i, k, D}


dist(j) = 20

dist(k) = 16 ’ Min
dist(D) = 9

Step 10: P= (S, a, g, b, f, c, i, e, h, j}, T =(k, D}


dist(k) = 16
dist(D) = 9 ’ Min
Thus we get shortest path s - a -g - i-d with shortest distance = 9
1. What is strongly connected graph ? Give an example. Dec.-09 Marks 4

4.11 Topological Sort


This is a direct implementation of decrease and conquer method. Following are the
steps to be followed in this algorithm -

1. From a given graph find a vertex with no incoming edges. Delete it along with all
the edges outgoing from it. Ifthere are more than one such vertices then break the
tie randomly.
2. Note the vertices that are deleted.
3. All these recorded vertices give topologically sorted list.
Let us understand this algorithm with some examples -
Example 4.11.1 Sort the digraph for topological sort using source removal algorithm.

Fig. 4.11.1

Solution: We will follow following steps to obtain topologically sorted list.


Choose vertex B, because it has no incoming edge, delete it along with its adjacent
edges.
TECHNICAL PUBLICATIONS-- An up thrust for knowledge
Data Structures and Algonthms 4- 48

Graphs
Delete
Delete
B
A Delete
D

B,A

delete delete
E
Sorted List: B.A,D
B,A,D,C B,A,D,C,E
Fig. 4.11.2
Hence the list after topological sorting will be B, A, D, C,
E.
Example 4.11.2 Sort the given digraph
using topological sort using source
removal algorithm.
B

Fig. 4.11.3
Solution: Let us start with the vertex with no
vertices Aand B. The tie can be incoming edge. There are two SlCn
broken arbitrarily. Let us first delete vertex B.
A

deiete
delete
B
A
B

delete
delete delete 8,A,C,D,E
C
D E

B,A
B,A,C
B,A,C,D
Fig. 4.11.4
Thus the topologically sorted list is B, A, C, D, E.
Structures and Algorithms
Dala.
4- 49
Graphs

nle 411.3 Apply source removal algorithm to solve topological sorting problem for the
followinggraph.

Fig. 4.11.5
Solution : We will find the node having no incoming edge. Such a vertex is d. Hence
delete it first along with its adjacent edges.
b

delete

delete
delete
a

d,a

delete
delete
b

d,a,c d,a,c,b

e e

delete
delete,
d,a,c,b,g d,a,c,b.g,f
Fig. 4.11.6

Hence topologically sorted list is d, a, c, b, g, f and e.


TECHNICAL PUBLICATIONS- An up thrust for knowledge

You might also like